@@ -16,28 +16,16 @@ function log(type) {
1616 }
1717}
1818
19- // var powerlasttime = new Date(); // UNIX time in ms
20-
21- // 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 = {};
19+ var savecount = 0 ;
3220var records_hourly = { } ;
3321var records_daily = { } ;
3422var records_lasttime = { } ;
3523
24+ // get last stored values from Redis
3625var execSync = require ( 'execSync' ) ;
3726var result = execSync . exec ( 'redis-cli get records_hourly' ) ;
3827records_hourly = JSON . parse ( result . stdout ) ;
3928console . log ( "Loading hourly records from redis..." ) ;
40-
4129result = execSync . exec ( 'redis-cli get records_daily' ) ;
4230records_daily = JSON . parse ( result . stdout ) ;
4331console . log ( "Loading daily records from redis..." ) ;
@@ -74,25 +62,30 @@ mqttclient.on('connect', function() {
7462
7563 // different hour?
7664 if ( records_lasttime [ topic ] . getHours ( ) != powercurrenttime . getHours ( ) ) {
77- powercumulativeHour = 0 ;
65+ records_hourly [ topic ] = 0 ;
7866 }
7967 // different day?
8068 if ( records_lasttime [ topic ] . getDate ( ) != powercurrenttime . getDate ( ) ) {
81- powercumulativeDay = 0 ;
69+ records_daily [ topic ] = 0 ;
8270 }
8371 // calculate cumulative power used in KWh
8472 var duration = ( powercurrenttime - records_lasttime [ topic ] ) / 1000.0 ;
8573 var powerused = parseInt ( message , 10 ) * ( duration / 3600.0 ) / 1000.0 ; // convert to KWh
8674 records_lasttime [ topic ] = powercurrenttime ;
8775 records_hourly [ topic ] += powerused ;
8876 records_daily [ topic ] += powerused ;
89- console . log ( "topic:" , topic , " duration " , duration , " period " , powerused , " hour " , records_hourly [ topic ] , "daily " , records_daily [ topic ] ) ;
77+ // console.log("topic:", topic, " duration ", duration, " period ", powerused, " hour ", records_hourly[topic], "daily ", records_daily[topic]);
9078
79+ // publish new data
9180 mqttclient . publish ( topic + "/cumulative/hour" , records_hourly [ topic ] . toFixed ( 2 ) ) ;
9281 mqttclient . publish ( topic + "/cumulative/daily" , records_daily [ topic ] . toFixed ( 2 ) ) ;
9382
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));
83+ // save to redis
84+ if ( savecount ++ > 24 ) {
85+ savecount = 0 ;
86+ redisClient . set ( "records_hourly" , JSON . stringify ( records_hourly ) ) ;
87+ redisClient . set ( "records_daily" , JSON . stringify ( records_daily ) ) ;
88+ // console.log("saving to redis...");
89+ }
9790 } ) ;
9891} ) ;
0 commit comments