forked from Mirrors/node-red-contrib-mi-devices
Added motion, updated icons
This commit is contained in:
@@ -31,6 +31,7 @@
|
||||
model.append($("<option></option>").val("plug").text("plug"));
|
||||
model.append($("<option></option>").val("gateway").text("gateway"));
|
||||
model.append($("<option></option>").val("switch").text("switch"));
|
||||
model.append($("<option></option>").val("motion").text("motion"));
|
||||
|
||||
sid.val(device.sid);
|
||||
desc.val(device.desc);
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 16 KiB |
@@ -75,11 +75,11 @@
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-row node-input-msg">
|
||||
<label for="node-input-openmsg"><i class="fa fa-power-off"></i> Open msg</label>
|
||||
<label for="node-input-openmsg"><i class="fa fa-circle-o-notch"></i> Open msg</label>
|
||||
<input type="text" id="node-input-openmsg">
|
||||
</div>
|
||||
<div class="form-row node-input-msg">
|
||||
<label for="node-input-closemsg"><i class="fa fa-power-off"></i> Close msg</label>
|
||||
<label for="node-input-closemsg"><i class="fa fa-circle-o"></i> Close msg</label>
|
||||
<input type="text" id="node-input-closemsg">
|
||||
</div>
|
||||
</script>
|
||||
|
||||
BIN
node-red-contrib-xiaomi-motion/icons/motion-icon.png
Normal file
BIN
node-red-contrib-xiaomi-motion/icons/motion-icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 5.7 KiB |
89
node-red-contrib-xiaomi-motion/xiaomi-motion.html
Normal file
89
node-red-contrib-xiaomi-motion/xiaomi-motion.html
Normal file
@@ -0,0 +1,89 @@
|
||||
<script type="text/javascript">
|
||||
RED.nodes.registerType('xiaomi-motion', {
|
||||
category: 'xiaomi',
|
||||
color: '#3FADB5',
|
||||
defaults: {
|
||||
gateway: {value:"", type:"xiaomi-configurator"},
|
||||
name: {value: ""},
|
||||
sid: {value: "", required: true},
|
||||
motionmsg: {value: ""},
|
||||
nomotionmsg: {value: ""},
|
||||
output: {value: "0"}
|
||||
},
|
||||
inputs: 1,
|
||||
outputs: 2,
|
||||
outputLabels: ["Status"],
|
||||
paletteLabel: "motion",
|
||||
icon: "motion-icon.png",
|
||||
label: function () {
|
||||
return this.name || "xiaomi-motion";
|
||||
},
|
||||
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 === "motion") {
|
||||
$('#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-motion">
|
||||
<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-motionmsg"><i class="fa fa-rss"></i> Motion msg</label>
|
||||
<input type="text" id="node-input-motionmsg">
|
||||
</div>
|
||||
<div class="form-row node-input-msg">
|
||||
<label for="node-input-nomotionmsg"><i class="fa fa-circle"></i> No motion msg</label>
|
||||
<input type="text" id="node-input-nomotionmsg">
|
||||
</div>
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-help-name="xiaomi-motion">
|
||||
<p>A simple node that converts the message payloads into a message</p>
|
||||
</script>
|
||||
86
node-red-contrib-xiaomi-motion/xiaomi-motion.js
Normal file
86
node-red-contrib-xiaomi-motion/xiaomi-motion.js
Normal file
@@ -0,0 +1,86 @@
|
||||
module.exports = function(RED) {
|
||||
"use strict";
|
||||
var mustache = require("mustache");
|
||||
|
||||
function XiaomiMotionNode(config) {
|
||||
RED.nodes.createNode(this, config);
|
||||
this.gateway = RED.nodes.getNode(config.gateway);
|
||||
this.sid = config.sid;
|
||||
this.output = config.output;
|
||||
this.motionmsg = config.motionmsg;
|
||||
this.nomotionmsg = config.nomotionmsg;
|
||||
|
||||
var node = this;
|
||||
var state = "";
|
||||
|
||||
// node.status({fill:"yellow", shape:"dot", text:"unknown state"});
|
||||
node.status({fill:"grey",shape:"ring",text:"battery"});
|
||||
|
||||
if (this.gateway) {
|
||||
node.on('input', function(msg) {
|
||||
// var payload = JSON.parse(msg);
|
||||
var payload = msg.payload;
|
||||
|
||||
if (payload.sid == node.sid && payload.model == "motion") {
|
||||
var data = JSON.parse(payload.data)
|
||||
|
||||
// if (data.status && data.status == "open") {
|
||||
// node.status({fill:"green", shape:"dot", text:"open"});
|
||||
// state = "open";
|
||||
// } else if (data.status && data.status == "close") {
|
||||
// node.status({fill:"red", shape:"dot", text:"closed"});
|
||||
// state = "closed";
|
||||
// }
|
||||
|
||||
if (data.voltage) {
|
||||
if (data.voltage < 2500) {
|
||||
node.status({fill:"red",shape:"dot",text:"battery"});
|
||||
} else if (data.voltage < 2900) {
|
||||
node.status({fill:"yellow",shape:"dot",text:"battery"});
|
||||
} else {
|
||||
node.status({fill:"green",shape:"dot",text:"battery"});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (node.output == "0") {
|
||||
msg.payload = payload;
|
||||
node.send([msg]);
|
||||
} else if (node.output == "1") {
|
||||
var status = null;
|
||||
var duration = null;
|
||||
|
||||
if (data.status) {
|
||||
status = {"payload": data.status};
|
||||
}
|
||||
if (data.no_motion) {
|
||||
status = {"payload": "no_motion"};
|
||||
duration = {"payload": {"no_motion": data.no_motion}};
|
||||
}
|
||||
|
||||
node.send([[status], [duration]]);
|
||||
} else if (node.output == "2") {
|
||||
var status = null;
|
||||
|
||||
if (data.status === 'motion') {
|
||||
status = {"payload": mustache.render(node.motionmsg, data)}
|
||||
} else {
|
||||
status = {"payload": mustache.render(node.nomotionmsg, data)}
|
||||
}
|
||||
node.send([status]);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
node.on("close", function() {
|
||||
});
|
||||
|
||||
} else {
|
||||
// no gateway configured
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
RED.nodes.registerType("xiaomi-motion", XiaomiMotionNode);
|
||||
|
||||
}
|
||||
@@ -16,7 +16,7 @@ module.exports = function(RED) {
|
||||
var currentToken = "";
|
||||
var state = "";
|
||||
|
||||
node.status({fill:"yellow", shape:"ring", text:"no key"});
|
||||
node.status({fill:"yellow", shape:"ring", text:"waiting for key"});
|
||||
|
||||
if (this.gateway) {
|
||||
node.on('input', function(msg) {
|
||||
@@ -61,7 +61,7 @@ module.exports = function(RED) {
|
||||
var data = JSON.parse(payload.data)
|
||||
|
||||
if (currentToken == "") {
|
||||
node.status({fill:"yellow", shape:"dot", text:"no key"});
|
||||
node.status({fill:"yellow", shape:"ring", text:"waiting for key"});
|
||||
} else if (data.status && data.status == "on") {
|
||||
node.status({fill:"green", shape:"dot", text:"on"});
|
||||
state = "on";
|
||||
|
||||
Reference in New Issue
Block a user