@@ -56,6 +56,10 @@ app.get('/hello.txt', function(req, res){
5656 console . log ( 'connection %j %s %s' , req . connection . remoteAddress , req . method , req . url ) ;
5757 res . send ( 'Hello World' ) ;
5858} ) ;
59+ app . get ( '/app.css' , function ( req , res ) {
60+ console . log ( 'connection %j %s %s' , req . connection . remoteAddress , req . method , req . url ) ;
61+ res . sendfile ( __dirname + '/app.css' ) ;
62+ } ) ;
5963app . get ( '/stats' , function ( req , res ) {
6064 console . log ( 'connection %j %s %s' , req . connection . remoteAddress , req . method , req . url ) ;
6165 res . send ( timeSince ( scriptStartDate ) ) ;
@@ -74,6 +78,8 @@ app.get('/', function(req, res) {
7478
7579} ) ;
7680
81+
82+
7783// start the server
7884server . listen ( 8500 ) ;
7985console . log ( 'listening on port 8500' ) ;
@@ -89,7 +95,8 @@ var client = mqtt.createClient(1883, 'localhost', function(err, client) {
8995 keepalive: 1000
9096} ) ;
9197// global variables for tracking cumulative power usage
92- var powercumulative = 0 ;
98+ var powercumulativeToday = 0 ;
99+ var powercumulativeHour = 0 ;
93100var powerlasttime = new Date ( ) ; // UNIX time in ms
94101
95102client . on ( 'connect' , function ( ) {
@@ -102,14 +109,19 @@ client.on('connect', function() {
102109 var powercurrenttime = new Date ( ) ;
103110 // Is it now a different day from the last time this block ran?
104111 if ( powerlasttime . getDate ( ) != powercurrenttime . getDate ( ) ) {
105- powerlasttime = 0 ;
112+ powercumulativeToday = 0 ;
113+ }
114+ if ( powerlasttime . getHours ( ) != powercurrenttime . getHours ( ) ) {
115+ powercumulativeHour = 0 ;
106116 }
107117 // caluclate cumlative power used in KWh
108118 var duration = ( powercurrenttime - powerlasttime ) / 1000.0 ;
109119 var powerused = parseInt ( message , 10 ) * ( duration / 3600.0 ) / 1000.0 ; // convert to KWh
110- powercumulative += powerused ;
120+ powercumulativeToday += powerused ;
121+ powercumulativeHour += powerused ;
111122 // console.log("duration ", duration, "powerused ", powerused, "cumulative ", powercumulative);
112- io . sockets . emit ( 'data' , { topic : "powercumulative" , value : powercumulative . toFixed ( 3 ) } ) ;
123+ io . sockets . emit ( 'data' , { topic : "powercumulativeToday" , value : powercumulativeToday . toFixed ( 3 ) } ) ;
124+ io . sockets . emit ( 'data' , { topic : "powercumulativeHour" , value : powercumulativeHour . toFixed ( 3 ) } ) ;
113125 powerlasttime = powercurrenttime ;
114126 }
115127 } ) ;
0 commit comments