From 864d60d2ccc75a485ae6ad71595bede5ea9f7b8b Mon Sep 17 00:00:00 2001 From: Harald Rietman Date: Sun, 2 Jul 2017 17:21:47 +0200 Subject: [PATCH] Added magnet --- node-red-contrib-xiaomi-ht/xiaomi-ht.html | 5 +- .../icons/door-icon.png | Bin 0 -> 4681 bytes .../icons/outlet-icon.png | Bin 0 -> 16281 bytes .../xiaomi-magnet.html | 89 +++++++++++++++++ .../xiaomi-magnet.js | 80 +++++++++++++++ .../xiaomi-socket.html | 5 +- .../icons/light-icon.png | Bin 0 -> 10323 bytes .../xiaomi-switch.html | 93 ++++++++++++++++++ .../xiaomi-switch.js | 72 ++++++++++++++ 9 files changed, 340 insertions(+), 4 deletions(-) create mode 100644 node-red-contrib-xiaomi-magnet/icons/door-icon.png create mode 100644 node-red-contrib-xiaomi-magnet/icons/outlet-icon.png create mode 100644 node-red-contrib-xiaomi-magnet/xiaomi-magnet.html create mode 100644 node-red-contrib-xiaomi-magnet/xiaomi-magnet.js create mode 100644 node-red-contrib-xiaomi-switch/icons/light-icon.png create mode 100644 node-red-contrib-xiaomi-switch/xiaomi-switch.html create mode 100644 node-red-contrib-xiaomi-switch/xiaomi-switch.js diff --git a/node-red-contrib-xiaomi-ht/xiaomi-ht.html b/node-red-contrib-xiaomi-ht/xiaomi-ht.html index d604a69..d26bb1c 100644 --- a/node-red-contrib-xiaomi-ht/xiaomi-ht.html +++ b/node-red-contrib-xiaomi-ht/xiaomi-ht.html @@ -1,7 +1,7 @@ + + + + diff --git a/node-red-contrib-xiaomi-magnet/xiaomi-magnet.js b/node-red-contrib-xiaomi-magnet/xiaomi-magnet.js new file mode 100644 index 0000000..55c39f1 --- /dev/null +++ b/node-red-contrib-xiaomi-magnet/xiaomi-magnet.js @@ -0,0 +1,80 @@ +module.exports = function(RED) { + "use strict"; + var mustache = require("mustache"); + + function XiaomiMagnetNode(config) { + RED.nodes.createNode(this, config); + this.gateway = RED.nodes.getNode(config.gateway); + this.sid = config.sid; + this.output = config.output; + this.openmsg = config.openmsg; + this.closemsg = config.closemsg; + + var node = this; + var state = ""; + + // node.status({fill:"yellow", shape:"dot", text:"unknown state"}); + node.status({fill:"grey",shape:"ring",text:"battery"}); + + if (this.gateway) { + node.on('input', function(msg) { + // var payload = JSON.parse(msg); + var payload = msg.payload; + + if (payload.sid == node.sid && payload.model == "magnet") { + var data = JSON.parse(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"; + // } + + if (data.voltage) { + if (data.voltage < 2500) { + node.status({fill:"red",shape:"dot",text:"battery"}); + } else if (data.voltage < 2900) { + node.status({fill:"yellow",shape:"dot",text:"battery"}); + } else { + node.status({fill:"green",shape:"dot",text:"battery"}); + } + } + + + 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 === 'open') { + status = {"payload": mustache.render(node.openmsg, data)} + } else { + status = {"payload": mustache.render(node.closemsg, data)} + } + node.send([status]); + } + } + }); + + node.on("close", function() { + }); + + } else { + // no gateway configured + } + + } + + RED.nodes.registerType("xiaomi-magnet", XiaomiMagnetNode); + +} diff --git a/node-red-contrib-xiaomi-socket/xiaomi-socket.html b/node-red-contrib-xiaomi-socket/xiaomi-socket.html index 1b82e79..1ef8cf6 100644 --- a/node-red-contrib-xiaomi-socket/xiaomi-socket.html +++ b/node-red-contrib-xiaomi-socket/xiaomi-socket.html @@ -1,7 +1,7 @@ + + + + diff --git a/node-red-contrib-xiaomi-switch/xiaomi-switch.js b/node-red-contrib-xiaomi-switch/xiaomi-switch.js new file mode 100644 index 0000000..fe1ccd4 --- /dev/null +++ b/node-red-contrib-xiaomi-switch/xiaomi-switch.js @@ -0,0 +1,72 @@ +module.exports = function(RED) { + "use strict"; + var mustache = require("mustache"); + + function XiaomiSwitchNode(config) { + RED.nodes.createNode(this, config); + this.gateway = RED.nodes.getNode(config.gateway); + this.sid = config.sid; + this.output = config.output; + this.outmsg = config.outmsg; + this.outmsgdbcl = config.outmsgdbcl; + + var node = this; + + node.status({fill:"grey",shape:"ring",text:"battery"}); + + if (this.gateway) { + node.on('input', function(msg) { + // var payload = JSON.parse(msg); + var payload = msg.payload; + + if (payload.sid == node.sid && payload.model == "switch") { + var data = JSON.parse(payload.data) + + if (data.voltage) { + if (data.voltage < 2500) { + node.status({fill:"red",shape:"dot",text:"battery"}); + } else if (data.voltage < 2900) { + node.status({fill:"yellow",shape:"dot",text:"battery"}); + } else { + node.status({fill:"green",shape:"dot",text:"battery"}); + } + } + + 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 && 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]]); + } + } + } + }); + + node.on("close", function() { + }); + + } else { + // no gateway configured + } + + } + + RED.nodes.registerType("xiaomi-switch", XiaomiSwitchNode); + +}