2019-09-09 10:48:09 -07:00
|
|
|
/* istanbul ignore file */
|
2019-09-17 09:32:16 -07:00
|
|
|
// DEPRECATED
|
2020-04-11 08:44:31 -07:00
|
|
|
const settings = require('../../util/settings');
|
|
|
|
const logger = require('../../util/logger');
|
|
|
|
const utils = require('../../util/utils');
|
2020-04-11 11:47:25 -07:00
|
|
|
const Extension = require('../extension');
|
2018-10-02 12:15:12 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This extensions soft resets the ZNP after a certain timeout.
|
|
|
|
*/
|
2020-04-11 09:10:56 -07:00
|
|
|
class SoftReset extends Extension {
|
2020-01-09 13:47:19 -07:00
|
|
|
constructor(zigbee, mqtt, state, publishEntityState, eventBus) {
|
|
|
|
super(zigbee, mqtt, state, publishEntityState, eventBus);
|
2018-10-02 12:15:12 -07:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
async handleTimeout() {
|
2018-10-02 12:15:12 -07:00
|
|
|
logger.warn('Soft reset timeout triggered');
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
try {
|
2019-10-07 10:34:10 -07:00
|
|
|
await this.zigbee.reset('soft');
|
2019-09-09 10:48:09 -07:00
|
|
|
logger.warn('Soft resetted ZNP due to timeout');
|
|
|
|
} catch (error) {
|
|
|
|
logger.warn('Soft reset failed, trying stop/start');
|
|
|
|
|
|
|
|
await this.zigbee.stop();
|
|
|
|
logger.warn('Zigbee stopped');
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.zigbee.start();
|
|
|
|
} catch (error) {
|
|
|
|
logger.error('Failed to restart!');
|
2018-10-02 12:15:12 -07:00
|
|
|
}
|
2019-09-09 10:48:09 -07:00
|
|
|
}
|
2018-10-02 12:15:12 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
this.resetTimer();
|
2018-10-02 12:15:12 -07:00
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
onZigbeeEvent() {
|
2018-10-02 12:15:12 -07:00
|
|
|
this.resetTimer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = SoftReset;
|