From 8f8de43e880706f1c9f3f1ec78b28ff3577f6430 Mon Sep 17 00:00:00 2001 From: root <165865819+brianshea2@users.noreply.github.com> Date: Sun, 25 Aug 2024 14:40:11 +0000 Subject: [PATCH] subscribe to each region-based topic separately (issue #21) --- internal/meshtastic/mqtt.go | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/internal/meshtastic/mqtt.go b/internal/meshtastic/mqtt.go index 3bde51c..01c414d 100644 --- a/internal/meshtastic/mqtt.go +++ b/internal/meshtastic/mqtt.go @@ -16,8 +16,6 @@ import ( "google.golang.org/protobuf/proto" ) -const RootTopic = "msh/#" - var DefaultKey = []byte{ 0xd4, 0xf1, 0xbb, 0x3a, 0x20, 0x29, 0x07, 0x59, @@ -37,6 +35,7 @@ type MQTTClient struct { TopicRegex *regexp.Regexp BlockCipher cipher.Block MessageHandler func(from uint32, topic string, portNum generated.PortNum, payload []byte) + topics []string mqtt.Client } @@ -57,18 +56,25 @@ func (c *MQTTClient) Connect() error { return err } log.Print("[info] connected") - token = c.Subscribe(RootTopic, 0, nil) - <-token.Done() - if err := token.Error(); err != nil { - return err + for i, region := range generated.Config_LoRaConfig_RegionCode_name { + if i == 0 { + continue + } + topic := "msh/" + region + "/#" + token = c.Subscribe(topic, 0, nil) + <-token.Done() + if err := token.Error(); err != nil { + return err + } + log.Printf("[info] subscribed to %v", topic) + c.topics = append(c.topics, topic) } - log.Print("[info] subscribed") return nil } func (c *MQTTClient) Disconnect() { if c.IsConnected() { - if c.Unsubscribe(RootTopic).WaitTimeout(time.Second) { + if c.Unsubscribe(c.topics...).WaitTimeout(time.Second) { log.Print("[info] unsubscribed") } c.Client.Disconnect(1000)