You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
898 B
JavaScript

const events = require('events')
class DeviceRegistry {
constructor() {
this.Devices = {}
this.events = new events.EventEmitter()
this.DeviceStates = {} // for keeping device states
}
register(device) {
this.Devices[ device.name ] = device
}
getDevices() {
return Object.values(this.Devices)
}
stateSet(deviceType, deviceId, stateData) {
const key = `${ deviceType }:${ deviceId }`
if (!this.DeviceStates[ key ])
this.DeviceStates[ key ] = {}
Object.assign(this.DeviceStates[ key], stateData)
}
deviceEvent(deviceType, deviceId, eventData) {
this.events.emit('event', deviceType, deviceId, eventData)
}
/*tryReceive(deviceName, data) {
return this.Devices[ deviceName ].tryReceive(data)
}*/
}
const registry = new DeviceRegistry()
module.exports = registry