Skip to content

Commit

Permalink
Improve how settings are saved for devices in storage #383
Browse files Browse the repository at this point in the history
  • Loading branch information
Octavian Ruda committed Oct 4, 2021
1 parent 7179c89 commit f5902bf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
54 changes: 35 additions & 19 deletions lib/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ module.exports = class Device extends EventEmitter {

this.initLogger();
this.initStorage();
this.initRefresh();
this.initInfo();
this.initEvents();

this.cache = new Cache(this);
this.remote = new Remote(this);
Expand Down Expand Up @@ -86,17 +85,46 @@ module.exports = class Device extends EventEmitter {
initStorage() {
this.storage = new Proxy(Storage.get(this.UUID), {
set: (obj, prop, value) => {
obj[prop] = value;
if (prop == 'update' && typeof value == 'object') {
for (const key in value) {
obj[key] = value[key];
}
} else {
obj[prop] = value;
}

Storage.save();
return true;
}
});
}

initRefresh() {
if ([null, false].indexOf(this.config.refresh) !== -1) { return; }
initEvents() {
// Save informations on storage every
// time we fetch them from TV
this.on('api.getInfo', (data) => {
if (!data) return;

this.storage['update'] = {
frametv: data.device.FrameTVSupport == 'true',
tokenauth: data.device.TokenAuthSupport == 'true',
powerstate: data.device.PowerState !== undefined
};
});

// Trigger request to get informations
// after device was loaded
this.on('loaded', () => {
this.remote.getInfo()
.then(() => this.remote.init())
.catch(() => {});
});

// Add accessories refresh interval
// after device was paired
this.on('paired', () => {
if ([null, false].indexOf(this.config.refresh) !== -1) { return; }

let intervals = require('./options/refresh')(this.config);

if (intervals.main) {
Expand All @@ -108,25 +136,13 @@ module.exports = class Device extends EventEmitter {
}
});

// Refresh all accessories when
// state of TV was changed
this.on('state.change', () => {
this.accessories.forEach(element => element.services.main && element.services.main.getValue())
});
}

initInfo() {
this.on('loaded', () => {
this.remote.getInfo().then(data => {
this.storage['frametv'] = data.device.FrameTVSupport == 'true';
this.storage['tokenauth'] = data.device.TokenAuthSupport == 'true';
this.storage['powerstate'] = data.device.PowerState !== undefined;

// Reinitialize Remote after we
// fetch device informations
this.remote.init();
}).catch(() => {});
});
}

hasOption(key) {
return key && this.config.options && this.config.options.includes(key);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/methods/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ module.exports = class Base {
return fetch(`http://${this.ip}:8001/api/v2/`, {
signal: timeoutSignal(this.timeout < 500 ? 500 : this.timeout)
})
.then(body => body.json());
.then(body => body.json())
.then(data => this.device.emit('api.getInfo', data) && data);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ module.exports = class Remote {
* @return {object}
*/
async getInfo() {
if (!await this.api.getActivePing()) {
if (!await this.api.getStatePing()) {
throw new TvOfflineError();
}

Expand Down

0 comments on commit f5902bf

Please sign in to comment.