Skip to content

Commit

Permalink
Periodically try to reopen if GPS disconnected
Browse files Browse the repository at this point in the history
  • Loading branch information
pascal-fb-martin committed Jan 8, 2023
1 parent ee3eaf6 commit a6140ea
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
4 changes: 2 additions & 2 deletions hc_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,13 @@ static void hc_background (int fd, int mode) {
if (nmea_db->fix && nmea_db->gpsdate[0] && nmea_db->gpstime[0]) {
if (!GpsTimeLock) {
houselog_event
("GPS", houselog_host(), "ACQUIRED", "CLOCK %s %s", nmea_db->gpsdate, nmea_db->gpstime);
("GPS", nmea_db->gpsdevice, "ACQUIRED", "CLOCK %s %s", nmea_db->gpsdate, nmea_db->gpstime);
GpsTimeLock = 1;
}
} else {
if (GpsTimeLock) {
houselog_event
("GPS", houselog_host(), "LOST", "CLOCK");
("GPS", nmea_db->gpsdevice, "LOST", "CLOCK");
GpsTimeLock = 0;
}
}
Expand Down
22 changes: 16 additions & 6 deletions hc_nmea.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ static void hc_nmea_reset (void) {
gpsCount = 0;
hc_nmea_status_db->fix = 0;
hc_nmea_status_db->fixtime = 0;
hc_nmea_status_db->gpsdevice[0] = 0;
hc_nmea_status_db->gpsdate[0] = 0;
hc_nmea_status_db->gpstime[0] = 0;
hc_nmea_status_db->latitude[0] = 0;
Expand Down Expand Up @@ -225,12 +226,7 @@ void hc_nmea_initialize (int argc, const char **argv) {
}

hc_nmea_reset();
gpsTty = open(gpsDevice, O_RDONLY);

// Remove echo of characters from the GPS device.
if (gpsTty >= 0) {
hc_tty_set (gpsTty, gpsSpeed);
}
hc_nmea_listen ();

gpsInitialized = time(0);
}
Expand Down Expand Up @@ -605,6 +601,20 @@ void hc_nmea_periodic (const struct timeval *now) {
}

int hc_nmea_listen (void) {
if (gpsTty >= 0) return gpsTty;

static time_t LastTry = 0;
time_t now = time(0);
if (now < LastTry + 5) return gpsTty;

LastTry = now;
gpsTty = open(gpsDevice, O_RDONLY);
if (gpsTty < 0) return gpsTty;

// Remove echo of characters from the GPS device.
hc_tty_set (gpsTty, gpsSpeed);
snprintf (hc_nmea_status_db->gpsdevice,
sizeof(hc_nmea_status_db->gpsdevice), "%s", gpsDevice);
return gpsTty;
}

Expand Down
1 change: 1 addition & 0 deletions hc_nmea.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ typedef struct {
typedef struct {
char fix;
time_t fixtime;
char gpsdevice[80];
char gpstime[20];
char gpsdate[20];
char latitude[20];
Expand Down

0 comments on commit a6140ea

Please sign in to comment.