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-06-27 16:43:53 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								var  nodemailer  =  require ( 'nodemailer' ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2015-07-09 12:33:53 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								var  moment  =  require ( 'moment-timezone' ) ; 
							 
						 
					
						
							
								
									
										
										
										
											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 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								var  logfile  =  fs . createWriteStream ( './db/log.log' ,  { flags :  'a' } ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											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-06-27 16:43:53 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    to :  'jody@kaplon.us' , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    subject :  'Alert received' , 
							 
						 
					
						
							
								
									
										
										
										
											2015-07-13 19:19:46 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    text :  'test alert'   // Get custom text in here with device info.
 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-27 16:43:53 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								} ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-06-21 14:04:07 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								var  file  =  "./db/test.db" ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								var  exists  =  fs . existsSync ( file ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								if ( ! exists )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    console . log ( "Creating DB file." ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    fs . openSync ( file ,  "w" ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								var  sqlite3  =  require ( "sqlite3" ) . verbose ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								var  db  =  new  sqlite3 . Database ( file ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								db . serialize ( function ( )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    if ( ! exists )  { 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-22 12:16:56 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								        db . run ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            "CREATE TABLE Alerts ("  + 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								                "origJSON TEXT,"  + 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								                "coreId TEXT,"  + 
							 
						 
					
						
							
								
									
										
										
										
											2015-07-06 22:08:08 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								                "coreName TEXT,"  + 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-22 12:16:56 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								                "locationDesc TEXT,"  + 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								                "status TEXT,"  + 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								                "published_at TEXT)" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-21 14:04:07 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								} ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-16 22:52:06 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								app . use ( bodyParser . json ( ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											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 ) ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    db . all ( "SELECT coreId, coreName, locationDesc FROM Alerts GROUP BY coreId, coreName, locationDesc;" ,  function ( err ,  rows ) { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        if ( err  !==  null )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            console . log ( err ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        }  else  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            //console.log(rows);
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            res . render ( 'index' ,  { cores :  rows } ,  function ( err ,  html )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								                if ( err  !==  null )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								                    console . log ( err ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								                }  else  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								                    res . send ( html ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								                } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            } ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        } 
							 
						 
					
						
							
								
									
										
										
										
											2015-07-06 22:08:08 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    } ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								} ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								app . get ( '/core/:id' ,  function ( req ,  res ) { 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-16 22:52:06 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								    //res.sendFile("/usr/src/app/index.html");
 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-25 14:05:20 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    //fs.createReadStream('./log.log').pipe(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-07-08 15:20:27 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    db . all ( "SELECT coreId, published_at, status, coreName FROM Alerts WHERE coreId = '"  +  coreId  +  "' ORDER BY published_at DESC LIMIT 30;" ,  function ( err ,  rows ) { 
							 
						 
					
						
							
								
									
										
										
										
											2015-07-07 10:18:41 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								        if ( err  !==  null )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            console . log ( err ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        }  else  { 
							 
						 
					
						
							
								
									
										
										
										
											2015-07-07 14:16:07 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								            //console.log("SELECT coreId, published_at FROM Alerts WHERE coreId = '" + coreId + "' ORDER BY published_at DESC LIMIT 30;");
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            //console.log(rows);
 
							 
						 
					
						
							
								
									
										
										
										
											2015-07-09 12:33:53 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            // Loop over elements in rows array, convert ugly UTC times to pretty local times.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            rows . forEach ( function ( row ) { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								                var  localDtTm  =  moment . utc ( ( row . published _at  ||  "" ) . replace ( /-/g , "/" ) . replace ( /[TZ]/g , " " ) ) . toDate ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								                localDtTm  =  moment ( localDtTm ) . tz ( 'America/New_York' ) . format ( 'MM-DD-YYYY HH:mm:ss' ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								                row . published _at  =  localDtTm ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            } ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-07-07 10:18:41 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								            res . render ( 'core' ,  { alerts :  rows } ,  function ( err ,  html )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								                res . send ( html ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            } ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        } 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-25 14:05:20 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    } ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    //res.send;
 
							 
						 
					
						
							
								
									
										
										
										
											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 ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    var  status  =  JSON . stringify ( innerDataJSON . status ,  null ,  4 ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    var  stmt  =  db . prepare ( "INSERT INTO Alerts (OrigJSON, coreid, published_at, status) VALUES (?, ?, ?, ?)" ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-22 12:16:56 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    stmt . run ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        JSON . stringify ( req . body ,  null ,  4 ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        JSON . stringify ( req . body . coreid ,  null ,  4 ) , 
							 
						 
					
						
							
								
									
										
										
										
											2015-07-14 17:40:23 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								        JSON . stringify ( req . body . published _at ,  null ,  4 ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        status 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-22 12:16:56 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-21 14:04:07 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    stmt . finalize ( ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2015-07-03 21:38:40 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-07-13 19:19:46 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    // Send emails on alerts only
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    if ( JSON . stringify ( req . body ,  null ,  4 ) . indexOf ( 'alert' )  >  - 1 ) { 
							 
						 
					
						
							
								
									
										
										
										
											2015-07-14 17:40:23 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								        mailOptions . text  =  status ; 
							 
						 
					
						
							
								
									
										
										
										
											2015-07-13 19:19:46 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								        transporter . sendMail ( mailOptions ,  function ( error ,  info ) { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            if ( error ) { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								                console . log ( error ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            } else { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								                console . log ( 'Message sent: '  +  info . response ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								            } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        } ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    } 
							 
						 
					
						
							
								
									
										
										
										
											2015-07-03 21:38:40 -04:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-06-16 22:52:06 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								    //res.send(JSON.stringify(req.body, null, 4));
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								} ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								app . listen ( 3000 ,  function ( )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    console . log ( "Started on PORT 3000" ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								} )