From 15cc65fa1bc7668cef8a79f017850a65853540da Mon Sep 17 00:00:00 2001 From: Joolee Date: Tue, 9 Jun 2020 12:30:29 +0200 Subject: [PATCH] Improved online/offline handeling of units --- drivers/unit/device.js | 7 ++++++- lib/ESPEasyUnit.js | 15 +++++++-------- lib/ESPEasyUnits.js | 4 ++-- lib/GeneralDevice.js | 8 +++++++- locales/en.json | 1 + locales/nl.json | 1 + 6 files changed, 24 insertions(+), 12 deletions(-) diff --git a/drivers/unit/device.js b/drivers/unit/device.js index ab0c468..bbc2318 100644 --- a/drivers/unit/device.js +++ b/drivers/unit/device.js @@ -8,7 +8,6 @@ module.exports = class UnitDevice extends Homey.Device { this.getData().mac, this.getSetting('host'), this.getSetting('port')); - this.unit.on('newhostname', this.updateHostname.bind(this)) this.unit.addDriver(this); this.unit.updateJSON(); @@ -17,14 +16,20 @@ module.exports = class UnitDevice extends Homey.Device { this.onRawMessage = this.onRawMessage.bind(this); this.onJSONUpdate = this.onJSONUpdate.bind(this); this.onUnitUpdate = this.onUnitUpdate.bind(this); + this.onUnitStateChange = this.onUnitStateChange.bind(this); this.unit.on('rawMessage', this.onRawMessage); this.unit.on('jsonUpdate', this.onJSONUpdate); this.unit.on('settingsUpdate', this.onUnitUpdate); + this.unit.on('stateChange', this.onUnitStateChange); this.unit.setPollInterval(this.getSetting('pollInterval')); this.log('Init:', this.getName()); } + onUnitStateChange(unit, state) { + state ? this.setAvailable() : this.setUnavailable(Homey.__("offline")); + } + onUnitUpdate(unit, newSettings) { this.updateHostname(unit, newSettings.host, newSettings.port); this.setSettings({ diff --git a/lib/ESPEasyUnit.js b/lib/ESPEasyUnit.js index 73644f7..c8e56eb 100644 --- a/lib/ESPEasyUnit.js +++ b/lib/ESPEasyUnit.js @@ -121,7 +121,6 @@ module.exports = class ESPEasyUnit extends Homey.SimpleClass { "idx": this.idx }); this.updateJSON(); - this.emit("newhostname", this, hostname, port); } } @@ -170,17 +169,17 @@ module.exports = class ESPEasyUnit extends Homey.SimpleClass { return Boolean(this.driver); } - isOnline() { - return this.online; + get online() { + return this._online; } - setOnline(state = null) { + set online(state = null) { if (state !== null) { - this.online = state; + this._online = state; + this.emit("stateChange", this, state); } if (this.driver) { - this.online ? this.driver.setAvailable() : this.driver.setUnavailable(Homey.__("offline")); } } @@ -342,7 +341,7 @@ module.exports = class ESPEasyUnit extends Homey.SimpleClass { }).then((response) => { this.lastEvent = new Date(); this.json = response.data; - this.setOnline(true); + this.online = true; this.updateJSONCallbacks.forEach(fn => fn(null, this)); this.updateJSONCallbacks = []; this.emit("jsonUpdate", this, response.data); @@ -350,7 +349,7 @@ module.exports = class ESPEasyUnit extends Homey.SimpleClass { }).catch((error) => { this.log('Error in updating json', error.code ? error.code : error); - this.setOnline(false); + this.online = false; this.updateJSONCallbacks.forEach(fn => fn('Could not reach device', null)); this.updateJSONCallbacks = []; }); diff --git a/lib/ESPEasyUnits.js b/lib/ESPEasyUnits.js index 1274ad1..910c504 100644 --- a/lib/ESPEasyUnits.js +++ b/lib/ESPEasyUnits.js @@ -36,11 +36,11 @@ module.exports = class ESPEasyUnits extends Homey.SimpleClass { } listUnregistered() { - return this.units.filter(unit => !unit.isRegistered() && unit.isOnline()); + return this.units.filter(unit => !unit.isRegistered() && unit.online); } listOnline() { - return this.units.filter(unit => unit.isOnline()); + return this.units.filter(unit => unit.online); } getUnit(mac, host = null, port = 80, autoInitialize = true, callback = () => { }) { diff --git a/lib/GeneralDevice.js b/lib/GeneralDevice.js index fca3326..6e913f2 100644 --- a/lib/GeneralDevice.js +++ b/lib/GeneralDevice.js @@ -4,11 +4,12 @@ const Homey = require('homey'); module.exports = class GeneralDevice extends Homey.Device { onInit() { - this.setUnavailable("Waiting for unit"); + this.setUnavailable(Homey.__("waiting_for_unit")); this.setAvailable = this.setAvailable.bind(this); this.setUnavailable = this.setUnavailable.bind(this); this._onUnitUpdate = this._onUnitUpdate.bind(this); + this.onUnitStateChange = this.onUnitStateChange.bind(this); this.unit = Homey.app.units.getUnit( this.getData().mac, @@ -17,6 +18,11 @@ module.exports = class GeneralDevice extends Homey.Device { ); this.unit.on('settingsUpdate', this._onUnitUpdate); + this.unit.on('stateChange', this.onUnitStateChange); + } + + onUnitStateChange(unit, state) { + state ? this.setAvailable() : this.setUnavailable(Homey.__("waiting_for_unit")); } _onUnitUpdate(unit, newSettings) { diff --git a/locales/en.json b/locales/en.json index 8655716..90674d8 100644 --- a/locales/en.json +++ b/locales/en.json @@ -1,5 +1,6 @@ { "offline": "Device offline", + "waiting_for_unit": "Waiting for unit", "pair": { "sensor": { "title": "Configure device", diff --git a/locales/nl.json b/locales/nl.json index ddd02fa..954b4b1 100644 --- a/locales/nl.json +++ b/locales/nl.json @@ -1,5 +1,6 @@ { "offline": "Apparaat offline", + "waiting_for_unit": "Wachten op unit", "pair": { "input": { "identity_info": "Het apparaat zal worden geïdentificeerd met task nummer: ",