From b43ee4b97d0df91e6864aa58235e746cb6d600c2 Mon Sep 17 00:00:00 2001 From: Deti Fliegl Date: Sat, 30 Apr 2022 11:22:46 +0200 Subject: [PATCH 1/2] load only *.js files from payload directory, do not abort on disabled MQTT / InfluxDB --- index.js | 63 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 27 deletions(-) diff --git a/index.js b/index.js index c8aeca1..825898b 100644 --- a/index.js +++ b/index.js @@ -12,6 +12,7 @@ var server = udp.createSocket('udp4'); var Parser = require('binary-parser').Parser; const Influx = require('influx'); var fs = require('fs'); +const path = require('path'); @@ -67,13 +68,16 @@ options={ password:mqttpassword, clean:true}; -var client = mqtt.connect('mqtt://' + mqtthost, options) +var client; -client.on("error",function(error){ - console.log("Can't connect to MQTT server" + error); - console.log(mqttenabled); - if (mqttenabled) { process.exit(1); } -}); +if(mqttenabled) { + client = mqtt.connect('mqtt://' + mqtthost, options) + client.on("error",function(error){ + console.log("Can't connect to MQTT server" + error); + console.log(mqttenabled); + if (mqttenabled) { process.exit(1); } + }); +} //console.log("connected flag "+client.connected); @@ -93,8 +97,10 @@ influx.ping(5000).then(hosts => { if (host.online) { console.log(`${host.url.host} responded in ${host.rtt}ms running ${host.version})`) } else { - console.log(`InfluxDB: ${host.url.host} is offline so quitting`) - if (influxenabled) process.exit(1); + if (influxenabled) { + console.log(`InfluxDB: ${host.url.host} is offline so quitting`) + process.exit(1); + } } }) }) @@ -121,9 +127,10 @@ function getPayload(data) { // Function to handle and send out data to MQTT service // It takes SystemId, MessageID and the data to send out. The data must be in Json format function sendMqtt(SystemId,MessageId,data) { - - client.publish('Batrium/' + SystemId + '/' + MessageId , JSON.stringify(data)); - if (debugMQTT) console.log('Data sent to MQTT: Batrium/' + SystemId + '/' + MessageId); + if(mqttenabled) { + client.publish('Batrium/' + SystemId + '/' + MessageId , JSON.stringify(data)); + if (debugMQTT) console.log('Data sent to MQTT: Batrium/' + SystemId + '/' + MessageId); + } } @@ -135,18 +142,17 @@ function sendInflux(data, tag) { tg = { systemId: data.SystemId, messageId: data.MessageId, messageType: (config[messageID] && config[messageID].tag ) ? config[messageID].tag: 'generic' }; // IF its node based we need to add the node-tag to it as well (config[messageID] && config[messageID].tagID ) ? tg['nodeID'] = data[config[messageID].tagID] : ''; - - influx.writeMeasurement((config[messageID] && config[messageID].serie ) ? config[messageID].serie : 'generic', [ - { - tags: tg, - fields: data, - } - ], { - precision: 's' + if(influxenabled) { + influx.writeMeasurement((config[messageID] && config[messageID].serie ) ? config[messageID].serie : 'generic', [ + { + tags: tg, + fields: data, + } + ], { + precision: 's' + } + ); } - - - ); }; function errorText(string) { @@ -171,16 +177,18 @@ server.on('error',function(error){ }); -var normalizedPath = require("path").join(__dirname, "payload"); +var normalizedPath = path.join(__dirname, "payload"); console.log('Batrium logger started'); // Function to load in all parsers from the payload folder. Those not able to load will be discarded during startup. var messages = {}; require("fs").readdirSync(normalizedPath).forEach(function(file) { try { - load = file.split("_")[1]; - messages[load.toLowerCase()] = require("./payload/" + file); - infoText('Loaded file: ' + file); + if (path.extname(file) == ".js") { + load = file.split("_")[1]; + messages[load.toLowerCase()] = require("./payload/" + file); + infoText('Loaded file: ' + file); + } } catch (e) { @@ -210,7 +218,8 @@ server.on('message',function(msg,info){ if (config[payload.MessageId] && config[payload.MessageId].mqtt || config.all.mqtt) sendMqtt(payload.SystemId,payload.MessageId,obj); if (config[payload.MessageId] && config[payload.MessageId].influx || config.all.influx) sendInflux(obj, tag); } catch (e) { - errorText('Couldnt get payload for ' + payload.MessageId + ' Size: %s',msg.length); + console.log(require("util").inspect(msg)); + errorText('Couldnt get payload for ' + payload.MessageId + ' Size: ' + msg.length); console.log(e); //process.exit(1); //Lets end the program if we find errors processing the data. for error searching } From d9c94243e517295374814de8a690e911300ee514 Mon Sep 17 00:00:00 2001 From: Deti Fliegl Date: Sat, 30 Apr 2022 11:28:34 +0200 Subject: [PATCH 2/2] Fix wrong data type for ShuntVoltage --- payload/Msg_3233_LiveDisplay.js | 2 +- payload/Msg_3e32_StatusRapid.js | 2 +- payload/Msg_3e5a_StatusRapid.js | 2 +- payload/Msg_3f34_StatusShunt.js | 2 +- payload/Msg_5732_SystemDiscovery.js | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/payload/Msg_3233_LiveDisplay.js b/payload/Msg_3233_LiveDisplay.js index 3b4dcb8..a74e29b 100644 --- a/payload/Msg_3233_LiveDisplay.js +++ b/payload/Msg_3233_LiveDisplay.js @@ -45,7 +45,7 @@ const status = new Parser() .uint8('MaxCellTemp', { formatter: (x) => {return x-40;}}) // index 19 - temperature ºC .uint8('AvgCellTemp', { formatter: (x) => {return x-40;}}) // index 20 - temperature ºC .uint8('NumOfCellsInBypass') - .int16le('ShuntVoltage', { formatter: (x) => {return x/100;}}) + .uint16le('ShuntVoltage', { formatter: (x) => {return x/100;}}) .floatle('ShuntCurrent', { formatter: (x) => {return x/1000;}}) .floatle('ShuntPowerVA', { formatter: (x) => {return x/1000;}}) // kW .int16le('ShuntSOC', { formatter: (x) => {return x/100;}}) // percent hires 2 dec.pt diff --git a/payload/Msg_3e32_StatusRapid.js b/payload/Msg_3e32_StatusRapid.js index 3fbc632..02b9cdb 100644 --- a/payload/Msg_3e32_StatusRapid.js +++ b/payload/Msg_3e32_StatusRapid.js @@ -37,7 +37,7 @@ const status = new Parser() .uint8('CmuTxOpStatusId') .uint8('CmuRxOpStatusId') .uint8('CmuRxOpStatusUSN') - .int16le('ShuntVoltage', { formatter: (x) => {return x/100;}}) // voltage + .uint16le('ShuntVoltage', { formatter: (x) => {return x/100;}}) // voltage .floatle('ShuntCurrent', { formatter: (x) => {return x/1000;}}) // amp .floatle('ShuntPowerVA', { formatter: (x) => {return x/1000;}}) // kW diff --git a/payload/Msg_3e5a_StatusRapid.js b/payload/Msg_3e5a_StatusRapid.js index 38e4d21..9e4a188 100644 --- a/payload/Msg_3e5a_StatusRapid.js +++ b/payload/Msg_3e5a_StatusRapid.js @@ -37,7 +37,7 @@ const status = new Parser() .uint8('CmuTxOpStatusId') .uint8('CmuRxOpStatusId') .uint8('CmuRxOpStatusUSN') - .int16le('ShuntVoltage', { formatter: (x) => {return x/100;}}) + .uint16le('ShuntVoltage', { formatter: (x) => {return x/100;}}) .floatle('ShuntCurrent', { formatter: (x) => {return x/1000;}}) .uint8('ShuntRxAmpTicks') .uint8('ShuntTxAmpTicks') diff --git a/payload/Msg_3f34_StatusShunt.js b/payload/Msg_3f34_StatusShunt.js index c32c891..1221024 100644 --- a/payload/Msg_3f34_StatusShunt.js +++ b/payload/Msg_3f34_StatusShunt.js @@ -12,7 +12,7 @@ const status = new Parser() .int16le('SupplyVolt', { formatter: (x) => {return x/100;}}) // index 8 .uint8('AmbientTemp', { formatter: (x) => {return x-40;}}) // index 10 - temperature ºC .uint8('ShuntTemp', { formatter: (x) => {return x-40;}}) // index 11 - temperature ºC - .int16le('ShuntVoltage', { formatter: (x) => {return x/100;}}) // index 12 - voltage + .uint16le('ShuntVoltage', { formatter: (x) => {return x/100;}}) // index 12 - voltage .floatle('ShuntCurrent', { formatter: (x) => {return x/1000;}}) // index 14 - amp .floatle('ShuntPowerVA', { formatter: (x) => {return x/1000;}}) // index 18 - kW .int16le('ShuntSOC', { formatter: (x) => {return x/100;}}) // index 22 - percent hi-res 2 dec.pt diff --git a/payload/Msg_5732_SystemDiscovery.js b/payload/Msg_5732_SystemDiscovery.js index 1ffadb6..ce96644 100644 --- a/payload/Msg_5732_SystemDiscovery.js +++ b/payload/Msg_5732_SystemDiscovery.js @@ -63,7 +63,7 @@ const status = new Parser() Simulator Start = 12, Simulator Running = 13, */ .uint8('ShuntSOC', { formatter: (x) => {return x/2-5;}}) // percent - .int16le('ShuntVoltage', { formatter: (x) => {return x/100;}}) + .uint16le('ShuntVoltage', { formatter: (x) => {return x/100;}}) .floatle('ShuntCurrent', { formatter: (x) => {return x/1000;}}) .uint8('ShuntStatus') /* Choices Timeout = 0,