2
0

refactor: code clean up

This commit is contained in:
Pierre CLEMENT
2018-01-03 12:12:45 +01:00
parent a02969a0dd
commit a33d616516
24 changed files with 371 additions and 1005 deletions

View File

@@ -16,21 +16,6 @@
icon: "outlet-wifi-icon.png",
label: function () {
return this.name || "xiaomi-plug-wifi";
},
oneditprepare: function() {
var node = this;
$("#node-input-output").change(function () {
if ($(this).val() == "2") {
$(".node-input-msg").show();
} else {
$(".node-input-msg").hide();
}
});
},
oneditsave: function() {
}
});
</script>
@@ -44,22 +29,6 @@
<label for="node-input-ip"><i class="icon-tag"></i> Ip</label>
<input type="text" id="node-input-ip" placeholder="ip address">
</div>
<div class="form-row">
<label for="node-input-output"><i class="icon-tag"></i> Output</label>
<select id="node-input-output" style="width:70%;">
<option value="0">Full data</option>
<option value="1">Just values</option>
<option value="2">Template</option>
</select>
</div>
<div class="form-row node-input-msg">
<label for="node-input-onmsg"><i class="fa fa-power-off"></i> On msg</label>
<input type="text" id="node-input-onmsg">
</div>
<div class="form-row node-input-msg">
<label for="node-input-offmsg"><i class="fa fa-power-off"></i> Off msg</label>
<input type="text" id="node-input-offmsg">
</div>
</script>
<script type="text/x-red" data-help-name="xiaomi-plug-wifi">
@@ -77,43 +46,22 @@
<h3>Outputs</h3>
<ol class="node-ports">
<li>Status output
<dl class="message-properties">
<dt>payload <span class="property-type">string | json</span></dt>
<dd>raw data, value or template.</dd>
</dl>
</li>
<dl class="message-properties">
<dt>payload <span class="property-type">object</span></dt>
<dd>Data from gateway, see below.</dd>
</dl>
</ol>
<h3>Details</h3>
<h4>Details</h4>
<p>On the input you can send the string <code>on</code> to switch the plug on. To turn it off just send the string <code>off</code></p>
<p>Output 1 reports the status</p>
<p>Three output types are supported:
<ul>
<li>Full data</li>
<li>Just values</li>
<li>Template</li>
</ul>
</p>
<h4>Full data</h4>
<p>Passes a json object with detailed information about the plug. Use this if you need the raw data.</p>
<h4>Just values</h4>
<p>Only passes the values <code>on</code> or <code>off</code></p>
<h4>Template</h4>
<p>Use your own template to pass the values on. The template can contain <a href="http://mustache.github.io/mustache.5.html">mustache-style</a> tags.
Any property from the data section of the full object can be used.</p>
<p>Sample message full data:</p>
<p><pre>
{
"type": "power-plug",
"model": "chuangmi.plug.m1",
"capabilities: [ {'0': "power-channels"} ],
"address": "192.168.178.31",
"port": 54321,
"power": { '0': false }
}"
}</pre></p>
<p><pre>{
type: "power-plug",
model: "chuangmi.plug.m1",
capabilities: [ {"0": "power-channels"} ],
address: "192.168.178.31",
port: 54321,
power: { "0": false },
state: "on"
}</pre></p>
</script>

View File

@@ -1,8 +1,6 @@
module.exports = function(RED) {
"use strict";
var mustache = require("mustache");
var crypto = require("crypto");
var miio = require("miio");
const miio = require("miio");
module.exports = (RED) => {
var connectionState = "timeout";
var retryTimer;
var delayedStatusMsgTimer;
@@ -11,23 +9,18 @@ module.exports = function(RED) {
function XiaomiPlugWifiNode(config) {
RED.nodes.createNode(this, config);
this.ip = config.ip;
this.output = config.output;
this.onmsg = config.onmsg;
this.offmsg = config.offmsg;
this.plug = null;
var node = this;
this.status({fill: "yellow", shape: "dot", text: "connecting"});
node.status({fill: "yellow", shape: "dot", text: "connecting"});
miio.device({address: node.ip})
.then(function (plug) {
node.plug = plug;
node.status({fill:"green", shape:"dot", text:"connected"});
miio.device({address: this.ip})
.then((plug) => {
this.plug = plug;
this.status({fill:"green", shape:"dot", text:"connected"});
connectionState = "connected";
delayedStatusMsgUpdate(node);
node.plug.on('propertyChanged', function(e) {
this.plug.on('propertyChanged', (e) => {
if (e.property === "power") {
if (e.value['0']) {
setState("on");
@@ -38,73 +31,58 @@ module.exports = function(RED) {
});
watchdog();
})
.catch(function (error) {
.catch((error) => {
connectionState = "reconnecting";
watchdog();
})
node.on('input', function (msg) {
this.on('input', (msg) => {
var payload = msg.payload;
if (connectionState === "connected") {
if (payload == 'on') {
node.plug.setPower(true);
this.plug.setPower(true);
}
if (payload == 'off') {
node.plug.setPower(false);
this.plug.setPower(false);
}
}
});
node.on('close', function (done) {
this.on('close', (done) => {
if (retryTimer) {
clearTimeout(retryTimer);
}
if (delayedStatusMsgTimer) {
clearTimeout(delayedStatusMsgTimer);
}
if (node.plug) {
node.plug.destroy();
if (this.plug) {
this.plug.destroy();
}
done();
});
var setState = function(state) {
if (node.plug) {
var status = null;
var info = {"payload": {
"id": node.plug.id,
"type": node.plug.type,
"model": node.plug.model,
"capabilities": node.plug.capabilities,
"address": node.plug.address,
"port": node.plug.port,
"power": node.plug.power()
}};
if (state === "on") {
node.status({fill:"green", shape:"dot", text:"on"});
status = {"payload": mustache.render(node.onmsg, info.payload)}
}
if (state === "off") {
node.status({fill:"red", shape:"dot", text:"off"});
status = {"payload": mustache.render(node.offmsg, info.payload)}
}
if (node.output == 0) {
status = info;
} else if (node.output == "1") {
status = {"payload": state}
} else if (node.output == "2") {
// do nothing, just send status parsed with mustache
}
node.send([status]);
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 = function() {
delayedStatusMsgTimer = setTimeout(function() {
if (node.plug.power()['0']) {
var delayedStatusMsgUpdate = () => {
delayedStatusMsgTimer = setTimeout(() => {
if (this.plug.power()['0']) {
setState("on");
} else {
setState("off");
@@ -112,12 +90,12 @@ module.exports = function(RED) {
}, 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) {
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");
@@ -128,28 +106,30 @@ module.exports = function(RED) {
});
}
if (connectionState === "reconnecting") {
node.status({fill:"green", shape:"dot", text:"connected"});
this.status({fill:"green", shape:"dot", text:"connected"});
connectionState = "connected";
delayedStatusMsgUpdate();
}
})
.catch(function (error) {
.catch((error) => {
connectionState = "reconnecting";
if (node.plug) {
node.plug.destroy();
node.plug = null;
if (this.plug) {
this.plug.destroy();
this.plug = null;
}
})
};
var watchdog = function() {
setTimeout(function retryTimer() {
var watchdog = () => {
var node = this;
function retryTimer() {
discoverDevice();
if (connectionState === "reconnecting") {
node.status({fill: "red", shape: "dot", text: "reconnecting"});
}
setTimeout(retryTimer, 30000);
}, 30000);
}
setTimeout(retryTimer, 30000);
}
}