2018-10-02 12:15:12 -07:00
|
|
|
const settings = require('../util/settings');
|
|
|
|
const logger = require('../util/logger');
|
|
|
|
const utils = require('../util/utils');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This extensions soft resets the ZNP after a certain timeout.
|
|
|
|
*/
|
|
|
|
class SoftReset {
|
2018-11-16 12:23:11 -07:00
|
|
|
constructor(zigbee, mqtt, state, publishDeviceState) {
|
2018-10-02 12:15:12 -07:00
|
|
|
this.zigbee = zigbee;
|
|
|
|
this.timer = null;
|
|
|
|
this.timeout = utils.secondsToMilliseconds(settings.get().advanced.soft_reset_timeout);
|
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() {
|
|
|
|
logger.debug(`Soft reset timeout set to ${utils.millisecondsToSeconds(this.timeout)} seconds`);
|
2018-10-02 12:15:12 -07:00
|
|
|
this.resetTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
clearTimer() {
|
|
|
|
if (this.timer) {
|
|
|
|
clearTimeout(this.timer);
|
|
|
|
this.timer = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resetTimer() {
|
|
|
|
if (this.timeout === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.clearTimer();
|
|
|
|
this.timer = setTimeout(() => this.handleTimeout(), this.timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleTimeout() {
|
|
|
|
logger.warn('Soft reset timeout triggered');
|
|
|
|
|
|
|
|
this.zigbee.softReset((error) => {
|
|
|
|
if (error) {
|
|
|
|
logger.warn('Soft reset failed, trying stop/start');
|
|
|
|
this.zigbee.stop((error) => {
|
|
|
|
logger.warn('Zigbee stopped');
|
|
|
|
this.zigbee.start((error) => {
|
|
|
|
if (error) {
|
|
|
|
logger.error('Failed to restart!');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
logger.warn('Soft resetted ZNP due to timeout');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.resetTimer();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
onZigbeeMessage(message, device, mappedDevice) {
|
2018-10-02 12:15:12 -07:00
|
|
|
this.resetTimer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = SoftReset;
|