2
0

feat(gateway): allow to change the light color

Major fix on devices, set output only when no payload.sid is present
This commit is contained in:
Pierre CLEMENT
2018-01-01 20:47:30 +01:00
parent 9da0427164
commit 5732c80039
13 changed files with 292 additions and 136 deletions
@@ -53,9 +53,9 @@
<p>Ask the gateway to the list of devices ids.</p> <p>Ask the gateway to the list of devices ids.</p>
<h3>Outputs</h3> <h3>Outputs</h3>
<p class="node-ports"> <ol class="node-ports">
Message to connect to a gateway out node. <li>Message to connect to a gateway out node.</li>
</p> </ol>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">
@@ -88,7 +88,7 @@
<script type="text/x-red" data-help-name="xiaomi-actions click"> <script type="text/x-red" data-help-name="xiaomi-actions click">
<p> <p>
Virtual single click for switch. Virtual single click for switch.
Note: a gateway input node must be present to get gateway tokens. Note: a gateway input node must be present and have receive a message to get gateway tokens and be able to do the action.
</p> </p>
<h3>Inputs</h3> <h3>Inputs</h3>
@@ -104,9 +104,9 @@
</dl> </dl>
<h3>Outputs</h3> <h3>Outputs</h3>
<p class="node-ports"> <ol class="node-ports">
Message to connect to a gateway out node. <li>Message to connect to a gateway out node.</li>
</p> </ol>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">
@@ -138,7 +138,7 @@
<script type="text/x-red" data-help-name="xiaomi-actions double_click"> <script type="text/x-red" data-help-name="xiaomi-actions double_click">
<p> <p>
Virtual double click for switch. Virtual double click for switch.
Note: a gateway input node must be present to get gateway tokens. Note: a gateway input node must be present and have receive a message to get gateway tokens and be able to do the action.
</p> </p>
<h3>Inputs</h3> <h3>Inputs</h3>
@@ -154,9 +154,9 @@
</dl> </dl>
<h3>Outputs</h3> <h3>Outputs</h3>
<p class="node-ports"> <ol class="node-ports">
Message to connect to a gateway out node. <li>Message to connect to a gateway out node.</li>
</p> </ol>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">
@@ -175,3 +175,69 @@
} }
}); });
</script> </script>
<!-- The Gateway light Node -->
<script type="text/javascript">
RED.nodes.registerType('xiaomi-actions gateway_light', {
category: 'xiaomi actions',
color: '#64C4CD',
defaults: {
gateway: {value:"", type:"xiaomi-configurator"},
name: {value: ""}
},
inputs: 1,
outputs: 1,
paletteLabel: "set light",
icon: "light-icon.png",
label: function () {
return this.name || "set light";
}
});
</script>
<script type="text/x-red" data-template-name="xiaomi-actions gateway_light">
<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>
</script>
<script type="text/x-red" data-help-name="xiaomi-actions gateway_light">
<p>Change the light of the gateway.
Note: a gateway input node must be present and have receive a message to get gateway tokens and be able to do the action.
</p>
<h3>Inputs</h3>
<dl class="message-properties">
<dt>brightness
<span class="property-type">number</span>
</dt>
<dd>The brightness value between <code>0</code> and <code>100</code>.</dd>
<dt>color
<span class="property-type">object</span>
</dt>
<dd>The color itself. This object must contain the followinf properties:
<ul>
<li>
<code>red</code> - amout of red, between <code>0</code> and <code>255</code>
</li>
<li>
<code>green</code> - amout of green, between <code>0</code> and <code>255</code>
</li>
<li>
<code>blue</code> - amout of blue, between <code>0</code> and <code>255</code>
</li>
</ul>
</dd>
</dl>
<h3>Outputs</h3>
<ol class="node-ports">
<li>Message to connect to a gateway out node.</li>
</ol>
</script>
@@ -73,4 +73,29 @@ module.exports = function(RED) {
}); });
} }
RED.nodes.registerType("xiaomi-actions double_click", XiaomiActionDoubleClick); RED.nodes.registerType("xiaomi-actions double_click", XiaomiActionDoubleClick);
function XiaomiActionGatewayLight(config) {
RED.nodes.createNode(this, config);
this.gateway = RED.nodes.getNode(config.gateway);
var node = this;
node.on('input', function(msg) {
if(node.gateway && node.gateway.sid && node.gateway.key && node.gateway.lastToken) {
var rgb = miDevicesUtils.computeColorValue(msg.brightness, msg.color.red, msg.color.green, msg.color.blue);
msg.payload = {
cmd: "write",
data: {
rgb: rgb,
sid: node.gateway.sid,
key: miDevicesUtils.getGatewayKey(node.gateway.key, node.gateway.lastToken)
}
};
node.send(msg);
}
});
}
RED.nodes.registerType("xiaomi-actions gateway_light", XiaomiActionGatewayLight);
} }
+1 -1
View File
@@ -29,7 +29,7 @@
</script> </script>
<script type="text/x-red" data-help-name="xiaomi-all"> <script type="text/x-red" data-help-name="xiaomi-all">
<p>All devices registred in the gateway.</p> <p>All devices registred in the gateway, except gateway itself.</p>
<h3>Outputs</h3> <h3>Outputs</h3>
<ol class="node-ports"> <ol class="node-ports">
@@ -4,6 +4,7 @@
defaults: { defaults: {
name: {value: ""}, name: {value: ""},
ip: {value: ""}, ip: {value: ""},
sid: {value: ""},
deviceList: {value:[{ sid:"", desc:"", model:"plug"}]}, deviceList: {value:[{ sid:"", desc:"", model:"plug"}]},
key: {value: ""} key: {value: ""}
}, },
@@ -92,6 +93,10 @@
<label for="node-config-input-ip"><i class="icon-tag"></i> IP address (v4 or v6)</label> <label for="node-config-input-ip"><i class="icon-tag"></i> IP address (v4 or v6)</label>
<input type="text" id="node-config-input-ip" placeholder="IP"> <input type="text" id="node-config-input-ip" placeholder="IP">
</div> </div>
<div class="form-row">
<label for="node-input-sid"><i class="icon-tag"></i> SID (optional)</label>
<input type="text" id="node-input-sid" placeholder="sid">
</div>
<div class="form-row"> <div class="form-row">
<label for="node-config-input-key"><i class="fa fa-ticket"></i> Key/Password</label> <label for="node-config-input-key"><i class="fa fa-ticket"></i> Key/Password</label>
<input type="text" id="node-config-input-key" placeholder="Key"> <input type="text" id="node-config-input-key" placeholder="Key">
@@ -6,6 +6,7 @@ module.exports = function(RED) {
this.deviceList = n.deviceList || []; this.deviceList = n.deviceList || [];
this.key = n.key; this.key = n.key;
this.ip = n.ip; this.ip = n.ip;
this.sid = this.sid || n.sid;
var node = this; var node = this;
} }
@@ -39,6 +39,10 @@
<dt>gateway <span class="property-type">xiaomi-configurator</span></dt> <dt>gateway <span class="property-type">xiaomi-configurator</span></dt>
<dd>The gateway.</dd> <dd>The gateway.</dd>
</dl> </dl>
<dl class="message-properties">
<dt>payload <span class="property-type">json</span></dt>
<dd>Data from gateway, with computed data.</dd>
</dl>
</li> </li>
</ol> </ol>
</script> </script>
@@ -12,8 +12,30 @@ module.exports = function(RED) {
if (this.gateway) { if (this.gateway) {
node.on('input', function(msg) { node.on('input', function(msg) {
msg.gateway = node.gateway; // var payload = JSON.parse(msg);
node.send(msg); var payload = msg.payload;
node.log("Received message from: " + payload.model + " sid: " + payload.sid + " payload: " + payload.data);
// Input from gateway
if(payload.sid) {
if (payload.sid == node.gateway.sid && ["gateway"].indexOf(payload.model) >= 0) {
if(payload.data.rgb) {
var decomposed = miDevicesUtils.computeColor(payload.data.rgb);
payload.data.brightness = decomposed.brightness;
payload.data.color = decomposed.color;
}
node.send([msg]);
}
}
// Prepare for request
else {
msg.gateway = node.gateway;
msg.sid = node.gateway.sid;
node.send(msg);
}
});
node.on("close", function() {
}); });
} }
} }
+43 -41
View File
@@ -23,55 +23,57 @@ module.exports = function(RED) {
node.log("Received message from: " + payload.model + " sid: " + payload.sid + " payload: " + payload.data); node.log("Received message from: " + payload.model + " sid: " + payload.sid + " payload: " + payload.data);
// Input from gateway // Input from gateway
if (payload.sid == node.sid && ["sensor_ht", "weather.v1"].indexOf(payload.model) >= 0) { if(payload.sid) {
var data = payload.data; if (payload.sid == node.sid && ["sensor_ht", "weather.v1"].indexOf(payload.model) >= 0) {
miDevicesUtils.setStatus(node, data); var data = payload.data;
miDevicesUtils.setStatus(node, data);
if (node.output == "0") { if (node.output == "0") {
node.send([msg]); node.send([msg]);
} else if (node.output == "1") { } else if (node.output == "1") {
var temp = null; var temp = null;
var humidity = null; var humidity = null;
var pressure = null; var pressure = null;
if (data.temperature) { if (data.temperature) {
temp = {"payload": data.temperature}; temp = {"payload": data.temperature};
}
if (data.humidity) {
humidity = {"payload": data.humidity};
}
if (data.pressure) {
pressure = {"payload": data.pressure};
}
node.send([temp, humidity, pressure]);
} else if (node.output == "2") {
var temp = null;
var humidity = null;
var pressure = null;
if (data.temperature) {
if (this.divide) {
data.temperature = String(data.temperature / 100);
} }
temp = {"payload": mustache.render(node.temperature, data)}
}
if (data.humidity) { if (data.humidity) {
if (this.divide) { humidity = {"payload": data.humidity};
data.humidity = String(data.humidity / 100);
} }
humidity = {"payload": mustache.render(node.humidity, data)}
}
if (data.pressure) { if (data.pressure) {
if (this.divide) { pressure = {"payload": data.pressure};
data.pressure = String(data.pressure / 100);
} }
pressure = {"payload": mustache.render(node.pressure, data)} node.send([temp, humidity, pressure]);
} else if (node.output == "2") {
var temp = null;
var humidity = null;
var pressure = null;
if (data.temperature) {
if (this.divide) {
data.temperature = String(data.temperature / 100);
}
temp = {"payload": mustache.render(node.temperature, data)}
}
if (data.humidity) {
if (this.divide) {
data.humidity = String(data.humidity / 100);
}
humidity = {"payload": mustache.render(node.humidity, data)}
}
if (data.pressure) {
if (this.divide) {
data.pressure = String(data.pressure / 100);
}
pressure = {"payload": mustache.render(node.pressure, data)}
}
node.send([temp, humidity, pressure]);
} }
node.send([temp, humidity, pressure]);
} }
} }
// Prepare for request // Prepare for request
+28 -26
View File
@@ -22,37 +22,39 @@ module.exports = function(RED) {
// var payload = JSON.parse(msg); // var payload = JSON.parse(msg);
var payload = msg.payload; var payload = msg.payload;
if (payload.sid == node.sid && ["magnet", "sensor_magnet.aq2"].indexOf(payload.model) >= 0) { if(payload.sid) {
var data = payload.data; if (payload.sid == node.sid && ["magnet", "sensor_magnet.aq2"].indexOf(payload.model) >= 0) {
var data = payload.data;
// if (data.status && data.status == "open") { // if (data.status && data.status == "open") {
// node.status({fill:"green", shape:"dot", text:"open"}); // node.status({fill:"green", shape:"dot", text:"open"});
// state = "open"; // state = "open";
// } else if (data.status && data.status == "close") { // } else if (data.status && data.status == "close") {
// node.status({fill:"red", shape:"dot", text:"closed"}); // node.status({fill:"red", shape:"dot", text:"closed"});
// state = "closed"; // state = "closed";
// } // }
miDevicesUtils.setStatus(node, data); miDevicesUtils.setStatus(node, data);
if (node.output == "0") { if (node.output == "0") {
node.send([msg]); node.send([msg]);
} else if (node.output == "1") { } else if (node.output == "1") {
var status = null; var status = null;
if (data.status) { if (data.status) {
status = {"payload": data.status}; status = {"payload": data.status};
}
node.send([status]);
} else if (node.output == "2") {
var status = null;
if (data.status === 'open') {
status = {"payload": mustache.render(node.openmsg, data)}
} else {
status = {"payload": mustache.render(node.closemsg, data)}
}
node.send([status]);
} }
node.send([status]);
} else if (node.output == "2") {
var status = null;
if (data.status === 'open') {
status = {"payload": mustache.render(node.openmsg, data)}
} else {
status = {"payload": mustache.render(node.closemsg, data)}
}
node.send([status]);
} }
} }
// Prepare for request // Prepare for request
+34 -32
View File
@@ -22,43 +22,45 @@ module.exports = function(RED) {
// var payload = JSON.parse(msg); // var payload = JSON.parse(msg);
var payload = msg.payload; var payload = msg.payload;
if (payload.sid == node.sid && payload.model == "motion") { if (payload.sid) {
var data = payload.data; if (payload.sid == node.sid && payload.model == "motion") {
var data = payload.data;
// if (data.status && data.status == "open") { // if (data.status && data.status == "open") {
// node.status({fill:"green", shape:"dot", text:"open"}); // node.status({fill:"green", shape:"dot", text:"open"});
// state = "open"; // state = "open";
// } else if (data.status && data.status == "close") { // } else if (data.status && data.status == "close") {
// node.status({fill:"red", shape:"dot", text:"closed"}); // node.status({fill:"red", shape:"dot", text:"closed"});
// state = "closed"; // state = "closed";
// } // }
miDevicesUtils.setStatus(node, data); miDevicesUtils.setStatus(node, data);
if (node.output == "0") { if (node.output == "0") {
node.send([msg]); node.send([msg]);
} else if (node.output == "1") { } else if (node.output == "1") {
var status = null; var status = null;
var duration = null; var duration = null;
if (data.status) { if (data.status) {
status = {"payload": 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]);
} }
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]);
} }
} }
// Prepare for request // Prepare for request
+22 -20
View File
@@ -21,30 +21,32 @@ module.exports = function(RED) {
var payload = msg.payload; var payload = msg.payload;
// Input from gateway // Input from gateway
if (payload.sid == node.sid && ["switch", "sensor_switch.aq2"].indexOf(payload.model) >= 0) { if (payload.sid) {
var data = payload.data; if (payload.sid == node.sid && ["switch", "sensor_switch.aq2"].indexOf(payload.model) >= 0) {
miDevicesUtils.setStatus(node, data); var data = payload.data;
miDevicesUtils.setStatus(node, data);
if (node.output == "0") { if (node.output == "0") {
node.send([msg]); node.send([msg]);
} else if (node.output == "1") { } else if (node.output == "1") {
var status = null; var status = null;
if (data.status) { if (data.status) {
status = {"payload": data.status}; status = {"payload": data.status};
} }
node.send([status]); node.send([status]);
} else if (node.output == "2") { } else if (node.output == "2") {
var status = null; var status = null;
if (data.status && data.status == "click") { if (data.status && data.status == "click") {
status = {"payload": mustache.render(node.outmsg, data)} status = {"payload": mustache.render(node.outmsg, data)}
node.send([[status],[]]); node.send([[status],[]]);
} }
if (data.status && data.status == "double_click") { if (data.status && data.status == "double_click") {
status = {"payload": mustache.render(node.outmsgdbcl, data)} status = {"payload": mustache.render(node.outmsgdbcl, data)}
node.send([[],[status]]); node.send([[],[status]]);
}
} }
} }
} }
+6 -3
View File
@@ -6,9 +6,12 @@
"type": "git", "type": "git",
"url": "git+ssh://git@github.com:hrietman/node-red-contrib-xiaomi-devices.git" "url": "git+ssh://git@github.com:hrietman/node-red-contrib-xiaomi-devices.git"
}, },
"license" : "MIT", "license": "MIT",
"keywords": ["Xiaomi", "node-red"], "keywords": [
"node-red" : { "Xiaomi",
"node-red"
],
"node-red": {
"nodes": { "nodes": {
"xiaomi-ht": "node-red-contrib-xiaomi-ht/xiaomi-ht.js", "xiaomi-ht": "node-red-contrib-xiaomi-ht/xiaomi-ht.js",
"xiaomi-magnet": "node-red-contrib-xiaomi-magnet/xiaomi-magnet.js", "xiaomi-magnet": "node-red-contrib-xiaomi-magnet/xiaomi-magnet.js",
+22
View File
@@ -42,5 +42,27 @@ module.exports = {
prepareForGatewayRequest: function(node, msg) { prepareForGatewayRequest: function(node, msg) {
msg.sid = node.sid; msg.sid = node.sid;
msg.gateway = node.gateway; msg.gateway = node.gateway;
},
computeColorValue: function (brightness, red, green, blue) {
return Math.round(256*256*256*brightness) + (256*256*red) + (256*green) + blue;
},
computeColor: function (rgb) {
var blue = rgb % 256;
rgb = Math.max(rgb - blue, 0);
var green = rgb % (256 * 256);
rgb = Math.max(rgb - green, 0);
green /= 256;
var red = rgb % (256 * 256 * 256);
rgb = Math.max(rgb - red, 0);
red /= 256 * 256;
var brightness = rgb / (256*256*256);
return {
brightness: brightness,
color: { red: red, green: green, blue: blue }
};
} }
} }