Adapters
The @espressif/rainmaker-base-sdk follows an adapter pattern to provide a flexible, platform-agnostic architecture. This design allows the SDK to work across different JavaScript frameworks and platforms (React Native, web, Electron, etc.) by implementing platform-specific functionality through adapters.
Why Adapters?
Adapters bridge the gap between the SDK's core TypeScript logic and platform-specific implementations. Different platforms have different capabilities:
- React Native requires native modules for Bluetooth, storage, and notifications
- Web applications use browser APIs like localStorage and Web Bluetooth
- Electron apps may use Node.js filesystem APIs
By using adapters, the SDK can:
- Remain platform-agnostic
- Enable consistent API across all platforms
- Allow developers to provide custom implementations for their specific needs
- Support multiple UI frameworks (React Native, Vue, Angular, etc.)
Available Adapters
The SDK provides interfaces for seven different adapters, each serving a specific purpose:
1. Storage Adapter
Purpose: Manages persistent data storage for user tokens, configurations, and SDK state.
Interface: ESPRMStorageAdapterInterface
Required Methods:
interface ESPRMStorageAdapterInterface {
setItem(name: string, value: string): Promise<void>;
getItem(name: string): Promise<string | null>;
removeItem(name: string): Promise<void>;
clear(): Promise<void>;
}
When to Use: Always required. The SDK needs persistent storage to maintain user sessions, cache data, and store configurations.
Common Implementations:
- Web:
localStorageorsessionStorage - React Native:
AsyncStorageorreact-native-mmkv - Node.js: File system or database storage
2. Provisioning Adapter
Purpose: Enables device provisioning functionality, allowing users to onboard new ESP devices to their WiFi network.
Interface: ESPProvisionAdapterInterface
Required Methods:
interface ESPProvisionAdapterInterface {
searchESPDevices(
devicePrefix: string,
transport: ESPTransport
): Promise<ESPDeviceInterface[]>;
stopESPDevicesSearch(): Promise<void>;
createESPDevice(
name: string,
transport: string,
security?: number,
proofOfPossession?: string,
softAPPassword?: string,
username?: string
): Promise<ESPDeviceInterface>;
connect(deviceName: string): Promise<ESPConnectStatus>;
getDeviceCapabilities(deviceName: string): Promise<string[]>;
getDeviceVersionInfo(deviceName: string): Promise<{ [key: string]: any }>;
setProofOfPossession(
deviceName: string,
proofOfPossession: string
): Promise<boolean>;
initializeSession(deviceName: string): Promise<boolean>;
scanWifiList(deviceName: string): Promise<ESPWifiList[]>;
sendData(deviceName: string, endPoint: string, data: string): Promise<string>;
provision(
deviceName: string,
ssid: string,
passphrase: string
): Promise<ESPProvisionStatus>;
disconnect(deviceName: string): Promise<void>;
}
When to Use: Required if your app needs to provision devices. This adapter handles communication with ESP devices during the onboarding process.
Common Implementations:
- React Native: Native modules for BLE/WiFi provisioning
- Web: Web Bluetooth API (for BLE provisioning)
3. Local Discovery Adapter
Purpose: Discovers ESP RainMaker devices on the local network using mDNS/Bonjour.
Interface: ESPLocalDiscoveryAdapterInterface
Required Methods:
interface ESPLocalDiscoveryAdapterInterface {
startDiscovery(callback: Function, params: DiscoveryParamsInterface): void;
stopDiscovery(): void;
}
interface DiscoveryParamsInterface {
serviceType: string; // e.g., "_esp_local_ctrl._tcp"
domain: string; // e.g., "local."
}
When to Use: Optional. Required if your app supports local control of devices (controlling devices without cloud connectivity).
Common Implementations:
- React Native: Native modules using platform mDNS/Bonjour APIs
- Node.js:
bonjourormdnspackages - Web: Limited support (no standard browser API)
4. Local Control Adapter
Purpose: Enables direct communication with devices on the local network without going through the cloud.
Interface: ESPLocalControlAdapterInterface
Required Methods:
interface ESPLocalControlAdapterInterface {
isConnected(nodeId: string): Promise<boolean>;
connect(
nodeId: string,
baseUrl: string,
securityType: number,
pop?: string,
username?: string
): Promise<Record<string, any>>;
sendData(nodeId: string, path: string, data: string): Promise<string>;
}
When to Use: Optional. Required if your app supports local control features for faster, more reliable device control when on the same network.
Common Implementations:
- React Native: Native modules for secure local communication
- Web/Node.js: HTTP/HTTPS clients with security protocol implementation
5. Notification Adapter
Purpose: Handles push notifications from the ESP RainMaker platform.
Interface: ESPNotificationAdapterInterface
Required Methods:
interface ESPNotificationAdapterInterface {
addNotificationListener(callback: Function): void;
}
When to Use: Optional. Required if your app needs to receive and handle push notifications about device events, alerts, or automation triggers.
Common Implementations:
- React Native: Firebase Cloud Messaging (FCM) or Apple Push Notification Service (APNS)
- Web: Web Push API or Firebase Cloud Messaging
6. OAuth Adapter
Purpose: Enables third-party authentication (Google, Apple, GitHub, etc.) for user login.
Interface: ESPOauthAdapterInterface
Required Methods:
interface ESPOauthAdapterInterface {
getCode(requestURL: string): Promise<string>;
}
When to Use: Optional. Required only if your app supports third-party login methods. The adapter opens an OAuth flow and returns the authorization code.
Common Implementations:
- React Native: In-app browser or native OAuth modules
- Web: Popup window or redirect-based OAuth flow
7. App Utility Adapter
Purpose: Provides platform-specific utility functions for permission checks and app features.
Interface: ESPAppUtilityAdapterInterface
Required Methods:
interface ESPAppUtilityAdapterInterface {
isBlePermissionGranted(): Promise<boolean>;
isLocationPermissionGranted(): Promise<boolean>;
}
When to Use: Optional. Useful for checking required permissions before attempting device provisioning or discovery.
Common Implementations:
- React Native: Native modules checking platform permissions
- Web: Browser permission APIs