2018-10-02 12:15:12 -07:00
|
|
|
const utils = require('../util/utils');
|
|
|
|
const interval = utils.secondsToMilliseconds(60);
|
|
|
|
|
|
|
|
/**
|
2019-02-13 12:55:14 -07:00
|
|
|
* This extensions handles Xiaomi devices.
|
|
|
|
* - Marks Xiaomi devices as online.
|
|
|
|
* - Polls Xiaomi routers to keep them awake.
|
2018-10-02 12:15:12 -07:00
|
|
|
*/
|
2019-02-13 12:55:14 -07:00
|
|
|
class Xiaomi {
|
2019-02-04 10:36:49 -07:00
|
|
|
constructor(zigbee, mqtt, state, publishEntityState) {
|
2018-10-02 12:15:12 -07:00
|
|
|
this.zigbee = zigbee;
|
|
|
|
this.timer = null;
|
2018-11-16 12:23:11 -07:00
|
|
|
}
|
2018-10-02 12:15:12 -07:00
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
onZigbeeStarted() {
|
2019-02-13 12:55:14 -07:00
|
|
|
// Set all Xiaomi devices to be online, so shepherd won't try
|
|
|
|
// to query info from devices (which would fail because they go to sleep).
|
|
|
|
const devices = this.zigbee.getAllClients();
|
|
|
|
devices.forEach((d) => {
|
|
|
|
if (utils.isXiaomiDevice(d)) {
|
|
|
|
const device = this.zigbee.shepherd.find(d.ieeeAddr, 1);
|
|
|
|
if (device) {
|
|
|
|
device.getDevice().update({
|
|
|
|
status: 'online',
|
|
|
|
joinTime: Math.floor(Date.now() / 1000),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-10-02 12:15:12 -07:00
|
|
|
this.startTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
startTimer() {
|
|
|
|
this.clearTimer();
|
|
|
|
this.timer = setInterval(() => this.handleInterval(), interval);
|
|
|
|
}
|
|
|
|
|
|
|
|
clearTimer() {
|
|
|
|
if (this.timer) {
|
|
|
|
clearTimeout(this.timer);
|
|
|
|
this.timer = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stop() {
|
|
|
|
this.clearTimer();
|
|
|
|
}
|
|
|
|
|
2019-01-26 13:19:20 -07:00
|
|
|
ping(ieeeAddr) {
|
2019-02-01 16:57:51 -07:00
|
|
|
this.zigbee.ping(ieeeAddr);
|
2019-01-26 13:19:20 -07:00
|
|
|
}
|
|
|
|
|
2018-10-02 12:15:12 -07:00
|
|
|
handleInterval() {
|
|
|
|
this.zigbee.getAllClients()
|
|
|
|
.filter((d) => utils.isXiaomiDevice(d)) // Filter Xiaomi devices
|
|
|
|
.filter((d) => d.type === 'Router') // Filter routers
|
|
|
|
.filter((d) => d.powerSource && d.powerSource !== 'Battery') // Remove battery powered devices
|
2019-01-26 13:19:20 -07:00
|
|
|
.forEach((d) => this.ping(d.ieeeAddr)); // Ping devices.
|
2018-10-02 12:15:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-13 12:55:14 -07:00
|
|
|
module.exports = Xiaomi;
|