diff --git a/node-red-contrib-xiaomi-gateway/xiaomi-gateway.html b/node-red-contrib-xiaomi-configurator/xiaomi-configurator.html similarity index 94% rename from node-red-contrib-xiaomi-gateway/xiaomi-gateway.html rename to node-red-contrib-xiaomi-configurator/xiaomi-configurator.html index 0ea9b93..e578dc7 100644 --- a/node-red-contrib-xiaomi-gateway/xiaomi-gateway.html +++ b/node-red-contrib-xiaomi-configurator/xiaomi-configurator.html @@ -1,12 +1,12 @@ - - diff --git a/node-red-contrib-xiaomi-gateway/xiaomi-gateway.js b/node-red-contrib-xiaomi-configurator/xiaomi-configurator.js similarity index 61% rename from node-red-contrib-xiaomi-gateway/xiaomi-gateway.js rename to node-red-contrib-xiaomi-configurator/xiaomi-configurator.js index e6838e7..c13db4f 100644 --- a/node-red-contrib-xiaomi-gateway/xiaomi-gateway.js +++ b/node-red-contrib-xiaomi-configurator/xiaomi-configurator.js @@ -1,6 +1,6 @@ module.exports = function(RED) { - function RemoteServerNode(n) { + function XiaomiConfiguratorNode(n) { RED.nodes.createNode(this, n); this.name = n.name; this.deviceList = n.deviceList || []; @@ -8,6 +8,6 @@ module.exports = function(RED) { var node = this; } - RED.nodes.registerType("xiaomi-gateway", RemoteServerNode); + RED.nodes.registerType("xiaomi-configurator", XiaomiConfiguratorNode); } diff --git a/node-red-contrib-xiaomi-ht/icons/temp.png b/node-red-contrib-xiaomi-ht/icons/temp.png deleted file mode 100644 index 5979561..0000000 Binary files a/node-red-contrib-xiaomi-ht/icons/temp.png and /dev/null differ diff --git a/node-red-contrib-xiaomi-ht/xiaomi-ht.html b/node-red-contrib-xiaomi-ht/xiaomi-ht.html index 2679131..d604a69 100644 --- a/node-red-contrib-xiaomi-ht/xiaomi-ht.html +++ b/node-red-contrib-xiaomi-ht/xiaomi-ht.html @@ -3,7 +3,7 @@ category: 'function', color: '#a6bbcf', defaults: { - gateway: {value:"", type:"xiaomi-gateway"}, + gateway: {value:"", type:"xiaomi-configurator"}, name: {value: ""}, sid: {value: "", required: true}, temperature: {value: "{{temperature}}"}, diff --git a/node-red-contrib-xiaomi-socket/icons/outlet-icon.png b/node-red-contrib-xiaomi-socket/icons/outlet-icon.png new file mode 100644 index 0000000..f9c7e4a Binary files /dev/null and b/node-red-contrib-xiaomi-socket/icons/outlet-icon.png differ diff --git a/node-red-contrib-xiaomi-socket/xiaomi-socket.html b/node-red-contrib-xiaomi-socket/xiaomi-socket.html new file mode 100644 index 0000000..022aac5 --- /dev/null +++ b/node-red-contrib-xiaomi-socket/xiaomi-socket.html @@ -0,0 +1,88 @@ + + + + + diff --git a/node-red-contrib-xiaomi-socket/xiaomi-socket.js b/node-red-contrib-xiaomi-socket/xiaomi-socket.js new file mode 100644 index 0000000..d12424e --- /dev/null +++ b/node-red-contrib-xiaomi-socket/xiaomi-socket.js @@ -0,0 +1,107 @@ +module.exports = function(RED) { + "use strict"; + var mustache = require("mustache"); + var crypto = require("crypto"); + + function XiaomiPlugNode(config) { + RED.nodes.createNode(this, config); + this.gateway = RED.nodes.getNode(config.gateway); + this.sid = config.sid; + this.output = config.output; + this.onmsg = config.onmsg; + this.offmsg = config.offmsg; + + var node = this; + var currentToken = ""; + var state = ""; + + node.status({fill:"yellow", shape:"ring", text:"no key"}); + + if (this.gateway) { + node.on('input', function(msg) { + // var payload = JSON.parse(msg); + var payload = msg.payload; + + if (payload.cmd == "heartbeat" && payload.model == "gateway") { + var key = "c7utmdo2acpzai5b"; + var token = payload.token; + + if (token) { + var cipher = crypto.createCipheriv('aes128', key, (new Buffer("17996d093d28ddb3ba695a2e6f58562e", "hex"))); + var encoded_string = cipher.update(token, 'utf8', 'hex'); + + encoded_string += cipher.final('hex'); + currentToken = encoded_string.substring(0,32); + if (state == "") { + node.status({fill:"yellow", shape:"dot", text:"unknown state"}); + } + } + } + if (payload == 'on') { + var cmd = + { "cmd":"write", + "sid": node.sid, + "model": "plug", + "data": JSON.stringify({"status":"on", "key": currentToken }) + } + msg.payload = JSON.stringify(cmd); + node.send([[],[msg]]); + + } else if (payload == "off") { + var cmd = + { "cmd":"write", + "sid": node.sid, + "model": "plug", + "data": JSON.stringify({"status":"off", "key": currentToken }) + } + msg.payload = JSON.stringify(cmd); + node.send([[],[msg]]); + + } else if (payload.sid == node.sid && payload.model == "plug") { + var data = JSON.parse(payload.data) + + if (currentToken == "") { + node.status({fill:"yellow", shape:"dot", text:"no key"}); + } else if (data.status && data.status == "on") { + node.status({fill:"green", shape:"dot", text:"on"}); + state = "on"; + } else if (data.status && data.status == "off") { + node.status({fill:"red", shape:"dot", text:"off"}); + state = "off"; + } + + if (node.output == "0") { + msg.payload = payload; + node.send([msg]); + } else if (node.output == "1") { + var status = null; + + if (data.status) { + status = {"payload": data.status}; + } + node.send([status]); + } else if (node.output == "2") { + var status = null; + + if (data.status === 'on') { + status = {"payload": mustache.render(node.onmsg, data)} + } else { + status = {"payload": mustache.render(node.offmsg, data)} + } + node.send([status]); + } + } + }); + + node.on("close", function() { + }); + + } else { + // no gateway configured + } + + } + + RED.nodes.registerType("xiaomi-plug", XiaomiPlugNode); + +} diff --git a/package.json b/package.json index a406e14..a4ca203 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ }, "homepage": "https://gitlab.com/h-rietman/xiaomi-devices-node-red#README", "dependencies": { + "cryptojs": "^2.5.3", "mustache": "^2.3.0" } }