2015-06-16 22:52:06 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								var  express  =  require ( "express" ) ;  
						 
					
						
							
								
									
										
										
										
											2015-06-25 14:05:20 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								var  hbs  =  require ( 'express-hbs' ) ;  
						 
					
						
							
								
									
										
										
										
											2015-07-18 15:13:10 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								require ( 'handlebars-form-helpers' ) . register ( hbs . handlebars ) ;  
						 
					
						
							
								
									
										
										
										
											2015-06-27 16:43:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								var  nodemailer  =  require ( 'nodemailer' ) ;  
						 
					
						
							
								
									
										
										
										
											2015-08-05 14:31:22 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								var  alertmonUtils  =  require ( './alertmonUtils.js' ) ;  
						 
					
						
							
								
									
										
										
										
											2015-06-16 22:52:06 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								var  fs  =  require ( "fs" ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								var  bodyParser  =  require ( "body-parser" ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								var  app  =  express ( ) ;  
						 
					
						
							
								
									
										
										
										
											2015-06-21 14:04:07 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-06-27 16:43:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// Setup email
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								var  transporter  =  nodemailer . createTransport ( {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    service :  'Gmail' , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    auth :  { 
							 
						 
					
						
							
								
									
										
										
										
											2015-07-13 19:19:46 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        user :  'alertmonitorfl@gmail.com' , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        pass :  '6g*hkvVc%91oo3#$' 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-27 16:43:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								} ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								var  mailOptions  =  {  
						 
					
						
							
								
									
										
										
										
											2015-07-13 19:19:46 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    from :  'Alert Monitor <alertmonitorfl@gmail.com>' , 
							 
						 
					
						
							
								
									
										
										
										
											2015-07-29 14:08:29 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    to :  'jody@kaplon.us,don@gettner.com' , 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-27 16:43:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    subject :  'Alert received' , 
							 
						 
					
						
							
								
									
										
										
										
											2015-08-07 11:20:22 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    text :  'test alert'   // Get custom text later on email generation.
 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-27 16:43:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								} ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-06-21 14:04:07 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-03 18:42:28 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								var  pg  =  require ( "pg" ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								var  conString  =  "postgres://alertmon:alertmon@db/alertmon" ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-06-16 22:52:06 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-07-18 15:13:10 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								app . use ( bodyParser . json ( ) ) ;                              // Needed for JSON POST requests from Particle Cores.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								app . use ( bodyParser . urlencoded ( {  extended :  false  } ) ) ;     // Needed for web POST requests from edit form.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-06-25 14:05:20 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// Use `.hbs` for extensions and find partials in `views/partials`.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								app . engine ( 'hbs' ,  hbs . express4 ( {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    partialsDir :  _ _dirname  +  '/views/partials' 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								} ) ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								app . set ( 'view engine' ,  'hbs' ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								app . set ( 'views' ,  _ _dirname  +  '/views' ) ;  
						 
					
						
							
								
									
										
										
										
											2015-06-16 22:52:06 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								app . get ( '/' ,  function ( req ,  res ) {  
						 
					
						
							
								
									
										
										
										
											2015-07-07 10:18:41 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    var  d  =  new  Date ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    console . log ( "GET /, "  +  JSON . stringify ( d ,  4 ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2015-10-05 21:44:03 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    pg . connect ( conString ,  function ( err ,  client ,  done )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if ( err )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            return  console . error ( 'error fetching client from pool' ,  err ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        var  devIndexQry  =  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            "select al.coreid, max(co.corename) as corename, max(co.locationdesc) as locationdesc, mpub.maxpub, al.status as maxstatus "  +  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            "from alerts al "  +  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                "inner join (select coreid, max(published_at) as maxpub from alerts group by coreid) mpub on al.coreid = mpub.coreid and al.published_at = mpub.maxpub "  +  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            "left join cores co on al.coreid = co.coreid "  +  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            "group by al.coreid, mpub.maxpub, al.status "  + 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            "order by mpub.maxpub desc, corename asc " ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        client . query ( devIndexQry ,  function ( err ,  result )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            //call `done()` to release the client back to the pool
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            done ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            if ( err )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                return  console . error ( 'error running query' ,  err ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            } 
							 
						 
					
						
							
								
									
										
										
										
											2015-07-28 14:07:56 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            // Loop over elements in rows array, convert ugly UTC times to pretty local times.
 
							 
						 
					
						
							
								
									
										
										
										
											2015-10-05 21:44:03 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								            result . rows . forEach ( function ( row ) { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                row . maxpubdate  =  alertmonUtils . getLocDateFromUTC ( row . maxpub ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                row . maxpubtime  =  alertmonUtils . getLocTimeFromUTC ( row . maxpub ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2015-08-08 09:47:21 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-05 21:44:03 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								                if ( row . maxstatus . toLowerCase ( ) . indexOf ( 'alert' )  >  - 1 ) { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                    row . rowclass  =  'alert-row' ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                }  else  {  row . rowclass  =  'non-alert-row' ;  } 
							 
						 
					
						
							
								
									
										
										
										
											2015-07-28 14:07:56 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								            } ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-05 21:44:03 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								            res . render ( 'index' ,  { cores :  result . rows } ,  function ( err ,  html )  { 
							 
						 
					
						
							
								
									
										
										
										
											2015-07-07 10:18:41 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								                if ( err  !==  null )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                    console . log ( err ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                }  else  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                    res . send ( html ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            } ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2015-10-05 21:44:03 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        } ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2015-07-06 22:08:08 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    } ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								} ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-07-18 15:13:10 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								app . get ( '/core/edit/:id' ,  function ( req ,  res ) {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var  d  =  new  Date ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var  coreId  =  req . params . id ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    console . log ( "GET /core/edit/"  +  coreId  +  ", "  +  JSON . stringify ( d ,  4 ) ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-08 12:05:03 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    pg . connect ( conString ,  function ( err ,  client ,  done )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if ( err )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            return  console . error ( 'error fetching client from pool' ,  err ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        client . query ( "select corename, locationdesc from cores where coreid = ($1);" ,  [ coreId ] ,  function ( err ,  result )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            //call `done()` to release the client back to the pool
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            done ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            if ( err )  {  return  console . error ( 'error running query' ,  err ) ;  } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            res . render ( 'core-edit' ,  { values :  result . rows } ,  function ( err ,  html )  { 
							 
						 
					
						
							
								
									
										
										
										
											2015-08-07 11:20:22 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								                if ( err  !==  null )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                    console . log ( err ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                }  else  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                    res . send ( html ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            } ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2015-10-08 12:05:03 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        } ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2015-07-18 15:13:10 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    } ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								} ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								app . post ( '/core/edit/:id' ,  function ( req ,  res ) {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var  d  =  new  Date ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var  coreId  =  req . params . id ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    console . log ( "POST /core/edit/"  +  coreId  +  "body: "  +  JSON . stringify ( req . body )  +  ", "  +  JSON . stringify ( d ,  4 ) ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if  ( ! req . body )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return  res . sendStatus ( 400 ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }  else  { 
							 
						 
					
						
							
								
									
										
										
										
											2015-10-11 16:11:32 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        pg . connect ( conString ,  function ( err ,  client ,  done )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            if ( err )  {  return  console . error ( 'error fetching client from pool' ,  err ) ;  } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            // check existence of row in Cores table, if not there insert, otherwise update. 
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            client . query ( "SELECT coreid FROM cores WHERE coreid = ($1);" ,  [ coreId ] ,  function ( err ,  result )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                done ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                if ( err )  {  return  console . error ( 'error running query' ,  err ) ;  } 
							 
						 
					
						
							
								
									
										
										
										
											2015-12-10 13:04:10 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								                if ( typeof  result . rowCount  ==  0 )  { 
							 
						 
					
						
							
								
									
										
										
										
											2015-10-11 16:11:32 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								                    client . query ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                        "INSERT INTO cores (coreid, corename, locationdesc) VALUES ($1, $2, $3);" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                        [ coreId ,  req . body . deviceName ,  req . body . locationDesc ] 
							 
						 
					
						
							
								
									
										
										
										
											2015-07-30 08:28:16 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								                    ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2015-10-11 16:11:32 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								                }  else  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                    if  ( req . body . deviceName  !==  "" )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                        client . query ( "UPDATE cores SET corename =($1) WHERE coreid =($2)" ,  [ req . body . deviceName ,  coreId ] ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                    } 
							 
						 
					
						
							
								
									
										
										
										
											2015-07-18 15:13:10 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-11 16:11:32 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								                    if  ( req . body . locationDesc  !==  "" )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                        client . query ( "UPDATE cores SET locationdesc =($1) WHERE coreid =($2)" ,  [ req . body . locationDesc ,  coreId ] ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                    } 
							 
						 
					
						
							
								
									
										
										
										
											2015-07-30 08:28:16 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								                } 
							 
						 
					
						
							
								
									
										
										
										
											2015-10-11 16:11:32 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								            } ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2015-07-30 08:28:16 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        } ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2015-07-18 15:13:10 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 13:01:00 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        res . redirect ( 204 ,  'https://particle.kaplon.us/' ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2015-07-18 15:13:10 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								} ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-07-06 22:08:08 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								app . get ( '/core/:id' ,  function ( req ,  res ) {  
						 
					
						
							
								
									
										
										
										
											2015-07-03 21:38:40 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    var  d  =  new  Date ( ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2015-07-07 10:18:41 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    var  coreId  =  req . params . id ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    console . log ( "GET /core/"  +  coreId  +  ", "  +  JSON . stringify ( d ,  4 ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2015-10-11 16:11:32 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    pg . connect ( conString ,  function ( err ,  client ,  done )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if ( err )  {  return  console . error ( 'error fetching client from pool' ,  err ) ;  } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        var  coreMsgQry  =  "SELECT al.coreid, al.published_at, al.status, co.corename "  + 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            "FROM alerts al left join cores co on al.coreid = co.coreid "  + 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            "WHERE al.coreid = ($1) ORDER BY al.published_at DESC LIMIT 30;" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        client . query ( coreMsgQry ,  [ coreId ] ,  function ( err ,  result )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            //call `done()` to release the client back to the pool
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            done ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            if ( err )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                return  console . error ( 'error running query' ,  err ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            } 
							 
						 
					
						
							
								
									
										
										
										
											2015-07-09 12:33:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            // Loop over elements in rows array, convert ugly UTC times to pretty local times.
 
							 
						 
					
						
							
								
									
										
										
										
											2015-10-11 16:11:32 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								            result . rows . forEach ( function ( row ) { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                row . pubdate  =  alertmonUtils . getLocDateFromUTC ( row . published _at ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                row . pubtime  =  alertmonUtils . getLocTimeFromUTC ( row . published _at ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2015-08-07 11:20:22 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								                if ( row . status . toLowerCase ( ) . indexOf ( 'alert' )  >  - 1 ) { 
							 
						 
					
						
							
								
									
										
										
										
											2015-10-11 16:11:32 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								                    row . rowclass  =  'alert-row' ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                }  else  {  row . rowclass  =  'non-alert-row' ;  } 
							 
						 
					
						
							
								
									
										
										
										
											2015-07-09 12:33:53 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								            } ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-11 16:11:32 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								            res . render ( 'core' ,  { alerts :  result . rows } ,  function ( err ,  html )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                if ( err  !==  null )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                    console . log ( err ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                }  else  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                    res . send ( html ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                } 
							 
						 
					
						
							
								
									
										
										
										
											2015-07-07 10:18:41 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								            } ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2015-10-11 16:11:32 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        } ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-25 14:05:20 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    } ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-16 22:52:06 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								} ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								app . post ( '/' ,  function ( req ,  res ) {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var  postEvent  =  req . body . postEvent ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var  source  =  req . body . source ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    console . log ( req . body ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-21 14:04:07 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-07-14 17:40:23 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    var  innerDataJSON  =  JSON . parse ( req . body . data ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2015-08-04 19:44:06 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    var  status  =  JSON . stringify ( innerDataJSON . status ,  null ,  4 ) . slice ( 1 , - 1 ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var  coreid  =  JSON . stringify ( req . body . coreid ,  null ,  4 ) . slice ( 1 , - 1 ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var  pubAt  =  JSON . stringify ( req . body . published _at ,  null ,  4 ) . slice ( 1 , - 1 ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2015-10-11 16:11:32 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    pg . connect ( conString ,  function ( err ,  client ,  done )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if ( err )  {  return  console . error ( 'error fetching client from pool' ,  err ) ;  } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        client . query ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            "INSERT INTO alerts (origjson, coreid, published_at, status) VALUES ($1, $2, $3, $4);" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            [ JSON . stringify ( req . body ,  null ,  4 ) ,  coreid ,  pubAt ,  status ] 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2015-07-03 21:38:40 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-07-13 19:19:46 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    // Send emails on alerts only
 
							 
						 
					
						
							
								
									
										
										
										
											2015-07-14 17:54:08 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    if ( status . toLowerCase ( ) . indexOf ( 'alert' )  >  - 1 ) { 
							 
						 
					
						
							
								
									
										
										
										
											2015-08-04 19:44:06 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        mailOptions . text  =  'An alert message was received: \n\n' ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        mailOptions . text  =  mailOptions . text  +  'Status message, '  +  status  +  '\n' ; 
							 
						 
					
						
							
								
									
										
										
										
											2015-08-05 14:46:33 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        mailOptions . text  =  mailOptions . text  +  'Published at, '  +  alertmonUtils . getLocDateFromUTC ( pubAt )  +  ' '  +  alertmonUtils . getLocTimeFromUTC ( pubAt )  +  '\n' ; 
							 
						 
					
						
							
								
									
										
										
										
											2015-08-07 11:20:22 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        //mailOptions.text = mailOptions.text + 'From device, ' + alertmonUtils.getCoreNameFromCoreId(db, coreid) + '\n';
 
							 
						 
					
						
							
								
									
										
										
										
											2015-10-11 16:11:32 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        pg . connect ( conString ,  function ( err ,  client ,  done )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            if ( err )  {  return  console . error ( 'error fetching client from pool' ,  err ) ;  } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            client . query ( 'SELECT corename FROM cores WHERE coreid = $1;' ,  [ coreid ] ,  function ( err ,  result )  { 
							 
						 
					
						
							
								
									
										
										
										
											2015-12-10 13:04:10 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								                if  ( ( err )  ||  ( typeof  result . rowCount  ==  0 ) )  { 
							 
						 
					
						
							
								
									
										
										
										
											2015-10-11 16:11:32 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								                    // Don't care about this error or empty result set, still need to send email.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                    result . rows [ 0 ] . corename  =  '# No Name #' ; 
							 
						 
					
						
							
								
									
										
										
										
											2015-08-07 11:20:22 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								                } 
							 
						 
					
						
							
								
									
										
										
										
											2015-10-11 16:11:32 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								                mailOptions . text  =  mailOptions . text  +  'From device, '  +  result . rows [ 0 ] . corename  +  '\n' ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                transporter . sendMail ( mailOptions ,  function ( error ,  info ) { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                    if ( error ) { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                        console . log ( error ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                    } else { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                        console . log ( 'Message sent: '  +  info . response ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                } ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2015-08-07 11:20:22 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								            } ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2015-07-13 19:19:46 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        } ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-16 22:52:06 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    //res.send(JSON.stringify(req.body, null, 4));
 
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 13:01:00 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    res . status ( 204 ) . send ( 'POST received' ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-16 22:52:06 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								} ) ;  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								app . listen ( 3000 ,  function ( )  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    console . log ( "Started on PORT 3000" ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								} )