2
0

Added xiaomi plug, changed xiaomi gateway into configurator

This commit is contained in:
Harald Rietman
2017-06-30 21:32:10 +02:00
parent a640734d9d
commit 34a94a3615
8 changed files with 203 additions and 7 deletions

View File

@@ -1,12 +1,12 @@
<script type="text/javascript">
RED.nodes.registerType('xiaomi-gateway', {
RED.nodes.registerType('xiaomi-configurator', {
category: 'config',
defaults: {
name: {value: ""},
deviceList: {value:[{ sid:"a", desc:"b", model:"plug"}]}
},
label: function () {
return this.name || "xiaomi-gateway";
return this.name || "xiaomi-configurator";
},
oneditprepare: function() {
var node = this;
@@ -76,7 +76,7 @@
});
</script>
<script type="text/x-red" data-template-name="xiaomi-gateway">
<script type="text/x-red" data-template-name="xiaomi-configurator">
<div class="form-row">
<label for="node-config-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-config-input-name" placeholder="Name">
@@ -86,6 +86,6 @@
</div>
</script>
<script type="text/x-red" data-help-name="xiaomi-gateway">
<script type="text/x-red" data-help-name="xiaomi-configurator">
<p>Configuration node for a xiaomi gateway device</p>
</script>

View File

@@ -1,6 +1,6 @@
module.exports = function(RED) {
function RemoteServerNode(n) {
function XiaomiConfiguratorNode(n) {
RED.nodes.createNode(this, n);
this.name = n.name;
this.deviceList = n.deviceList || [];
@@ -8,6 +8,6 @@ module.exports = function(RED) {
var node = this;
}
RED.nodes.registerType("xiaomi-gateway", RemoteServerNode);
RED.nodes.registerType("xiaomi-configurator", XiaomiConfiguratorNode);
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.7 KiB

View File

@@ -3,7 +3,7 @@
category: 'function',
color: '#a6bbcf',
defaults: {
gateway: {value:"", type:"xiaomi-gateway"},
gateway: {value:"", type:"xiaomi-configurator"},
name: {value: ""},
sid: {value: "", required: true},
temperature: {value: "{{temperature}}"},

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

View File

@@ -0,0 +1,88 @@
<script type="text/javascript">
RED.nodes.registerType('xiaomi-plug', {
category: 'function',
color: '#a6bbcf',
defaults: {
gateway: {value:"", type:"xiaomi-configurator"},
name: {value: ""},
sid: {value: "", required: true},
onmsg: {value: ""},
offmsg: {value: ""},
output: {value: "0"}
},
inputs: 1,
outputs: 2,
outputLabels: ["Status","Control"],
icon: "outlet-icon.png",
label: function () {
return this.name || "xiaomi-plug";
},
oneditprepare: function() {
var node = this;
// Get the config node id from the select box:
var configNodeID = $('#node-input-gateway').val();
// Get the config node using the ID:
var configNode = RED.nodes.node(configNodeID);
$("#node-input-output").change(function () {
if ($(this).val() == "2") {
$(".node-input-msg").show();
} else {
$(".node-input-msg").hide();
}
});
$("#node-input-gateway").change(function () {
});
for (key in configNode.deviceList) {
var device = configNode.deviceList[key];
if (device.model === "plug") {
$('#node-input-sid').append('<option value="' + device.sid + '">' + device.desc + '</option>');
}
}
$('#node-input-sid').val(node.sid);
},
oneditsave: function() {
var node = this;
node.sid = $("#node-input-sid").val();
}
});
</script>
<script type="text/x-red" data-template-name="xiaomi-plug">
<div class="form-row">
<label for="node-input-gateway"><i class="icon-tag"></i> Gateway</label>
<input type="text" id="node-input-gateway" placeholder="xiaomi gateway">
</div>
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-row">
<label for="node-input-sid"><i class="icon-tag"></i> Device</label>
<select id="node-input-sid" placeholder="xiaomi gateway"></select>
</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-thermometer-three-quarters"></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-mixcloud"></i> Off msg</label>
<input type="text" id="node-input-offmsg">
</div>
</script>
<script type="text/x-red" data-help-name="xiaomi-plug">
<p>A simple node that converts the message payloads into a ON or OFF message</p>
</script>

View File

@@ -0,0 +1,107 @@
module.exports = function(RED) {
"use strict";
var mustache = require("mustache");
var crypto = require("crypto");
function XiaomiPlugNode(config) {
RED.nodes.createNode(this, config);
this.gateway = RED.nodes.getNode(config.gateway);
this.sid = config.sid;
this.output = config.output;
this.onmsg = config.onmsg;
this.offmsg = config.offmsg;
var node = this;
var currentToken = "";
var state = "";
node.status({fill:"yellow", shape:"ring", text:"no key"});
if (this.gateway) {
node.on('input', function(msg) {
// var payload = JSON.parse(msg);
var payload = msg.payload;
if (payload.cmd == "heartbeat" && payload.model == "gateway") {
var key = "c7utmdo2acpzai5b";
var token = payload.token;
if (token) {
var cipher = crypto.createCipheriv('aes128', key, (new Buffer("17996d093d28ddb3ba695a2e6f58562e", "hex")));
var encoded_string = cipher.update(token, 'utf8', 'hex');
encoded_string += cipher.final('hex');
currentToken = encoded_string.substring(0,32);
if (state == "") {
node.status({fill:"yellow", shape:"dot", text:"unknown state"});
}
}
}
if (payload == 'on') {
var cmd =
{ "cmd":"write",
"sid": node.sid,
"model": "plug",
"data": JSON.stringify({"status":"on", "key": currentToken })
}
msg.payload = JSON.stringify(cmd);
node.send([[],[msg]]);
} else if (payload == "off") {
var cmd =
{ "cmd":"write",
"sid": node.sid,
"model": "plug",
"data": JSON.stringify({"status":"off", "key": currentToken })
}
msg.payload = JSON.stringify(cmd);
node.send([[],[msg]]);
} else if (payload.sid == node.sid && payload.model == "plug") {
var data = JSON.parse(payload.data)
if (currentToken == "") {
node.status({fill:"yellow", shape:"dot", text:"no key"});
} else if (data.status && data.status == "on") {
node.status({fill:"green", shape:"dot", text:"on"});
state = "on";
} else if (data.status && data.status == "off") {
node.status({fill:"red", shape:"dot", text:"off"});
state = "off";
}
if (node.output == "0") {
msg.payload = payload;
node.send([msg]);
} else if (node.output == "1") {
var status = null;
if (data.status) {
status = {"payload": data.status};
}
node.send([status]);
} else if (node.output == "2") {
var status = null;
if (data.status === 'on') {
status = {"payload": mustache.render(node.onmsg, data)}
} else {
status = {"payload": mustache.render(node.offmsg, data)}
}
node.send([status]);
}
}
});
node.on("close", function() {
});
} else {
// no gateway configured
}
}
RED.nodes.registerType("xiaomi-plug", XiaomiPlugNode);
}

View File

@@ -18,6 +18,7 @@
},
"homepage": "https://gitlab.com/h-rietman/xiaomi-devices-node-red#README",
"dependencies": {
"cryptojs": "^2.5.3",
"mustache": "^2.3.0"
}
}