mqttagent

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

commit 2c7a0443390b47f9f02e902f884ecd24ac0354c0
parent c227f20ce4516e36764537cb958b0a5148ff1eab
Author: Natasha Kerensikova <natgh@instinctive.eu>
Date:   Thu,  6 Feb 2025 18:30:24 +0000

Re-subscription is skipped when updating a callback
Diffstat:
Mmqttagent.go | 19++++++++++++++-----
1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/mqttagent.go b/mqttagent.go @@ -410,10 +410,13 @@ func luaSubscribe(L *lua.LState) int { topic := L.CheckString(2) callback := L.OptFunction(3, nil) client := L.RawGetInt(cnx, keyClient).(*lua.LUserData).Value.(*mqtt.Client) + tbl := L.RawGetInt(cnx, keySubTable).(*lua.LTable) + + _, is_new := L.GetField(tbl, topic).(*lua.LNilType) if callback == nil { err = client.Unsubscribe(nil, topic) - } else { + } else if is_new { err = client.Subscribe(nil, topic) } @@ -423,13 +426,19 @@ func luaSubscribe(L *lua.LState) int { L.Push(lua.LString(err.Error())) return 2 } else { - tbl := L.RawGetInt(cnx, keySubTable).(*lua.LTable) - if callback == nil { - log.Println("Unsubscribed from", topic) + if is_new { + log.Printf("Not subscribed to %q", topic) + } else { + log.Printf("Unsubscribed from %q", topic) + } L.SetField(tbl, topic, lua.LNil) } else { - log.Println("Subscribed to", topic) + if is_new { + log.Printf("Subscribed to %q", topic) + } else { + log.Printf("Updating subscription to %q", topic) + } L.SetField(tbl, topic, callback) }