114 lines
3.9 KiB
TypeScript
114 lines
3.9 KiB
TypeScript
import * as plugins from '../plugins.js';
|
|
|
|
export class DeviceRegistry {
|
|
private devices: plugins.shxInterfaces.data.IDeviceDefinition[] = [
|
|
{
|
|
id: 'climate.office',
|
|
name: 'Office Thermostat',
|
|
room: 'Office',
|
|
protocol: 'homeassistant',
|
|
manufacturer: 'Demo',
|
|
model: 'Climate v1',
|
|
online: true,
|
|
features: [
|
|
{ id: 'temperature', capability: 'sensor', name: 'Temperature', readable: true, writable: false, unit: 'C' },
|
|
{ id: 'target', capability: 'climate', name: 'Target temperature', readable: true, writable: true, unit: 'C' },
|
|
],
|
|
state: [
|
|
{ featureId: 'temperature', value: 22.4, updatedAt: new Date().toISOString() },
|
|
{ featureId: 'target', value: 22.4, updatedAt: new Date().toISOString() },
|
|
],
|
|
},
|
|
{
|
|
id: 'lock.front',
|
|
name: 'Front Lock',
|
|
room: 'Hall',
|
|
protocol: 'matter',
|
|
manufacturer: 'Demo',
|
|
model: 'Lock v1',
|
|
online: true,
|
|
features: [
|
|
{ id: 'locked', capability: 'lock', name: 'Locked', readable: true, writable: true },
|
|
],
|
|
state: [
|
|
{ featureId: 'locked', value: false, updatedAt: new Date().toISOString() },
|
|
],
|
|
},
|
|
{
|
|
id: 'energy.solar',
|
|
name: 'Solar Inverter',
|
|
room: 'Roof',
|
|
protocol: 'mqtt',
|
|
manufacturer: 'Demo',
|
|
model: 'PV v1',
|
|
online: true,
|
|
features: [
|
|
{ id: 'production', capability: 'energy', name: 'Production', readable: true, writable: false, unit: 'kW' },
|
|
],
|
|
state: [
|
|
{ featureId: 'production', value: 3.8, updatedAt: new Date().toISOString() },
|
|
],
|
|
},
|
|
{
|
|
id: 'light.living.floor',
|
|
name: 'Living Floor Lamp',
|
|
room: 'Living',
|
|
protocol: 'zigbee',
|
|
manufacturer: 'Demo',
|
|
model: 'Light v1',
|
|
online: true,
|
|
features: [
|
|
{ id: 'on', capability: 'light', name: 'Power', readable: true, writable: true },
|
|
{ id: 'brightness', capability: 'light', name: 'Brightness', readable: true, writable: true, unit: '%' },
|
|
],
|
|
state: [
|
|
{ featureId: 'on', value: true, updatedAt: new Date().toISOString() },
|
|
{ featureId: 'brightness', value: 65, updatedAt: new Date().toISOString() },
|
|
],
|
|
},
|
|
];
|
|
|
|
public listDevices(filterArg: { room?: string; capability?: plugins.shxInterfaces.data.TDeviceCapability } = {}) {
|
|
return this.devices.filter((deviceArg) => {
|
|
const roomMatches = !filterArg.room || deviceArg.room === filterArg.room;
|
|
const capabilityMatches = !filterArg.capability || deviceArg.features.some((featureArg: plugins.shxInterfaces.data.IDeviceFeature) => featureArg.capability === filterArg.capability);
|
|
return roomMatches && capabilityMatches;
|
|
});
|
|
}
|
|
|
|
public getDeviceById(deviceIdArg: string) {
|
|
return this.devices.find((deviceArg) => deviceArg.id === deviceIdArg);
|
|
}
|
|
|
|
public upsertDevices(devicesArg: plugins.shxInterfaces.data.IDeviceDefinition[]) {
|
|
for (const device of devicesArg) {
|
|
const existingIndex = this.devices.findIndex((existingDeviceArg) => existingDeviceArg.id === device.id);
|
|
if (existingIndex >= 0) {
|
|
this.devices[existingIndex] = device;
|
|
} else {
|
|
this.devices.push(device);
|
|
}
|
|
}
|
|
}
|
|
|
|
public updateDeviceState(deviceIdArg: string, featureIdArg: string, valueArg: plugins.shxInterfaces.data.TDeviceStateValue) {
|
|
const device = this.getDeviceById(deviceIdArg);
|
|
if (!device) {
|
|
throw new Error(`Device not found: ${deviceIdArg}`);
|
|
}
|
|
const existingState = device.state.find((stateArg: plugins.shxInterfaces.data.IDeviceState) => stateArg.featureId === featureIdArg);
|
|
if (existingState) {
|
|
existingState.value = valueArg;
|
|
existingState.updatedAt = new Date().toISOString();
|
|
return existingState;
|
|
}
|
|
const newState = {
|
|
featureId: featureIdArg,
|
|
value: valueArg,
|
|
updatedAt: new Date().toISOString(),
|
|
};
|
|
device.state.push(newState);
|
|
return newState;
|
|
}
|
|
}
|