From e1cd5c39e71ffb345099bf1339dee937fefc3587 Mon Sep 17 00:00:00 2001 From: Jan-Piet Mens Date: Thu, 8 Dec 2016 18:07:15 +0100 Subject: [PATCH] geofences now use correct topic and enhanced payload as per Booklet for transition events --- doc/FENCES.md | 8 ++++++-- doc/HOOKS.md | 2 +- fences.c | 6 +++--- fences.h | 4 +++- gcache.c | 4 +++- gcache.h | 2 +- hooks.c | 21 ++++++++++----------- hooks.h | 2 +- recorder.c | 2 +- 9 files changed, 29 insertions(+), 22 deletions(-) diff --git a/doc/FENCES.md b/doc/FENCES.md index 0764a25..b73c1aa 100644 --- a/doc/FENCES.md +++ b/doc/FENCES.md @@ -35,7 +35,7 @@ Note how the `io` (in / out) element in the JSON indicates whether the last posi ### Transition hook -When Recorder determines that the user's device has entered or left the geo fence, it invokes a user-provided Lua function called `transition`: +When Recorder determines that the user's device has entered or left the geo fence, it invokes a user-provided Lua function called `otr_transition`: ```lua function otr_init() @@ -44,8 +44,12 @@ end function otr_exit() end -function transition(topic, _type, data) +function otr_transition(topic, _type, data) print("IN TRANSITION " .. _type .. " " .. topic) otr.publish('special/topic', data['event'] .. " " .. data['desc']) end ``` + +In addition to the payload as described in the Booklet, recorder enhances the table passed to the Lua function with the following elements: + +- `wplat` / `wplon` are the latitude / longitude of the original waypoint definition diff --git a/doc/HOOKS.md b/doc/HOOKS.md index f441fab..2be7afb 100644 --- a/doc/HOOKS.md +++ b/doc/HOOKS.md @@ -86,7 +86,7 @@ non-zero value, the Recorder will *not* write the REC file for this publish. An optional function you provide is called `otr_httpobject(u, d, t, data)` where `u` is the username used by the client (`?u=`), `d` is the device name (`&d=` in the URI), `t` is the OwnTracks JSON `_type` and `data` a Lua table built from the OwnTracks JSON payload of `_type`. If it exists, this function is called whenever a POST is received in httpmode and the Recorder is gathering data to return to the client app. The function *must* return a Lua table containing any number of string, number, or boolean values which are converted to a JSON object and appended to the JSON array returned to the client. An [example](etc/example.lua) shows how, say, a transition event can be used to open the Featured content tab in the app. -## `transition` +## `otr_transition` See [geo fences](FENCES.md). diff --git a/fences.c b/fences.c index 4fbe165..a221a0d 100644 --- a/fences.c +++ b/fences.c @@ -62,7 +62,7 @@ static int check_a_waypoint(char *key, wpoint *wp, double lat, double lon) // printf("%s - %s: EVENT == %s\n", wp->user, wp->device, wp->event == 0 ? "ENTER" : "LEAVE"); #ifdef WITH_LUA if (wp->ud->luadata) { - hooks_transition(wp->ud, wp->user, wp->device, wp->event, wp->desc, wp->lat, wp->lon, lat, lon); + hooks_transition(wp->ud, wp->user, wp->device, wp->event, wp->desc, wp->lat, wp->lon, lat, lon, wp->topic, wp->json); } #endif /* WITH_LUA */ } @@ -78,7 +78,7 @@ static int check_a_waypoint(char *key, wpoint *wp, double lat, double lon) * the Haversine formula. */ -void check_fences(struct udata *ud, char *username, char *device, double lat, double lon, JsonNode *json) +void check_fences(struct udata *ud, char *username, char *device, double lat, double lon, JsonNode *json, char *topic) { static UT_string *userdev; @@ -90,5 +90,5 @@ void check_fences(struct udata *ud, char *username, char *device, double lat, do * and do as described above. */ - gcache_enum(username, device, ud->wpdb, UB(userdev), check_a_waypoint, lat, lon, ud); + gcache_enum(username, device, ud->wpdb, UB(userdev), check_a_waypoint, lat, lon, ud, topic, json); } diff --git a/fences.h b/fences.h index 5e93511..9f5eca6 100644 --- a/fences.h +++ b/fences.h @@ -13,9 +13,11 @@ typedef struct { char *device; enum { ENTER, LEAVE } event; struct udata *ud; + char *topic; /* original publish topic */ + JsonNode *json; /* full JSON payload of publish */ } wpoint; -void check_fences(struct udata *ud, char *username, char *device, double lat, double lon, JsonNode *json); +void check_fences(struct udata *ud, char *username, char *device, double lat, double lon, JsonNode *json, char *topic); #endif diff --git a/gcache.c b/gcache.c index ea009ba..2613503 100644 --- a/gcache.c +++ b/gcache.c @@ -353,7 +353,7 @@ void gcache_load(char *path, char *lmdbname) * update the data. */ -bool gcache_enum(char *user, char *device, struct gcache *gc, char *key_part, int (*func)(char *key, wpoint *wp, double lat, double lon), double lat, double lon, struct udata *ud) +bool gcache_enum(char *user, char *device, struct gcache *gc, char *key_part, int (*func)(char *key, wpoint *wp, double lat, double lon), double lat, double lon, struct udata *ud, char *topic, JsonNode *jsonpayload) { MDB_val key, data; MDB_txn *txn; @@ -419,6 +419,8 @@ bool gcache_enum(char *user, char *device, struct gcache *gc, char *key_part, in wp.ud = ud; wp.user = user; wp.device = device; + wp.topic = topic; + wp.json = jsonpayload; if (func && func(UB(ks), &wp, lat, lon) == true) { json_remove_from_parent(jio); diff --git a/gcache.h b/gcache.h index 2baf08b..439c211 100644 --- a/gcache.h +++ b/gcache.h @@ -20,6 +20,6 @@ JsonNode *gcache_json_get(struct gcache *, char *key); void gcache_dump(char *path, char *lmdbname); void gcache_load(char *path, char *lmdbname); int gcache_del(struct gcache *gc, char *keystr); -bool gcache_enum(char *user, char *device, struct gcache *gc, char *key_part, int (*func)(char *key, wpoint *wp, double lat, double lon), double lat, double lon, struct udata *ud); +bool gcache_enum(char *user, char *device, struct gcache *gc, char *key_part, int (*func)(char *key, wpoint *wp, double lat, double lon), double lat, double lon, struct udata *ud, char *topic, JsonNode *json); #endif diff --git a/hooks.c b/hooks.c index a69b341..a06f4d3 100644 --- a/hooks.c +++ b/hooks.c @@ -382,29 +382,28 @@ void hooks_hook(struct udata *ud, char *topic, JsonNode *fullo) /* * This hook is invoked through fences.c when we determine that a movement * into or out of a geofence has caused a transition. + * json is the original JSON we received enhanced with stuff from recorder. */ -void hooks_transition(struct udata *ud, char *user, char *device, int event, char *desc, double wplat, double wplon, double lat, double lon) +void hooks_transition(struct udata *ud, char *user, char *device, int event, char *desc, double wplat, double wplon, double lat, double lon, char *topic, JsonNode *json) { - JsonNode *json = json_mkobject(); - char *topic = "transition"; + JsonNode *j; - json_append_member(json, "_type", json_mkstring("event")); - json_append_member(json, "user", json_mkstring(user)); - json_append_member(json, "device", json_mkstring(device)); - json_append_member(json, "desc", json_mkstring(desc)); + if ((j = json_find_member(json, "_type")) != NULL) { + json_remove_from_parent(j); + } + json_append_member(json, "_type", json_mkstring("transition")); json_append_member(json, "event", event == ENTER ? json_mkstring("enter") : json_mkstring("leave")); + json_append_member(json, "desc", json_mkstring(desc)); json_append_member(json, "wplat", json_mknumber(wplat)); json_append_member(json, "wplon", json_mknumber(wplon)); - json_append_member(json, "lat", json_mknumber(lat)); - json_append_member(json, "lon", json_mknumber(lon)); olog(LOG_DEBUG, "**** Lua hook for %s %s\n", event == ENTER ? "ENTER" : "LEAVE", desc); - do_hook("transition", ud, topic, json); - json_delete(json); + + do_hook("otr_transition", ud, topic, json); } diff --git a/hooks.h b/hooks.h index 090f84c..2a22a0e 100644 --- a/hooks.h +++ b/hooks.h @@ -15,7 +15,7 @@ void hooks_exit(struct luadata *, char *reason); void hooks_hook(struct udata *ud, char *topic, JsonNode *obj); int hooks_norec(struct udata *ud, char *user, char *device, char *payload); JsonNode *hooks_http(struct udata *ud, char *user, char *device, char *payload); -void hooks_transition(struct udata *ud, char *user, char *device, int event, char *desc, double wplat, double wplon, double lat, double lon); +void hooks_transition(struct udata *ud, char *user, char *device, int event, char *desc, double wplat, double wplon, double lat, double lon, char *topic, JsonNode *json); #endif /* WITH_LUA */ diff --git a/recorder.c b/recorder.c index d08ec49..6e56e30 100644 --- a/recorder.c +++ b/recorder.c @@ -1052,7 +1052,7 @@ void handle_message(void *userdata, char *topic, char *payload, size_t payloadle } } - check_fences(ud, UB(username), UB(device), lat, lon, json); + check_fences(ud, UB(username), UB(device), lat, lon, json, topic); cleanup: if (geo) json_delete(geo);