From 421cd5b944576499a4003c3e68cd9eefe69a5749 Mon Sep 17 00:00:00 2001 From: Jan-Piet Mens Date: Sun, 28 Jan 2024 14:04:21 +0100 Subject: [PATCH] clarify and add a Lua http->mqtt republisher --- doc/HOOKS.md | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/doc/HOOKS.md b/doc/HOOKS.md index 2f35e66..5a38e23 100644 --- a/doc/HOOKS.md +++ b/doc/HOOKS.md @@ -94,7 +94,7 @@ See [geo fences](FENCES.md). If the user-defined `otr_revgeo()` function exists in the Lua script, it is invoked by the Recorder to obtain reverse-geo data on a publish. (Unless Recorder was launched with `--norevgeo` in which case no such information is gathered at all.) -`otr_revgeo()` overrides the built-in Google reverse-geo lookups. So, to clarify, if this function is defined, Google lookups will not be attempted. +`otr_revgeo()` overrides the built-in Google reverse-geo lookups. So, to clarify, if this function is defined, none of the default reverse-geo lookups will be attempted. The function is invoked with _topic_, _user_, _device_, _lat_, and _lon_, and it should return a table with at least `cc` and `addr` populated. Any other elements in the table are added and passed on to, say, the Websocket interface. If the element `_rec` is defined in the table and its value is _true_, the data will be merged into the payload stored in the REC file. @@ -162,4 +162,31 @@ function otr_hook(topic, _type, data) end ``` -This function allows your code to publish via MQTT from the Recorder, using the same MQTT connection (including TLS, authentication, etc) as the Recorder uses. +This function allows your code to publish via MQTT from the Recorder, using the same MQTT connection (including TLS, authentication, etc) as the Recorder uses. Note also, that the Recorder's MQTT connection defines the ACL in use. + +The following example republishes messages received via HTTP to MQTT: + +```lua +JSON = (loadfile "JSON.lua")() -- http://regex.info/blog/lua/json + +function otr_init() +end + +function otr_exit() +end + +function otr_hook(topic, _type, data) + -- republish http messages to MQTT + if data['_http'] then + data["_http"] = nil + if data["topic"] ~= nil then + topic = data['topic'] + data["tst"] = math.floor(data["tst"]) + data["_lua"] = true + local payload = JSON:encode(data) + + otr.publish(topic, payload, 1, 0) + end + end +end +```