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 // connect to MQTT
client := &meshtastic.MQTTClient{ 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 { Accept: func(from uint32) bool {
_, found := blocked[from] _, found := blocked[from]
return !found return !found

View file

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