2017-03-15 02:01:55 -06:00
'use strict' ;
2017-03-14 07:58:56 -06:00
// Imports
2017-05-23 11:32:40 -06:00
const fs = require ( 'fs' ) ,
debug = require ( 'debug' ) ( 'tracman-sockets' ) ,
2017-05-06 21:25:32 -06:00
User = require ( './models.js' ) . user ;
2017-03-14 07:58:56 -06:00
// Check for tracking clients
function checkForUsers ( io , user ) {
2017-05-08 11:47:53 -06:00
debug ( ` Checking for clients receiving updates for ${ user } ` ) ;
2017-03-14 07:58:56 -06:00
// Checks if any sockets are getting updates for this user
2017-04-28 13:59:43 -06:00
if ( Object . values ( io . sockets . connected ) . some ( ( socket ) => {
2017-04-27 14:44:49 -06:00
return socket . gets === user ;
2017-03-14 07:58:56 -06:00
} ) ) {
2017-05-08 11:47:53 -06:00
debug ( ` Activating updates for ${ user } . ` ) ;
2017-03-14 07:58:56 -06:00
io . to ( user ) . emit ( 'activate' , 'true' ) ;
} else {
2017-05-08 11:47:53 -06:00
debug ( ` Deactivating updates for ${ user } . ` ) ;
2017-03-14 07:58:56 -06:00
io . to ( user ) . emit ( 'activate' , 'false' ) ;
}
}
module . exports = {
checkForUsers : checkForUsers ,
2017-04-13 16:53:18 -06:00
init : ( io ) => {
io . on ( 'connection' , ( socket ) => {
2017-05-08 11:47:53 -06:00
debug ( ` ${ socket . id } connected. ` ) ;
2017-04-24 11:24:41 -06:00
// Set a few variables
2017-04-27 14:44:49 -06:00
//socket.ip = socket.client.request.headers['x-real-ip'];
//socket.ua = socket.client.request.headers['user-agent'];
2017-03-15 04:30:03 -06:00
2017-04-13 16:53:18 -06:00
/* Log */
2017-05-06 21:27:49 -06:00
socket . on ( 'log' , ( text ) => {
2017-05-08 11:47:53 -06:00
debug ( ` LOG: ${ text } ` ) ;
2017-05-06 21:27:49 -06:00
} ) ;
2017-03-14 07:58:56 -06:00
// This socket can set location (app)
2017-04-13 16:53:18 -06:00
socket . on ( 'can-set' , ( userId ) => {
2017-05-08 11:47:53 -06:00
debug ( ` ${ socket . id } can set updates for ${ userId } . ` ) ;
2017-04-13 16:53:18 -06:00
socket . join ( userId , ( ) => {
2017-05-08 11:47:53 -06:00
debug ( ` ${ socket . id } joined ${ userId } ` ) ;
2017-03-14 07:58:56 -06:00
} ) ;
checkForUsers ( io , userId ) ;
} ) ;
// This socket can receive location (map)
2017-04-13 16:53:18 -06:00
socket . on ( 'can-get' , ( userId ) => {
2017-03-14 07:58:56 -06:00
socket . gets = userId ;
2017-05-08 11:47:53 -06:00
debug ( ` ${ socket . id } can get updates for ${ userId } . ` ) ;
2017-04-13 16:53:18 -06:00
socket . join ( userId , ( ) => {
2017-05-08 11:47:53 -06:00
debug ( ` ${ socket . id } joined ${ userId } ` ) ;
2017-03-14 07:58:56 -06:00
socket . to ( userId ) . emit ( 'activate' , 'true' ) ;
} ) ;
} ) ;
// Set location
2017-04-13 16:53:18 -06:00
socket . on ( 'set' , ( loc ) => {
2017-05-08 11:47:53 -06:00
debug ( ` ${ socket . id } set location for ${ loc . usr } ` ) ;
debug ( ` Location was set to: ${ JSON . stringify ( loc ) } ` ) ;
2017-03-14 07:58:56 -06:00
2017-05-06 23:12:54 -06:00
// Get android timestamp or use server timestamp
if ( loc . ts ) { loc . tim = Date ( loc . ts ) ; }
else { loc . tim = Date . now ( ) ; }
2017-04-23 07:05:35 -06:00
// Check for user and sk32 token
if ( ! loc . usr ) {
2017-04-25 07:56:04 -06:00
console . error ( "❌" , new Error ( ` Recieved an update from ${ socket . ip } without a usr! ` ) . message ) ;
2017-04-23 07:05:35 -06:00
}
else if ( ! loc . tok ) {
2017-04-25 07:56:04 -06:00
console . error ( "❌" , new Error ( ` Recieved an update from ${ socket . ip } for usr ${ loc . usr } without an sk32! ` ) . message ) ;
2017-04-23 07:05:35 -06:00
}
2017-03-14 07:58:56 -06:00
else {
// Get loc.usr
2017-04-20 21:07:35 -06:00
User . findById ( loc . usr )
2017-04-25 18:01:35 -06:00
. where ( 'sk32' ) . equals ( loc . tok )
2017-04-20 21:07:35 -06:00
. then ( ( user ) => {
2017-04-23 07:05:35 -06:00
if ( ! user ) {
2017-04-25 18:01:35 -06:00
console . error ( "❌" , new Error ( ` Recieved an update from ${ socket . ip } for ${ loc . usr } with tok of ${ loc . tok } , but no such user was found in the db! ` ) . message ) ;
2017-04-23 07:05:35 -06:00
}
else {
2017-03-14 07:58:56 -06:00
2017-04-25 18:01:35 -06:00
// Broadcast location
io . to ( loc . usr ) . emit ( 'get' , loc ) ;
2017-05-08 11:47:53 -06:00
debug ( ` Broadcasting ${ loc . lat } , ${ loc . lon } to ${ loc . usr } ` ) ;
2017-04-25 18:01:35 -06:00
// Save in db as last seen
user . last = {
lat : parseFloat ( loc . lat ) ,
lon : parseFloat ( loc . lon ) ,
dir : parseFloat ( loc . dir || 0 ) ,
spd : parseFloat ( loc . spd || 0 ) ,
2017-05-06 23:12:54 -06:00
time : loc . tim
2017-04-25 18:01:35 -06:00
} ;
user . save ( )
. catch ( ( err ) => { console . error ( "❌" , err . stack ) ; } ) ;
2017-05-23 11:32:40 -06:00
// If Keith, record location to file
if ( user . id === '56b020279f740067540f96e9' ) {
2017-05-23 11:53:56 -06:00
fs . appendFile ( '/srv/tracman/demo.txt' , JSON . stringify ( loc ) , function ( err ) {
2017-05-23 11:48:54 -06:00
if ( err ) { console . error ( 'Error saving demo stuff:' , err . stack ) ; }
2017-05-23 11:32:40 -06:00
} ) ;
}
2017-03-14 07:58:56 -06:00
}
2017-04-20 21:07:35 -06:00
} )
2017-04-25 07:56:04 -06:00
. catch ( ( err ) => { console . error ( "❌" , err . stack ) ; } ) ;
2017-03-14 07:58:56 -06:00
}
} ) ;
// Shutdown (check for remaining clients)
2017-04-13 16:53:18 -06:00
socket . on ( 'disconnect' , ( reason ) => {
2017-05-08 11:47:53 -06:00
debug ( ` ${ socket . id } disconnected because of a ${ reason } . ` ) ;
2017-03-14 07:58:56 -06:00
// Check if client was receiving updates
if ( socket . gets ) {
2017-05-08 11:47:53 -06:00
debug ( ` ${ socket . id } left ${ socket . gets } ` ) ;
2017-03-14 07:58:56 -06:00
checkForUsers ( io , socket . gets ) ;
}
} ) ;
// Log errors
2017-04-25 07:56:04 -06:00
socket . on ( 'error' , ( err ) => { console . error ( '❌' , err . stack ) ; } ) ;
2017-03-14 07:58:56 -06:00
} ) ;
}
} ;