add support for barometric pressure

This commit is contained in:
2026-01-29 14:04:18 +11:00
parent 7a0081b2ed
commit 5d07c5d54b
9 changed files with 401 additions and 51 deletions

View File

@@ -15,6 +15,12 @@ type MQTTConfig struct {
Password string
Topic string
QoS byte
Topics []Subscription
}
type Subscription struct {
Topic string
QoS byte
}
type Handler func(ctx context.Context, topic string, payload []byte) error
@@ -38,7 +44,22 @@ func RunSubscriber(ctx context.Context, cfg MQTTConfig, h Handler) error {
}
// Subscribe
if tok := client.Subscribe(cfg.Topic, cfg.QoS, func(_ mqtt.Client, msg mqtt.Message) {
subs := map[string]byte{}
if len(cfg.Topics) > 0 {
for _, sub := range cfg.Topics {
if sub.Topic == "" {
continue
}
subs[sub.Topic] = sub.QoS
}
} else if cfg.Topic != "" {
subs[cfg.Topic] = cfg.QoS
}
if len(subs) == 0 {
return fmt.Errorf("mqtt subscribe: no topics configured")
}
if tok := client.SubscribeMultiple(subs, func(_ mqtt.Client, msg mqtt.Message) {
// Keep callback short; do work with context
_ = h(ctx, msg.Topic(), msg.Payload())
}); tok.Wait() && tok.Error() != nil {