diff --git a/.gitignore b/.gitignore index ac753a6..724246b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ .DS_Store .idea -dist/ /node_modules .log package-lock.json diff --git a/node-red-contrib-xiaomi-socket-wifi/icons/outlet-wifi-icon.png b/node-red-contrib-xiaomi-socket-wifi/icons/outlet-wifi-icon.png deleted file mode 100644 index 4da9524..0000000 Binary files a/node-red-contrib-xiaomi-socket-wifi/icons/outlet-wifi-icon.png and /dev/null differ diff --git a/node-red-contrib-xiaomi-socket-wifi/xiaomi-socket-wifi.html b/node-red-contrib-xiaomi-socket-wifi/xiaomi-socket-wifi.html deleted file mode 100644 index 41d1969..0000000 --- a/node-red-contrib-xiaomi-socket-wifi/xiaomi-socket-wifi.html +++ /dev/null @@ -1,67 +0,0 @@ - - - - - diff --git a/node-red-contrib-xiaomi-socket-wifi/xiaomi-socket-wifi.js b/node-red-contrib-xiaomi-socket-wifi/xiaomi-socket-wifi.js deleted file mode 100644 index d8e6748..0000000 --- a/node-red-contrib-xiaomi-socket-wifi/xiaomi-socket-wifi.js +++ /dev/null @@ -1,149 +0,0 @@ -const miio = require("miio"); - -module.exports = (RED) => { - var connectionState = "timeout"; - var retryTimer; - var delayedStatusMsgTimer; - - - function XiaomiPlugWifiNode(config) { - RED.nodes.createNode(this, config); - this.ip = config.ip; - this.plug = null; - - this.status({fill: "yellow", shape: "dot", text: "connecting"}); - - miio.device({address: this.ip}) - .then((plug) => { - this.plug = plug; - this.status({fill:"green", shape:"dot", text:"connected"}); - connectionState = "connected"; - delayedStatusMsgUpdate(this); - - this.plug.on('propertyChanged', (e) => { - if (e.property === "power") { - if (e.value['0']) { - setState("on"); - } else { - setState("off"); - } - } - }); - watchdog(); - }) - .catch((error) => { - connectionState = "reconnecting"; - watchdog(); - }) - - this.on('input', (msg) => { - var payload = msg.payload; - if (connectionState === "connected") { - if (payload == 'on') { - this.plug.setPower(true); - } - - if (payload == 'off') { - this.plug.setPower(false); - } - } - }); - - this.on('close', (done) => { - if (retryTimer) { - clearTimeout(retryTimer); - } - if (delayedStatusMsgTimer) { - clearTimeout(delayedStatusMsgTimer); - } - if (this.plug) { - this.plug.destroy(); - } - done(); - }); - - var setState = (state) => { - if (this.plug) { - let status = { - payload: { - id: this.plug.id, - type: this.plug.type, - model: this.plug.model, - capabilities: this.plug.capabilities, - address: this.plug.address, - port: this.plug.port, - power: this.plug.power(), - state: state - } - }; - this.send(status); - } - }; - - var delayedStatusMsgUpdate = () => { - delayedStatusMsgTimer = setTimeout(() => { - if (this.plug.power()['0']) { - setState("on"); - } else { - setState("off"); - } - }, 1500); - }; - - var discoverDevice = () => { - miio.device({address: this.ip}) - .then((plug) => { - if (this.plug == null) { - this.plug = plug; - this.plug.on('propertyChanged', (e) => { - if (e.property === "power") { - if (e.value['0']) { - setState("on"); - } else { - setState("off"); - } - } - }); - } - if (connectionState === "reconnecting") { - this.status({fill:"green", shape:"dot", text:"connected"}); - connectionState = "connected"; - delayedStatusMsgUpdate(); - } - }) - .catch((error) => { - connectionState = "reconnecting"; - if (this.plug) { - this.plug.destroy(); - this.plug = null; - } - }) - }; - - var watchdog = () => { - var node = this; - function retryTimer() { - discoverDevice(); - if (connectionState === "reconnecting") { - node.status({fill: "red", shape: "dot", text: "reconnecting"}); - } - setTimeout(retryTimer, 30000); - } - setTimeout(retryTimer, 30000); - } - } - - RED.nodes.registerType("xiaomi-plug-wifi", XiaomiPlugWifiNode); - - process.on('unhandledRejection', function(reason, p) { - // console.log("Possibly Unhandled Rejection at: Promise ", p, " reason: ", reason); - var message = reason + ""; - if (message.indexOf("Call to device timed out") >= 0) { - if (this.plug) { - console.log("Issue with miio package; discard plug and reconnect."); - this.plug.destroy(); - this.plug = null; - } - } - }); -} diff --git a/node-red-contrib-xiaomi-socket/icons/outlet-icon.png b/node-red-contrib-xiaomi-socket/icons/outlet-icon.png deleted file mode 100644 index f9c7e4a..0000000 Binary files a/node-red-contrib-xiaomi-socket/icons/outlet-icon.png and /dev/null differ diff --git a/node-red-contrib-xiaomi-socket/xiaomi-socket.html b/node-red-contrib-xiaomi-socket/xiaomi-socket.html deleted file mode 100644 index 66bd3b5..0000000 --- a/node-red-contrib-xiaomi-socket/xiaomi-socket.html +++ /dev/null @@ -1,126 +0,0 @@ - - - - - diff --git a/node-red-contrib-xiaomi-socket/xiaomi-socket.js b/node-red-contrib-xiaomi-socket/xiaomi-socket.js deleted file mode 100644 index d53128d..0000000 --- a/node-red-contrib-xiaomi-socket/xiaomi-socket.js +++ /dev/null @@ -1,36 +0,0 @@ -const crypto = require("crypto"); - -module.exports = (RED) => { - function XiaomiPlugNode(config) { - RED.nodes.createNode(this, config); - this.gateway = RED.nodes.getNode(config.gateway); - this.sid = config.sid; - - this.status({fill:"grey", shape:"ring", text:"status"}); - - if (this.gateway && this.key != "") { - this.on('input', (msg) => { - var payload = msg.payload; - if(payload.sid) { - if (payload.sid == this.sid) { - if (data.status && data.status == "on") { - this.status({fill:"green", shape:"dot", text:"on"}); - } else if (data.status && data.status == "off") { - this.status({fill:"red", shape:"dot", text:"off"}); - } - this.send(msg); - } - } - // Prepare for request - else { - miDevicesUtils.prepareForGatewayRequest(this, msg); - this.send(msg); - } - }); - } - - } - - RED.nodes.registerType("xiaomi-plug", XiaomiPlugNode); - -}