X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fgps.c;fp=src%2Fgps.c;h=944932ab27c7b9a3f7e34ffa840858e8b0cb3250;hb=6341066aae9ed687ee9463f3d269c70c02f481c5;hp=e793cfa6c744d1920b1c7a0598d6892d5ee6022e;hpb=6ca14739ed8cc76de14bd9ac4d549ba2c7c72a93;p=collectd.git diff --git a/src/gps.c b/src/gps.c index e793cfa6..944932ab 100644 --- a/src/gps.c +++ b/src/gps.c @@ -34,6 +34,8 @@ #define GPS_DEFAULT_PORT "2947" #define GPS_DEFAULT_TIMEOUT TIME_T_TO_CDTIME_T (15) #define GPS_DEFAULT_PAUSE TIME_T_TO_CDTIME_T (1) +#define GPS_MAX_ERROR 100 +#define GPS_CONFIG "?WATCH={\"enable\":true,\"json\":true,\"nmea\":false}\r\n" #include #include @@ -63,12 +65,15 @@ static pthread_mutex_t data_lock = PTHREAD_MUTEX_INITIALIZER; /** * Thread reading from gpsd. */ -static void * gps_collectd_thread (void * pData) +static void * cgps_thread (void * pData) { struct gps_data_t conn; + int err_count; while (1) { + err_count = 0; + int status = gps_open (config.host, config.port, &conn); if (status < 0) { @@ -79,12 +84,12 @@ static void * gps_collectd_thread (void * pData) } gps_stream (&conn, WATCH_ENABLE | WATCH_JSON | WATCH_NEWSTYLE, NULL); - gps_send (&conn, "?WATCH={\"enable\":true,\"json\":true,\"nmea\":false}\r\n"); + gps_send (&conn, GPS_CONFIG); while (1) { - long timeout_us = CDTIME_T_TO_US (config.timeout); - if (!gps_waiting (&conn, (int) timeout_us)) + long timeout_ms = CDTIME_T_TO_MS (config.timeout); + if (!gps_waiting (&conn, (int) timeout_ms)) { struct timespec pause_ns; CDTIME_T_TO_TIMESPEC (config.pause, &pause_ns); @@ -94,7 +99,24 @@ static void * gps_collectd_thread (void * pData) if (gps_read (&conn) == -1) { - WARNING ("gps plugin: incorrect data!"); + WARNING ("gps plugin: incorrect data! (err_count: %d)", err_count); + err_count++; + + if (err_count > GPS_MAX_ERROR) + { + // Server is not responding ... + if (gps_send (&conn, GPS_CONFIG) == -1) + { + WARNING ("gps plugin: gpsd seems to be done, reconnecting"); + break; + } + // Server is responding ... + else + { + err_count = 0; + } + } + continue; } @@ -202,7 +224,7 @@ static int cgps_init (void) config.host, config.port, CDTIME_T_TO_DOUBLE (config.timeout), CDTIME_T_TO_DOUBLE (config.pause)); - status = plugin_thread_create (&connector, NULL, gps_collectd_thread, NULL); + status = plugin_thread_create (&connector, NULL, cgps_thread, NULL); if (status != 0) { ERROR ("gps plugin: pthread_create() failed.");