2018-05-25 07:42:29 -07:00
|
|
|
const yaml = require('js-yaml');
|
2018-04-25 10:29:03 -07:00
|
|
|
const fs = require('fs');
|
2018-05-11 20:04:15 -07:00
|
|
|
const data = require('./data');
|
2018-05-12 01:58:06 -07:00
|
|
|
const file = data.joinPath('configuration.yaml');
|
|
|
|
|
2018-05-25 07:42:29 -07:00
|
|
|
let settings = read();
|
2018-04-18 09:25:40 -07:00
|
|
|
|
2018-05-17 08:20:46 -07:00
|
|
|
function writeRead() {
|
2018-05-12 06:54:02 -07:00
|
|
|
write();
|
2018-05-25 07:42:29 -07:00
|
|
|
settings = read();
|
2018-05-12 06:54:02 -07:00
|
|
|
}
|
|
|
|
|
2018-04-18 09:25:40 -07:00
|
|
|
function write() {
|
2018-05-25 07:42:29 -07:00
|
|
|
fs.writeFileSync(file, yaml.safeDump(settings));
|
2018-04-18 09:25:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function read() {
|
2018-05-25 07:42:29 -07:00
|
|
|
return yaml.safeLoad(fs.readFileSync(file, 'utf8'));
|
2018-04-18 09:25:40 -07:00
|
|
|
}
|
|
|
|
|
2018-04-25 10:29:03 -07:00
|
|
|
function addDevice(id) {
|
2018-05-25 07:42:29 -07:00
|
|
|
if (!settings.devices) {
|
|
|
|
settings.devices = {};
|
2018-04-25 10:29:03 -07:00
|
|
|
}
|
|
|
|
|
2018-05-25 07:42:29 -07:00
|
|
|
settings.devices[id] = {friendly_name: id, retain: false};
|
2018-05-17 08:20:46 -07:00
|
|
|
writeRead();
|
2018-04-25 10:29:03 -07:00
|
|
|
}
|
2018-04-18 09:25:40 -07:00
|
|
|
|
|
|
|
module.exports = {
|
2018-05-25 07:42:29 -07:00
|
|
|
get: () => settings,
|
2018-04-18 11:53:22 -07:00
|
|
|
write: () => write(),
|
2018-05-25 07:42:29 -07:00
|
|
|
getDevice: (id) => settings.devices ? settings.devices[id] : false,
|
2018-04-25 10:29:03 -07:00
|
|
|
addDevice: (id) => addDevice(id),
|
2018-05-17 08:20:46 -07:00
|
|
|
};
|