DeviceRegistry Class
The DeviceRegistry
class manages IoT device registration and grouping. This class provides a centralized registry for all IoT devices, with features for device management, grouping, and status updates.
Class Overview
Constructor
Creates a new device registry instance.
Device Management Methods
registerDevice
public registerDevice(
id: string,
name: string,
type: DeviceType,
config: DeviceConfig = {},
groups: string[] = []
): Device
Registers a new device in the system.
Parameters
id
: Unique identifier for the devicename
: Human-readable device nametype
: Type of device from the DeviceType enumconfig
(optional): Device configuration parametersgroups
(optional): Initial groups to assign the device to
Returns
The newly registered device object.
Example
const device = registry.registerDevice(
'sensor1',
'Temperature Sensor',
DeviceType.SENSOR,
{
reportingInterval: 60,
sleepThreshold: 20
},
['kitchen']
);
updateDevice
Updates an existing device's properties.
Parameters
id
: ID of the device to updateupdates
: Object containing properties to update
Returns
The updated device object.
Example
const updatedDevice = registry.updateDevice('sensor1', {
name: 'Updated Sensor Name',
config: {
reportingInterval: 120
}
});
updateDeviceStatus
Updates a device's status information.
Parameters
id
: ID of the device to updatestatus
: New status information
Returns
The updated device object.
Example
const device = registry.updateDeviceStatus('sensor1', {
deviceId: 'sensor1',
batteryLevel: 80,
powerMode: PowerMode.NORMAL,
connectionStatus: ConnectionStatus.ONLINE,
lastSeen: Date.now()
});
removeDevice
Removes a device from the registry and all its groups.
Parameters
id
: ID of the device to remove
Returns
True if device was found and removed, false otherwise.
Example
getDevice
Retrieves a device by its ID.
Parameters
id
: ID of the device to retrieve
Returns
The device object.
Throws
EnergyManagerError
if device not found.
Example
try {
const device = registry.getDevice('sensor1');
console.log(device);
} catch (error) {
console.error('Device not found');
}
hasDevice
Checks if a device exists in the registry.
Parameters
id
: ID of the device to check
Returns
True if device exists, false otherwise.
Example
Group Management Methods
createGroup
Creates a new device group.
Parameters
name
: Name for the new group
Returns
True if group was created, false if it already exists.
Example
addDeviceToGroup
Adds a device to a group.
Parameters
deviceId
: ID of the device to addgroupName
: Name of the group to add the device todevice
(optional): Device object (to avoid lookup if already available)
Returns
True if the device was added to the group.
Example
removeDeviceFromGroup
Removes a device from a group.
Parameters
deviceId
: ID of the device to removegroupName
: Name of the group to remove the device from
Returns
True if the device was removed from the group.
Example
removeGroup
Removes a group and disassociates all devices from it.
Parameters
name
: Name of the group to remove
Returns
True if group was found and removed.
Example
getDevicesInGroup
Retrieves all devices in a group.
Parameters
groupName
: Name of the group to query
Returns
Array of devices in the group.
Example
const devices = registry.getDevicesInGroup('bedroom');
console.log(`Found ${devices.length} devices in the group`);
getDeviceIdsInGroup
Retrieves IDs of all devices in a group.
Parameters
groupName
: Name of the group to query
Returns
Array of device IDs in the group.
Example
Utility Methods
getAllGroups
Retrieves all existing group names.
Returns
Array of group names.
Example
getAllDevices
Retrieves all devices in the registry.
Returns
Array of all devices.
Example
getAllDeviceIds
Retrieves all device IDs in the registry.
Returns
Array of all device IDs.