feat(clean): remove sockets
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,6 +1,5 @@
|
||||
.DS_Store
|
||||
.idea
|
||||
dist/
|
||||
/node_modules
|
||||
.log
|
||||
package-lock.json
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 16 KiB |
@@ -1,67 +0,0 @@
|
||||
<script type="text/javascript">
|
||||
RED.nodes.registerType('xiaomi-plug-wifi', {
|
||||
category: 'xiaomi',
|
||||
color: '#3FADB5',
|
||||
defaults: {
|
||||
name: {value: ""},
|
||||
ip: {value: "", required: true},
|
||||
onmsg: {value: ""},
|
||||
offmsg: {value: ""},
|
||||
output: {value: "0"}
|
||||
},
|
||||
inputs: 1,
|
||||
outputs: 1,
|
||||
outputLabels: ["Status"],
|
||||
paletteLabel: "plug (wifi)",
|
||||
icon: "outlet-wifi-icon.png",
|
||||
label: function () {
|
||||
return this.name || "xiaomi-plug-wifi";
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-template-name="xiaomi-plug-wifi">
|
||||
<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-ip"><i class="icon-tag"></i> Ip</label>
|
||||
<input type="text" id="node-input-ip" placeholder="ip address">
|
||||
</div>
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-help-name="xiaomi-plug-wifi">
|
||||
<p>The Xiaomi plug (wifi) node</p>
|
||||
|
||||
<p>This is the wiFi plug (socket). To control the Wifi-Plug, extensive use is made of the miio library created by <a href="https://github.com/aholstenson/miio">Andreas Holstenson</a>. Make sure to check his page for compatible devices.</p>
|
||||
|
||||
<h3>Inputs</h3>
|
||||
<dl class="message-properties">
|
||||
<dt>payload
|
||||
<span class="property-type">string</span>
|
||||
</dt>
|
||||
<dd><code>on</code> or <code>off</code>.</dd>
|
||||
</dl>
|
||||
|
||||
<h3>Outputs</h3>
|
||||
<ol class="node-ports">
|
||||
<dl class="message-properties">
|
||||
<dt>payload <span class="property-type">object</span></dt>
|
||||
<dd>Data from gateway, see below.</dd>
|
||||
</dl>
|
||||
</ol>
|
||||
|
||||
<h4>Details</h4>
|
||||
<p>On the input you can send the string <code>on</code> to switch the plug on. To turn it off just send the string <code>off</code></p>
|
||||
<p>Sample message full data:</p>
|
||||
<p><pre>{
|
||||
type: "power-plug",
|
||||
model: "chuangmi.plug.m1",
|
||||
capabilities: [ {"0": "power-channels"} ],
|
||||
address: "192.168.178.31",
|
||||
port: 54321,
|
||||
power: { "0": false },
|
||||
state: "on"
|
||||
}</pre></p>
|
||||
</script>
|
||||
@@ -1,149 +0,0 @@
|
||||
const miio = require("miio");
|
||||
|
||||
module.exports = (RED) => {
|
||||
var connectionState = "timeout";
|
||||
var retryTimer;
|
||||
var delayedStatusMsgTimer;
|
||||
|
||||
|
||||
function XiaomiPlugWifiNode(config) {
|
||||
RED.nodes.createNode(this, config);
|
||||
this.ip = config.ip;
|
||||
this.plug = null;
|
||||
|
||||
this.status({fill: "yellow", shape: "dot", text: "connecting"});
|
||||
|
||||
miio.device({address: this.ip})
|
||||
.then((plug) => {
|
||||
this.plug = plug;
|
||||
this.status({fill:"green", shape:"dot", text:"connected"});
|
||||
connectionState = "connected";
|
||||
delayedStatusMsgUpdate(this);
|
||||
|
||||
this.plug.on('propertyChanged', (e) => {
|
||||
if (e.property === "power") {
|
||||
if (e.value['0']) {
|
||||
setState("on");
|
||||
} else {
|
||||
setState("off");
|
||||
}
|
||||
}
|
||||
});
|
||||
watchdog();
|
||||
})
|
||||
.catch((error) => {
|
||||
connectionState = "reconnecting";
|
||||
watchdog();
|
||||
})
|
||||
|
||||
this.on('input', (msg) => {
|
||||
var payload = msg.payload;
|
||||
if (connectionState === "connected") {
|
||||
if (payload == 'on') {
|
||||
this.plug.setPower(true);
|
||||
}
|
||||
|
||||
if (payload == 'off') {
|
||||
this.plug.setPower(false);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
this.on('close', (done) => {
|
||||
if (retryTimer) {
|
||||
clearTimeout(retryTimer);
|
||||
}
|
||||
if (delayedStatusMsgTimer) {
|
||||
clearTimeout(delayedStatusMsgTimer);
|
||||
}
|
||||
if (this.plug) {
|
||||
this.plug.destroy();
|
||||
}
|
||||
done();
|
||||
});
|
||||
|
||||
var setState = (state) => {
|
||||
if (this.plug) {
|
||||
let status = {
|
||||
payload: {
|
||||
id: this.plug.id,
|
||||
type: this.plug.type,
|
||||
model: this.plug.model,
|
||||
capabilities: this.plug.capabilities,
|
||||
address: this.plug.address,
|
||||
port: this.plug.port,
|
||||
power: this.plug.power(),
|
||||
state: state
|
||||
}
|
||||
};
|
||||
this.send(status);
|
||||
}
|
||||
};
|
||||
|
||||
var delayedStatusMsgUpdate = () => {
|
||||
delayedStatusMsgTimer = setTimeout(() => {
|
||||
if (this.plug.power()['0']) {
|
||||
setState("on");
|
||||
} else {
|
||||
setState("off");
|
||||
}
|
||||
}, 1500);
|
||||
};
|
||||
|
||||
var discoverDevice = () => {
|
||||
miio.device({address: this.ip})
|
||||
.then((plug) => {
|
||||
if (this.plug == null) {
|
||||
this.plug = plug;
|
||||
this.plug.on('propertyChanged', (e) => {
|
||||
if (e.property === "power") {
|
||||
if (e.value['0']) {
|
||||
setState("on");
|
||||
} else {
|
||||
setState("off");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
if (connectionState === "reconnecting") {
|
||||
this.status({fill:"green", shape:"dot", text:"connected"});
|
||||
connectionState = "connected";
|
||||
delayedStatusMsgUpdate();
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
connectionState = "reconnecting";
|
||||
if (this.plug) {
|
||||
this.plug.destroy();
|
||||
this.plug = null;
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
var watchdog = () => {
|
||||
var node = this;
|
||||
function retryTimer() {
|
||||
discoverDevice();
|
||||
if (connectionState === "reconnecting") {
|
||||
node.status({fill: "red", shape: "dot", text: "reconnecting"});
|
||||
}
|
||||
setTimeout(retryTimer, 30000);
|
||||
}
|
||||
setTimeout(retryTimer, 30000);
|
||||
}
|
||||
}
|
||||
|
||||
RED.nodes.registerType("xiaomi-plug-wifi", XiaomiPlugWifiNode);
|
||||
|
||||
process.on('unhandledRejection', function(reason, p) {
|
||||
// console.log("Possibly Unhandled Rejection at: Promise ", p, " reason: ", reason);
|
||||
var message = reason + "";
|
||||
if (message.indexOf("Call to device timed out") >= 0) {
|
||||
if (this.plug) {
|
||||
console.log("Issue with miio package; discard plug and reconnect.");
|
||||
this.plug.destroy();
|
||||
this.plug = null;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 16 KiB |
@@ -1,126 +0,0 @@
|
||||
<script type="text/javascript">
|
||||
RED.nodes.registerType('xiaomi-plug', {
|
||||
category: 'xiaomi',
|
||||
color: '#3FADB5',
|
||||
defaults: {
|
||||
gateway: {value:"", type:"xiaomi-configurator"},
|
||||
name: {value: ""},
|
||||
sid: {value: "", required: true},
|
||||
onmsg: {value: ""},
|
||||
offmsg: {value: ""},
|
||||
output: {value: "0"}
|
||||
},
|
||||
inputs: 1,
|
||||
outputs: 1,
|
||||
paletteLabel: "plug (zigbee)",
|
||||
icon: "outlet-icon.png",
|
||||
label: function () {
|
||||
return this.name || "xiaomi-plug";
|
||||
},
|
||||
oneditprepare: function() {
|
||||
var node = this;
|
||||
|
||||
if(node.sid) {
|
||||
$('#node-input-sid').val(node.sid);
|
||||
}
|
||||
|
||||
function changeGateway(model) {
|
||||
var configNodeID = $('#node-input-gateway').val();
|
||||
if (configNodeID) {
|
||||
var configNode = RED.nodes.node(configNodeID);
|
||||
if(configNode) {
|
||||
$('#node-input-sid').empty();
|
||||
for (key in configNode.deviceList) {
|
||||
var device = configNode.deviceList[key];
|
||||
if (device.model === model) {
|
||||
$('#node-input-sid').append('<option value="' + device.sid + '">' + device.desc + '</option>');
|
||||
}
|
||||
}
|
||||
if(node.sid) {
|
||||
$('#node-input-sid option[value="' + node.sid + '"]').prop('selected', true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$("#node-input-sid").change(function () {
|
||||
if(!this.name) {
|
||||
$("#node-input-name").val($('#node-input-sid option:selected').text());
|
||||
}
|
||||
});
|
||||
$("#node-input-gateway").change(function () {
|
||||
changeGateway("plug");
|
||||
});
|
||||
},
|
||||
oneditsave: function() {
|
||||
var node = this;
|
||||
node.sid = $("#node-input-sid").val();
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-template-name="xiaomi-plug">
|
||||
<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>
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-help-name="xiaomi-plug">
|
||||
<p>The Xiaomi plug (zigbee) node</p>
|
||||
|
||||
<p>This is the plug (socket) version which is attached to a Xiaomi gateway. The Wifi version is not yet supported.</p>
|
||||
<p>To switch an output you need to specify the key of the gateway in the gateway configuration; without the key
|
||||
no output can be switched. To retrieve the gateway key consult the Xiaomi Mi Home App.</p>
|
||||
|
||||
<h3>Inputs</h3>
|
||||
<dl class="message-properties">
|
||||
<dt>payload
|
||||
<span class="property-type">string | json</span>
|
||||
</dt>
|
||||
<dd>When the node is used as filter, gateway <code>plug</code> message of type <code>read_ack</code>, <code>heartbeat</code> or <code>report</code>. Or <code>on</code> or <code>off</code>.</dd>
|
||||
</dl>
|
||||
|
||||
<h3>Outputs</h3>
|
||||
<ol class="node-ports">
|
||||
<li>Status output
|
||||
<dl class="message-properties">
|
||||
<dt>payload <span class="property-type">string | json</span></dt>
|
||||
<dd>raw data, value or template.</dd>
|
||||
</dl>
|
||||
</li>
|
||||
<li>Control output
|
||||
<dl class="message-properties">
|
||||
<dt>payload <span class="property-type">json</span></dt>
|
||||
<dd>Gateway <code>write_cmd</code> to switch the output on or off.</dd>
|
||||
</dl>
|
||||
</li>
|
||||
</ol>
|
||||
|
||||
<h4>Details</h4>
|
||||
<p>The incoming json message is parsed if the type model is <code>plug</code> and
|
||||
the <code>sid</code> matches the configured value for this device.</p>
|
||||
<p>On the input you can send the string <code>on</code> to switch the plug on. To turn it off just send the string <code>off</code></p>
|
||||
<p>Sample message:</p>
|
||||
<p><pre>{
|
||||
cmd: "write_ack"
|
||||
model: "plug"
|
||||
sid: "158d00012f1fb5"
|
||||
short_id: 47414
|
||||
data: {
|
||||
voltage:3600,
|
||||
status:"off",
|
||||
inuse:"0",
|
||||
power_consumed:"4000",
|
||||
load_power:"0"
|
||||
}
|
||||
}</pre></p>
|
||||
</script>
|
||||
@@ -1,36 +0,0 @@
|
||||
const crypto = require("crypto");
|
||||
|
||||
module.exports = (RED) => {
|
||||
function XiaomiPlugNode(config) {
|
||||
RED.nodes.createNode(this, config);
|
||||
this.gateway = RED.nodes.getNode(config.gateway);
|
||||
this.sid = config.sid;
|
||||
|
||||
this.status({fill:"grey", shape:"ring", text:"status"});
|
||||
|
||||
if (this.gateway && this.key != "") {
|
||||
this.on('input', (msg) => {
|
||||
var payload = msg.payload;
|
||||
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);
|
||||
}
|
||||
}
|
||||
// Prepare for request
|
||||
else {
|
||||
miDevicesUtils.prepareForGatewayRequest(this, msg);
|
||||
this.send(msg);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
RED.nodes.registerType("xiaomi-plug", XiaomiPlugNode);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user