Skip to content

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

import { DeviceRegistry } from 'energy-manager-iot';

const registry = new DeviceRegistry();

Constructor

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 device
  • name: Human-readable device name
  • type: Type of device from the DeviceType enum
  • config (optional): Device configuration parameters
  • groups (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

public updateDevice(
  id: string,
  updates: Partial<Omit<Device, 'id' | 'createdAt'>>
): Device

Updates an existing device's properties.

Parameters

  • id: ID of the device to update
  • updates: Object containing properties to update

Returns

The updated device object.

Example

const updatedDevice = registry.updateDevice('sensor1', {
  name: 'Updated Sensor Name',
  config: {
    reportingInterval: 120
  }
});

updateDeviceStatus

public updateDeviceStatus(id: string, status: DeviceStatus): Device

Updates a device's status information.

Parameters

  • id: ID of the device to update
  • status: 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

public removeDevice(id: string): boolean

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

const wasRemoved = registry.removeDevice('sensor1');

getDevice

public getDevice(id: string): Device

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

public hasDevice(id: string): boolean

Checks if a device exists in the registry.

Parameters

  • id: ID of the device to check

Returns

True if device exists, false otherwise.

Example

if (registry.hasDevice('sensor1')) {
  console.log('Device exists!');
}

Group Management Methods

createGroup

public createGroup(name: string): boolean

Creates a new device group.

Parameters

  • name: Name for the new group

Returns

True if group was created, false if it already exists.

Example

registry.createGroup('bedroom');

addDeviceToGroup

public addDeviceToGroup(
  deviceId: string,
  groupName: string,
  device?: Device
): boolean

Adds a device to a group.

Parameters

  • deviceId: ID of the device to add
  • groupName: Name of the group to add the device to
  • device (optional): Device object (to avoid lookup if already available)

Returns

True if the device was added to the group.

Example

registry.addDeviceToGroup('sensor1', 'bedroom');

removeDeviceFromGroup

public removeDeviceFromGroup(deviceId: string, groupName: string): boolean

Removes a device from a group.

Parameters

  • deviceId: ID of the device to remove
  • groupName: Name of the group to remove the device from

Returns

True if the device was removed from the group.

Example

registry.removeDeviceFromGroup('sensor1', 'bedroom');

removeGroup

public removeGroup(name: string): boolean

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

registry.removeGroup('bedroom');

getDevicesInGroup

public getDevicesInGroup(groupName: string): Device[]

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

public getDeviceIdsInGroup(groupName: string): string[]

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

const deviceIds = registry.getDeviceIdsInGroup('bedroom');

Utility Methods

getAllGroups

public getAllGroups(): string[]

Retrieves all existing group names.

Returns

Array of group names.

Example

const groups = registry.getAllGroups();
console.log(`There are ${groups.length} groups defined`);

getAllDevices

public getAllDevices(): Device[]

Retrieves all devices in the registry.

Returns

Array of all devices.

Example

const devices = registry.getAllDevices();

getAllDeviceIds

public getAllDeviceIds(): string[]

Retrieves all device IDs in the registry.

Returns

Array of all device IDs.

Example

const deviceIds = registry.getAllDeviceIds();