2018-08-05 09:38:33 -07:00
|
|
|
const logger = require('./util/logger');
|
|
|
|
const data = require('./util/data');
|
|
|
|
const fs = require('fs');
|
2019-02-23 08:22:01 -07:00
|
|
|
const settings = require('./util/settings');
|
2019-03-15 15:18:19 -07:00
|
|
|
const objectAssignDeep = require('object-assign-deep');
|
2018-08-05 09:38:33 -07:00
|
|
|
|
|
|
|
const saveInterval = 1000 * 60 * 5; // 5 minutes
|
|
|
|
|
2019-03-15 15:18:19 -07:00
|
|
|
const dontCacheProperties = [
|
|
|
|
'action', 'button', 'button_left', 'button_right', 'click', 'forgotten', 'keyerror',
|
|
|
|
'step_size', 'transition_time', 'action_color_temperature', 'action_color',
|
2019-03-15 15:33:24 -07:00
|
|
|
'action_group', 'group_list', 'group_capacity', 'no_occupancy_since',
|
2019-03-15 15:18:19 -07:00
|
|
|
];
|
|
|
|
|
2018-08-05 09:38:33 -07:00
|
|
|
class State {
|
|
|
|
constructor() {
|
|
|
|
this.state = {};
|
|
|
|
this.file = data.joinPath('state.json');
|
2018-11-28 11:34:37 -07:00
|
|
|
this.timer = null;
|
2019-02-23 08:22:01 -07:00
|
|
|
|
|
|
|
this.handleSettingsChanged = this.handleSettingsChanged.bind(this);
|
2018-11-28 11:34:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
start() {
|
2018-08-05 09:38:33 -07:00
|
|
|
this._load();
|
|
|
|
|
|
|
|
// Save the state on every interval
|
2018-11-28 11:34:37 -07:00
|
|
|
this.clearTimer();
|
2018-08-05 09:38:33 -07:00
|
|
|
this.timer = setInterval(() => this.save(), saveInterval);
|
2019-02-23 08:22:01 -07:00
|
|
|
|
|
|
|
// Listen for on settings changed events.
|
|
|
|
settings.addOnChangeHandler(this.handleSettingsChanged);
|
|
|
|
|
|
|
|
this.checkLastSeen();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSettingsChanged() {
|
|
|
|
this.checkLastSeen();
|
|
|
|
}
|
|
|
|
|
|
|
|
checkLastSeen() {
|
|
|
|
if (settings.get().advanced.last_seen === 'disable') {
|
|
|
|
Object.values(this.state).forEach((s) => {
|
|
|
|
if (s.hasOwnProperty('last_seen')) {
|
|
|
|
delete s.last_seen;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.save();
|
|
|
|
}
|
2018-08-05 09:38:33 -07:00
|
|
|
}
|
2018-11-28 11:34:37 -07:00
|
|
|
|
|
|
|
clearTimer() {
|
|
|
|
if (this.timer) {
|
|
|
|
clearTimeout(this.timer);
|
|
|
|
this.timer = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stop() {
|
|
|
|
this.clearTimer();
|
|
|
|
this.save();
|
|
|
|
}
|
2018-08-05 09:38:33 -07:00
|
|
|
|
|
|
|
_load() {
|
|
|
|
if (fs.existsSync(this.file)) {
|
|
|
|
try {
|
|
|
|
this.state = JSON.parse(fs.readFileSync(this.file, 'utf8'));
|
|
|
|
logger.debug(`Loaded state from file ${this.file}`);
|
|
|
|
} catch (e) {
|
|
|
|
logger.debug(`Failed to load state from file ${this.file} (corrupt file?)`);
|
|
|
|
}
|
|
|
|
} else {
|
2019-02-19 11:16:18 -07:00
|
|
|
logger.debug(`Can't load state from file ${this.file} (doesn't exist)`);
|
2018-08-05 09:38:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
save() {
|
|
|
|
logger.debug(`Saving state to file ${this.file}`);
|
|
|
|
const json = JSON.stringify(this.state, null, 4);
|
|
|
|
fs.writeFileSync(this.file, json, 'utf8');
|
|
|
|
}
|
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
exists(ieeeAddr) {
|
|
|
|
return this.state.hasOwnProperty(ieeeAddr);
|
2018-08-05 09:38:33 -07:00
|
|
|
}
|
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
get(ieeeAddr) {
|
|
|
|
return this.state[ieeeAddr];
|
2018-08-05 09:38:33 -07:00
|
|
|
}
|
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
set(ieeeAddr, state) {
|
2019-03-15 15:18:19 -07:00
|
|
|
const s = objectAssignDeep.noMutate(state);
|
|
|
|
dontCacheProperties.forEach((property) => {
|
|
|
|
if (s.hasOwnProperty(property)) {
|
|
|
|
delete s[property];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.state[ieeeAddr] = s;
|
2018-08-05 09:38:33 -07:00
|
|
|
}
|
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
remove(ieeeAddr) {
|
|
|
|
if (this.exists(ieeeAddr)) {
|
|
|
|
delete this.state[ieeeAddr];
|
2018-08-05 09:38:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = State;
|