@@ -16,20 +16,34 @@ function log(type) {
1616 }
1717}
1818
19-
20- var powerlasttime = new Date ( ) ; // UNIX time in ms
19+ // var powerlasttime = new Date(); // UNIX time in ms
2120
2221// get last stored values from Redis
22+ // var execSync = require('execSync');
23+ // var result = execSync.exec('redis-cli get powercumulativeHour');
24+ // var powercumulativeHour = parseFloat(result.stdout);
25+ // var result = execSync.exec('redis-cli get powercumulativeToday');
26+ // var powercumulativeToday = parseFloat(result.stdout);
27+
28+ // var records_hourly = new Array();
29+ // var records_daily = new Array();
30+ // var records_lasttime = new Array();
31+ // var records_hourly = {};
32+ var records_hourly = { } ;
33+ var records_daily = { } ;
34+ var records_lasttime = { } ;
35+
2336var execSync = require ( 'execSync' ) ;
24- var result = execSync . exec ( 'redis-cli get powercumulativeHour' ) ;
25- var powercumulativeHour = parseFloat ( result . stdout ) ;
26- var result = execSync . exec ( 'redis-cli get powercumulativeToday' ) ;
27- var powercumulativeToday = parseFloat ( result . stdout ) ;
37+ var result = execSync . exec ( 'redis-cli get records_hourly' ) ;
38+ records_hourly = JSON . parse ( result . stdout ) ;
39+ console . log ( "Loading hourly records from redis..." ) ;
2840
29- console . log ( "loaded powercumulativeHour: " + powercumulativeHour + " (" + typeof powercumulativeHour + ")" ) ;
30- console . log ( "loaded powercumulativeToday: " + powercumulativeToday + " (" + typeof powercumulativeToday + ")" ) ;
41+ result = execSync . exec ( 'redis-cli get records_daily' ) ;
42+ records_daily = JSON . parse ( result . stdout ) ;
43+ console . log ( "Loading daily records from redis..." ) ;
3144
32- // subscribe to MQTT
45+
46+ // connect to to MQTT
3347var mqtt = require ( 'mqtt' ) ;
3448var mqttclient = mqtt . createClient ( 1883 , config . mqtt . host , function ( err , client ) {
3549 keepalive: 1000
@@ -42,27 +56,43 @@ mqttclient.on('connect', function() {
4256 console . log ( 'subscribing to sensors/power/+ on ' + config . mqtt . host + '(' + config . mqtt . port + ')' ) ;
4357
4458 mqttclient . on ( 'message' , function ( topic , message ) {
45- // console.log('topic: ' + topic + ' payload: ' + message);
46- if ( topic == "sensors/power/0" ) {
47- var powercurrenttime = new Date ( ) ;
48- // Is it now a different day from the last time this block ran?
49- if ( powerlasttime . getDate ( ) != powercurrenttime . getDate ( ) ) {
50- powercumulativeToday = 0 ;
51- }
52- if ( powerlasttime . getHours ( ) != powercurrenttime . getHours ( ) ) {
53- powercumulativeHour = 0 ;
54- }
55- // caluclate cumlative power used in KWh
56- var duration = ( powercurrenttime - powerlasttime ) / 1000.0 ;
57- var powerused = parseInt ( message , 10 ) * ( duration / 3600.0 ) / 1000.0 ; // convert to KWh
58- powercumulativeToday += powerused ;
59- powercumulativeHour += powerused ;
60- redisClient . set ( "powercumulativeToday" , powercumulativeToday ) ;
61- redisClient . set ( "powercumulativeHour" , powercumulativeHour ) ;
62- // console.log("duration ", duration, "period ", powerused, "today ", powercumulativeToday, "hour ", powercumulativeHour);
63- mqttclient . publish ( "sensors/power/0/cumulative/today" , powercumulativeToday . toFixed ( 2 ) ) ;
64- mqttclient . publish ( "sensors/power/0/cumulative/hour" , powercumulativeHour . toFixed ( 2 ) ) ;
65- powerlasttime = powercurrenttime ;
59+ var powercurrenttime = new Date ( ) ;
60+
61+ // have we seen this mesage before?
62+ if ( records_lasttime [ topic ] == undefined ) {
63+ console . log ( "initialising lasttime" + topic ) ;
64+ records_lasttime [ topic ] = new Date ( ) ;
65+ }
66+ if ( records_hourly [ topic ] == undefined ) {
67+ console . log ( "initialising records_hourly " + topic ) ;
68+ records_hourly [ topic ] = 0 ;
6669 }
70+ if ( records_daily [ topic ] == undefined ) {
71+ console . log ( "initialising records_daily " + topic ) ;
72+ records_daily [ topic ] = 0 ;
73+ }
74+
75+ // different hour?
76+ if ( records_lasttime [ topic ] . getHours ( ) != powercurrenttime . getHours ( ) ) {
77+ powercumulativeHour = 0 ;
78+ }
79+ // different day?
80+ if ( records_lasttime [ topic ] . getDate ( ) != powercurrenttime . getDate ( ) ) {
81+ powercumulativeDay = 0 ;
82+ }
83+ // calculate cumulative power used in KWh
84+ var duration = ( powercurrenttime - records_lasttime [ topic ] ) / 1000.0 ;
85+ var powerused = parseInt ( message , 10 ) * ( duration / 3600.0 ) / 1000.0 ; // convert to KWh
86+ records_lasttime [ topic ] = powercurrenttime ;
87+ records_hourly [ topic ] += powerused ;
88+ records_daily [ topic ] += powerused ;
89+ console . log ( "topic:" , topic , " duration " , duration , " period " , powerused , " hour " , records_hourly [ topic ] , "daily " , records_daily [ topic ] ) ;
90+
91+ mqttclient . publish ( topic + "/cumulative/hour" , records_hourly [ topic ] . toFixed ( 2 ) ) ;
92+ mqttclient . publish ( topic + "/cumulative/daily" , records_daily [ topic ] . toFixed ( 2 ) ) ;
93+
94+ redisClient . set ( "records_hourly" , JSON . stringify ( records_hourly ) ) ;
95+ redisClient . set ( "records_daily" , JSON . stringify ( records_daily ) ) ;
96+ // console.log( JSON.stringify(records_hourly));
6797 } ) ;
6898} ) ;
0 commit comments