Add settings.set api.

This commit is contained in:
Koen Kanters 2019-02-18 19:46:19 +01:00
parent ba77860489
commit cc45413e2d

View File

@ -75,6 +75,25 @@ function read() {
return yaml.safeLoad(fs.readFileSync(file, 'utf8')); return yaml.safeLoad(fs.readFileSync(file, 'utf8'));
} }
function set(path, value) {
let obj = settings;
for (let i = 0; i < path.length; i++) {
const key = path[i];
if (i === path.length - 1) {
obj[key] = value;
} else {
if (!obj[key]) {
obj[key] = {};
}
obj = obj[key];
}
}
writeRead();
}
function addDevice(ieeeAddr) { function addDevice(ieeeAddr) {
if (!settings.devices) { if (!settings.devices) {
settings.devices = {}; settings.devices = {};
@ -126,6 +145,7 @@ function changeFriendlyName(old, new_) {
module.exports = { module.exports = {
get: () => objectAssignDeep.noMutate(defaults, settings), get: () => objectAssignDeep.noMutate(defaults, settings),
write: () => write(), write: () => write(),
set: (path, value) => set(path, value),
getDevice: (ieeeAddr) => settings.devices ? settings.devices[ieeeAddr] : null, getDevice: (ieeeAddr) => settings.devices ? settings.devices[ieeeAddr] : null,
getGroup: (ID) => settings.groups ? settings.groups[ID]: null, getGroup: (ID) => settings.groups ? settings.groups[ID]: null,