node-red-contrib-miio-airfresh
Version:
Miio node for Air Fresh (Xiaomi MJXFJ-150-A1)
127 lines (103 loc) • 4.67 kB
JavaScript
const EventEmitter = require('events');
const miio = require('miio');
module.exports = function (RED) {
class ServerNode {
constructor(n) {
RED.nodes.createNode(this, n);
var node = this;
node.config = n;
node.state = [];
node.status = {};
node.setMaxListeners(255);
node.refreshFindTimer = null;
node.refreshFindInterval = node.config.polling * 1000;
node.on('close', () => this.onClose());
node.connect().then(result => {
node.getStatus(true).then(result => {
node.emit("onInitEnd", result);
});
});
node.refreshStatusTimer = setInterval(function () {
node.getStatus(true);
}, node.refreshFindInterval);
}
onClose() {
var that = this;
clearInterval(that.refreshStatusTimer);
if (that.device) {
that.device.destroy();
that.device = null;
}
}
connect() {
var node = this;
return new Promise(function (resolve, reject) {
node.miio = miio.device({
address: node.config.ip,
token: node.config.token
}).then(device => {
node.device = device;
node.device.updateMaxPollFailures(0);
node.device.on('thing:initialized', () => {
node.log('Miio AirFresh: Initialized');
});
node.device.on('thing:destroyed', () => {
node.log('Miio AirFresh: Destroyed');
});
resolve(device);
}).catch(err => {
node.emit('onConnectionError', err.message);
node.warn('Miio AirFresh Error: ' + err.message);
reject(err);
});
});
}
getStatus(force = false) {
var that = this;
return new Promise(function (resolve, reject) {
if (force || !that.status) {
if (that.device !== null && that.device !== undefined) {
that.device.loadProperties(["power", "mode", "favourite_speed", "control_speed", "pm25", "co2", "temperature_outside", "filter_rate", "filter_day", "ptc_status", "child_lock", "sound", "display"]).then(result => {
that.emit("onState", result);
for (var key in result) {
var value = result[key];
if (value === undefined || value === null) { continue; }
if (key in that.status) {
if (!(key in that.status) || that.status[key] !== value) {
that.status[key] = value;
that.emit("onStateChanged", {
key: key,
value: value
}, true);
}
} else { //init: silent add
that.status[key] = value;
that.emit("onStateChanged", {
key: key,
value: value
}, false);
}
}
resolve(that.status);
}).catch(err => {
console.log('Encountered an error while controlling device');
console.log('Error(1) was:');
console.log(err.message);
that.status = {};
that.emit('onConnectionError', err.message);
reject(err);
});
} else {
that.connect();
that.status = {};
that.emit('onConnectionError', 'No device');
reject('No device');
}
} else {
resolve(that.status);
}
});
}
}
RED.nodes.registerType('miio-airfresh-server', ServerNode, {});
};