2
0
Files
node-red-contrib-mi-devices/node-red-contrib-xiaomi-all/xiaomi-all.js

51 lines
1.7 KiB
JavaScript
Raw Normal View History

2018-01-03 12:12:45 +01:00
module.exports = (RED) => {
2018-01-08 21:02:40 +01:00
function getOnlyModelsValue(input) {
var cleanOnlyModels = [];
input.forEach((value) => {
cleanOnlyModels = cleanOnlyModels.concat(value.split(','));
});
return cleanOnlyModels;
}
2018-01-01 03:54:28 +01:00
function XiaomiAllNode(config) {
RED.nodes.createNode(this, config);
this.gateway = RED.nodes.getNode(config.gateway);
2018-01-08 21:02:40 +01:00
this.onlyModels = getOnlyModelsValue(config.onlyModels || []);
this.excludedSids = config.excludedSids;
this.isDeviceValid = (device) => {
if((!this.onlyModels || this.onlyModels.length == 0) && (!this.excludedSids || this.excludedSids.length == 0)) {
return true;
}
// Is excluded
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;
}
2018-01-01 03:54:28 +01:00
if (this.gateway) {
2018-01-03 12:12:45 +01:00
this.on('input', (msg) => {
// Filter input
if(msg.payload && 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));
}
2018-01-03 12:12:45 +01:00
this.send(msg);
2018-01-01 03:54:28 +01:00
});
}
}
RED.nodes.registerType("xiaomi-all", XiaomiAllNode);
}