add user/device to location JSON for multilister()

closes #49
This commit is contained in:
Jan-Piet Mens
2015-10-02 16:26:21 +02:00
parent 0f44203178
commit 815ea6908c
4 changed files with 26 additions and 5 deletions

2
http.c
View File

@@ -386,7 +386,7 @@ static int dispatch(struct mg_connection *conn, const char *uri)
if ((arr = json_find_member(json, "results")) != NULL) {
JsonNode *f;
json_foreach(f, arr) {
locations(f->string_, obj, locs, s_lo, s_hi, otype, limit, NULL);
locations(f->string_, obj, locs, s_lo, s_hi, otype, limit, NULL, NULL, NULL);
// printf("%s\n", f->string_);
}
}

5
ocat.c
View File

@@ -519,7 +519,7 @@ int main(int argc, char **argv)
int n;
for (n = 0; n < argc; n++) {
locations(argv[n], obj, locs, s_lo, s_hi, otype, 0, fields);
locations(argv[n], obj, locs, s_lo, s_hi, otype, 0, fields, NULL, NULL);
}
} else {
JsonNode *arr, *f;
@@ -532,7 +532,8 @@ int main(int argc, char **argv)
if ((json = lister(username, device, s_lo, s_hi, (limit > 0) ? TRUE : FALSE)) != NULL) {
if ((arr = json_find_member(json, "results")) != NULL) { // get array
json_foreach(f, arr) {
locations(f->string_, obj, locs, s_lo, s_hi, otype, limit, fields);
// locations(f->string_, obj, locs, s_lo, s_hi, otype, limit, fields, username, device);
locations(f->string_, obj, locs, s_lo, s_hi, otype, limit, fields, NULL, NULL);
// printf("%s\n", f->string_);
}
}

View File

@@ -513,6 +513,8 @@ struct jparam {
output_type otype;
int limit; /* if non-zero, we're searching backwards */
JsonNode *fields;
char *username;
char *device;
};
/*
@@ -600,6 +602,8 @@ static int candidate_line(char *line, void *param)
time_t s_hi = jarg->s_hi;
JsonNode *fields = jarg->fields;
output_type otype = jarg->otype;
char *username = jarg->username;
char *device = jarg->device;
if (obj == NULL || obj->tag != JSON_OBJECT)
return (-1);
@@ -658,6 +662,16 @@ static int candidate_line(char *line, void *param)
// fprintf(stderr, "-->[%s]\n", line);
if ((o = line_to_location(line)) != NULL) {
/*
* Username/device are added typically for multilister() only.
*/
if (username)
json_append_member(o, "username", json_mkstring(username));
if (device)
json_append_member(o, "device", json_mkstring(device));
if (fields) {
/* Create a new object, copying members we're interested in into it */
JsonNode *f, *node;
@@ -672,6 +686,7 @@ static int candidate_line(char *line, void *param)
}
json_delete(o);
o = newo;
}
json_append_element(locs, o);
++counter;
@@ -689,9 +704,12 @@ static int candidate_line(char *line, void *param)
* contains `arr'.
* If limit is zero, we're going forward, else backwards.
* Fields, if not NULL, is a JSON array of desired element names.
*
* If username & device are not NULL, populate the JSON locations
* with them for multilister().
*/
void locations(char *filename, JsonNode *obj, JsonNode *arr, time_t s_lo, time_t s_hi, output_type otype, int limit, JsonNode *fields)
void locations(char *filename, JsonNode *obj, JsonNode *arr, time_t s_lo, time_t s_hi, output_type otype, int limit, JsonNode *fields, char *username, char *device)
{
struct jparam jarg;
@@ -705,6 +723,8 @@ void locations(char *filename, JsonNode *obj, JsonNode *arr, time_t s_lo, time_t
jarg.otype = otype;
jarg.limit = limit;
jarg.fields = fields;
jarg.username = username;
jarg.device = device;
if (limit == 0) {
cat(filename, candidate_line, &jarg);

View File

@@ -37,7 +37,7 @@ typedef enum {
JsonNode *lister(char *username, char *device, time_t s_lo, time_t s_hi, int reverse);
JsonNode *multilister(JsonNode *udpairs, time_t s_lo, time_t s_hi, int reverse);
void locations(char *filename, JsonNode *obj, JsonNode *arr, time_t s_lo, time_t s_hi, output_type otype, int limit, JsonNode *fields);
void locations(char *filename, JsonNode *obj, JsonNode *arr, time_t s_lo, time_t s_hi, output_type otype, int limit, JsonNode *fields, char *username, char *device);
int make_times(char *time_from, time_t *s_lo, char *time_to, time_t *s_to);
JsonNode *geo_json(JsonNode *json);
JsonNode *geo_linestring(JsonNode *location_array);