1- var app = require ( 'http' ) . createServer ( handler ) ;
2- var io = require ( 'socket.io' ) . listen ( app , {
3- 'log level' : 1
4- } ) ;
1+ var express = require ( 'express' ) ;
2+ var app = express ( )
3+ , http = require ( 'http' )
4+ , server = http . createServer ( app )
5+ , io = require ( 'socket.io' ) . listen ( server , { 'log level' : 1 } ) ;
56var fs = require ( 'fs' ) ;
6-
7- app . listen ( 8500 ) ;
8- console . log ( 'listening on port 8500' ) ;
97
10- // server web pages
11- function handler ( req , res ) {
8+
9+
10+
11+ var scriptStartDate = new Date ( ) ;
12+
13+
14+ function timeSince ( ts ) {
15+ now = new Date ( ) ;
16+ ts = new Date ( ts ) ;
17+ var delta = now . getTime ( ) - ts . getTime ( ) ;
18+
19+ delta = delta / 1000 ; //us to s
20+
21+ var ps , pm , ph , pd , min , hou , sec , days ;
22+
23+ if ( delta <= 59 ) {
24+ ps = ( delta > 1 ) ? "s" : "" ;
25+ return delta + " second" + ps
26+ }
27+
28+ if ( delta >= 60 && delta <= 3599 ) {
29+ min = Math . floor ( delta / 60 ) ;
30+ sec = delta - ( min * 60 ) ;
31+ pm = ( min > 1 ) ? "s" : "" ;
32+ ps = ( sec > 1 ) ? "s" : "" ;
33+ return min + " minute" + pm + " " + sec + " second" + ps ;
34+ }
35+
36+ if ( delta >= 3600 && delta <= 86399 ) {
37+ hou = Math . floor ( delta / 3600 ) ;
38+ min = Math . floor ( ( delta - ( hou * 3600 ) ) / 60 ) ;
39+ ph = ( hou > 1 ) ? "s" : "" ;
40+ pm = ( min > 1 ) ? "s" : "" ;
41+ return hou + " hour" + ph + " " + min + " minute" + pm ;
42+ }
43+
44+ if ( delta >= 86400 ) {
45+ days = Math . floor ( delta / 86400 ) ;
46+ hou = Math . floor ( ( delta - ( days * 86400 ) ) / 60 ) ;
47+ pd = ( days > 1 ) ? "s" : "" ;
48+ ph = ( hou > 1 ) ? "s" : "" ;
49+ return delta + " day" + pd + " " + hou + " hour" + ph ;
50+ }
51+
52+ }
53+
54+
55+ app . get ( '/hello.txt' , function ( req , res ) {
56+ console . log ( 'connection %j %s %s' , req . connection . remoteAddress , req . method , req . url ) ;
57+ res . send ( 'Hello World' ) ;
58+ } ) ;
59+ app . get ( '/stats' , function ( req , res ) {
60+ console . log ( 'connection %j %s %s' , req . connection . remoteAddress , req . method , req . url ) ;
61+ res . send ( timeSince ( scriptStartDate ) ) ;
62+ } ) ;
63+ app . get ( '/' , function ( req , res ) {
1264 console . log ( 'connection %j %s %s' , req . connection . remoteAddress , req . method , req . url ) ;
1365 fs . readFile ( __dirname + '/index.html' ,
1466 function ( err , data ) {
@@ -19,8 +71,12 @@ function handler (req, res) {
1971 res . writeHead ( 200 ) ;
2072 res . end ( data ) ;
2173 } ) ;
22- }
2374
75+ } ) ;
76+
77+ // start the server
78+ server . listen ( 8500 ) ;
79+ console . log ( 'listening on port 8500' ) ;
2480
2581
2682io . sockets . on ( 'connection' , function ( socket ) {
@@ -47,7 +103,7 @@ client.on('connect', function() {
47103 var duration = ( powercurrenttime - powerlasttime ) / 1000.0 ;
48104 var powerused = parseInt ( message , 10 ) * ( duration / 3600.0 ) / 1000.0 ; // convert to KWh
49105 powercumulative += powerused ;
50- console . log ( "duration " , duration , "powerused " , powerused , "cumulative " , powercumulative ) ;
106+ // console.log("duration ", duration, "powerused ", powerused, "cumulative ", powercumulative);
51107 io . sockets . emit ( 'data' , { topic : "powercumulative" , value : powercumulative . toFixed ( 3 ) } ) ;
52108 powerlasttime = powercurrenttime ;
53109 }
0 commit comments