configurable Lua revgeo name

This commit is contained in:
Jan-Piet Mens 2017-05-17 13:00:12 +02:00
parent a5061d7ae8
commit a00e64fb18
4 changed files with 20 additions and 6 deletions

View File

@ -119,6 +119,12 @@ Running the recorder with the above function (and `_rec` set) could cause it to
{"_type":"location","cog":16,"batt":11,"lat":48.85833,"lon":3.29513,"acc":5,"vel":12,"alt":0,"tid":"JJ","_geoprec":3,"tst":1495010110,"addr":"Some place or other","cc":"JP","beverage":"tea"}
```
The name of the Lua function defaults to `otr_revgeo` but can be modified/set on a per/payload basis by populating the JSON element `_lua` before the payload reaches the recorder. The recorder will use the string contained in that element as name of the Lua function. So, assuming you have a Lua function called `geo2` defined in your Lua script and you wish that to be used, the JSON sent to the Recorder would look like
```json
{"_type":"location","lat":48.85833,"lon":3.29513,"tid":"JJ","_lua":"geo2","tst":1495018728}
```
## Hooklets
After running `otr_hook()`, the Recorder attempts to invoke a Lua function for each of the elements in the extended JSON. If, say, your Lua script contains a function called `hooklet_lat`, it will be invoked every time a `lat` is received as part of the JSON payload. Similarly with `hooklet_addr`, `hooklet_cc`, `hooklet_tst`, etc. These _hooklets_ are invoked with the same parameters as `otr_hook()`.

View File

@ -409,10 +409,11 @@ void hooks_transition(struct udata *ud, char *user, char *device, int event, cha
/*
* If the Lua function otr_revgeo() is defined, invoke that to obtain a result
* of reverse geocoding.
* of reverse geocoding. The function name proper (otr_revgeo) is contained in
* `luafunc'
*/
JsonNode *hook_revgeo(struct udata *ud, char *topic, char *user, char *device, double lat, double lon)
JsonNode *hook_revgeo(struct udata *ud, char *luafunc, char *topic, char *user, char *device, double lat, double lon)
{
struct luadata *ld = ud->luadata;
JsonNode *obj = NULL;
@ -422,9 +423,9 @@ JsonNode *hook_revgeo(struct udata *ud, char *topic, char *user, char *device, d
return (0);
lua_settop(ld->L, 0);
lua_getglobal(ld->L, "otr_revgeo");
lua_getglobal(ld->L, luafunc);
if (lua_type(ld->L, -1) != LUA_TFUNCTION) {
debug(ud, "no otr_revgeo function in Lua file: returning");
debug(ud, "no %s function in Lua file: returning", luafunc);
return (0);
}

View File

@ -16,7 +16,7 @@ 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, char *topic, JsonNode *json, long meters);
JsonNode *hook_revgeo(struct udata *ud, char *topic, char *user, char *device, double lat, double lon);
JsonNode *hook_revgeo(struct udata *ud, char *luafunc, char *topic, char *user, char *device, double lat, double lon);
#endif /* WITH_LUA */

View File

@ -878,7 +878,14 @@ void handle_message(void *userdata, char *topic, char *payload, size_t payloadle
cached = FALSE;
if (ud->revgeo == TRUE) {
#ifdef WITH_LUA
if ((geo = hook_revgeo(ud, topic, UB(username), UB(device), lat, lon)) != NULL) {
char *lua_func = "otr_revgeo";
if ((j = json_find_member(json, "_lua")) != NULL) {
if (j->tag == JSON_STRING) {
lua_func = j->string_;
}
}
if ((geo = hook_revgeo(ud, lua_func, topic, UB(username), UB(device), lat, lon)) != NULL) {
if ((j = json_find_member(geo, "_rec")) != NULL) {
if (j->bool_ == true) {
json_remove_from_parent(j);