2
0

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
+29 -1
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);
});
}