Skip to content

Commit

Permalink
create listeners for optional characteristics (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
hobbyquaker committed Oct 20, 2018
1 parent b4e301e commit 7d6de73
Showing 1 changed file with 41 additions and 29 deletions.
70 changes: 41 additions & 29 deletions nodes/redmatic-homekit-universal.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,42 +40,24 @@ module.exports = function (RED) {
service = acc.addService(hap.Service[s.service], s.name, s.subtype);
}
service.characteristics.forEach(c => {
const cName = c.displayName.replace(/ /g, '');

this.debug('create change listener', s.subtype, cName);

const changeListener = obj => {
const topic = s.subtype + '/' + cName;
this.debug('hap ->', topic, obj.newValue);
if (obj && obj.context && obj.context.request) {
this.send({
topic,
payload: obj.newValue
});
}
};

this.listeners.push({subtype: s.subtype, characteristic: c, listener: changeListener, cName});

c.on('change', changeListener);
this.addListener(s.subtype, c);
});
});

this.on('input', msg => {
const [subtype, c] = msg.topic.split('/');
const service = acc.getService(subtype);
if (service) {
//if (service.testCharacteristic(hap.Characteristic[c])) {
if (typeof msg.payload === 'object') {
this.debug('setProps ' + msg.topic + ' ' + JSON.stringify(msg.payload));
service.getCharacteristic(hap.Characteristic[c]).setProps(msg.payload);
} else {
this.debug('-> hap ' + msg.topic + ' ' + msg.payload);
service.updateCharacteristic(hap.Characteristic[c], msg.payload);
}
//} else {
// this.error('unknown characteristic ' + c + ' on subtype ' + subtype);
//}
if (!this.hasListener(subtype, c)) {
this.addListener(subtype, service.getCharacteristic(hap.Characteristic[c]));
}
if (typeof msg.payload === 'object') {
this.debug('setProps ' + msg.topic + ' ' + JSON.stringify(msg.payload));
service.getCharacteristic(hap.Characteristic[c]).setProps(msg.payload);
} else {
this.debug('-> hap ' + msg.topic + ' ' + msg.payload);
service.updateCharacteristic(hap.Characteristic[c], msg.payload);
}
} else {
this.error('unknown subtype ' + subtype);
}
Expand All @@ -88,6 +70,36 @@ module.exports = function (RED) {
});
});
}

addListener(subtype, c) {
const cName = c.displayName.replace(/ /g, '');
this.debug('create change listener ' + subtype + ' ' + cName);

const changeListener = obj => {
const topic = subtype + '/' + cName;
this.debug('hap ->', topic, obj.newValue);
if (obj && obj.context && obj.context.request) {
this.send({
topic,
payload: obj.newValue
});
}
};

this.listeners.push({subtype, characteristic: c, listener: changeListener, cName});

c.on('change', changeListener);
}

hasListener(subtype, characteristicName) {
let res = false;
this.listeners.forEach(l => {
if (subtype === l.subtype && characteristicName === l.cName) {
res = true;
}
});
return res;
}
}

RED.nodes.registerType('redmatic-homekit-universal', RedMaticHomeKitUniversal);
Expand Down

0 comments on commit 7d6de73

Please sign in to comment.