2
0
Files
node-red-contrib-mi-devices/node-red-contrib-xiaomi-socket-wifi/xiaomi-socket-wifi.js

150 lines
4.8 KiB
JavaScript
Raw Normal View History

2018-01-03 12:12:45 +01:00
const miio = require("miio");
module.exports = (RED) => {
2017-07-14 23:40:14 +02:00
var connectionState = "timeout";
var retryTimer;
var delayedStatusMsgTimer;
function XiaomiPlugWifiNode(config) {
RED.nodes.createNode(this, config);
this.ip = config.ip;
2017-07-14 23:40:14 +02:00
this.plug = null;
2018-01-03 12:12:45 +01:00
this.status({fill: "yellow", shape: "dot", text: "connecting"});
2018-01-03 12:12:45 +01:00
miio.device({address: this.ip})
.then((plug) => {
this.plug = plug;
this.status({fill:"green", shape:"dot", text:"connected"});
2017-07-14 23:40:14 +02:00
connectionState = "connected";
delayedStatusMsgUpdate(this);
2018-01-03 12:12:45 +01:00
this.plug.on('propertyChanged', (e) => {
2017-07-14 23:40:14 +02:00
if (e.property === "power") {
if (e.value['0']) {
setState("on");
} else {
setState("off");
}
2017-07-14 23:40:14 +02:00
}
});
watchdog();
})
2018-01-03 12:12:45 +01:00
.catch((error) => {
connectionState = "reconnecting";
watchdog();
2017-07-14 23:40:14 +02:00
})
2018-01-03 12:12:45 +01:00
this.on('input', (msg) => {
var payload = msg.payload;
2017-07-14 23:40:14 +02:00
if (connectionState === "connected") {
if (payload == 'on') {
2018-01-03 12:12:45 +01:00
this.plug.setPower(true);
}
2017-07-14 23:40:14 +02:00
if (payload == 'off') {
2018-01-03 12:12:45 +01:00
this.plug.setPower(false);
}
}
});
2018-01-03 12:12:45 +01:00
this.on('close', (done) => {
if (retryTimer) {
clearTimeout(retryTimer);
2017-07-14 23:40:14 +02:00
}
if (delayedStatusMsgTimer) {
clearTimeout(delayedStatusMsgTimer);
}
2018-01-03 12:12:45 +01:00
if (this.plug) {
this.plug.destroy();
}
done();
});
2018-01-03 12:12:45 +01:00
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);
}
2017-07-14 23:40:14 +02:00
};
2018-01-03 12:12:45 +01:00
var delayedStatusMsgUpdate = () => {
delayedStatusMsgTimer = setTimeout(() => {
if (this.plug.power()['0']) {
2017-07-14 23:40:14 +02:00
setState("on");
} else {
setState("off");
}
}, 1500);
};
2018-01-03 12:12:45 +01:00
var discoverDevice = () => {
miio.device({address: this.ip})
.then((plug) => {
if (this.plug == null) {
this.plug = plug;
this.plug.on('propertyChanged', (e) => {
2017-07-14 23:40:14 +02:00
if (e.property === "power") {
if (e.value['0']) {
setState("on");
} else {
setState("off");
}
}
});
}
if (connectionState === "reconnecting") {
2018-01-03 12:12:45 +01:00
this.status({fill:"green", shape:"dot", text:"connected"});
2017-07-14 23:40:14 +02:00
connectionState = "connected";
delayedStatusMsgUpdate();
}
})
2018-01-03 12:12:45 +01:00
.catch((error) => {
2017-07-14 23:40:14 +02:00
connectionState = "reconnecting";
2018-01-03 12:12:45 +01:00
if (this.plug) {
this.plug.destroy();
this.plug = null;
2017-07-14 23:40:14 +02:00
}
})
};
2018-01-03 12:12:45 +01:00
var watchdog = () => {
var node = this;
function retryTimer() {
discoverDevice();
if (connectionState === "reconnecting") {
node.status({fill: "red", shape: "dot", text: "reconnecting"});
}
setTimeout(retryTimer, 30000);
2018-01-03 12:12:45 +01:00
}
setTimeout(retryTimer, 30000);
}
2017-07-14 23:40:14 +02:00
}
RED.nodes.registerType("xiaomi-plug-wifi", XiaomiPlugWifiNode);
2017-07-15 10:15:07 +02:00
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;
}
}
2017-07-14 23:40:14 +02:00
});
}