2018-08-05 09:38:33 -07:00
|
|
|
const logger = require('./util/logger');
|
|
|
|
const data = require('./util/data');
|
|
|
|
const fs = require('fs');
|
2019-03-15 15:18:19 -07:00
|
|
|
const objectAssignDeep = require('object-assign-deep');
|
2019-09-17 09:33:27 -07:00
|
|
|
const events = require('events');
|
2019-09-25 03:51:17 -07:00
|
|
|
const equals = require('fast-deep-equal');
|
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-05-26 11:03:43 -07:00
|
|
|
'step_mode', 'transition_time', 'duration', 'elapsed',
|
2019-03-15 15:18:19 -07:00
|
|
|
];
|
|
|
|
|
2019-09-17 09:33:27 -07:00
|
|
|
class State extends events.EventEmitter {
|
2018-08-05 09:38:33 -07:00
|
|
|
constructor() {
|
2019-09-17 09:33:27 -07:00
|
|
|
super();
|
2018-08-05 09:38:33 -07:00
|
|
|
this.state = {};
|
|
|
|
this.file = data.joinPath('state.json');
|
2018-11-28 11:34:37 -07:00
|
|
|
this.timer = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
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');
|
|
|
|
}
|
|
|
|
|
2019-09-17 09:33:27 -07:00
|
|
|
exists(ID) {
|
|
|
|
return this.state.hasOwnProperty(ID);
|
2018-08-05 09:38:33 -07:00
|
|
|
}
|
|
|
|
|
2019-09-17 09:33:27 -07:00
|
|
|
get(ID) {
|
|
|
|
return this.state[ID];
|
2018-08-05 09:38:33 -07:00
|
|
|
}
|
|
|
|
|
2019-09-25 03:51:17 -07:00
|
|
|
is(ID, expected) {
|
|
|
|
const current = this.get(ID);
|
|
|
|
if (!current) return false;
|
|
|
|
|
|
|
|
for (const [key, value] of Object.entries(expected)) {
|
|
|
|
if (!equals(current[key], value)) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-17 09:33:27 -07:00
|
|
|
set(ID, state) {
|
2019-04-29 11:38:40 -07:00
|
|
|
const toState = objectAssignDeep.noMutate(state);
|
2019-03-15 15:18:19 -07:00
|
|
|
dontCacheProperties.forEach((property) => {
|
2019-04-29 11:38:40 -07:00
|
|
|
if (toState.hasOwnProperty(property)) {
|
|
|
|
delete toState[property];
|
2019-03-15 15:18:19 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-09-17 09:33:27 -07:00
|
|
|
const fromState = this.state[ID];
|
|
|
|
|
|
|
|
this.state[ID] = toState;
|
|
|
|
|
|
|
|
this.emit('stateChange', {ID, from: fromState, to: toState});
|
2018-08-05 09:38:33 -07:00
|
|
|
}
|
|
|
|
|
2019-09-17 09:33:27 -07:00
|
|
|
remove(ID) {
|
|
|
|
if (this.exists(ID)) {
|
|
|
|
delete this.state[ID];
|
2018-08-05 09:38:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = State;
|