added option for config file
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
"author": "Dennis Gunia",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"commander": "^12.1.0",
|
||||
"log4js": "^6.9.1",
|
||||
"mqtt": "^5.9.1",
|
||||
"tuyapi": "github:codetheweb/tuyapi",
|
||||
|
||||
40
readme.md
40
readme.md
@@ -0,0 +1,40 @@
|
||||
# luminea2mqtt
|
||||
This programm allows to controll and monitor Luminea smart plugs via mqtt.
|
||||
|
||||
the bridge requires node.js version 18 and is based on the following libs:
|
||||
* https://github.com/codetheweb/tuyapi
|
||||
* https://github.com/mqttjs
|
||||
* https://github.com/log4js-node/log4js-node
|
||||
|
||||
## Supported devices
|
||||
* luminea nx-4458
|
||||
|
||||
## Base Installation
|
||||
```
|
||||
nvm install 18
|
||||
nvm use 18
|
||||
sudo ln -s $(realpath `which npm`) /usr/local/bin/npm
|
||||
sudo ln -s `which node` /usr/local/bin/node
|
||||
|
||||
sudo useradd luminea2mqtt -s /bin/bash -d /opt/luminea2mqtt
|
||||
sudo mkdir /opt/luminea2mqtt
|
||||
sudo chown luminea2mqtt /opt/luminea2mqtt
|
||||
sudo su - luminea2mqtt
|
||||
git clone https://github.com/dennis9819/luminea2mqtt.git /opt/luminea2mqtt/bridge
|
||||
cd bridge
|
||||
|
||||
npm install
|
||||
cp config-example.yaml config.yaml
|
||||
```
|
||||
Configure your devices and mqtt settings (still as user luminea2mqtt):
|
||||
```
|
||||
vim config.yaml
|
||||
```
|
||||
Run:
|
||||
```
|
||||
sudo -u luminea2mqtt node /opt/luminea2mqtt/bridge/src/index.js -c /opt/luminea2mqtt/bridge/config.yaml
|
||||
```
|
||||
### Install as systemd service
|
||||
```
|
||||
|
||||
```
|
||||
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