Skip to content

Commit

Permalink
use distinct init id, try to fix rdmtc/RedMatic-HomeKit#71
Browse files Browse the repository at this point in the history
  • Loading branch information
hobbyquaker committed Jan 15, 2019
1 parent beab10d commit 5b7a5d5
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 26 deletions.
63 changes: 39 additions & 24 deletions nodes/ccu-connection.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const os = require('os');
const path = require('path');
const fs = require('fs');
const crypto = require('crypto');

const base62 = require('buffer-base62').toBase62;
const stringSimilarity = require('string-similarity');
const nextport = require('nextport');
const hmDiscover = require('hm-discover');
Expand Down Expand Up @@ -1009,7 +1011,10 @@ module.exports = function (RED) {
rpcInit(iface) {
return new Promise((resolve, reject) => {
const initUrl = this.rpcServer(iface);
const initId = 'nr_' + (Math.round(Math.random() * 65535)).toString(16) + '_' + iface;
let initId;
const hash = base62(crypto.createHash('sha1').update(initUrl).digest()).slice(0, 6);
initId = 'nr_' + hash + '_' + iface;

this.logger.info('init ' + iface + ' ' + initUrl + ' ' + initId);
this.methodCall(iface, 'init', [initUrl, initId])
.then(() => {
Expand Down Expand Up @@ -1187,9 +1192,9 @@ module.exports = function (RED) {
Object.keys(this.rpcMethods).forEach(method => {
this.servers[protocol].on(method, (err, params, callback) => {
if (err) {
this.logger.error('rpc <', iface, method, err);
this.logger.error('rpc <', protocol, method, err);
}
this.logger.debug('rpc <', iface, method, JSON.stringify(params));
this.logger.debug('rpc <', protocol, method, JSON.stringify(params));
if (method === 'event') {
method = 'eventSingle';
}
Expand Down Expand Up @@ -1380,42 +1385,47 @@ module.exports = function (RED) {
}
}

getIfaceFromIdInit(idInit) {
const match = idInit.match(/^nr_[0-9a-zA-Z]{6}_([a-zA-Z-]+)$/);
return match && match[1];
}

get rpcMethods() {
return {
'system.listMethods': (_, params, callback) => {
const [idInit] = params;
const iface = idInit.replace(/^nr_[0-9a-f]*_?/, '');
const iface = this.getIfaceFromIdInit(idInit);
const res = Object.keys(this.rpcMethods);
this.logger.debug(' >', iface, 'system.listMethods', JSON.stringify(res));
callback(null, res);
},
setReadyConfig: (_, params, callback) => {
const [idInit] = params;
const iface = idInit.replace(/^nr_[0-9a-f]*_?/, '');
const iface = this.getIfaceFromIdInit(idInit);
this.logger.debug(' >', iface, 'setReadyConfig ""');
callback(null, '');
},
updateDevice: (_, params, callback) => {
const [idInit] = params;
const iface = idInit.replace(/^nr_[0-9a-f]*_?/, '');
const iface = this.getIfaceFromIdInit(idInit);
this.logger.debug(' >', iface, 'updateDevice ""');
callback(null, '');
},
replaceDevice: (_, params, callback) => {
const [idInit] = params;
const iface = idInit.replace(/^nr_[0-9a-f]*_?/, '');
const iface = this.getIfaceFromIdInit(idInit);
this.logger.debug(' >', iface, 'replaceDevice ""');
callback(null, '');
},
readdedDevice: (_, params, callback) => {
const [idInit] = params;
const iface = idInit.replace(/^nr_[0-9a-f]*_?/, '');
const iface = this.getIfaceFromIdInit(idInit);
this.logger.debug(' >', iface, 'readdedDevice ""');
callback(null, '');
},
newDevices: (_, params, callback) => {
const [idInit, devices] = params;
const iface = idInit.replace(/^nr_[0-9a-f]*_?/, '');
const iface = this.getIfaceFromIdInit(idInit);

devices.forEach(device => {
this.newDevice(iface, device);
Expand All @@ -1428,7 +1438,7 @@ module.exports = function (RED) {
},
deleteDevices: (_, params, callback) => {
const [idInit, devices] = params;
const iface = idInit.replace(/^nr_[0-9a-f]*_?/, '');
const iface = this.getIfaceFromIdInit(idInit);

devices.forEach(device => {
this.deleteDevice(iface, device);
Expand All @@ -1441,21 +1451,21 @@ module.exports = function (RED) {
},
listDevices: (_, params, callback) => {
const [idInit] = params;
const iface = idInit.replace(/^nr_[0-9a-f]*_?/, '');
const iface = this.getIfaceFromIdInit(idInit);
const res = this.listDevices(iface) || [];
this.logger.debug(' >', iface, 'listDevices', JSON.stringify(res));
callback(null, res);
},
event: (_, params, callback) => {
const [idInit] = params;
const iface = idInit.replace(/^nr_[0-9a-f]*_?/, '');
const iface = this.getIfaceFromIdInit(idInit);
this.logger.debug(' >', iface, 'event ""');
this.publishEvent(params);
callback(null, '');
},
eventSingle: (_, params, callback) => {
const [idInit] = params;
const iface = idInit.replace(/^nr_[0-9a-f]*_?/, '');
const iface = this.getIfaceFromIdInit(idInit);
this.logger.debug(' >', iface, 'event ""');
this.publishEvent(params);

Expand Down Expand Up @@ -1486,7 +1496,7 @@ module.exports = function (RED) {
pong = false;
}

if (datapoint === 'WORKING') {
if (datapoint === 'WORKING' || datapoint === 'WORKING_SLATS') {
working = value;
} else if (datapoint === 'PROCESS') {
working = Boolean(value);
Expand All @@ -1501,7 +1511,7 @@ module.exports = function (RED) {
direction = value;
}
}
iface = idInit.replace(/^nr_[0-9a-f]*_?/, '');
iface = this.getIfaceFromIdInit(idInit);
}
result.push('');
} else if (call && this.rpcMethods[call.methodName]) {
Expand Down Expand Up @@ -1557,7 +1567,7 @@ module.exports = function (RED) {
*/
unsubscribeSysvar(id) {
if (this.sysvarCallbacks[id]) {
this.logger.debug('unsubscribeSysvar', id);
this.logger.trace('unsubscribeSysvar', id);
delete this.sysvarCallbacks[id];
return true;
}
Expand Down Expand Up @@ -1591,7 +1601,7 @@ module.exports = function (RED) {
*/
unsubscribeProgram(id) {
if (this.programCallbacks[id]) {
this.logger.debug('unsubscribeProgram', id);
this.logger.trace('unsubscribeProgram', id);
delete this.programCallbacks[id];
return true;
}
Expand Down Expand Up @@ -1667,7 +1677,7 @@ module.exports = function (RED) {

unsubscribe(id) {
if (this.callbacks[id]) {
this.logger.debug('unsubscribe', id);
this.logger.trace('unsubscribe', id);
delete this.callbacks[id];

Object.keys(this.callbackBlacklists).forEach(dp => {
Expand Down Expand Up @@ -1777,7 +1787,7 @@ module.exports = function (RED) {

publishEvent(params, working, direction) {
const [idInit, channel, datapoint, payload] = params;
const iface = idInit.replace(/^nr_[0-9a-f]*_?/, '');
const iface = this.getIfaceFromIdInit(idInit);

this.lastEvent[iface] = now();
if (this.hadTimeout.has(iface)) {
Expand All @@ -1793,12 +1803,15 @@ module.exports = function (RED) {

const msg = this.createMessage(iface, channel, datapoint, payload, {cache: false, working, direction});

if (msg.channelType && msg.channelType.match(/BLIND|DIMMER/) && msg.datapoint === 'LEVEL' && !working) {
if (msg.channelType && msg.channelType.match(/BLIND|DIMMER|JALOUSIE/) && (msg.datapoint === 'LEVEL' || msg.datapoint === 'LEVEL_SLATS') && !working) {
clearTimeout(this.workingTimeout[msg.datapointName]);
this.workingTimeout[msg.datapointName] = setTimeout(() => {
const datapointNamePrefix = iface + '.' + channel + '.';
if (this.values[datapointNamePrefix + 'WORKING']) {
msg.working = this.values[datapointNamePrefix + 'WORKING'].value;
if (this.values[datapointNamePrefix + 'WORKING_SLATS']) {
msg.working = this.values[datapointNamePrefix + 'WORKING'].value || this.values[datapointNamePrefix + 'WORKING_SLATS'].value;
}
} else if (this.values[datapointNamePrefix + 'PROCESS']) {
msg.working = Boolean(this.values[datapointNamePrefix + 'PROCESS'].value);
}
Expand Down Expand Up @@ -1984,10 +1997,12 @@ module.exports = function (RED) {
// Resolve();
// });
} else {
this.setValueTimers[id] = setTimeout(() => {
delete this.setValueTimers[id];
this.setValueDeferred(id);
}, this.setValueThrottle);
if (iface === 'BidCos-RF') {
this.setValueTimers[id] = setTimeout(() => {
delete this.setValueTimers[id];
this.setValueDeferred(id);
}, this.setValueThrottle);
}

return new Promise((resolve, reject) => {
this.methodCall(iface, 'setValue', params).then(resolve).catch(err => {
Expand Down
30 changes: 30 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
"license": "MIT",
"dependencies": {
"binrpc": "^3.2.2",
"buffer-base62": "^0.1.2",
"hm-discover": "^1.1.0",
"homematic-rega": "^1.3.2",
"homematic-xmlrpc": "^1.0.2",
"string-similarity": "^3.0.0",
"mqtt-wildcard": "^3.0.9",
"nextport": "^1.0.0"
"nextport": "^1.0.0",
"obj-ease": "^1.0.1",
"string-similarity": "^3.0.0"
},
"node-red": {
"nodes": {
Expand Down

0 comments on commit 5b7a5d5

Please sign in to comment.