Skip to content

Commit

Permalink
Improved online/offline handeling of units
Browse files Browse the repository at this point in the history
  • Loading branch information
Joolee committed Jun 9, 2020
1 parent 0cdf407 commit 15cc65f
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 12 deletions.
7 changes: 6 additions & 1 deletion drivers/unit/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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({
Expand Down
15 changes: 7 additions & 8 deletions lib/ESPEasyUnit.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ module.exports = class ESPEasyUnit extends Homey.SimpleClass {
"idx": this.idx
});
this.updateJSON();
this.emit("newhostname", this, hostname, port);
}
}

Expand Down Expand Up @@ -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"));
}
}

Expand Down Expand Up @@ -342,15 +341,15 @@ 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);


}).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 = [];
});
Expand Down
4 changes: 2 additions & 2 deletions lib/ESPEasyUnits.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => { }) {
Expand Down
8 changes: 7 additions & 1 deletion lib/GeneralDevice.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"offline": "Device offline",
"waiting_for_unit": "Waiting for unit",
"pair": {
"sensor": {
"title": "Configure device",
Expand Down
1 change: 1 addition & 0 deletions locales/nl.json
Original file line number Diff line number Diff line change
@@ -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: ",
Expand Down

0 comments on commit 15cc65f

Please sign in to comment.