Add possibility for multiple device classes
This commit is contained in:
@@ -3,8 +3,10 @@ mqtt:
|
|||||||
port: 1883
|
port: 1883
|
||||||
username: "username"
|
username: "username"
|
||||||
password: "password"
|
password: "password"
|
||||||
lineaplug:
|
clientid: "test"
|
||||||
|
devices:
|
||||||
- id: "sqy709956ply4inkx6ac87"
|
- id: "sqy709956ply4inkx6ac87"
|
||||||
key: "xxxxxxxxxxxxxxxx"
|
key: "xxxxxxxxxxxxxxxx"
|
||||||
topic: "tuya/device1"
|
topic: "tuya/device1"
|
||||||
refresh: 30 #refresh intervall in s
|
refresh: 30 #refresh intervall in s
|
||||||
|
type: luminea_nx_4458
|
||||||
45
src/index.js
45
src/index.js
@@ -17,13 +17,20 @@ async function main() {
|
|||||||
const mqtt = require("mqtt");
|
const mqtt = require("mqtt");
|
||||||
const YAML = require('yaml')
|
const YAML = require('yaml')
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const Lineplug = require('./lineaplug')
|
|
||||||
loggerInit.info("Read configfile")
|
loggerInit.info("Read configfile")
|
||||||
|
|
||||||
const options = program.opts();
|
const options = program.opts();
|
||||||
loggerInit.info(`User config from ${options.config}`)
|
loggerInit.info(`User config from ${options.config}`)
|
||||||
const file = fs.readFileSync(options.config, 'utf8')
|
let config = {}
|
||||||
const config = YAML.parse(file)
|
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}`
|
const mqttserver = `mqtt://${config.mqtt.host}:${config.mqtt.port}`
|
||||||
@@ -33,9 +40,10 @@ async function main() {
|
|||||||
// Authentication
|
// Authentication
|
||||||
username: config.mqtt.username,
|
username: config.mqtt.username,
|
||||||
password: config.mqtt.password,
|
password: config.mqtt.password,
|
||||||
clientId: "test",
|
clientId: config.mqtt.clientid,
|
||||||
debug: true,
|
debug: true,
|
||||||
});
|
});
|
||||||
|
loggerInit.info(`Connect to ${mqttserver}, user: ${config.mqtt.username}, clientid: ${config.mqtt.clientid}`)
|
||||||
client.on('connect', function () {
|
client.on('connect', function () {
|
||||||
loggerInit.info(`Connected to ${mqttserver}`)
|
loggerInit.info(`Connected to ${mqttserver}`)
|
||||||
// Subscribe to a topic
|
// Subscribe to a topic
|
||||||
@@ -44,20 +52,31 @@ async function main() {
|
|||||||
loggerInit.info("reconnecting!")
|
loggerInit.info("reconnecting!")
|
||||||
})
|
})
|
||||||
client.stream.on('error', (err) => {
|
client.stream.on('error', (err) => {
|
||||||
loggerInit.error('error', err);
|
loggerInit.error('error')
|
||||||
|
loggerInit.error(err)
|
||||||
client.end()
|
client.end()
|
||||||
});
|
});
|
||||||
|
|
||||||
let devices = []
|
let devices = []
|
||||||
|
|
||||||
config.lineaplug.forEach((device) => {
|
if (config.devices){
|
||||||
loggerInit.info(`Setup device ${device.id} Type: Lineplug`)
|
config.devices.forEach((device) => {
|
||||||
const newdev = new Lineplug(device, client)
|
const deviceClassFile = `./modules/${device.type}`
|
||||||
devices.push(newdev)
|
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', () => {
|
process.on('SIGINT', () => {
|
||||||
for (let device of devices) {
|
for (let device of devices) {
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
const log4js = require('log4js');
|
|
||||||
const logger = log4js.getLogger();
|
|
||||||
logger.level = 'info';
|
|
||||||
|
|
||||||
module.exports.def = logger
|
|
||||||
@@ -38,7 +38,6 @@ class Lineplug {
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
this.device = new TuyaDevice({
|
this.device = new TuyaDevice({
|
||||||
//ip: "10.110.0.126",
|
|
||||||
id: deviceconfig.id,
|
id: deviceconfig.id,
|
||||||
key: deviceconfig.key,
|
key: deviceconfig.key,
|
||||||
issueRefreshOnConnect: true,
|
issueRefreshOnConnect: true,
|
||||||
@@ -79,7 +78,6 @@ class Lineplug {
|
|||||||
// monitor queue
|
// monitor queue
|
||||||
this.mqtt.on('message', (topic, message) => {
|
this.mqtt.on('message', (topic, message) => {
|
||||||
// message is Buffer
|
// message is Buffer
|
||||||
//if (topic == this.topicname){
|
|
||||||
let payload = message.toString()
|
let payload = message.toString()
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -95,17 +93,14 @@ class Lineplug {
|
|||||||
this.logger.trace(payload)
|
this.logger.trace(payload)
|
||||||
this.logger.trace(error)
|
this.logger.trace(error)
|
||||||
}
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
processData(data) {
|
processData(data) {
|
||||||
//console.log(data)
|
|
||||||
if (!data.dps) {
|
if (!data.dps) {
|
||||||
|
this.logger.warn(`Received unexpected data from device`)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const dps = data.dps
|
const dps = data.dps
|
||||||
const updatedValues = Object.keys(dps)
|
const updatedValues = Object.keys(dps)
|
||||||
let changed = false
|
let changed = false
|
||||||
@@ -125,7 +120,6 @@ class Lineplug {
|
|||||||
this.lastdata.power = dps['17'] / 100
|
this.lastdata.power = dps['17'] / 100
|
||||||
changed = true
|
changed = true
|
||||||
}
|
}
|
||||||
|
|
||||||
if (updatedValues.includes('9')) {
|
if (updatedValues.includes('9')) {
|
||||||
this.lastdata.countdown_1 = dps['9']
|
this.lastdata.countdown_1 = dps['9']
|
||||||
changed = true
|
changed = true
|
||||||
@@ -138,7 +132,6 @@ class Lineplug {
|
|||||||
this.lastdata.random_time = dps['42']
|
this.lastdata.random_time = dps['42']
|
||||||
changed = true
|
changed = true
|
||||||
}
|
}
|
||||||
|
|
||||||
if (updatedValues.includes('1')) {
|
if (updatedValues.includes('1')) {
|
||||||
this.lastdata.status = dps['1']
|
this.lastdata.status = dps['1']
|
||||||
changed = true
|
changed = true
|
||||||
@@ -149,7 +142,6 @@ class Lineplug {
|
|||||||
if (changed) {
|
if (changed) {
|
||||||
this.mqtt.publish(this.topicname, JSON.stringify(this.lastdata))
|
this.mqtt.publish(this.topicname, JSON.stringify(this.lastdata))
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
disconnect() {
|
disconnect() {
|
||||||
Reference in New Issue
Block a user