Compare commits

..

No commits in common. "7a8a2ce030e89f5efddaa319a06a3fd7b35a12d5" and "f33853a4ddf969595826d9f47a93e63875dc0930" have entirely different histories.

2 changed files with 18 additions and 21 deletions

View file

@ -227,17 +227,7 @@ func main() {
} }
// connect to MQTT // connect to MQTT
client := &meshtastic.MQTTClient{ client := &meshtastic.MQTTClient{
Topics: []string{ TopicRegex: regexp.MustCompile(`/2/e/[^/]+/![0-9a-f]+$|/2/map/$`),
"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,6 +9,7 @@ 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"
@ -31,11 +32,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
} }
@ -56,21 +57,27 @@ func (c *MQTTClient) Connect() error {
return err return err
} }
log.Print("[info] connected") log.Print("[info] connected")
topics := make(map[string]byte) for i, region := range generated.Config_LoRaConfig_RegionCode_name {
for _, topic := range c.Topics { if i == 0 {
topics[topic] = 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)
} }
token = c.SubscribeMultiple(topics, nil)
<-token.Done()
if err := token.Error(); err != nil {
return err
}
log.Print("[info] subscribed")
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)
} }
} }