From 0bb35b87a49dee4507d403cc01c80a980e946306 Mon Sep 17 00:00:00 2001 From: Pierre CLEMENT Date: Sat, 6 Jan 2018 00:44:24 +0100 Subject: [PATCH] feat(gateway): add gateway online/offline statuses Close #2 --- .../xiaomi-gateway.js | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/node-red-contrib-xiaomi-gateway/xiaomi-gateway.js b/node-red-contrib-xiaomi-gateway/xiaomi-gateway.js index 5a5d3e9..4053c09 100644 --- a/node-red-contrib-xiaomi-gateway/xiaomi-gateway.js +++ b/node-red-contrib-xiaomi-gateway/xiaomi-gateway.js @@ -10,11 +10,11 @@ module.exports = (RED) => { RED.nodes.createNode(this, config); this.gateway = RED.nodes.getNode(config.gateway); + this.status({fill:"red", shape:"ring", text: "offline"}); + if (this.gateway) { this.on('input', (msg) => { - // var payload = JSON.parse(msg); var payload = msg.payload; - //this.log("Received message from: " + payload.model + " sid: " + payload.sid + " payload: " + payload.data); // Input from gateway if(payload.sid) { @@ -41,6 +41,7 @@ module.exports = (RED) => { // The Input Node function GatewayIn(n) { RED.nodes.createNode(this,n); + this.gatewayNodeId = n.gateway; this.gateway = RED.nodes.getNode(n.gateway); this.group = "224.0.0.50"; this.port = 9898; @@ -48,6 +49,8 @@ module.exports = (RED) => { this.addr = n.ip; this.ipv = this.ip && this.ip.indexOf(":") >= 0 ? "udp6" : "udp4"; + this.status({fill:"red", shape:"ring", text: "offline"}); + var opts = {type:this.ipv, reuseAddr:true}; if (process.version.indexOf("v0.10") === 0) { opts = this.ipv; } var server; @@ -84,10 +87,18 @@ module.exports = (RED) => { } } msg = { payload: jsonMsg }; - if(jsonMsg.token && this.gateway && jsonMsg.data.ip && jsonMsg.data.ip === this.gateway.ip) { - this.gateway.lastToken = jsonMsg.token; - if(!this.gateway.sid) { - this.gateway.sid = jsonMsg.sid; + if(this.gateway && jsonMsg.data.ip && jsonMsg.data.ip === this.gateway.ip) { + RED.nodes.eachNode((tmpNode) => { + if(tmpNode.type.indexOf("xiaomi-gateway") === 0 && tmpNode.gateway == this.gatewayNodeId) { + let tmpNodeInst = RED.nodes.getNode(tmpNode.id); + tmpNodeInst.status({fill:"blue", shape:"dot", text: "online"}); + } + }); + if(jsonMsg.token) { + this.gateway.lastToken = jsonMsg.token; + if(!this.gateway.sid) { + this.gateway.sid = jsonMsg.sid; + } } } this.send(msg); @@ -144,6 +155,8 @@ module.exports = (RED) => { this.ipv = this.ip && this.ip.indexOf(":") >= 0 ? "udp6" : "udp4"; this.multicast = false; + this.status({fill:"red", shape:"ring", text: "offline"}); + var opts = {type:this.ipv, reuseAddr:true}; if (process.version.indexOf("v0.10") === 0) { opts = this.ipv; }