mirror of
https://github.com/brianshea2/meshmap.net.git
synced 2025-03-05 21:00:01 -08:00
Compare commits
4 commits
f33853a4dd
...
7a8a2ce030
Author | SHA1 | Date | |
---|---|---|---|
|
7a8a2ce030 | ||
|
1b62110914 | ||
|
ec6cb716a6 | ||
|
77509cb40e |
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
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)
|
||||
topics := make(map[string]byte)
|
||||
for _, topic := range c.Topics {
|
||||
topics[topic] = 0
|
||||
}
|
||||
token = c.SubscribeMultiple(topics, nil)
|
||||
<-token.Done()
|
||||
if err := token.Error(); err != nil {
|
||||
return err
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue