diff --git a/dump1090.c b/dump1090.c index b78fc21..11e563d 100644 --- a/dump1090.c +++ b/dump1090.c @@ -82,6 +82,7 @@ void modesInitConfig(void) { Modes.interactive_display_ttl = MODES_INTERACTIVE_DISPLAY_TTL; Modes.fUserLat = MODES_USER_LATITUDE_DFLT; Modes.fUserLon = MODES_USER_LONGITUDE_DFLT; + Modes.json_interval = 1; } // //========================================================================= @@ -444,6 +445,8 @@ void showHelp(void) { "--quiet Disable output to stdout. Use for daemon applications\n" "--ppm Set receiver error in parts per million (default 0)\n" "--no-decode Don't decode the message contents beyond the minimum necessary\n" +"--write-json Periodically write json output to (for serving by a separate webserver)\n" +"--write-json-every Write json output every t seconds (default 1)\n" "--help Show this help\n" "\n" "Debug mode flags: d = Log frames decoded with errors\n" @@ -603,6 +606,7 @@ static void display_stats(void) { // void backgroundTasks(void) { static time_t next_stats; + static time_t next_json; if (Modes.net) { modesNetPeriodicWork(); @@ -626,6 +630,14 @@ void backgroundTasks(void) { next_stats = now + Modes.stats; } } + + if (Modes.json_path && Modes.json_interval > 0) { + time_t now = time(NULL); + if (now > next_json) { + modesWriteJson(Modes.json_path); + next_json = now + Modes.json_interval; + } + } } // //========================================================================= @@ -817,6 +829,15 @@ int main(int argc, char **argv) { } else if (!strcmp(argv[j],"--oversample")) { Modes.oversample = 1; fprintf(stderr, "Oversampling enabled. Be very afraid.\n"); +#ifndef _WIN32 + } else if (!strcmp(argv[j], "--write-json") && more) { + ++j; + Modes.json_path = malloc(strlen(argv[j]) + 11); + strcpy(Modes.json_path, argv[j]); + strcat(Modes.json_path, "/aircraft.json"); + } else if (!strcmp(argv[j], "--write-json-every") && more) { + Modes.json_interval = atoi(argv[++j]); +#endif } else { fprintf(stderr, "Unknown or not enough arguments for option '%s'.\n\n", diff --git a/dump1090.h b/dump1090.h index cc47d34..2dfd491 100644 --- a/dump1090.h +++ b/dump1090.h @@ -58,6 +58,7 @@ #include #include #include + #include #include "rtl-sdr.h" #include "anet.h" #else @@ -355,6 +356,8 @@ struct { // Internal state int mlat; // Use Beast ascii format for raw data output, i.e. @...; iso *...; int interactive_rtl1090; // flight table in interactive mode is formatted like RTL1090 int no_decode; // Disable decoding and aircraft tracking + char *json_path; // Path to json data file to write, or NULL not to. + int json_interval; // Interval between rewriting the json data file // User details double fUserLat; // Users receiver/antenna lat/lon needed for initial surface location @@ -479,6 +482,7 @@ void modesInitNet (void); void modesQueueOutput (struct modesMessage *mm); void modesReadFromClient(struct client *c, char *sep, int(*handler)(struct client *, char *)); void modesNetPeriodicWork (void); +void modesWriteJson (const char *path); #ifdef __cplusplus } diff --git a/net_io.c b/net_io.c index 632e013..a5d1262 100644 --- a/net_io.c +++ b/net_io.c @@ -725,6 +725,43 @@ char *aircraftsToJson(int *len) { *len = p-buf; return buf; } + +// Write JSON state to json_path +void modesWriteJson(const char *path) +{ +#ifndef _WIN32 + char tmppath[PATH_MAX]; + int fd; + int len = 0; + char *content; + + snprintf(tmppath, PATH_MAX, "%s.XXXXXX", path); + tmppath[PATH_MAX-1] = 0; + fd = mkstemp(tmppath); + if (fd < 0) + return; + + content = aircraftsToJson(&len); + if (write(fd, content, len) != len) + goto error_1; + + if (close(fd) < 0) + goto error_2; + + free(content); + rename(tmppath, path); + return; + + error_1: + close(fd); + error_2: + free(content); + unlink(tmppath); + return; + +#endif +} + // //========================================================================= // @@ -785,8 +822,8 @@ int handleHTTPRequest(struct client *c, char *p) { // Select the content to send, we have just two so far: // "/" -> Our google map application. - // "/data.json" -> Our ajax request to update planes. - if (strstr(url, "/data.json")) { + // "/aircraft.json" -> Our ajax request to update planes. + if (strstr(url, "/data/aircraft.json")) { statuscode = 200; content = aircraftsToJson(&clen); //snprintf(ctype, sizeof ctype, MODES_CONTENT_TYPE_JSON); diff --git a/public_html/script.js b/public_html/script.js index 8212e50..cf12e41 100644 --- a/public_html/script.js +++ b/public_html/script.js @@ -18,7 +18,7 @@ CenterLon = Number(localStorage['CenterLon']) || CONST_CENTERLON; ZoomLvl = Number(localStorage['ZoomLvl']) || CONST_ZOOMLVL; function fetchData() { - $.getJSON('/dump1090/data.json', function(data) { + $.getJSON('data/aircraft.json', function(data) { PlanesOnMap = 0 SpecialSquawk = false;