diff --git a/dump1090.c b/dump1090.c index 60882fc..00d907f 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; } // //========================================================================= @@ -436,6 +437,8 @@ void showHelp(void) { "--debug Debug mode (verbose), see README for details\n" "--quiet Disable output to stdout. Use for daemon applications\n" "--ppm Set receiver error in parts per million (default 0)\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" @@ -571,6 +574,7 @@ static void display_stats(void) { // void backgroundTasks(void) { static time_t next_stats; + static time_t next_json; if (Modes.net) { modesReadFromClients(); @@ -594,6 +598,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; + } + } } // //========================================================================= @@ -778,6 +790,15 @@ int main(int argc, char **argv) { } else if (!strcmp(argv[j],"--interactive-rtl1090")) { Modes.interactive = 1; Modes.interactive_rtl1090 = 1; +#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 9ad4de0..d38e32c 100644 --- a/dump1090.h +++ b/dump1090.h @@ -57,6 +57,7 @@ #include #include #include + #include #include "rtl-sdr.h" #include "anet.h" #else @@ -318,6 +319,9 @@ struct { // Internal state int metric; // Use metric units 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 + + 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 @@ -458,6 +462,7 @@ void modesReadFromClients (void); void modesSendAllClients (int service, void *msg, int len); void modesQueueOutput (struct modesMessage *mm); void modesReadFromClient(struct client *c, char *sep, int(*handler)(struct client *, char *)); +void modesWriteJson (const char *path); #ifdef __cplusplus } diff --git a/net_io.c b/net_io.c index a979883..85246fc 100644 --- a/net_io.c +++ b/net_io.c @@ -694,6 +694,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 +} + // //========================================================================= // @@ -754,8 +791,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;