Refactor softResetTimeout

This commit is contained in:
Koenkk 2018-05-21 11:59:01 +02:00
parent 78c4f43496
commit 02818ddaf5

View File

@ -67,34 +67,36 @@ class Controller {
// Start poll timer.
this.pollTimer(true);
this.resetSoftResetTimeout();
this.softResetTimeout(true);
}
resetSoftResetTimeout() {
softResetTimeout(start) {
if (this._softResetTimer) {
clearTimeout(this._softResetTimer);
this._softResetTimer = null;
}
this._softResetTimer = setTimeout(() => {
this.zigbee.softReset((error) => {
if (error) {
logger.warn('Soft reset error', error);
this.zigbee.stop((error) => {
logger.warn('Zigbee stopped');
this.zigbee.start(this.handleZigbeeMessage, (error) => {
if (error) {
logger.error('Failed to restart!');
}
if (start) {
this._softResetTimer = setTimeout(() => {
this.zigbee.softReset((error) => {
if (error) {
logger.warn('Soft reset error', error);
this.zigbee.stop((error) => {
logger.warn('Zigbee stopped');
this.zigbee.start(this.handleZigbeeMessage, (error) => {
if (error) {
logger.error('Failed to restart!');
}
});
});
});
} else {
logger.warn('Soft resetted zigbee');
}
} else {
logger.warn('Soft resetted zigbee');
}
this.resetSoftResetTimeout();
});
}, softResetTimeout);
this.softResetTimeout(true);
});
}, softResetTimeout);
}
}
pollTimer(start) {
@ -117,6 +119,7 @@ class Controller {
stop(callback) {
this.mqtt.disconnect();
this.pollTimer(false);
this.softResetTimeout(false);
this.zigbee.stop(callback);
}
@ -138,7 +141,7 @@ class Controller {
handleZigbeeMessage(message) {
// Zigbee message receieved, reset soft reset timeout.
this.resetSoftResetTimeout();
this.softResetTimeout(true);
debug('Recieved zigbee message with data', message.data);