feat(all): add filter on model and devices

Close #1
This commit is contained in:
Pierre CLEMENT
2018-01-05 22:21:50 +01:00
parent e0f4d589f9
commit 98158c6dd1
12 changed files with 186 additions and 36 deletions

View File

@@ -4,7 +4,9 @@
color: '#3FADB5',
defaults: {
gateway: {value:"", type:"xiaomi-configurator"},
name: {value: ""}
name: {value: ""},
onlyModels: {value: []},
excludedSids: { value: []}
},
inputs: 1,
outputs: 1,
@@ -13,6 +15,44 @@
icon: "mi-all.png",
label: function () {
return this.name || "xiaomi-all";
},
oneditprepare: function() {
var node = this;
function changeGateway(gateway, onlyModels, excludedSids) {
var configNodeID = gateway || $('#node-input-gateway').val();
if (configNodeID) {
var configNode = RED.nodes.node(configNodeID);
if(configNode) {
onlyModels = onlyModels || $('#node-input-onlyModels').val() || [];
excludedSids = excludedSids || $('#node-input-excludedSids').val() || [];
$('#node-input-excludedSids').empty();
for (key in configNode.deviceList) {
var device = configNode.deviceList[key];
if (onlyModels.length == 0 || onlyModels.indexOf(device.model) >= 0) {
var option = $('<option value="' + device.sid + '">' + device.desc + '</option>');
if(excludedSids && excludedSids.indexOf(device.sid) >= 0) {
option.prop('selected', true);
}
$('#node-input-excludedSids').append(option);
}
}
}
}
}
changeGateway(this.gateway, this.onlyModels, this.excludedSids);
$("#node-input-gateway, #node-input-onlyModels").change(function () {
changeGateway();
});
},
oneditsave: function() {
if(!$('#node-input-onlyModels').val()) {
this.onlyModels = [];
}
if(!$('#node-input-excludedSids').val()) {
this.excludedSids = [];
}
}
});
</script>
@@ -26,11 +66,35 @@
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<hr />
<h5>Filters</h5>
<div class="form-row">
<label for="node-input-onlyModels"><i class="icon-tag"></i> Only</label>
<select multiple id="node-input-onlyModels">
<option value="sensor_ht">Temperature/humidty</option>
<option value="motion">Motion</option>
<option value="switch">Switches</option>
<option value="magnet">Contacts</option>
<option value="plug">Plugs</option>
</select>
</div>
<div class="form-row">
<label for="node-input-excludedSids"><i class="icon-tag"></i> Exclude</label>
<select multiple id="node-input-excludedSids" size=10></select>
</div>
</script>
<script type="text/x-red" data-help-name="xiaomi-all">
<p>All devices registred in the gateway, except gateway itself.</p>
<h3>Inputs</h3>
<dl class="message-properties">
<dt>payload
<span class="property-type">object</span>
</dt>
<dd>When use as an incoming filter node, <code>sid</code> and <code>model</code> are mandatory.</dd>
</dl>
<h3>Outputs</h3>
<ol class="node-ports">
<li>Devices output

View File

@@ -1,11 +1,39 @@
module.exports = (RED) => {
function XiaomiAllNode(config) {
RED.nodes.createNode(this, config);
this.gateway = RED.nodes.getNode(config.gateway);
this.onlyModels = config.onlyModels;
this.excludedSids = config.excludedSids;
this.isDeviceValid = (device) => {
if((!this.onlyModels || this.onlyModels.length == 0) && (!this.excludedSids || this.excludedSids.length == 0)) {
return true;
}
if((this.excludedSids && this.excludedSids.length != 0) && this.excludedSids.indexOf(device.sid) >= 0) {
return false;
}
if((this.onlyModels && this.onlyModels.length != 0) && this.onlyModels.indexOf(device.model) >= 0) {
return true;
}
return false;
}
if (this.gateway) {
this.on('input', (msg) => {
msg.payload = this.gateway.deviceList;
// Filter input
if(msg.payload.model && msg.payload.sid) {
if(!this.isDeviceValid(msg.payload)) {
msg = null;
}
}
// Prepare for request
else {
msg.payload = this.gateway.deviceList.filter((device) => this.isDeviceValid(device));
}
this.send(msg);
});
}

View File

@@ -48,8 +48,6 @@
$("#node-input-gateway").change(function () {
changeGateway("sensor_ht");
});
$(".node-input-msg").hide();
},
oneditsave: function() {
var node = this;
@@ -79,21 +77,31 @@
<h3>Inputs</h3>
<dl class="message-properties">
<dt>payload
<span class="property-type">json</span>
<span class="property-type">object</span>
</dt>
<dd>Gateway <code>sensor_ht</code> and <code>weather.v1</code> message of type <code>read_ack</code>, <code>heartbeat</code> or <code>report</code></dd>
<dd>
When the message contains a <code>sid</code> field, the node will filter the input and output only if the <code>sid</code> is the device's sid.<br>
<hr>
If the message doesn't contain a <code>sid</code> field, the node will be used to inject <code>sid</code> and <code>gateway</code> fields in the incoming <code>msg</code>.<br>
<hr>
Input Gateway node produces message of type <code>read_ack</code>, <code>heartbeat</code> or <code>report</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>
<dd>Data from gateway when used as a filter (see below).</dd>
<dt>sid <span class="property-type">string</span></dt>
<dd>Device SID.</dd>
<dt>gateway <span class="property-type">object</span></dt>
<dd>The <code>xiaomi-configurator</code> object where the device is registred.</dd>
</dl>
</ol>
<h4>Details</h4>
<p>The incoming message is processed if the <code>sid</code> matches the configured value for this device.</p>
<p>The incoming message is processed if the input <code>sid</code> matches the configured value for this device.</p>
<p>Sample payload from Aqara (which brings pressure):</p>
<p><pre>{
cmd: "read_ack"

View File

@@ -1,6 +1,8 @@
const miDevicesUtils = require('../src/utils');
module.exports = (RED) => {
// sensor_ht, weather.v1
function XiaomiHtNode(config) {
RED.nodes.createNode(this, config);
this.gateway = RED.nodes.getNode(config.gateway);

View File

@@ -77,21 +77,31 @@
<h3>Inputs</h3>
<dl class="message-properties">
<dt>payload
<span class="property-type">json</span>
<span class="property-type">object</span>
</dt>
<dd>Gateway <code>magnet</code> and <code>sensor_magnet.aq2</code> message of type <code>read_ack</code>, <code>heartbeat</code> or <code>report</code></dd>
<dd>
When the message contains a <code>sid</code> field, the node will filter the input and output only if the <code>sid</code> is the device's sid.<br>
<hr>
If the message doesn't contain a <code>sid</code> field, the node will be used to inject <code>sid</code> and <code>gateway</code> fields in the incoming <code>msg</code>.<br>
<hr>
Input Gateway node produces message of type <code>read_ack</code>, <code>heartbeat</code> or <code>report</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>
<dd>Data from gateway when used as a filter (see below).</dd>
<dt>sid <span class="property-type">string</span></dt>
<dd>Device SID.</dd>
<dt>gateway <span class="property-type">object</span></dt>
<dd>The <code>xiaomi-configurator</code> object where the device is registred.</dd>
</dl>
</ol>
<h3>Details</h3>
<p>The incoming message is processed if the <code>sid</code> matches the configured value for this device.</p>
<h4>Details</h4>
<p>The incoming message is processed if the input <code>sid</code> matches the configured value for this device.</p>
<p>Sample message:</p>
<p><pre>{
cmd: "read_ack"

View File

@@ -1,6 +1,7 @@
const miDevicesUtils = require('../src/utils');
module.exports = (RED) => {
// magnet, sensor_magnet.aq2
function XiaomiMagnetNode(config) {
miDevicesUtils.defaultNode(RED, config, this);
}

View File

@@ -80,21 +80,31 @@
<h3>Inputs</h3>
<dl class="message-properties">
<dt>payload
<span class="property-type">json</span>
<span class="property-type">object</span>
</dt>
<dd>Gateway <code>motion</code> message of type <code>read_ack</code>, <code>heartbeat</code> or <code>report</code></dd>
<dd>
When the message contains a <code>sid</code> field, the node will filter the input and output only if the <code>sid</code> is the device's sid.<br>
<hr>
If the message doesn't contain a <code>sid</code> field, the node will be used to inject <code>sid</code> and <code>gateway</code> fields in the incoming <code>msg</code>.<br>
<hr>
Input Gateway node produces message of type <code>read_ack</code>, <code>heartbeat</code> or <code>report</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>
<dd>Data from gateway when used as a filter (see below).</dd>
<dt>sid <span class="property-type">string</span></dt>
<dd>Device SID.</dd>
<dt>gateway <span class="property-type">object</span></dt>
<dd>The <code>xiaomi-configurator</code> object where the device is registred.</dd>
</dl>
</ol>
<h4>Details</h4>
<p>The incoming message is processed if the <code>sid</code> matches the configured value for this device.</p>
<p>The incoming message is processed if the input <code>sid</code> matches the configured value for this device.</p>
<p>Sample message:</p>
<p><pre>{
cmd: "read_ack"

View File

@@ -1,6 +1,7 @@
const miDevicesUtils = require('../src/utils');
module.exports = (RED) => {
// motion
function XiaomiMotionNode(config) {
miDevicesUtils.defaultNode(RED, config, this);
}

View File

@@ -20,22 +20,37 @@
oneditprepare: function() {
var node = this;
// Get the config node id from the select box:
var configNodeID = $('#node-input-gateway').val();
// Get the config node using the ID:
var configNode = RED.nodes.node(configNodeID);
if(node.sid) {
$('#node-input-sid').val(node.sid);
}
$("#node-input-gateway").change(function () {
});
for (key in configNode.deviceList) {
var device = configNode.deviceList[key];
if (device.model === "plug") {
$('#node-input-sid').append('<option value="' + device.sid + '">' + device.desc + '</option>');
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').val(node.sid);
$("#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;
@@ -71,7 +86,7 @@
<dt>payload
<span class="property-type">string | json</span>
</dt>
<dd>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>
<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>

View File

@@ -83,21 +83,31 @@
<h3>Inputs</h3>
<dl class="message-properties">
<dt>payload
<span class="property-type">json</span>
<span class="property-type">object</span>
</dt>
<dd>Gateway <code>switch</code> and <code>sensor_switch.aq2</code> message of type <code>read_ack</code>, <code>heartbeat</code> or <code>report</code></dd>
<dd>
When the message contains a <code>sid</code> field, the node will filter the input and output only if the <code>sid</code> is the device's sid.<br>
<hr>
If the message doesn't contain a <code>sid</code> field, the node will be used to inject <code>sid</code> and <code>gateway</code> fields in the incoming <code>msg</code>.<br>
<hr>
Input Gateway node produces message of type <code>read_ack</code>, <code>heartbeat</code> or <code>report</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>
<dd>Data from gateway when used as a filter (see below).</dd>
<dt>sid <span class="property-type">string</span></dt>
<dd>Device SID.</dd>
<dt>gateway <span class="property-type">object</span></dt>
<dd>The <code>xiaomi-configurator</code> object where the device is registred.</dd>
</dl>
</ol>
<h4>Details</h4>
<p>The incoming message is processed if the <code>sid</code> matches the configured value for this device.</p>
<p>The incoming message is processed if the input <code>sid</code> matches the configured value for this device.</p>
<p>Sample message:</p>
<p><pre>{
cmd: "report"

View File

@@ -1,6 +1,7 @@
const miDevicesUtils = require('../src/utils');
module.exports = (RED) => {
// switch, sensor_switch.aq2
function XiaomiSwitchNode(config) {
miDevicesUtils.defaultNode(RED, config, this);
}

View File

@@ -35,6 +35,6 @@
"miio": "0.13.0"
},
"engines": {
"node" : ">=4.4.5"
"node": ">=4.4.5"
}
}