Add possibility for multiple device classes

This commit is contained in:
Dennis Gunia
2024-08-03 18:40:06 +02:00
parent 62e3488780
commit 8e2759a5ce
4 changed files with 37 additions and 29 deletions

View File

@@ -3,8 +3,10 @@ mqtt:
port: 1883
username: "username"
password: "password"
lineaplug:
clientid: "test"
devices:
- id: "sqy709956ply4inkx6ac87"
key: "xxxxxxxxxxxxxxxx"
topic: "tuya/device1"
refresh: 30 #refresh intervall in s
type: luminea_nx_4458

View File

@@ -17,13 +17,20 @@ async function main() {
const mqtt = require("mqtt");
const YAML = require('yaml')
const fs = require('fs')
const Lineplug = require('./lineaplug')
loggerInit.info("Read configfile")
const options = program.opts();
loggerInit.info(`User config from ${options.config}`)
const file = fs.readFileSync(options.config, 'utf8')
const config = YAML.parse(file)
let config = {}
try {
const file = fs.readFileSync(options.config, 'utf8')
config = YAML.parse(file)
} catch (error) {
loggerInit.error(`error reading config: ${error.message}`)
process.exit(10)
}
const mqttserver = `mqtt://${config.mqtt.host}:${config.mqtt.port}`
@@ -33,9 +40,10 @@ async function main() {
// Authentication
username: config.mqtt.username,
password: config.mqtt.password,
clientId: "test",
clientId: config.mqtt.clientid,
debug: true,
});
loggerInit.info(`Connect to ${mqttserver}, user: ${config.mqtt.username}, clientid: ${config.mqtt.clientid}`)
client.on('connect', function () {
loggerInit.info(`Connected to ${mqttserver}`)
// Subscribe to a topic
@@ -44,20 +52,31 @@ async function main() {
loggerInit.info("reconnecting!")
})
client.stream.on('error', (err) => {
loggerInit.error('error', err);
loggerInit.error('error')
loggerInit.error(err)
client.end()
});
let devices = []
config.lineaplug.forEach((device) => {
loggerInit.info(`Setup device ${device.id} Type: Lineplug`)
const newdev = new Lineplug(device, client)
devices.push(newdev)
})
if (config.devices){
config.devices.forEach((device) => {
const deviceClassFile = `./modules/${device.type}`
loggerInit.info(`Setup device ${device.id}, type: ${device.type}, class:'${deviceClassFile}.js'`)
try {
const DeviceClass = require(deviceClassFile)
const newdev = new DeviceClass(device, client)
devices.push(newdev)
} catch (error) {
loggerInit.error(`Error initializing device class ${deviceClassFile}`);
loggerInit.error(error.message);
}
})
}else{
loggerInit.error(`Missing 'devices' in config.`)
process.exit(10)
}
process.on('SIGINT', () => {
for (let device of devices) {

View File

@@ -1,5 +0,0 @@
const log4js = require('log4js');
const logger = log4js.getLogger();
logger.level = 'info';
module.exports.def = logger

View File

@@ -38,7 +38,6 @@ class Lineplug {
}
try {
this.device = new TuyaDevice({
//ip: "10.110.0.126",
id: deviceconfig.id,
key: deviceconfig.key,
issueRefreshOnConnect: true,
@@ -79,7 +78,6 @@ class Lineplug {
// monitor queue
this.mqtt.on('message', (topic, message) => {
// message is Buffer
//if (topic == this.topicname){
let payload = message.toString()
try {
@@ -95,17 +93,14 @@ class Lineplug {
this.logger.trace(payload)
this.logger.trace(error)
}
// }
})
}
processData(data) {
//console.log(data)
if (!data.dps) {
this.logger.warn(`Received unexpected data from device`)
return
}
const dps = data.dps
const updatedValues = Object.keys(dps)
let changed = false
@@ -125,7 +120,6 @@ class Lineplug {
this.lastdata.power = dps['17'] / 100
changed = true
}
if (updatedValues.includes('9')) {
this.lastdata.countdown_1 = dps['9']
changed = true
@@ -138,7 +132,6 @@ class Lineplug {
this.lastdata.random_time = dps['42']
changed = true
}
if (updatedValues.includes('1')) {
this.lastdata.status = dps['1']
changed = true
@@ -149,7 +142,6 @@ class Lineplug {
if (changed) {
this.mqtt.publish(this.topicname, JSON.stringify(this.lastdata))
}
}
disconnect() {