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
|
|
|
|
2018-06-07 10:41:11 -07:00
|
|
|
function removeDevice(id) {
|
2018-06-10 08:35:14 -07:00
|
|
|
if (settings.devices && settings.devices[id]) {
|
2018-06-07 10:41:11 -07:00
|
|
|
delete settings.devices[id];
|
|
|
|
writeRead();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-09 03:27:04 -07:00
|
|
|
function getIDByFriendlyName(friendlyName) {
|
|
|
|
if (!settings.devices) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Object.keys(settings.devices).find((id) =>
|
|
|
|
settings.devices[id].friendly_name === friendlyName
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-07-24 09:25:16 -07:00
|
|
|
function changeFriendlyName(old, new_) {
|
|
|
|
const ID = getIDByFriendlyName(old);
|
|
|
|
|
|
|
|
if (!ID) {
|
|
|
|
return false;
|
2018-07-21 09:15:56 -07:00
|
|
|
}
|
2018-07-24 09:25:16 -07:00
|
|
|
|
|
|
|
settings.devices[ID].friendly_name = new_;
|
2018-07-21 09:15:56 -07:00
|
|
|
writeRead();
|
2018-07-24 09:25:16 -07:00
|
|
|
return true;
|
2018-07-21 09:15:56 -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-06-10 08:35:14 -07:00
|
|
|
getDevice: (id) => settings.devices ? settings.devices[id] : null,
|
2018-04-25 10:29:03 -07:00
|
|
|
addDevice: (id) => addDevice(id),
|
2018-06-07 10:41:11 -07:00
|
|
|
removeDevice: (id) => removeDevice(id),
|
2018-06-09 03:27:04 -07:00
|
|
|
getIDByFriendlyName: (friendlyName) => getIDByFriendlyName(friendlyName),
|
2018-07-24 09:25:16 -07:00
|
|
|
changeFriendlyName: (old, new_) => changeFriendlyName(old, new_),
|
2018-05-17 08:20:46 -07:00
|
|
|
};
|