Skip to content

Commit d4edc1a

Browse files
committed
Initial Graphite support
1 parent aba95d8 commit d4edc1a

File tree

3 files changed

+67
-6
lines changed

3 files changed

+67
-6
lines changed

config.json_dist

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,51 @@
44
"mqttusername" : "mqtt",
55
"mqttpassword" : "mqtt",
66
"influxhost" : "localhost",
7-
"influxdatabase" : "batrium"
7+
"influxdatabase" : "batrium",
8+
"graphiteurl": "plaintext://graphite.example.org:2003/",
9+
"graphiteprefix": "solar.battery"
810
},
911
"3e" : {
1012
"mqtt" : true,
1113
"influx" : true,
14+
"graphite" : true,
1215
"tag" : "general",
1316
"serie" : "generic"
1417
},
1518
"78" : {
1619
"mqtt" : false,
1720
"influx" : false,
21+
"graphite" : false,
1822
"info" : "Dont use influx on message 7857",
1923
"tag" : "general",
2024
"serie" : "generic"
2125
},
2226
"57" : {
2327
"mqtt" : false,
2428
"influx" : false,
29+
"graphite" : false,
2530
"tag" : "general",
2631
"serie" : "generic"
2732
},
2833
"42" : {
2934
"mqtt" : false,
3035
"influx" : false,
36+
"graphite" : false,
3137
"tag" : "general",
3238
"serie" : "nodes",
3339
"tagID" : "ID"
3440
},
3541
"54" : {
3642
"mqtt" : false,
3743
"influx" : true,
44+
"graphite" : false,
3845
"tag" : "general",
3946
"serie" : "daily"
4047
},
4148
"41" : {
4249
"mqtt" : true,
4350
"influx" : false,
51+
"graphite" : false,
4452
"tag" : "general",
4553
"serie" : "nodes2",
4654
"info" : "Dont send this message to influx. its not supported. if you need the data stored in influx use MQTT and then parse it first"
@@ -50,6 +58,7 @@
5058
"all" : {
5159
"mqtt" : false,
5260
"influx" :false,
61+
"graphite" : false,
5362
"info" : "Do not run every message to influx. you will kill the machine if running raspberry pi!"
5463
}
5564
}

index.js

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var mqtt = require('mqtt')
1111
var server = udp.createSocket('udp4');
1212
var Parser = require('binary-parser').Parser;
1313
const Influx = require('influx');
14+
const graphite = require('graphite');
1415
var fs = require('fs');
1516

1617

@@ -27,11 +28,13 @@ catch (e) {
2728
}
2829

2930
//MQTT server generally localhost
30-
var mqtthost = (config.config.mqtthost) ? config.config.mqtthost : 'localhost';
31-
var mqttusername = (config.config.mqttusername) ? config.config.mqttusername : '';
32-
var mqttpassword = (config.config.mqttpassword) ? config.config.mqttpassword : '';
33-
var influxhost = (config.config.influxhost) ? config.config.influxhost :'localhost';
34-
var influxdatabase = (config.config.influxdatabase) ? config.config.influxdatabase :'localhost';
31+
const mqtthost = config.config.mqtthost || 'localhost';
32+
const mqttusername = config.config.mqttusername || '';
33+
const mqttpassword = config.config.mqttpassword || '';
34+
const influxhost = config.config.influxhost || 'localhost';
35+
const influxdatabase = config.config.influxdatabase || 'localhost';
36+
const graphiteurl = config.config.graphiteurl || '';
37+
const graphiteprefix = config.config.graphiteprefix || '';
3538

3639
//Setup MQTT
3740
options={
@@ -47,6 +50,8 @@ const influx = new Influx.InfluxDB({
4750
database: influxdatabase,
4851
})
4952

53+
const graphiteClient = graphite.createClient(graphiteurl);
54+
5055

5156
// Function to get payload data
5257
// input data object
@@ -104,6 +109,49 @@ function sendInflux(data, tag) {
104109
);
105110
};
106111

112+
113+
function sendGraphite(systemId,messageId,data) {
114+
const prefix = graphiteprefix + '.batrium' + systemId + '.';
115+
var metrics = {};
116+
117+
switch (messageId) {
118+
case "54":
119+
metrics = {
120+
[prefix + 'DailySessionCumulShuntkWhCharge']: data.DailySessionCumulShuntkWhCharge,
121+
[prefix + 'DailySessionCumulShuntkWhDischg']: data.DailySessionCumulShuntkWhDischg,
122+
};
123+
break;
124+
case "3e":
125+
['ShuntVoltage', 'ShuntCurrent', 'ShuntPowerVA'].forEach((x) => {
126+
metrics[prefix + x] = data[x];
127+
});
128+
break;
129+
case "57":
130+
['SystemOpStatus', 'ShuntSOC', 'ShuntVoltage', 'ShuntCurrent'].forEach((x) => {
131+
metrics[prefix + x] = data[x];
132+
});
133+
break;
134+
case "41":
135+
data.nodes.forEach((node) => {
136+
//Rename these two, so the naming is nicer
137+
metrics[prefix + 'cells.' + node.ID + '.Volt'] = node['MinCellVolt'];
138+
metrics[prefix + 'cells.' + node.ID + '.Temp'] = node['MinCellTemp'];
139+
['BypassTemp', 'BypassAmp', 'Status'].forEach((x) => {
140+
metrics[prefix + 'cells.' + node.ID + '.' + x] = node[x];
141+
});
142+
});
143+
break;
144+
}
145+
146+
if (debugGraphite) {
147+
console.log(metrics);
148+
}
149+
if (metrics) {
150+
graphiteClient.write(metrics);
151+
}
152+
}
153+
154+
107155
function errorText(string) {
108156
console.log('\x1b[31m%s\x1b[0m', string);
109157
}
@@ -150,6 +198,7 @@ require("fs").readdirSync(normalizedPath).forEach(function(file) {
150198
// Time to process incomming data
151199
debug = false;
152200
debugMQTT = false;
201+
debugGraphite = true;
153202
var tag;
154203
// Parse new messages incomming from Batrium
155204
server.on('message',function(msg,info){
@@ -164,9 +213,11 @@ server.on('message',function(msg,info){
164213
// check if the message id is present in the config. This dont care what version is there if file exist
165214
if (config[messageID] && config[messageID].mqtt || config.all.mqtt) sendMqtt(payload.SystemId,payload.MessageId,obj);
166215
if (config[messageID] && config[messageID].influx || config.all.influx) sendInflux(obj, tag);
216+
if (config[messageID] && config[messageID].graphite || config.all.graphite) sendGraphite(payload.SystemId,messageID,obj);
167217
// Below is used if you use messageid and version in the configuration file
168218
if (config[payload.MessageId] && config[payload.MessageId].mqtt || config.all.mqtt) sendMqtt(payload.SystemId,payload.MessageId,obj);
169219
if (config[payload.MessageId] && config[payload.MessageId].influx || config.all.influx) sendInflux(obj, tag);
220+
if (config[payload.MessageId] && config[payload.MessageId].graphite || config.all.graphite) sendGraphite(payload.SystemId,payload.MessageId,obj);
170221
} catch (e) {
171222
errorText('Couldnt get payload for ' + payload.MessageId + ' Size: %s',msg.length);
172223
console.log(e);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"dependencies": {
2828
"mqtt": "latest",
2929
"binary-parser": "latest",
30+
"graphite": "latest",
3031
"influx": "latest"
3132
}
3233
}

0 commit comments

Comments
 (0)