From c6cc532384398e924491dc0391e9c39e54d2fdd0 Mon Sep 17 00:00:00 2001 From: Teodor Ivanov Date: Thu, 5 Mar 2020 10:37:58 +0200 Subject: [PATCH] Return unique for each device instead of "00:11:22:33:44:55:66:77-88" (#95) Remove deviceid from meta.input --- api/hue/templates/lights/all.json | 2 +- index.js | 20 +++++++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/api/hue/templates/lights/all.json b/api/hue/templates/lights/all.json index 78c7719..5032164 100644 --- a/api/hue/templates/lights/all.json +++ b/api/hue/templates/lights/all.json @@ -61,7 +61,7 @@ "function": "mixed", "direction": "omnidirectional" }, - "uniqueid": "00:11:22:33:44:55:66:77-88", + "uniqueid": "{{ uniqueid }}", "swversion": "5.105.0.21169" }, {{/lights}} diff --git a/index.js b/index.js index b031af0..dd4cbb4 100644 --- a/index.js +++ b/index.js @@ -101,7 +101,7 @@ module.exports = function(RED) { if (config.processinput > 0 && nodeDeviceId !== null) { - msg.payload.deviceid = formatUUID(nodeDeviceId); + var deviceid = formatUUID(nodeDeviceId); var meta = { insert: { @@ -110,13 +110,13 @@ module.exports = function(RED) { } } - var deviceAttributes = setDeviceAttributes(msg.payload.deviceid, msg.payload, meta, hubNode.context()); + var deviceAttributes = setDeviceAttributes(deviceid, msg.payload, meta, hubNode.context()); // Output if // 'Process and output' OR // 'Process and output on state change' option is selected if (config.processinput == 2 || (config.processinput == 3 && Object.keys(deviceAttributes.meta.changes).length > 0)) { - payloadHandler(hubNode, msg.payload.deviceid); + payloadHandler(hubNode, deviceid); } } @@ -204,7 +204,10 @@ module.exports = function(RED) { lights: getDevicesAttributes(hubNode.context()), address: req.hostname, username: req.params.username, - date: new Date().toISOString().split('.').shift() + date: new Date().toISOString().split('.').shift(), + uniqueid: function () { + return hueUniqueId(this.id); + } } var output = Mustache.render(template, data, { @@ -221,7 +224,10 @@ module.exports = function(RED) { var data = { lights: getDevicesAttributes(hubNode.context()), - date: new Date().toISOString().split('.').shift() + date: new Date().toISOString().split('.').shift(), + uniqueid: function () { + return hueUniqueId(this.id); + } } var output = Mustache.render(template, data); @@ -333,6 +339,10 @@ module.exports = function(RED) { return ('' + id).replace('.', '').trim(); } + function hueUniqueId(id) { + return (id + '0000').replace(/(.{2})/g,"$1:").substring(0, 23) + '-00'; + } + function getHueHubId(config) { var uuid = '00112233-4455-6677-8899-';