refactor(socket): move on/off to dedicated actions
12
README.md
@@ -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.
|
||||
|
||||

|
||||

|
||||
|
||||
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:
|
||||

|
||||

|
||||
|
||||
|
||||
### Sample flows
|
||||
|
||||
Here are different flow (screenshot of [importable sample-flows.json](sample-flows.json "Different flows using Mi Devices")):
|
||||

|
||||

|
||||
|
||||
## Enable LAN mode
|
||||
|
||||
@@ -69,8 +69,8 @@ Mainland China and language can set on English.
|
||||
|
||||
If you change here something, you lose your password!
|
||||
|
||||

|
||||

|
||||

|
||||

|
||||
|
||||
## 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
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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") {
|
||||
|
||||
@@ -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"});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
Before Width: | Height: | Size: 10 KiB After Width: | Height: | Size: 10 KiB |
|
Before Width: | Height: | Size: 77 KiB After Width: | Height: | Size: 77 KiB |
|
Before Width: | Height: | Size: 61 KiB After Width: | Height: | Size: 61 KiB |
|
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 11 KiB |
|
Before Width: | Height: | Size: 39 KiB After Width: | Height: | Size: 39 KiB |