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) {
// var payload = JSON.parse(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.gateway = node.gateway;
msg.sid = node.gateway.sid;
node.send(msg); node.send(msg);
}
});
node.on("close", function() {
}); });
} }
} }
+2
View File
@@ -23,6 +23,7 @@ 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) {
if (payload.sid == node.sid && ["sensor_ht", "weather.v1"].indexOf(payload.model) >= 0) { if (payload.sid == node.sid && ["sensor_ht", "weather.v1"].indexOf(payload.model) >= 0) {
var data = payload.data; var data = payload.data;
miDevicesUtils.setStatus(node, data); miDevicesUtils.setStatus(node, data);
@@ -74,6 +75,7 @@ module.exports = function(RED) {
node.send([temp, humidity, pressure]); node.send([temp, humidity, pressure]);
} }
} }
}
// Prepare for request // Prepare for request
else { else {
miDevicesUtils.prepareForGatewayRequest(node, msg); miDevicesUtils.prepareForGatewayRequest(node, msg);
@@ -22,6 +22,7 @@ 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) {
if (payload.sid == node.sid && ["magnet", "sensor_magnet.aq2"].indexOf(payload.model) >= 0) { if (payload.sid == node.sid && ["magnet", "sensor_magnet.aq2"].indexOf(payload.model) >= 0) {
var data = payload.data; var data = payload.data;
@@ -55,6 +56,7 @@ module.exports = function(RED) {
node.send([status]); node.send([status]);
} }
} }
}
// Prepare for request // Prepare for request
else { else {
miDevicesUtils.prepareForGatewayRequest(node, msg); miDevicesUtils.prepareForGatewayRequest(node, msg);
@@ -22,6 +22,7 @@ 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) {
if (payload.sid == node.sid && payload.model == "motion") { if (payload.sid == node.sid && payload.model == "motion") {
var data = payload.data; var data = payload.data;
@@ -61,6 +62,7 @@ module.exports = function(RED) {
node.send([status]); node.send([status]);
} }
} }
}
// Prepare for request // Prepare for request
else { else {
miDevicesUtils.prepareForGatewayRequest(node, msg); miDevicesUtils.prepareForGatewayRequest(node, msg);
@@ -21,6 +21,7 @@ module.exports = function(RED) {
var payload = msg.payload; var payload = msg.payload;
// Input from gateway // Input from gateway
if (payload.sid) {
if (payload.sid == node.sid && ["switch", "sensor_switch.aq2"].indexOf(payload.model) >= 0) { if (payload.sid == node.sid && ["switch", "sensor_switch.aq2"].indexOf(payload.model) >= 0) {
var data = payload.data; var data = payload.data;
miDevicesUtils.setStatus(node, data); miDevicesUtils.setStatus(node, data);
@@ -48,6 +49,7 @@ module.exports = function(RED) {
} }
} }
} }
}
// Prepare for request // Prepare for request
else { else {
miDevicesUtils.prepareForGatewayRequest(node, msg); miDevicesUtils.prepareForGatewayRequest(node, msg);
+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 }
};
} }
} }