Compare commits

...

4 commits

Author SHA1 Message Date
root 7a8a2ce030 filter invalid topics 2024-12-01 05:35:39 +00:00
root 1b62110914 allow custom root topics 2024-12-01 04:34:59 +00:00
root ec6cb716a6 use SubscribeMultiple 2024-12-01 03:53:45 +00:00
root 77509cb40e remove unnecessary unsubscribe before disconnect 2024-12-01 03:05:50 +00:00
2 changed files with 21 additions and 18 deletions

View file

@ -227,7 +227,17 @@ func main() {
}
// connect to MQTT
client := &meshtastic.MQTTClient{
TopicRegex: regexp.MustCompile(`/2/e/[^/]+/![0-9a-f]+$|/2/map/$`),
Topics: []string{
"msh/+/2/map/",
"msh/+/2/e/+/+",
"msh/+/+/2/map/",
"msh/+/+/2/e/+/+",
"msh/+/+/+/2/map/",
"msh/+/+/+/2/e/+/+",
"msh/+/+/+/+/2/map/",
"msh/+/+/+/+/2/e/+/+",
},
TopicRegex: regexp.MustCompile(`^msh(?:/[^/]+)+/2/(?:e/[^/]+/![0-9a-f]+|map/)$`),
Accept: func(from uint32) bool {
_, found := blocked[from]
return !found

View file

@ -9,7 +9,6 @@ import (
"log"
"os"
"regexp"
"time"
"github.com/brianshea2/meshmap.net/internal/meshtastic/generated"
mqtt "github.com/eclipse/paho.mqtt.golang"
@ -32,11 +31,11 @@ func NewBlockCipher(key []byte) cipher.Block {
}
type MQTTClient struct {
Topics []string
TopicRegex *regexp.Regexp
Accept func(from uint32) bool
BlockCipher cipher.Block
MessageHandler func(from uint32, topic string, portNum generated.PortNum, payload []byte)
topics []string
mqtt.Client
}
@ -57,27 +56,21 @@ func (c *MQTTClient) Connect() error {
return err
}
log.Print("[info] connected")
for i, region := range generated.Config_LoRaConfig_RegionCode_name {
if i == 0 {
continue
topics := make(map[string]byte)
for _, topic := range c.Topics {
topics[topic] = 0
}
topic := "msh/" + region + "/#"
token = c.Subscribe(topic, 0, nil)
token = c.SubscribeMultiple(topics, 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(c.topics...).WaitTimeout(time.Second) {
log.Print("[info] unsubscribed")
}
c.Client.Disconnect(1000)
}
}