diff --git a/node-red-contrib-xiaomi-actions/xiaomi-actions.html b/node-red-contrib-xiaomi-actions/xiaomi-actions.html index c573973..ad8a517 100644 --- a/node-red-contrib-xiaomi-actions/xiaomi-actions.html +++ b/node-red-contrib-xiaomi-actions/xiaomi-actions.html @@ -53,9 +53,9 @@

Ask the gateway to the list of devices ids.

Outputs

-

- Message to connect to a gateway out node. -

+
    +
  1. Message to connect to a gateway out node.
  2. +
+ + + + + + + + diff --git a/node-red-contrib-xiaomi-actions/xiaomi-actions.js b/node-red-contrib-xiaomi-actions/xiaomi-actions.js index cfe0de4..445ea2b 100644 --- a/node-red-contrib-xiaomi-actions/xiaomi-actions.js +++ b/node-red-contrib-xiaomi-actions/xiaomi-actions.js @@ -73,4 +73,29 @@ module.exports = function(RED) { }); } RED.nodes.registerType("xiaomi-actions double_click", XiaomiActionDoubleClick); + + + + + function XiaomiActionGatewayLight(config) { + RED.nodes.createNode(this, config); + this.gateway = RED.nodes.getNode(config.gateway); + var node = this; + + node.on('input', function(msg) { + if(node.gateway && node.gateway.sid && node.gateway.key && node.gateway.lastToken) { + var rgb = miDevicesUtils.computeColorValue(msg.brightness, msg.color.red, msg.color.green, msg.color.blue); + msg.payload = { + cmd: "write", + data: { + rgb: rgb, + sid: node.gateway.sid, + key: miDevicesUtils.getGatewayKey(node.gateway.key, node.gateway.lastToken) + } + }; + node.send(msg); + } + }); + } + RED.nodes.registerType("xiaomi-actions gateway_light", XiaomiActionGatewayLight); } diff --git a/node-red-contrib-xiaomi-all/xiaomi-all.html b/node-red-contrib-xiaomi-all/xiaomi-all.html index 9f74fc4..ec815d6 100644 --- a/node-red-contrib-xiaomi-all/xiaomi-all.html +++ b/node-red-contrib-xiaomi-all/xiaomi-all.html @@ -29,7 +29,7 @@ diff --git a/node-red-contrib-xiaomi-gateway/xiaomi-gateway.js b/node-red-contrib-xiaomi-gateway/xiaomi-gateway.js index ffff975..75dc238 100644 --- a/node-red-contrib-xiaomi-gateway/xiaomi-gateway.js +++ b/node-red-contrib-xiaomi-gateway/xiaomi-gateway.js @@ -12,8 +12,30 @@ module.exports = function(RED) { if (this.gateway) { node.on('input', function(msg) { - msg.gateway = node.gateway; - node.send(msg); + // var payload = JSON.parse(msg); + var payload = msg.payload; + node.log("Received message from: " + payload.model + " sid: " + payload.sid + " payload: " + payload.data); + + // Input from gateway + if(payload.sid) { + if (payload.sid == node.gateway.sid && ["gateway"].indexOf(payload.model) >= 0) { + if(payload.data.rgb) { + var decomposed = miDevicesUtils.computeColor(payload.data.rgb); + payload.data.brightness = decomposed.brightness; + payload.data.color = decomposed.color; + } + node.send([msg]); + } + } + // Prepare for request + else { + msg.gateway = node.gateway; + msg.sid = node.gateway.sid; + node.send(msg); + } + }); + + node.on("close", function() { }); } } diff --git a/node-red-contrib-xiaomi-ht/xiaomi-ht.js b/node-red-contrib-xiaomi-ht/xiaomi-ht.js index 392a12f..f4c0efa 100644 --- a/node-red-contrib-xiaomi-ht/xiaomi-ht.js +++ b/node-red-contrib-xiaomi-ht/xiaomi-ht.js @@ -23,55 +23,57 @@ module.exports = function(RED) { node.log("Received message from: " + payload.model + " sid: " + payload.sid + " payload: " + payload.data); // Input from gateway - if (payload.sid == node.sid && ["sensor_ht", "weather.v1"].indexOf(payload.model) >= 0) { - var data = payload.data; - miDevicesUtils.setStatus(node, data); + if(payload.sid) { + if (payload.sid == node.sid && ["sensor_ht", "weather.v1"].indexOf(payload.model) >= 0) { + var data = payload.data; + miDevicesUtils.setStatus(node, data); - if (node.output == "0") { - node.send([msg]); - } else if (node.output == "1") { - var temp = null; - var humidity = null; - var pressure = null; + if (node.output == "0") { + node.send([msg]); + } else if (node.output == "1") { + var temp = null; + var humidity = null; + var pressure = null; - if (data.temperature) { - temp = {"payload": data.temperature}; - } - - if (data.humidity) { - humidity = {"payload": data.humidity}; - } - - if (data.pressure) { - pressure = {"payload": data.pressure}; - } - node.send([temp, humidity, pressure]); - } else if (node.output == "2") { - var temp = null; - var humidity = null; - var pressure = null; - - if (data.temperature) { - if (this.divide) { - data.temperature = String(data.temperature / 100); + if (data.temperature) { + temp = {"payload": data.temperature}; } - temp = {"payload": mustache.render(node.temperature, data)} - } - if (data.humidity) { - if (this.divide) { - data.humidity = String(data.humidity / 100); + if (data.humidity) { + humidity = {"payload": data.humidity}; } - humidity = {"payload": mustache.render(node.humidity, data)} - } - if (data.pressure) { - if (this.divide) { - data.pressure = String(data.pressure / 100); + if (data.pressure) { + pressure = {"payload": data.pressure}; } - pressure = {"payload": mustache.render(node.pressure, data)} + node.send([temp, humidity, pressure]); + } else if (node.output == "2") { + var temp = null; + var humidity = null; + var pressure = null; + + if (data.temperature) { + if (this.divide) { + data.temperature = String(data.temperature / 100); + } + temp = {"payload": mustache.render(node.temperature, data)} + } + + if (data.humidity) { + if (this.divide) { + data.humidity = String(data.humidity / 100); + } + humidity = {"payload": mustache.render(node.humidity, data)} + } + + if (data.pressure) { + if (this.divide) { + data.pressure = String(data.pressure / 100); + } + pressure = {"payload": mustache.render(node.pressure, data)} + } + node.send([temp, humidity, pressure]); } - node.send([temp, humidity, pressure]); } } // Prepare for request diff --git a/node-red-contrib-xiaomi-magnet/xiaomi-magnet.js b/node-red-contrib-xiaomi-magnet/xiaomi-magnet.js index 2879838..cfb0d3a 100644 --- a/node-red-contrib-xiaomi-magnet/xiaomi-magnet.js +++ b/node-red-contrib-xiaomi-magnet/xiaomi-magnet.js @@ -22,37 +22,39 @@ module.exports = function(RED) { // var payload = JSON.parse(msg); var payload = msg.payload; - if (payload.sid == node.sid && ["magnet", "sensor_magnet.aq2"].indexOf(payload.model) >= 0) { - var data = payload.data; + if(payload.sid) { + if (payload.sid == node.sid && ["magnet", "sensor_magnet.aq2"].indexOf(payload.model) >= 0) { + var data = payload.data; - // if (data.status && data.status == "open") { - // node.status({fill:"green", shape:"dot", text:"open"}); - // state = "open"; - // } else if (data.status && data.status == "close") { - // node.status({fill:"red", shape:"dot", text:"closed"}); - // state = "closed"; - // } - miDevicesUtils.setStatus(node, data); + // if (data.status && data.status == "open") { + // node.status({fill:"green", shape:"dot", text:"open"}); + // state = "open"; + // } else if (data.status && data.status == "close") { + // node.status({fill:"red", shape:"dot", text:"closed"}); + // state = "closed"; + // } + miDevicesUtils.setStatus(node, data); - if (node.output == "0") { - node.send([msg]); - } else if (node.output == "1") { - var status = null; + if (node.output == "0") { + node.send([msg]); + } else if (node.output == "1") { + var status = null; - if (data.status) { - status = {"payload": data.status}; + if (data.status) { + status = {"payload": data.status}; + } + node.send([status]); + } else if (node.output == "2") { + var status = null; + + if (data.status === 'open') { + status = {"payload": mustache.render(node.openmsg, data)} + } else { + status = {"payload": mustache.render(node.closemsg, data)} + } + node.send([status]); } - node.send([status]); - } else if (node.output == "2") { - var status = null; - - if (data.status === 'open') { - status = {"payload": mustache.render(node.openmsg, data)} - } else { - status = {"payload": mustache.render(node.closemsg, data)} - } - node.send([status]); } } // Prepare for request diff --git a/node-red-contrib-xiaomi-motion/xiaomi-motion.js b/node-red-contrib-xiaomi-motion/xiaomi-motion.js index 5153f6d..1981a0a 100644 --- a/node-red-contrib-xiaomi-motion/xiaomi-motion.js +++ b/node-red-contrib-xiaomi-motion/xiaomi-motion.js @@ -22,43 +22,45 @@ module.exports = function(RED) { // var payload = JSON.parse(msg); var payload = msg.payload; - if (payload.sid == node.sid && payload.model == "motion") { - var data = payload.data; + if (payload.sid) { + if (payload.sid == node.sid && payload.model == "motion") { + var data = payload.data; - // if (data.status && data.status == "open") { - // node.status({fill:"green", shape:"dot", text:"open"}); - // state = "open"; - // } else if (data.status && data.status == "close") { - // node.status({fill:"red", shape:"dot", text:"closed"}); - // state = "closed"; - // } - miDevicesUtils.setStatus(node, data); + // if (data.status && data.status == "open") { + // node.status({fill:"green", shape:"dot", text:"open"}); + // state = "open"; + // } else if (data.status && data.status == "close") { + // node.status({fill:"red", shape:"dot", text:"closed"}); + // state = "closed"; + // } + miDevicesUtils.setStatus(node, data); - if (node.output == "0") { - node.send([msg]); - } else if (node.output == "1") { - var status = null; - var duration = null; + if (node.output == "0") { + node.send([msg]); + } else if (node.output == "1") { + var status = null; + var duration = null; - if (data.status) { - status = {"payload": data.status}; + if (data.status) { + status = {"payload": data.status}; + } + if (data.no_motion) { + status = {"payload": "no_motion"}; + duration = {"payload": {"no_motion": data.no_motion}}; + } + + node.send([[status], [duration]]); + } else if (node.output == "2") { + var status = null; + + if (data.status === 'motion') { + status = {"payload": mustache.render(node.motionmsg, data)} + } else { + status = {"payload": mustache.render(node.nomotionmsg, data)} + } + node.send([status]); } - if (data.no_motion) { - status = {"payload": "no_motion"}; - duration = {"payload": {"no_motion": data.no_motion}}; - } - - node.send([[status], [duration]]); - } else if (node.output == "2") { - var status = null; - - if (data.status === 'motion') { - status = {"payload": mustache.render(node.motionmsg, data)} - } else { - status = {"payload": mustache.render(node.nomotionmsg, data)} - } - node.send([status]); } } // Prepare for request diff --git a/node-red-contrib-xiaomi-switch/xiaomi-switch.js b/node-red-contrib-xiaomi-switch/xiaomi-switch.js index fd5938d..fb19d3c 100644 --- a/node-red-contrib-xiaomi-switch/xiaomi-switch.js +++ b/node-red-contrib-xiaomi-switch/xiaomi-switch.js @@ -21,30 +21,32 @@ module.exports = function(RED) { var payload = msg.payload; // Input from gateway - if (payload.sid == node.sid && ["switch", "sensor_switch.aq2"].indexOf(payload.model) >= 0) { - var data = payload.data; - miDevicesUtils.setStatus(node, data); + if (payload.sid) { + if (payload.sid == node.sid && ["switch", "sensor_switch.aq2"].indexOf(payload.model) >= 0) { + var data = payload.data; + miDevicesUtils.setStatus(node, data); - if (node.output == "0") { - node.send([msg]); - } else if (node.output == "1") { - var status = null; + if (node.output == "0") { + 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) { + status = {"payload": data.status}; + } + node.send([status]); + } else if (node.output == "2") { + var status = null; - if (data.status && data.status == "click") { - status = {"payload": mustache.render(node.outmsg, data)} - node.send([[status],[]]); - } + if (data.status && data.status == "click") { + status = {"payload": mustache.render(node.outmsg, data)} + node.send([[status],[]]); + } - if (data.status && data.status == "double_click") { - status = {"payload": mustache.render(node.outmsgdbcl, data)} - node.send([[],[status]]); + if (data.status && data.status == "double_click") { + status = {"payload": mustache.render(node.outmsgdbcl, data)} + node.send([[],[status]]); + } } } } diff --git a/package.json b/package.json index e36393d..701ba88 100644 --- a/package.json +++ b/package.json @@ -6,9 +6,12 @@ "type": "git", "url": "git+ssh://git@github.com:hrietman/node-red-contrib-xiaomi-devices.git" }, - "license" : "MIT", - "keywords": ["Xiaomi", "node-red"], - "node-red" : { + "license": "MIT", + "keywords": [ + "Xiaomi", + "node-red" + ], + "node-red": { "nodes": { "xiaomi-ht": "node-red-contrib-xiaomi-ht/xiaomi-ht.js", "xiaomi-magnet": "node-red-contrib-xiaomi-magnet/xiaomi-magnet.js", diff --git a/utils.js b/utils.js index b9793f5..44a7caa 100644 --- a/utils.js +++ b/utils.js @@ -42,5 +42,27 @@ module.exports = { prepareForGatewayRequest: function(node, msg) { msg.sid = node.sid; msg.gateway = node.gateway; + }, + computeColorValue: function (brightness, red, green, blue) { + return Math.round(256*256*256*brightness) + (256*256*red) + (256*green) + blue; + }, + computeColor: function (rgb) { + var blue = rgb % 256; + rgb = Math.max(rgb - blue, 0); + + var green = rgb % (256 * 256); + rgb = Math.max(rgb - green, 0); + green /= 256; + + var red = rgb % (256 * 256 * 256); + rgb = Math.max(rgb - red, 0); + red /= 256 * 256; + + var brightness = rgb / (256*256*256); + + return { + brightness: brightness, + color: { red: red, green: green, blue: blue } + }; } }