From 2cfee69be382c342e5a7048f5a839259cbf9bda7 Mon Sep 17 00:00:00 2001 From: Jan-Piet Mens Date: Wed, 24 Feb 2016 14:00:55 +0100 Subject: [PATCH] optionally return content of last/user/device/http.json --- http.c | 1 + recorder.c | 23 ++++++++++++++++++----- storage.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ storage.h | 1 + 4 files changed, 70 insertions(+), 5 deletions(-) diff --git a/http.c b/http.c index 76d088d..8a0cbe7 100644 --- a/http.c +++ b/http.c @@ -587,6 +587,7 @@ static int dopublish(struct mg_connection *conn, const char *uri) free(payload); jarray = populate_friends(conn, u, d); + extra_http_json(jarray, u, d); free(u); free(d); diff --git a/recorder.c b/recorder.c index 89b4f0c..5e12e9d 100644 --- a/recorder.c +++ b/recorder.c @@ -76,6 +76,7 @@ double number(JsonNode *j, char *element) d = atof(m->string_); /* Normalize to number */ json_remove_from_parent(m); + json_delete(m); json_append_member(j, element, json_mknumber(d)); return (d); } @@ -380,6 +381,7 @@ void waypoints_dump(struct udata *ud, UT_string *username, UT_string *device, ch if ((j = json_find_member(json, "r")) != NULL) { json_remove_from_parent(j); + json_delete(j); js = json_stringify(json, NULL); json_delete(json); } @@ -432,12 +434,15 @@ static void ronly_set(struct udata *ud, UT_string *basetopic, int active) touch = TRUE; } - if ((j = json_find_member(json, "last")) != NULL) + if ((j = json_find_member(json, "last")) != NULL) { json_remove_from_parent(j); + json_delete(j); + } if ((j = json_find_member(json, "active")) != NULL) { if (active != j->bool_) { json_remove_from_parent(j); + json_delete(j); json_append_member(json, "active", json_mkbool(active)); json_append_member(json, "last", json_mknumber(time(0))); touch = TRUE; @@ -843,7 +848,7 @@ void handle_message(void *userdata, char *topic, char *payload, size_t payloadle case T_ENCRYPTED: /* * Obtain the `data' element from JSON, and try and decrypt - * that. If successful, we the decrypted message as + * that. If successful, we use the decrypted message as * payload, and invoke this function again to do the * heavy lifting. */ @@ -890,6 +895,7 @@ void handle_message(void *userdata, char *topic, char *payload, size_t payloadle if (j->tag == JSON_STRING) { tst = strtoul(j->string_, NULL, 10); json_remove_from_parent(j); + json_delete(j); json_append_member(json, "tst", json_mknumber(tst)); } else { tst = (unsigned long)j->number_; @@ -905,6 +911,7 @@ void handle_message(void *userdata, char *topic, char *payload, size_t payloadle if (j->tag == JSON_STRING) { acc = atof(j->string_); json_remove_from_parent(j); + json_delete(j); json_append_member(json, "acc", json_mknumber(acc)); } } @@ -940,8 +947,10 @@ void handle_message(void *userdata, char *topic, char *payload, size_t payloadle long blen; if ((blen = gcache_get(ud->t2t, topic, newtid, sizeof(newtid))) > 0) { - if ((j = json_find_member(json, "tid")) != NULL) + if ((j = json_find_member(json, "tid")) != NULL) { json_remove_from_parent(j); + json_delete(j); + } json_append_member(json, "tid", json_mkstring(newtid)); } } @@ -1240,7 +1249,7 @@ int main(int argc, char **argv) #if WITH_MQTT struct mosquitto *mosq = NULL; char *username, *password, *cafile; - char *hostname = "localhost"; + char *hostname = strdup("localhost"); int port = 1883; UT_string *clientid; int rc, i; @@ -1259,7 +1268,7 @@ int main(int argc, char **argv) #ifdef WITH_HTTP int http_port = 8083; char *doc_root = DOCROOT; - char *http_host = "localhost"; + char *http_host = strdup("localhost"); #endif char *progname = *argv; @@ -1397,6 +1406,7 @@ int main(int argc, char **argv) ud->ignoreretained = FALSE; break; case 'H': + free(hostname); hostname = strdup(optarg); break; case 'p': @@ -1417,6 +1427,7 @@ int main(int argc, char **argv) doc_root = strdup(optarg); break; case 3: /* no short char */ + free(http_host); http_host = strdup(optarg); break; #endif @@ -1734,7 +1745,9 @@ int main(int argc, char **argv) mosquitto_destroy(mosq); mosquitto_lib_cleanup(); + free(hostname); #endif + free(http_host); return (0); } diff --git a/storage.c b/storage.c index 9986d48..5d648a2 100644 --- a/storage.c +++ b/storage.c @@ -496,6 +496,7 @@ static void lsscan(char *pathpat, time_t s_lo, time_t s_hi, JsonNode *obj, int r jarr = json_mkarray(); } else { json_remove_from_parent(jarr); + json_delete(jarr); } if (reverse) { @@ -1427,3 +1428,52 @@ char *storage_userphoto(char *username) return (path); } + +/* + * array is an array of JSON objects. Get the JSON at http.json + * and append that object to `array`. + */ + +void extra_http_json(JsonNode *array, char *user, char *device) +{ + char path[BUFSIZ], *js_string; + JsonNode *node; + + if (!array || !user || !*user || !device || !*device) + return; + if (array->tag != JSON_ARRAY) + return; + + /* Extra data */ + snprintf(path, BUFSIZ, "%s/last/%s/%s/http.json", + STORAGEDIR, user, device); + + if ((js_string = slurp_file(path, TRUE)) == NULL) { + return; + } + + if ((node = json_decode(js_string)) == NULL) { + fprintf(stderr, "extra_http_json: can't decode JSON from %s\n", path); + free(js_string); + return; + } + + if (node->tag == JSON_OBJECT) { + json_append_element(array, node); + } else if (node->tag == JSON_ARRAY) { + JsonNode *f; + + json_foreach(f, node) { + JsonNode *o; + + if (f->tag != JSON_OBJECT) + continue; + + o = json_mkobject(); + json_copy_to_object(o, f, TRUE); + json_append_element(array, o); + } + json_delete(node); + } + free(js_string); +} diff --git a/storage.h b/storage.h index e622d8e..e636edd 100644 --- a/storage.h +++ b/storage.h @@ -54,5 +54,6 @@ void xml_output(JsonNode *json, output_type otype, JsonNode *fields, void (*func void csv_output(JsonNode *json, output_type otype, JsonNode *fields, void (*func)(char *s, void *param), void *param); char *storage_userphoto(char *username); void append_card_to_object(JsonNode *obj, char *user); +void extra_http_json(JsonNode *array, char *user, char *device); #endif