The Device Control module provides functionality for controlling IoT devices in the ESP RainMaker system. It dynamically renders device-specific control panels based on device type and parameters, with support for custom control screens and parameter configurations.
Device control is implemented using a configuration-driven approach where device types, parameters, and control panels are defined in configuration files. The system automatically selects appropriate control interfaces based on device capabilities.
Device Control
This guide explains how the Device Control module works, focusing on the major operations and how device configurations, parameters, and control panels are managed throughout the system.
User Flow Overview
The Device Control module has three main user flows:
- Device Control: View and control device parameters
- Device Settings: Configure device name, OTA updates, and removal
Architecture Overview
Components Structure
(device)/
├── Control.tsx # Main device control screen
├── Settings.tsx # Device settings and configuration
└── device_panels/ # Device-specific control panels
├── Light.tsx # Light device control panel
├── Switch.tsx # Switch device control panel
├── Temperature.tsx # Temperature sensor control panel
└── Fallback.tsx # Generic fallback control panel
config/
├── devices.config.ts # Device type configurations
└── params.config.ts # Parameter type configurations
utils/
└── device.ts # Device utility functions
1. Main Device Control (Control.tsx)
This is the main screen that dynamically renders device-specific control panels based on device type.
Identify the selected device
// Get CDF store for device operations
const { store } = useCDF();
const { nodeStore } = store;
// Get device from node
const nodeList = store?.nodeStore?.nodeList || [];
const node = nodeList.find((n) => n.id === id) as ESPRMNode | undefined;
const device = node?.nodeConfig?.devices.find((d) => d.name === _device) as ESPRMDevice | undefined;
Device Type Detection
// Device type detection using utility function
const deviceType = extractDeviceType(device.type);
// Find device configuration from config
const deviceConfig = findDeviceConfig(deviceType);
// Control panel mapping
const CONTROL_PANELS: Record<string, React.FC<any>> = {
light: LightControl,
switch: SwitchControl,
temperature: TemperatureControl,
};
// Device control renderer
const renderDeviceControl = () => {
const deviceConfig = findDeviceConfig(deviceType);
if (!deviceConfig || !deviceConfig.controlPanel) {
return <Fallback node={node as any} device={device} />;
}
const ControlPanel = CONTROL_PANELS[deviceConfig.controlPanel as keyof typeof CONTROL_PANELS];
return <ControlPanel node={node} device={device} />;
};
What this does:
- Dynamically detects device type from node configuration
- Maps device types to appropriate control panel components
- Falls back to generic control panel if no specific panel is configured
- Uses CDF node store to access device information
2. Device Configuration (devices.config.ts)
Device Type Configuration Structure
export const DEVICE_TYPE_LIST = [
{
label: "Lighting", // Human-readable category label
groupLabel: "Lights", // Group label for organization
type: [ // Supported device types
"lightbulb",
"lightbulb3",
"lightbulb4",
"lightbulb5",
"lightstrip",
"lightstrip1",
"light",
],
name: "Light", // Device name
param: "Light", // Parameter name
deviceType: ["1", "2"], // ESP device type codes
icon: { // Icon mappings for different types
lightbulb: { icon: "light-3" },
lightbulb3: { icon: "light-1" },
lightbulb4: { icon: "light-1" },
lightbulb5: { icon: "light-1" },
lightstrip: { icon: "light-5" },
lightstrip1: { icon: "light-5" },
},
defaultIcon: "light-1", // Default icon if no specific mapping
disabled: false, // Whether device type is enabled
controlPanel: "light", // Control panel component to use
}
];
What this does:
- Defines all supported device types in the system
- Maps device types to control panel components
- Provides icon mappings for different device variants
- Enables/disables specific device types
- Associates ESP device type codes with human-readable names