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

46 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-01-03 12:12:45 +01:00
module.exports = (RED) => {
2018-01-05 22:21:50 +01:00
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-05 22:21:50 +01:00
this.onlyModels = config.onlyModels;
this.excludedSids = config.excludedSids;
2018-01-08 20:41:04 +01:00
console.log(this.onlyModels);
2018-01-05 22:21:50 +01:00
this.isDeviceValid = (device) => {
if((!this.onlyModels || this.onlyModels.length == 0) && (!this.excludedSids || this.excludedSids.length == 0)) {
return true;
}
2018-01-08 20:41:04 +01:00
// Is excluded
2018-01-05 22:21:50 +01:00
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) => {
2018-01-05 22:21:50 +01:00
// 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));
}
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);
}