optionally return content of last/user/device/http.json

This commit is contained in:
Jan-Piet Mens
2016-02-24 14:00:55 +01:00
parent 215e1beee2
commit 2cfee69be3
4 changed files with 70 additions and 5 deletions
+1
View File
@@ -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);
+18 -5
View File
@@ -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);
}
+50
View File
@@ -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);
}
+1
View File
@@ -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