@@ -5,11 +5,11 @@ var app = express()
55 , io = require ( 'socket.io' ) . listen ( server , { 'log level' : 1 } ) ;
66var fs = require ( 'fs' ) ;
77
8+ // service settings file
9+ var config = require ( './config.json' ) ;
810
911
10-
11- var scriptStartDate = new Date ( ) ;
12-
12+ var connectionCount = 0 ;
1313
1414function timeSince ( ts ) {
1515 now = new Date ( ) ;
@@ -58,11 +58,13 @@ app.get('/hello.txt', function(req, res){
5858} ) ;
5959app . get ( '/app.css' , function ( req , res ) {
6060 console . log ( 'connection %j %s %s' , req . connection . remoteAddress , req . method , req . url ) ;
61- res . sendfile ( __dirname + '/app.css' ) ;
61+ res . sendfile ( __dirname + '/app.css' ) ;
6262} ) ;
6363app . get ( '/stats' , function ( req , res ) {
64+ res . send ( connectionCount + " clients connected" ) ;
6465 console . log ( 'connection %j %s %s' , req . connection . remoteAddress , req . method , req . url ) ;
65- res . send ( timeSince ( scriptStartDate ) ) ;
66+ console . log ( 'This process is pid ' + process . pid + " with an uptime of " + process . uptime ( ) ) ;
67+ console . log ( 'Running on ' + process . platform + ' (' + process . arch + ')' ) ;
6668} ) ;
6769app . get ( '/' , function ( req , res ) {
6870 console . log ( 'connection %j %s %s' , req . connection . remoteAddress , req . method , req . url ) ;
@@ -86,21 +88,27 @@ console.log('listening on port 8500');
8688
8789
8890io . sockets . on ( 'connection' , function ( socket ) {
91+ connectionCount ++ ;
92+ } ) ;
93+ io . sockets . on ( 'disconnet' , function ( socket ) {
94+ connectionCount -- ;
8995} ) ;
9096
9197
9298// subscribe to MQTT
9399var mqtt = require ( 'mqtt' ) ;
94- var client = mqtt . createClient ( 1883 , 'localhost' , function ( err , client ) {
100+ var client = mqtt . createClient ( 1883 , config . mqtt . host , function ( err , client ) {
95101 keepalive: 1000
96102} ) ;
103+ console . log ( 'connecting to mqtt on ' + config . mqtt . host + '(' + config . mqtt . port + ')' ) ;
97104// global variables for tracking cumulative power usage
98105var powercumulativeToday = 0 ;
99106var powercumulativeHour = 0 ;
100107var powerlasttime = new Date ( ) ; // UNIX time in ms
101108
102109client . on ( 'connect' , function ( ) {
103110 client . subscribe ( 'sensors/+/+' ) ;
111+ console . log ( 'subscribing to sensors/+/+ on ' + config . mqtt . host + '(' + config . mqtt . port + ')' ) ;
104112
105113 client . on ( 'message' , function ( topic , message ) {
106114 // console.log('topic: ' + topic + ' payload: ' + message);
0 commit comments