subscribe to each region-based topic separately (issue #21)

This commit is contained in:
root 2024-08-25 14:40:11 +00:00
parent 59b76c9087
commit 8f8de43e88

View file

@ -16,8 +16,6 @@ import (
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
) )
const RootTopic = "msh/#"
var DefaultKey = []byte{ var DefaultKey = []byte{
0xd4, 0xf1, 0xbb, 0x3a, 0xd4, 0xf1, 0xbb, 0x3a,
0x20, 0x29, 0x07, 0x59, 0x20, 0x29, 0x07, 0x59,
@ -37,6 +35,7 @@ type MQTTClient struct {
TopicRegex *regexp.Regexp TopicRegex *regexp.Regexp
BlockCipher cipher.Block BlockCipher cipher.Block
MessageHandler func(from uint32, topic string, portNum generated.PortNum, payload []byte) MessageHandler func(from uint32, topic string, portNum generated.PortNum, payload []byte)
topics []string
mqtt.Client mqtt.Client
} }
@ -57,18 +56,25 @@ func (c *MQTTClient) Connect() error {
return err return err
} }
log.Print("[info] connected") log.Print("[info] connected")
token = c.Subscribe(RootTopic, 0, nil) for i, region := range generated.Config_LoRaConfig_RegionCode_name {
if i == 0 {
continue
}
topic := "msh/" + region + "/#"
token = c.Subscribe(topic, 0, nil)
<-token.Done() <-token.Done()
if err := token.Error(); err != nil { if err := token.Error(); err != nil {
return err return err
} }
log.Print("[info] subscribed") log.Printf("[info] subscribed to %v", topic)
c.topics = append(c.topics, topic)
}
return nil return nil
} }
func (c *MQTTClient) Disconnect() { func (c *MQTTClient) Disconnect() {
if c.IsConnected() { if c.IsConnected() {
if c.Unsubscribe(RootTopic).WaitTimeout(time.Second) { if c.Unsubscribe(c.topics...).WaitTimeout(time.Second) {
log.Print("[info] unsubscribed") log.Print("[info] unsubscribed")
} }
c.Client.Disconnect(1000) c.Client.Disconnect(1000)