Skip to content

Commit

Permalink
Loading of properties is now done via loadProperties
Browse files Browse the repository at this point in the history
getProperties has been modified to return properties that have been
defined and are monitored. It now returns an object instead of a
promise.
  • Loading branch information
aholstenson committed Apr 17, 2017
1 parent f37da07 commit 4c65731
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
4 changes: 3 additions & 1 deletion docs/devices/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 17 additions & 1 deletion lib/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Device extends EventEmitter {
this._properties = {};
this._propertiesToMonitor = [];
this._propertyDefinitions = {};
this._reversePropertyDefinitions = {};

this._loadProperties = this._loadProperties.bind(this);

Expand Down Expand Up @@ -250,6 +251,9 @@ class Device extends EventEmitter {
def.mapper = IDENTITY_MAPPER;
}

if(def.name) {
this._reversePropertyDefinitions[def.name] = name;
}
this._propertyDefinitions[name] = def;
}

Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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 = {};
Expand Down
29 changes: 29 additions & 0 deletions lib/devices/gateway/subdevice.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
4 changes: 2 additions & 2 deletions lib/devices/vacuum.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 4c65731

Please sign in to comment.