added option for config file
This commit is contained in:
26
src/index.js
26
src/index.js
@@ -1,21 +1,31 @@
|
||||
const log4js = require('log4js');
|
||||
const logger = log4js.getLogger();
|
||||
const loggerInit = log4js.getLogger("initializer");
|
||||
const { Command } = require('commander');
|
||||
const program = new Command();
|
||||
|
||||
logger.level = 'info';
|
||||
loggerInit.level = 'info';
|
||||
|
||||
program
|
||||
.name('luminea2mqtt')
|
||||
.version('1.0.0')
|
||||
.option('-c, --config <value>', 'Path to configfile', './config.yaml')
|
||||
.parse(process.argv);
|
||||
|
||||
async function main() {
|
||||
const mqtt = require("mqtt");
|
||||
const YAML = require('yaml')
|
||||
const fs = require('fs')
|
||||
const Lineplug = require('./lineaplug')
|
||||
loggerInit.info("Read configfile")
|
||||
const file = fs.readFileSync('./config.yaml', 'utf8')
|
||||
|
||||
const options = program.opts();
|
||||
loggerInit.info(`User config from ${options.config}`)
|
||||
const file = fs.readFileSync(options.config, 'utf8')
|
||||
const config = YAML.parse(file)
|
||||
|
||||
|
||||
|
||||
|
||||
const mqttserver = `mqtt://${config.mqtt.host}:${config.mqtt.port}`
|
||||
let client = mqtt.connect(mqttserver, {
|
||||
// Clean session
|
||||
@@ -32,7 +42,7 @@ async function main() {
|
||||
})
|
||||
client.on("reconnect", () => {
|
||||
loggerInit.info("reconnecting!")
|
||||
})
|
||||
})
|
||||
client.stream.on('error', (err) => {
|
||||
loggerInit.error('error', err);
|
||||
client.end()
|
||||
@@ -42,15 +52,15 @@ async function main() {
|
||||
|
||||
config.lineaplug.forEach((device) => {
|
||||
loggerInit.info(`Setup device ${device.id} Type: Lineplug`)
|
||||
const newdev = new Lineplug(device,client)
|
||||
const newdev = new Lineplug(device, client)
|
||||
devices.push(newdev)
|
||||
|
||||
})
|
||||
|
||||
|
||||
|
||||
process.on('SIGINT',() =>{
|
||||
for (let device of devices){
|
||||
|
||||
process.on('SIGINT', () => {
|
||||
for (let device of devices) {
|
||||
device.disconnect()
|
||||
}
|
||||
process.exit(2);
|
||||
|
||||
Reference in New Issue
Block a user