@@ -11,6 +11,7 @@ var mqtt = require('mqtt')
1111var server = udp . createSocket ( 'udp4' ) ;
1212var Parser = require ( 'binary-parser' ) . Parser ;
1313const Influx = require ( 'influx' ) ;
14+ const graphite = require ( 'graphite' ) ;
1415var 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
3740options = {
@@ -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+
107155function 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
151199debug = false ;
152200debugMQTT = false ;
201+ debugGraphite = true ;
153202var tag ;
154203// Parse new messages incomming from Batrium
155204server . 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 ) ;
0 commit comments