zigbee2mqtt/lib/util/settings.js

36 lines
739 B
JavaScript
Raw Normal View History

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');
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();
settings = read();
2018-05-12 06:54:02 -07:00
}
2018-04-18 09:25:40 -07:00
function write() {
fs.writeFileSync(file, yaml.safeDump(settings));
2018-04-18 09:25:40 -07:00
}
function read() {
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) {
if (!settings.devices) {
settings.devices = {};
2018-04-25 10:29:03 -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 = {
get: () => settings,
2018-04-18 11:53:22 -07:00
write: () => write(),
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
};