geofences now use correct topic and enhanced payload as per Booklet for transition events

This commit is contained in:
Jan-Piet Mens
2016-12-08 18:07:15 +01:00
parent 491a8f6f20
commit e1cd5c39e7
9 changed files with 29 additions and 22 deletions

View File

@@ -35,7 +35,7 @@ Note how the `io` (in / out) element in the JSON indicates whether the last posi
### Transition hook ### 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 ```lua
function otr_init() function otr_init()
@@ -44,8 +44,12 @@ end
function otr_exit() function otr_exit()
end end
function transition(topic, _type, data) function otr_transition(topic, _type, data)
print("IN TRANSITION " .. _type .. " " .. topic) print("IN TRANSITION " .. _type .. " " .. topic)
otr.publish('special/topic', data['event'] .. " " .. data['desc']) otr.publish('special/topic', data['event'] .. " " .. data['desc'])
end 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

View File

@@ -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. 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). See [geo fences](FENCES.md).

View File

@@ -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"); // printf("%s - %s: EVENT == %s\n", wp->user, wp->device, wp->event == 0 ? "ENTER" : "LEAVE");
#ifdef WITH_LUA #ifdef WITH_LUA
if (wp->ud->luadata) { 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 */ #endif /* WITH_LUA */
} }
@@ -78,7 +78,7 @@ static int check_a_waypoint(char *key, wpoint *wp, double lat, double lon)
* the Haversine formula. * 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; 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. * 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);
} }

View File

@@ -13,9 +13,11 @@ typedef struct {
char *device; char *device;
enum { ENTER, LEAVE } event; enum { ENTER, LEAVE } event;
struct udata *ud; struct udata *ud;
char *topic; /* original publish topic */
JsonNode *json; /* full JSON payload of publish */
} wpoint; } 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 #endif

View File

@@ -353,7 +353,7 @@ void gcache_load(char *path, char *lmdbname)
* update the data. * 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_val key, data;
MDB_txn *txn; 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.ud = ud;
wp.user = user; wp.user = user;
wp.device = device; wp.device = device;
wp.topic = topic;
wp.json = jsonpayload;
if (func && func(UB(ks), &wp, lat, lon) == true) { if (func && func(UB(ks), &wp, lat, lon) == true) {
json_remove_from_parent(jio); json_remove_from_parent(jio);

View File

@@ -20,6 +20,6 @@ JsonNode *gcache_json_get(struct gcache *, char *key);
void gcache_dump(char *path, char *lmdbname); void gcache_dump(char *path, char *lmdbname);
void gcache_load(char *path, char *lmdbname); void gcache_load(char *path, char *lmdbname);
int gcache_del(struct gcache *gc, char *keystr); 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 #endif

21
hooks.c
View File

@@ -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 * This hook is invoked through fences.c when we determine that a movement
* into or out of a geofence has caused a transition. * 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(); JsonNode *j;
char *topic = "transition";
json_append_member(json, "_type", json_mkstring("event")); if ((j = json_find_member(json, "_type")) != NULL) {
json_append_member(json, "user", json_mkstring(user)); json_remove_from_parent(j);
json_append_member(json, "device", json_mkstring(device)); }
json_append_member(json, "desc", json_mkstring(desc)); json_append_member(json, "_type", json_mkstring("transition"));
json_append_member(json, "event", json_append_member(json, "event",
event == ENTER ? json_mkstring("enter") : json_mkstring("leave")); 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, "wplat", json_mknumber(wplat));
json_append_member(json, "wplon", json_mknumber(wplon)); 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", olog(LOG_DEBUG, "**** Lua hook for %s %s\n",
event == ENTER ? "ENTER" : "LEAVE", desc); event == ENTER ? "ENTER" : "LEAVE", desc);
do_hook("transition", ud, topic, json);
json_delete(json); do_hook("otr_transition", ud, topic, json);
} }

View File

@@ -15,7 +15,7 @@ void hooks_exit(struct luadata *, char *reason);
void hooks_hook(struct udata *ud, char *topic, JsonNode *obj); void hooks_hook(struct udata *ud, char *topic, JsonNode *obj);
int hooks_norec(struct udata *ud, char *user, char *device, char *payload); int hooks_norec(struct udata *ud, char *user, char *device, char *payload);
JsonNode *hooks_http(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 */ #endif /* WITH_LUA */

View File

@@ -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: cleanup:
if (geo) json_delete(geo); if (geo) json_delete(geo);