Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 36 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');



Expand Down Expand Up @@ -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);

Expand All @@ -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);
}
}
})
})
Expand All @@ -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);
}
}


Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion payload/Msg_3233_LiveDisplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion payload/Msg_3e32_StatusRapid.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion payload/Msg_3e5a_StatusRapid.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion payload/Msg_3f34_StatusShunt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion payload/Msg_5732_SystemDiscovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down