mirror of
https://github.com/Koenkk/zigbee2mqtt.git
synced 2026-08-14 06:40:05 +00:00
YAML configuration.
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
default:
|
||||
mqtt:
|
||||
server: "mqtt://localhost"
|
||||
base_topic: xiaomi
|
||||
serial:
|
||||
port: /dev/ttyACM0
|
||||
|
||||
user:
|
||||
@@ -6,31 +6,16 @@ const mqtt = require('mqtt')
|
||||
const ArgumentParser = require('argparse').ArgumentParser;
|
||||
const fs = require('fs');
|
||||
const parsers = require('./parsers');
|
||||
const config = require('yaml-config');
|
||||
const configFile = `${__dirname}/data/configuration.yaml`
|
||||
const settings = config.readConfig(configFile, 'user');
|
||||
|
||||
const configFile = `${__dirname}/data/config.json`
|
||||
|
||||
// Create configFile if does not exsist.
|
||||
if (!fs.existsSync(configFile)) {
|
||||
console.log(`Created config file at ${configFile}`);
|
||||
console.log('Modify this config file according to your situation.');
|
||||
console.log('"mqtt": the MQTT host, E.G. mqtt://192.168.1.10')
|
||||
console.log('"device": location of CC2531 usb stick, E.G. /dev/ttyACM0')
|
||||
console.log("")
|
||||
console.log('Once finished, restart the application.');
|
||||
console.log('Exiting...');
|
||||
|
||||
const template = {
|
||||
'mqtt': 'mqtt://192.168.1.10',
|
||||
'device': '/dev/ttyACM0',
|
||||
'friendlyNames': {}
|
||||
}
|
||||
|
||||
writeConfig(template);
|
||||
process.exit();
|
||||
// Create empty device array if not set yet.
|
||||
if (!settings.devices) {
|
||||
settings.devices = {};
|
||||
writeConfig();
|
||||
}
|
||||
|
||||
const config = readConfig();
|
||||
|
||||
// Parse arguments
|
||||
const parser = new ArgumentParser({
|
||||
version: '1.0.0',
|
||||
@@ -49,10 +34,10 @@ parser.addArgument(
|
||||
const args = parser.parseArgs();
|
||||
|
||||
// Setup client
|
||||
console.log(`Connecting to MQTT server at ${config.mqtt}`)
|
||||
const client = mqtt.connect(config.mqtt)
|
||||
console.log(`Connecting to MQTT server at ${settings.mqtt.server}`)
|
||||
const client = mqtt.connect(settings.mqtt.server)
|
||||
const shepherd = new ZShepherd(
|
||||
config.device,
|
||||
settings.serial.port,
|
||||
{
|
||||
net: {panId: 0x1a62},
|
||||
dbPath: `${__dirname}/data/database.db`
|
||||
@@ -66,7 +51,7 @@ shepherd.on('ind', handleMessage);
|
||||
process.on('SIGINT', handleQuit);
|
||||
|
||||
// Start server
|
||||
console.log(`Starting zigbee-shepherd with device ${config.device}`)
|
||||
console.log(`Starting zigbee-shepherd with device ${settings.serial.port}`)
|
||||
shepherd.start((err) => {
|
||||
if (err) {
|
||||
console.error('Error while starting zigbee-shepherd');
|
||||
@@ -122,10 +107,13 @@ function handleMessage(msg) {
|
||||
const device = msg.endpoints[0].device;
|
||||
|
||||
// Check if new device, add to config if new.
|
||||
if (!config.devices[device.ieeeAddr]) {
|
||||
if (!settings.devices[device.ieeeAddr]) {
|
||||
console.log(`Detected new device: ${device.ieeeAddr} ${device.nwkAddr} ${device.modelId}`);
|
||||
config.devices[device.ieeeAddr] = device.ieeeAddr;
|
||||
writeConfig(config);
|
||||
|
||||
settings.devices[device.ieeeAddr] = {
|
||||
friendly_name: device.ieeeAddr
|
||||
};
|
||||
writeConfig();
|
||||
}
|
||||
|
||||
// Check if we have a parser for this type of message.
|
||||
@@ -140,7 +128,7 @@ function handleMessage(msg) {
|
||||
}
|
||||
|
||||
// Parse the message.
|
||||
const friendlyName = config.devices[device.ieeeAddr];
|
||||
const friendlyName = settings.devices[device.ieeeAddr].friendly_name;
|
||||
const payload = parser.parse(msg).toString();
|
||||
const topic = `xiaomi/${friendlyName}/${parser.topic}`;
|
||||
|
||||
@@ -161,11 +149,7 @@ function handleQuit() {
|
||||
});
|
||||
}
|
||||
|
||||
function readConfig() {
|
||||
return JSON.parse(fs.readFileSync(configFile, 'utf8'));
|
||||
}
|
||||
|
||||
function writeConfig(content) {
|
||||
const pretty = JSON.stringify(content, null, 2);
|
||||
fs.writeFileSync(configFile, pretty);
|
||||
function writeConfig() {
|
||||
config.updateConfig(settings, configFile, 'user');
|
||||
}
|
||||
|
||||
Generated
+61
-3
@@ -38,6 +38,13 @@
|
||||
"integrity": "sha1-fbs7IQ/coIJFDa0jNMMErzm9x4Q=",
|
||||
"requires": {
|
||||
"underscore": "1.4.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"underscore": {
|
||||
"version": "1.4.4",
|
||||
"resolved": "https://registry.npmjs.org/underscore/-/underscore-1.4.4.tgz",
|
||||
"integrity": "sha1-YaajIBBiKvoHljvzJSA88SI51gQ="
|
||||
}
|
||||
}
|
||||
},
|
||||
"bindings": {
|
||||
@@ -239,6 +246,11 @@
|
||||
"is-buffer": "1.1.6"
|
||||
}
|
||||
},
|
||||
"esprima": {
|
||||
"version": "1.0.4",
|
||||
"resolved": "https://registry.npmjs.org/esprima/-/esprima-1.0.4.tgz",
|
||||
"integrity": "sha1-n1V+CPw7TSbs6d00+Pv0drYlha0="
|
||||
},
|
||||
"extend": {
|
||||
"version": "3.0.1",
|
||||
"resolved": "https://registry.npmjs.org/extend/-/extend-3.0.1.tgz",
|
||||
@@ -391,6 +403,26 @@
|
||||
"resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz",
|
||||
"integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE="
|
||||
},
|
||||
"js-yaml": {
|
||||
"version": "2.1.3",
|
||||
"resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-2.1.3.tgz",
|
||||
"integrity": "sha1-D/tWF75VUlh4Bj16Fq7n/dKC6Ew=",
|
||||
"requires": {
|
||||
"argparse": "0.1.16",
|
||||
"esprima": "1.0.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"argparse": {
|
||||
"version": "0.1.16",
|
||||
"resolved": "https://registry.npmjs.org/argparse/-/argparse-0.1.16.tgz",
|
||||
"integrity": "sha1-z9AeD7uj1srtBJ+9dY1A9lGW9Xw=",
|
||||
"requires": {
|
||||
"underscore": "1.7.0",
|
||||
"underscore.string": "2.4.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"json-stable-stringify": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/json-stable-stringify/-/json-stable-stringify-1.0.1.tgz",
|
||||
@@ -435,6 +467,11 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"log": {
|
||||
"version": "1.4.0",
|
||||
"resolved": "https://registry.npmjs.org/log/-/log-1.4.0.tgz",
|
||||
"integrity": "sha1-S6HYkP3iSbAx3KA7w36q8yVlbxw="
|
||||
},
|
||||
"minimatch": {
|
||||
"version": "3.0.4",
|
||||
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz",
|
||||
@@ -514,6 +551,13 @@
|
||||
"localforage": "1.7.1",
|
||||
"mkdirp": "0.5.1",
|
||||
"underscore": "1.4.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"underscore": {
|
||||
"version": "1.4.4",
|
||||
"resolved": "https://registry.npmjs.org/underscore/-/underscore-1.4.4.tgz",
|
||||
"integrity": "sha1-YaajIBBiKvoHljvzJSA88SI51gQ="
|
||||
}
|
||||
}
|
||||
},
|
||||
"object-keys": {
|
||||
@@ -1653,9 +1697,14 @@
|
||||
"integrity": "sha1-5z3T17DXxe2G+6xrCufYxqadUPo="
|
||||
},
|
||||
"underscore": {
|
||||
"version": "1.4.4",
|
||||
"resolved": "https://registry.npmjs.org/underscore/-/underscore-1.4.4.tgz",
|
||||
"integrity": "sha1-YaajIBBiKvoHljvzJSA88SI51gQ="
|
||||
"version": "1.7.0",
|
||||
"resolved": "https://registry.npmjs.org/underscore/-/underscore-1.7.0.tgz",
|
||||
"integrity": "sha1-a7rwh3UA02vjTsqlhODbn+8DUgk="
|
||||
},
|
||||
"underscore.string": {
|
||||
"version": "2.4.0",
|
||||
"resolved": "https://registry.npmjs.org/underscore.string/-/underscore.string-2.4.0.tgz",
|
||||
"integrity": "sha1-jN2PusTi0uoefi6Al8QvRCKA+Fs="
|
||||
},
|
||||
"unique-stream": {
|
||||
"version": "2.2.1",
|
||||
@@ -1713,6 +1762,15 @@
|
||||
"resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.1.tgz",
|
||||
"integrity": "sha1-pcbVMr5lbiPbgg77lDofBJmNY68="
|
||||
},
|
||||
"yaml-config": {
|
||||
"version": "0.3.0",
|
||||
"resolved": "https://registry.npmjs.org/yaml-config/-/yaml-config-0.3.0.tgz",
|
||||
"integrity": "sha1-vfOpBOaBWMgw+g/lE/7IfZVeN6I=",
|
||||
"requires": {
|
||||
"js-yaml": "2.1.3",
|
||||
"log": "1.4.0"
|
||||
}
|
||||
},
|
||||
"zcl-id": {
|
||||
"version": "0.3.2",
|
||||
"resolved": "https://registry.npmjs.org/zcl-id/-/zcl-id-0.3.2.tgz",
|
||||
|
||||
+2
-1
@@ -26,6 +26,7 @@
|
||||
"mqtt": "*",
|
||||
"perfy": "*",
|
||||
"zigbee-shepherd": "*",
|
||||
"argparse": "*"
|
||||
"argparse": "*",
|
||||
"yaml-config": "*"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user