Experimental json changes

This commit is contained in:
Oliver Jowett 2014-11-23 16:02:11 +00:00
parent bff92c4ad7
commit 6910a4bf4d
4 changed files with 66 additions and 3 deletions

View file

@ -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 <flags> Debug mode (verbose), see README for details\n"
"--quiet Disable output to stdout. Use for daemon applications\n"
"--ppm <error> Set receiver error in parts per million (default 0)\n"
"--write-json <dir> Periodically write json output to <dir> (for serving by a separate webserver)\n"
"--write-json-every <t> 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",

View file

@ -57,6 +57,7 @@
#include <ctype.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <limits.h>
#include "rtl-sdr.h"
#include "anet.h"
#else
@ -319,6 +320,9 @@ 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
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
double fUserLon; // 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
}

View file

@ -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);

View file

@ -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;