2
0

refactor(socket): move on/off to dedicated actions

This commit is contained in:
Pierre CLEMENT
2018-01-03 13:05:01 +01:00
parent a33d616516
commit 8a40a6ad54
11 changed files with 715 additions and 66 deletions

View File

@@ -36,20 +36,20 @@ Note that the Wifi power plug is not configured through the configurator as it i
The Xiaomi configurator screen with ease of use to configure your different devices.
![Mi configurator in node-red](mi-configurator.png?raw=true "Mi configurator in node-red")
![Mi configurator in node-red](resources/mi-configurator.png?raw=true "Mi configurator in node-red")
Tip: use the configurator from the side-panel (hamburger menu, configuration nodes) to manage your devices. Node-red doesn't update underlying edit screens if the configuration panel is opened / closed from the edit node screen. (If you do, you need to first close the edit node screen and reopen it by double-clicking the node you want to edit the properties for.)
### How to use different nodes
Here an example of how to use the different nodes:
![Mi devices example in node-red](mi-devices-overview.png?raw=true "Mi devices example in node-red")
![Mi devices example in node-red](resources/mi-devices-overview.png?raw=true "Mi devices example in node-red")
### Sample flows
Here are different flow (screenshot of [importable sample-flows.json](sample-flows.json "Different flows using Mi Devices")):
![Mi devices example in node-red](mi-devices-sample.png?raw=true "Mi devices flow sample")
![Mi devices example in node-red](resources/mi-devices-sample.png?raw=true "Mi devices flow sample")
## Enable LAN mode
@@ -69,8 +69,8 @@ Mainland China and language can set on English.
If you change here something, you lose your password!
![Gateway advanced mode](xiaomi-gateway-advanced-mode.png?raw=true "Gateway advanced mode")
![Gateway LAN mode enabled](xiaomi-gateway-lan-enabled.png?raw=true "Gateway LAN mode enabled")
![Gateway advanced mode](resources/xiaomi-gateway-advanced-mode.png?raw=true "Gateway advanced mode")
![Gateway LAN mode enabled](resources/xiaomi-gateway-lan-enabled.png?raw=true "Gateway LAN mode enabled")
## Roadmap
@@ -80,7 +80,7 @@ If you change here something, you lose your password!
- [ ] Set action status when no token available
- [ ] Add gateway status
- [ ] Update icons
- [ ] Refactor socket and set on/off actions
- [X] Refactor socket and add on/off actions
- [X] Add device SID in output
- [X] Remove different output styles
- [X] Code cleanup

View File

@@ -393,3 +393,87 @@
<li>Message to connect to a gateway out node.</li>
</ol>
</script>
<!-- The plug "on" Node -->
<script type="text/javascript">
RED.nodes.registerType('xiaomi-actions on',{
category: 'xiaomi actions',
color: '#64C4CD',
defaults: {
gateway: {value:"", type:"xiaomi-configurator"},
name: {value:""}
},
inputs:1,
outputs:1,
paletteLabel: "on",
icon: "function.png",
label: function() {
return this.name||"power on";
}
});
</script>
<script type="text/x-red" data-template-name="xiaomi-actions on">
<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 on">
<p>
Turn input device to on.
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>Outputs</h3>
<ol class="node-ports">
<li>Message to connect to a gateway out node.</li>
</ol>
</script>
<!-- The plug "off" Node -->
<script type="text/javascript">
RED.nodes.registerType('xiaomi-actions off',{
category: 'xiaomi actions',
color: '#64C4CD',
defaults: {
gateway: {value:"", type:"xiaomi-configurator"},
name: {value:""}
},
inputs:1,
outputs:1,
paletteLabel: "off",
icon: "function.png",
label: function() {
return this.name||"power off";
}
});
</script>
<script type="text/x-red" data-template-name="xiaomi-actions off">
<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 off">
<p>
Turn input device to off.
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>Outputs</h3>
<ol class="node-ports">
<li>Message to connect to a gateway out node.</li>
</ol>
</script>

View File

@@ -97,11 +97,35 @@ module.exports = (RED) => {
RED.nodes.createNode(this, config);
this.gateway = RED.nodes.getNode(config.gateway);
this.on('input', function(msg) {
miDevicesUtils.sendWritePayloadToGateway(this, msg, {
mid: 1000
});
this.on('input', (msg) => {
miDevicesUtils.sendWritePayloadToGateway(this, msg, { mid: 1000 });
});
}
RED.nodes.registerType("xiaomi-actions gateway_stop_sound", XiaomiActionGatewayStopSound);
/*********************************************
Turn device on
*********************************************/
function XiaomiActionPowerOn(config) {
RED.nodes.createNode(this, config);
this.gateway = RED.nodes.getNode(config.gateway);
this.on('input', (msg) => {
miDevicesUtils.sendWritePayloadToGateway(this, msg, { status: "on", sid: msg.sid});
});
}
RED.nodes.registerType("xiaomi-actions on", XiaomiActionPowerOn);
/*********************************************
Turn device off
*********************************************/
function XiaomiActionPowerOff(config) {
RED.nodes.createNode(this, config);
this.gateway = RED.nodes.getNode(config.gateway);
this.on('input', (msg) => {
miDevicesUtils.sendWritePayloadToGateway(this, msg, { status: "off", sid: msg.sid});
});
}
RED.nodes.registerType("xiaomi-actions off", XiaomiActionPowerOff);
}

View File

@@ -18,7 +18,7 @@ module.exports = (RED) => {
this.plug = plug;
this.status({fill:"green", shape:"dot", text:"connected"});
connectionState = "connected";
delayedStatusMsgUpdate(node);
delayedStatusMsgUpdate(this);
this.plug.on('propertyChanged', (e) => {
if (e.property === "power") {

View File

@@ -5,70 +5,28 @@ module.exports = (RED) => {
RED.nodes.createNode(this, config);
this.gateway = RED.nodes.getNode(config.gateway);
this.sid = config.sid;
this.key = this.gateway.key;
var currentToken = "";
var state = "";
this.status({fill:"yellow", shape:"ring", text:"waiting for key"});
this.status({fill:"grey", shape:"ring", text:"status"});
if (this.gateway && this.key != "") {
this.on('input', (msg) => {
var payload = msg.payload;
if (payload.cmd == "heartbeat" && payload.model == "gateway") {
var token = payload.token;
if (token) {
var cipher = crypto.createCipheriv('aes128', node.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.sid) {
if (payload.sid == this.sid) {
if (data.status && data.status == "on") {
this.status({fill:"green", shape:"dot", text:"on"});
} else if (data.status && data.status == "off") {
this.status({fill:"red", shape:"dot", text:"off"});
}
this.send(msg);
}
}
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:"ring", text:"waiting for 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";
}
msg.payload = payload;
node.send([msg]);
// Prepare for request
else {
miDevicesUtils.prepareForGatewayRequest(this, msg);
this.send(msg);
}
});
} else if (this.key == "") {
node.status({fill:"red", shape:"dot", text:"no key configured"});
}
}

View File

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 10 KiB

View File

Before

Width:  |  Height:  |  Size: 77 KiB

After

Width:  |  Height:  |  Size: 77 KiB

View File

Before

Width:  |  Height:  |  Size: 61 KiB

After

Width:  |  Height:  |  Size: 61 KiB

View File

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB

View File

Before

Width:  |  Height:  |  Size: 39 KiB

After

Width:  |  Height:  |  Size: 39 KiB

File diff suppressed because one or more lines are too long