diff --git a/docs/devices/README.md b/docs/devices/README.md index bc7daee..3dc5c06 100644 --- a/docs/devices/README.md +++ b/docs/devices/README.md @@ -95,10 +95,12 @@ and methods of generic devices are also available for specific devices types. * `device.defineProperty(string)`, indicate that a property should be fetched from the device * `device.defineProperty(string, function)`, indicate that a property should be fetched from the device and mapped with the given function. * `device.setProperty(string, mixed)`, set the value of a property +* `device.getProperties(Array[string]): Object`, get the given properties if they are monitored * `device.monitor()`, monitor the device for changes in defined properties * `device.stopMonitoring()`, stop monitoring the device for changes * `device.on('propertyChanged', function)`, receive changes to defined properties -* `device.getProperties(array)`, get the given properties from the device, returns a promise + +* `device.loadProperties(Array[string])`, load properties from the device ### Methods diff --git a/lib/device.js b/lib/device.js index 93f9cbe..eeece7d 100644 --- a/lib/device.js +++ b/lib/device.js @@ -45,6 +45,7 @@ class Device extends EventEmitter { this._properties = {}; this._propertiesToMonitor = []; this._propertyDefinitions = {}; + this._reversePropertyDefinitions = {}; this._loadProperties = this._loadProperties.bind(this); @@ -250,6 +251,9 @@ class Device extends EventEmitter { def.mapper = IDENTITY_MAPPER; } + if(def.name) { + this._reversePropertyDefinitions[def.name] = name; + } this._propertyDefinitions[name] = def; } @@ -280,7 +284,7 @@ class Device extends EventEmitter { _loadProperties() { if(this._propertiesToMonitor.length === 0 || this.writeOnly) return Promise.resolve(); - return this.getProperties(this._propertiesToMonitor) + return this.loadProperties(this._propertiesToMonitor) .then(values => { Object.keys(values).forEach(key => { this.setProperty(key, values[key]); @@ -314,6 +318,18 @@ class Device extends EventEmitter { } getProperties(props) { + const result = {}; + props.forEach(key => { + result[key] = this._properties[key]; + }); + return result; + } + + loadProperties(props) { + // Rewrite property names to device internal ones + props = props.map(key => this._reversePropertyDefinitions[key] || key); + + // Call get_prop to map everything return this.call('get_prop', props) .then(result => { const obj = {}; diff --git a/lib/devices/gateway/subdevice.js b/lib/devices/gateway/subdevice.js index e5e59e8..dfa5cf6 100644 --- a/lib/devices/gateway/subdevice.js +++ b/lib/devices/gateway/subdevice.js @@ -79,6 +79,35 @@ class SubDevice extends EventEmitter { this._propertyDefinitions[name] = def; } + setProperty(key, value) { + const oldValue = this._properties[key]; + + if(oldValue !== value) { + this._properties[key] = value; + this.debug('Property', key, 'changed from', oldValue, 'to', value); + this.emit('propertyChanged', { + property: key, + oldValue: oldValue, + value: value + }); + } + } + + getProperties(props) { + const result = {}; + props.forEach(key => { + result[key] = this._properties[key]; + }); + return result; + } + + /** + * Stub for loadProperties to match full device. + */ + loadProperties(props) { + return Promise.resolve(this.getProperties(props)); + } + /** * Call a method for this sub device. */ diff --git a/lib/devices/vacuum.js b/lib/devices/vacuum.js index e7a0e52..3aea89c 100644 --- a/lib/devices/vacuum.js +++ b/lib/devices/vacuum.js @@ -233,8 +233,8 @@ class Vacuum extends Device { })); } - getProperties(props) { - // We override getProperties to use get_status and get_consumables + loadProperties(props) { + // We override loadProperties to use get_status and get_consumables return Promise.all([ this.call('get_status'), this.call('get_consumable')