2
0

Refactored handling retry timer

This commit is contained in:
Harald Rietman
2017-07-14 23:40:14 +02:00
parent 11accee715
commit 735b48d4df

View File

@@ -3,6 +3,10 @@ module.exports = function(RED) {
var mustache = require("mustache"); var mustache = require("mustache");
var crypto = require("crypto"); var crypto = require("crypto");
var miio = require("miio"); var miio = require("miio");
var connectionState = "timeout";
var retryTimer;
var delayedStatusMsgTimer;
function XiaomiPlugWifiNode(config) { function XiaomiPlugWifiNode(config) {
RED.nodes.createNode(this, config); RED.nodes.createNode(this, config);
@@ -10,143 +14,158 @@ module.exports = function(RED) {
this.output = config.output; this.output = config.output;
this.onmsg = config.onmsg; this.onmsg = config.onmsg;
this.offmsg = config.offmsg; this.offmsg = config.offmsg;
this.plug = null;
var node = this; var node = this;
var connectionState = "timeout";
var plug = {};
var retryTimer;
node.status({fill:"yellow", shape:"dot", text:"connecting"}); node.status({fill: "yellow", shape: "dot", text: "connecting"});
miio.device({ address: node.ip }) miio.device({address: node.ip})
.then(function(result) { .then(function (plug) {
if (result.type === "power-plug") { node.plug = plug;
plug = result; node.status({fill:"green", shape:"dot", text:"connected"});
node.status({fill:"green", shape:"dot", text:"connected"}); connectionState = "connected";
connectionState = "connected"; delayedStatusMsgUpdate(node);
setTimeout(function() { node.plug.on('propertyChanged', function(e) {
if (result.power()['0']) { if (e.property === "power") {
if (e.value['0']) {
setState("on"); setState("on");
} else { } else {
setState("off"); setState("off");
} }
}, 1500); }
});
watchdog(); watchdog();
plug.on('propertyChanged', function(e) {
if (e.property === "power") {
if (e.value['0']) {
setState("on");
} else {
setState("off");
}
}
});
}
}) })
.catch(function(error) { .catch(function (error) {
node.status({fill:"red", shape:"dot", text:"time out"});
connectionState = "reconnecting"; connectionState = "reconnecting";
watchdog(); watchdog();
}); })
node.on('input', function(msg) { node.on('input', function (msg) {
var payload = msg.payload; var payload = msg.payload;
if (connectionState === "connected") {
if (payload == 'on') {
node.plug.setPower(true);
}
if (payload == 'on') { if (payload == 'off') {
if (connectionState === "connected") { node.plug.setPower(false);
plug.setPower(true);
} }
} }
if (payload == 'off') {
if (connectionState === "connected") {
plug.setPower(false);
}
}
}); });
this.on('close', function(done) { node.on('close', function (done) {
if (retryTimer) { if (retryTimer) {
clearTimeout(retryTimer); clearInterval(retryTimer);
} }
if (plug) { if (delayedStatusMsgTimer) {
plug.destroy(); clearTimeout(delayedStatusMsgTimer);
}
if (node.plug) {
node.plug.destroy();
} }
done(); done();
}); });
var setState = function(state) { var setState = function(state) {
var status = null; if (node.plug) {
var info = null; var status = null;
if (plug) { var info = {"payload": {
info = {"payload": { "id": node.plug.id,
"id": plug.id, "type": node.plug.type,
"type": plug.type, "model": node.plug.model,
"model": plug.model, "capabilities": node.plug.capabilities,
"capabilities": plug.capabilities, "address": node.plug.address,
"address": plug.address, "port": node.plug.port,
"port": plug.port, "power": node.plug.power()
"power": plug.power()
}}; }};
}
if (state === "on") { if (state === "on") {
node.status({fill:"green", shape:"dot", text:"on"}); node.status({fill:"green", shape:"dot", text:"on"});
status = {"payload": mustache.render(node.onmsg, info.payload)} status = {"payload": mustache.render(node.onmsg, info.payload)}
} }
if (state === "off") { if (state === "off") {
node.status({fill:"red", shape:"dot", text:"off"}); node.status({fill:"red", shape:"dot", text:"off"});
status = {"payload": mustache.render(node.offmsg, info.payload)} status = {"payload": mustache.render(node.offmsg, info.payload)}
} }
if (node.output == 0) { if (node.output == 0) {
status = info; status = info;
} else if (node.output == "1") { } else if (node.output == "1") {
status = {"payload": state} status = {"payload": state}
} else if (node.output == "2") { } else if (node.output == "2") {
// do nothing, just send status parsed with mustache // do nothing, just send status parsed with mustache
}
node.send([status]);
} }
node.send([status]); };
var delayedStatusMsgUpdate = function() {
delayedStatusMsgTimer = setTimeout(function() {
if (node.plug.power()['0']) {
setState("on");
} else {
setState("off");
}
}, 1500);
};
var discoverDevice = function() {
miio.device({address: node.ip})
.then(function (plug) {
if (node.plug == null) {
node.plug = plug;
node.plug.on('propertyChanged', function(e) {
if (e.property === "power") {
if (e.value['0']) {
setState("on");
} else {
setState("off");
}
}
});
}
if (connectionState === "reconnecting") {
node.status({fill:"green", shape:"dot", text:"connected"});
connectionState = "connected";
delayedStatusMsgUpdate();
}
watchdog();
})
.catch(function (error) {
connectionState = "reconnecting";
if (node.plug) {
node.plug.destroy();
node.plug = null;
}
watchdog();
})
}; };
var watchdog = function() { var watchdog = function() {
console.log("retrytimer started");
var interval = 10; var interval = 10;
retryTimer = setInterval(function() { retryTimer = setInterval(function() {
if (interval == 0) { if (interval == 0) {
miio.device({address: node.ip})
.then(function (result) {
if (connectionState === "reconnecting") {
node.status({fill:"green", shape:"dot", text:"connected"});
connectionState = "connected";
setTimeout(function() {
if (result.power()['0']) {
setState("on");
} else {
setState("off");
}
}, 1500);
}
})
.catch(function (error) {
connectionState = "reconnecting";
})
interval = 10; interval = 10;
clearInterval(retryTimer);
discoverDevice();
} else { } else {
interval--; interval--;
if (connectionState === "reconnecting") { if (connectionState === "reconnecting") {
node.status({fill: "red", shape: "dot", text: "retrying in " + interval + " sec."}); node.status({fill: "red", shape: "dot", text: "retrying in " + interval + " sec."});
} }
} }
}, 1000); }, 1000);
} }
}
}
RED.nodes.registerType("xiaomi-plug-wifi", XiaomiPlugWifiNode); RED.nodes.registerType("xiaomi-plug-wifi", XiaomiPlugWifiNode);
process.on('unhandledRejection', function(reason, p){
console.log("Possibly Unhandled Rejection at: Promise ", p, " reason: ", reason);
// application specific logging here
});
} }