Skip to content
This repository has been archived by the owner on Aug 1, 2020. It is now read-only.
/ readsb Public archive

various improvements for your consideration #26

Merged
merged 8 commits into from
Dec 7, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove redundant history code
Probably was once there for internal webserver.
  • Loading branch information
wiedehopf committed Dec 7, 2019
commit abfa278d24ea7e636f684a458b49fdfcbb10a957
25 changes: 1 addition & 24 deletions net_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -1744,21 +1744,14 @@ char *generateStatsJson(const char *url_path, int *len) {
//
char *generateReceiverJson(const char *url_path, int *len) {
char *buf = (char *) malloc(1024), *p = buf;
int history_size;

MODES_NOTUSED(url_path);

// work out number of valid history entries
if (Modes.json_aircraft_history[HISTORY_SIZE - 1].content == NULL)
history_size = Modes.json_aircraft_history_next;
else
history_size = HISTORY_SIZE;

p += snprintf(p, 1024, "{ " \
"\"version\" : \"%s\", "
"\"refresh\" : %.0f, "
"\"history\" : %d",
MODES_READSB_VERSION, 1.0 * Modes.json_interval, history_size);
MODES_READSB_VERSION, 1.0 * Modes.json_interval, Modes.json_aircraft_history_next + 1 );

if (Modes.json_location_accuracy && (Modes.fUserLat != 0.0 || Modes.fUserLon != 0.0)) {
if (Modes.json_location_accuracy == 1) {
Expand All @@ -1780,22 +1773,6 @@ char *generateReceiverJson(const char *url_path, int *len) {
return buf;
}

char *generateHistoryJson(const char *url_path, int *len) {
int history_index = -1;

if (sscanf(url_path, "/data/history_%d.json", &history_index) != 1)
return NULL;

if (history_index < 0 || history_index >= HISTORY_SIZE)
return NULL;

if (!Modes.json_aircraft_history[history_index].content)
return NULL;

*len = Modes.json_aircraft_history[history_index].clen;
return strdup(Modes.json_aircraft_history[history_index].content);
}

// Write JSON to file
void writeJsonToFile(const char *file, char * (*generator) (const char *, int*)) {
#ifndef _WIN32
Expand Down
24 changes: 8 additions & 16 deletions readsb.c
Original file line number Diff line number Diff line change
Expand Up @@ -388,24 +388,19 @@ static void backgroundTasks(void) {
next_json = now + Modes.json_interval;
}

if (now >= next_history) {
int rewrite_receiver_json = (Modes.json_dir && Modes.json_aircraft_history[HISTORY_SIZE - 1].content == NULL);
if (Modes.json_dir && now >= next_history) {

free(Modes.json_aircraft_history[Modes.json_aircraft_history_next].content); // might be NULL, that's OK.
Modes.json_aircraft_history[Modes.json_aircraft_history_next].content =
generateAircraftJson("/data/aircraft.json", (int *) &Modes.json_aircraft_history[Modes.json_aircraft_history_next].clen);
char filebuf[PATH_MAX];
snprintf(filebuf, PATH_MAX, "history_%d.json", Modes.json_aircraft_history_next);
writeJsonToFile(filebuf, generateAircraftJson);

if (Modes.json_dir) {
char filebuf[PATH_MAX];
snprintf(filebuf, PATH_MAX, "history_%d.json", Modes.json_aircraft_history_next);
writeJsonToFile(filebuf, generateHistoryJson);
if (!Modes.json_aircraft_history_full) {
writeJsonToFile("receiver.json", generateReceiverJson); // number of history entries changed
if (Modes.json_aircraft_history_next == HISTORY_SIZE - 1)
Modes.json_aircraft_history_full = 1;
}

Modes.json_aircraft_history_next = (Modes.json_aircraft_history_next + 1) % HISTORY_SIZE;

if (rewrite_receiver_json)
writeJsonToFile("receiver.json", generateReceiverJson); // number of history entries changed

next_history = now + HISTORY_INTERVAL;
}
}
Expand Down Expand Up @@ -444,9 +439,6 @@ static void cleanup_and_exit(int code) {
for (i = 0; i < MODES_MAG_BUFFERS; ++i) {
free(Modes.mag_buffers[i].data);
}
for (i = 0; i < HISTORY_SIZE; ++i) {
free(Modes.json_aircraft_history[i].content);
}
crcCleanupTables();

/* Cleanup network setup */
Expand Down
7 changes: 1 addition & 6 deletions readsb.h
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ struct
int mlat; // Use Beast ascii format for raw data output, i.e. @...; iso *...;
int json_location_accuracy; // Accuracy of location metadata: 0=none, 1=approx, 2=exact
int json_aircraft_history_next;
int json_aircraft_history_full;
int stats_latest_1min;
int bUserFlags; // Flags relating to the user details
int biastee;
Expand All @@ -402,12 +403,6 @@ struct
struct stats stats_15min;
struct timespec reader_cpu_accumulator; // CPU time used by the reader thread, copied out and reset by the main thread under the mutex
struct mag_buf mag_buffers[MODES_MAG_BUFFERS]; // Converted magnitude buffers from RTL or file input

struct
{
long clen;
char *content;
} json_aircraft_history[HISTORY_SIZE];
} Modes;

// The struct we use to store information about a decoded message.
Expand Down