From 77a5545d926bf4c8a4cedda17887764b54177cbf Mon Sep 17 00:00:00 2001 From: Jan-Piet Mens Date: Fri, 4 Mar 2016 12:40:54 +0100 Subject: [PATCH] override precision with _geoprec closes #58 --- README.md | 3 +++ recorder.c | 18 ++++++++++++++++-- storage.c | 7 ++++++- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9e3c831..407d883 100644 --- a/README.md +++ b/README.md @@ -1091,3 +1091,6 @@ We also have a Docker image to create containers which integrate a [Mosquitto br It actually is possible to gateway location publishes arriving via HTTP into MQTT, though you should be careful not to create loops. You can accomplish this with one or more Lua hooks using MQTT from within Lua. +### Override reverse-geo precision + +If a payload is received with an element called `_geoprec` it contains an ovveride for the Recorder's configured reverse-geo precision. diff --git a/recorder.c b/recorder.c index 1f55ed2..0abf1a0 100644 --- a/recorder.c +++ b/recorder.c @@ -536,7 +536,7 @@ void handle_message(void *userdata, char *topic, char *payload, size_t payloadle static UT_string *reltopic = NULL, *filename = NULL; char *jsonstring, *_typestr = NULL; time_t now; - int pingping = FALSE, skipslash = 0; + int pingping = FALSE, skipslash = 0, geoprec = geohash_prec(); int r_ok = TRUE; /* True if recording enabled for a publish */ payload_type _type; @@ -777,6 +777,12 @@ void handle_message(void *userdata, char *topic, char *payload, size_t payloadle goto cleanup; } +/* + if (impossible_rule(ud, UB(username), UB(device), tst, lat, lon) == TRUE) { + goto cleanup; + } +*/ + if ((j = json_find_member(json, "acc")) != NULL) { if (j->tag == JSON_STRING) { acc = atof(j->string_); @@ -798,6 +804,14 @@ void handle_message(void *userdata, char *topic, char *payload, size_t payloadle } } + if ((j = json_find_member(json, "_geoprec")) != NULL) { + if (j->tag == JSON_STRING) { + geoprec = atoi(j->string_); + } else { + geoprec = j->number_; + } + } + #if 0 /* Haversine */ @@ -833,7 +847,7 @@ void handle_message(void *userdata, char *topic, char *payload, size_t payloadle utstring_renew(ghash); utstring_renew(addr); utstring_renew(cc); - p = geohash_encode(lat, lon, geohash_prec()); + p = geohash_encode(lat, lon, geoprec); if (p != NULL) { utstring_printf(ghash, "%s", p); free(p); diff --git a/storage.c b/storage.c index 08a13e3..db52242 100644 --- a/storage.c +++ b/storage.c @@ -634,6 +634,7 @@ static JsonNode *line_to_location(char *line) char tstamp[64], *bp; double lat, lon; long tst; + int geoprec = geohash_prec(); snprintf(tstamp, 21, "%s", line); @@ -670,7 +671,11 @@ static JsonNode *line_to_location(char *line) lon = j->number_; } - if ((ghash = geohash_encode(lat, lon, geohash_prec())) != NULL) { + if ((j = json_find_member(o, "_geoprec")) != NULL) { + geoprec = j->number_; + } + + if ((ghash = geohash_encode(lat, lon, geoprec)) != NULL) { json_append_member(o, "ghash", json_mkstring(ghash)); get_geo(o, ghash); free(ghash);