195 lines
4.9 KiB
C++
195 lines
4.9 KiB
C++
//
|
|
// Track Daemon
|
|
// Neal Probert
|
|
//
|
|
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
#include <sys/timeb.h>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/types.h>
|
|
|
|
#include "NmeaParser.h"
|
|
#include "UriParse.h"
|
|
#include "TrackDaemon.h"
|
|
#include "netlib.h"
|
|
|
|
//****************************************************************************
|
|
// defines
|
|
//***************************************************************************/
|
|
|
|
//****************************************************************************
|
|
// macros
|
|
//***************************************************************************/
|
|
|
|
//****************************************************************************
|
|
// structs & typedefs
|
|
//***************************************************************************/
|
|
|
|
//****************************************************************************
|
|
// global constants
|
|
//***************************************************************************/
|
|
|
|
//****************************************************************************
|
|
// global variables
|
|
//***************************************************************************/
|
|
|
|
//****************************************************************************
|
|
// static constants
|
|
//***************************************************************************/
|
|
|
|
//****************************************************************************
|
|
// static variables
|
|
//***************************************************************************/
|
|
|
|
//****************************************************************************
|
|
// static functions
|
|
//***************************************************************************/
|
|
|
|
//****************************************************************************
|
|
// C++ functions
|
|
//***************************************************************************/
|
|
|
|
TrackDaemon::TrackDaemon()
|
|
{
|
|
log_open( TRACKD_NAME );
|
|
|
|
gethostname( Id, sizeof(Id) );
|
|
}
|
|
|
|
TrackDaemon::~TrackDaemon()
|
|
{
|
|
}
|
|
|
|
/* UDP ***********************************************************************/
|
|
|
|
void TrackDaemon::PositionListener( const PositionFix &Pos, const ErrorInfo &Err )
|
|
{
|
|
char buf[256];
|
|
int n = 0;
|
|
char datetime[40];
|
|
|
|
// convert date & time (MySQL friendly)
|
|
struct tm *tmp = gmtime(&Pos.Time.tv_sec);
|
|
strftime( datetime, sizeof(datetime), "%Y-%m-%d,%T", tmp );
|
|
|
|
// convert to unicsv
|
|
n = snprintf( buf, sizeof(buf),
|
|
"%s,%s,%lf,%lf,%lf,%lf,%lf\n",
|
|
Id, // name
|
|
datetime, // time
|
|
Pos.Latitude, // lat
|
|
Pos.Longitude, // long
|
|
Pos.Altitude, // elev
|
|
Pos.Speed, // speed (m/sec -> kph)
|
|
Pos.Heading // heading
|
|
);
|
|
|
|
// send upstream
|
|
if ( TrackOut >= 0 )
|
|
Track.SendTo( buf, n );
|
|
|
|
// log locally
|
|
Log.LogWrite( buf, n );
|
|
}
|
|
|
|
void TrackDaemon::TimeListener( const struct timeval &tv )
|
|
{
|
|
}
|
|
|
|
int TrackDaemon::ServerSocket(void)
|
|
{
|
|
char buf[2048];
|
|
int n = 0;
|
|
|
|
if ( IsSet( GpsIn ) )
|
|
{
|
|
// Gpsd
|
|
if ( (n = Gpsd.Recv( buf, sizeof(buf) )) > 0 )
|
|
{
|
|
// use NMEA parser (should trigger on RMC or GGA)
|
|
Gps.Nmea.NmeaParse(buf);
|
|
}
|
|
}
|
|
return n;
|
|
}
|
|
|
|
int TrackDaemon::ClientInOut( char *buf, int n, int max )
|
|
{
|
|
// check
|
|
if ( n <= 0 )
|
|
return 0;
|
|
|
|
// parse $PVII
|
|
if ( buf[0] == '$' )
|
|
{
|
|
// use NMEA parser (should trigger on RMC or GGA)
|
|
Gps.Nmea.NmeaParse( buf );
|
|
}
|
|
else
|
|
{
|
|
// forward
|
|
if ( TrackOut >= 0 )
|
|
Track.SendTo( buf, n );
|
|
|
|
// log
|
|
Log.LogWrite( buf , n );
|
|
}
|
|
|
|
// no response
|
|
return 0;
|
|
}
|
|
|
|
/* GPS ***********************************************************************/
|
|
|
|
int TrackDaemon::GpsdServer( const char *uri )
|
|
{
|
|
UriParse parsed;
|
|
parsed.setProto( "gpsd" );
|
|
parsed.setService( "gpsd" );
|
|
parsed.setUri( uri );
|
|
bGpsd = 0;
|
|
|
|
const char *proto = parsed.getProto();
|
|
if ( !strlen(proto) )
|
|
proto = "gpsd";
|
|
GpsDeviceType type = Gps.Proto2Type( proto );
|
|
|
|
// open device
|
|
if ( strcmp( proto, "gpsd" ) == 0 || type == GPS_TYPE_UNDEF )
|
|
GpsIn = Gpsd.GpsdConnect( parsed.getHost() );
|
|
else
|
|
GpsIn = Gps.GpsOpen( parsed.getPath(), parsed.getRate(), type );
|
|
|
|
if ( GpsIn >= 0 )
|
|
AddFd( GpsIn );
|
|
return GpsIn;
|
|
}
|
|
|
|
int TrackDaemon::TrackOpen( const char *uri )
|
|
{
|
|
UriParse parsed;
|
|
parsed.setService( TRACKD_PORT );
|
|
parsed.setUri( uri );
|
|
|
|
// connect to remote web server to upload data
|
|
return TrackOut = Track.Connect( parsed.getHost(), TRACKD_PORT );
|
|
}
|
|
|
|
int TrackDaemon::LogOpen( const char *uri )
|
|
{
|
|
if ( Log.LogOpen( uri ) )
|
|
{
|
|
Log.LogFields( "name,date,time,lat,long,ele,speed,head" );
|
|
return 1;
|
|
}
|
|
return -1;
|
|
}
|