mqttagent

MQTT Lua Agent
git clone https://git.instinctive.eu/mqttagent.git
Log | Files | Refs | README | LICENSE

commit edbee44ed701bc9eadf3de59c61353ee3b10c425
parent 5f8347ada6f0e1a436bd12d623e69861935c3b95
Author: Natasha Kerensikova <natgh@instinctive.eu>
Date:   Sun, 19 Jan 2025 22:50:44 +0000

MQTT ping is exposed to Lua scripts
Diffstat:
Mmqttagent.go | 19+++++++++++++++----
1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/mqttagent.go b/mqttagent.go @@ -369,13 +369,24 @@ func deleteMqttClient(L *lua.LState) int { func luaPublish(L *lua.LState) int { cnx := L.CheckTable(1) - message := L.CheckString(2) - topic := L.CheckString(3) client := L.RawGetInt(cnx, keyClient).(*lua.LUserData).Value.(*mqtt.Client) - err := client.Publish(nil, []byte(message), topic) + if L.GetTop() == 1 { + if err := client.Ping(nil); err != nil { + log.Println("luaPing:", err) + L.Push(lua.LNil) + L.Push(lua.LString(err.Error())) + return 2 + } else { + L.Push(lua.LTrue) + return 1 + } + } - if err != nil { + message := L.CheckString(2) + topic := L.CheckString(3) + + if err := client.Publish(nil, []byte(message), topic); err != nil { L.Push(lua.LNil) L.Push(lua.LString(err.Error())) return 2