2019-09-09 10:48:09 -07:00
|
|
|
/* istanbul ignore file */
|
2019-09-17 09:32:16 -07:00
|
|
|
// DEPRECATED
|
2018-10-02 12:15:12 -07:00
|
|
|
const settings = require('../util/settings');
|
|
|
|
const logger = require('../util/logger');
|
|
|
|
const utils = require('../util/utils');
|
2019-09-17 09:32:16 -07:00
|
|
|
const BaseExtension = require('./baseExtension');
|
2018-10-02 12:15:12 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This extensions soft resets the ZNP after a certain timeout.
|
|
|
|
*/
|
2019-09-17 09:32:16 -07:00
|
|
|
class SoftReset extends BaseExtension {
|
2019-02-04 10:36:49 -07:00
|
|
|
constructor(zigbee, mqtt, state, publishEntityState) {
|
2019-09-17 09:32:16 -07:00
|
|
|
super(zigbee, mqtt, state, publishEntityState);
|
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 {
|
|
|
|
await this.zigbee.softReset();
|
|
|
|
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;
|