1
This commit is contained in:
parent
e7c0dc821a
commit
f062cb41d7
3 changed files with 30 additions and 12 deletions
|
|
@ -102,6 +102,11 @@ func hiddenValues(key string, value map[string]any, ch *config.Channel) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
value["webhooks"] = webhooks
|
value["webhooks"] = webhooks
|
||||||
|
case "mqtt":
|
||||||
|
if settings, ok := v.(*config.MQTTSettings); ok {
|
||||||
|
value["username"] = settings.Username.String()
|
||||||
|
value["password"] = settings.Password.String()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
pahomqtt "github.com/eclipse/paho.mqtt.golang"
|
pahomqtt "github.com/eclipse/paho.mqtt.golang"
|
||||||
|
|
@ -86,9 +87,13 @@ func (c *MQTTChannel) Start(ctx context.Context) error {
|
||||||
opts.SetPassword(c.cfg.Password.String())
|
opts.SetPassword(c.cfg.Password.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
firstSubscribe := make(chan error, 1)
|
||||||
|
var once sync.Once
|
||||||
|
|
||||||
opts.SetOnConnectHandler(func(client pahomqtt.Client) {
|
opts.SetOnConnectHandler(func(client pahomqtt.Client) {
|
||||||
logger.InfoC("mqtt", "MQTT connected, subscribing to inbound topic")
|
logger.InfoC("mqtt", "MQTT connected, subscribing to inbound topic")
|
||||||
c.subscribe(client)
|
err := c.subscribe(client)
|
||||||
|
once.Do(func() { firstSubscribe <- err })
|
||||||
})
|
})
|
||||||
|
|
||||||
opts.SetConnectionLostHandler(func(_ pahomqtt.Client, err error) {
|
opts.SetConnectionLostHandler(func(_ pahomqtt.Client, err error) {
|
||||||
|
|
@ -98,12 +103,19 @@ func (c *MQTTChannel) Start(ctx context.Context) error {
|
||||||
client := pahomqtt.NewClient(opts)
|
client := pahomqtt.NewClient(opts)
|
||||||
token := client.Connect()
|
token := client.Connect()
|
||||||
if !token.WaitTimeout(10 * time.Second) {
|
if !token.WaitTimeout(10 * time.Second) {
|
||||||
|
client.Disconnect(250)
|
||||||
return fmt.Errorf("mqtt connect timed out after 10s (broker: %s)", c.cfg.Broker)
|
return fmt.Errorf("mqtt connect timed out after 10s (broker: %s)", c.cfg.Broker)
|
||||||
}
|
}
|
||||||
if err := token.Error(); err != nil {
|
if err := token.Error(); err != nil {
|
||||||
|
client.Disconnect(250)
|
||||||
return fmt.Errorf("mqtt connect failed: %w", err)
|
return fmt.Errorf("mqtt connect failed: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := <-firstSubscribe; err != nil {
|
||||||
|
client.Disconnect(250)
|
||||||
|
return fmt.Errorf("mqtt subscribe failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
c.client = client
|
c.client = client
|
||||||
c.SetRunning(true)
|
c.SetRunning(true)
|
||||||
|
|
||||||
|
|
@ -144,7 +156,7 @@ func (c *MQTTChannel) clientIDFromTopic(topic string) (string, bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// subscribe subscribes to the inbound topic for this agent.
|
// subscribe subscribes to the inbound topic for this agent.
|
||||||
func (c *MQTTChannel) subscribe(client pahomqtt.Client) {
|
func (c *MQTTChannel) subscribe(client pahomqtt.Client) error {
|
||||||
topic := fmt.Sprintf("%s/%s/+/request", c.topicPrefix(), c.cfg.AgentID)
|
topic := fmt.Sprintf("%s/%s/+/request", c.topicPrefix(), c.cfg.AgentID)
|
||||||
token := client.Subscribe(topic, c.qos, func(_ pahomqtt.Client, msg pahomqtt.Message) {
|
token := client.Subscribe(topic, c.qos, func(_ pahomqtt.Client, msg pahomqtt.Message) {
|
||||||
c.handleInbound(msg)
|
c.handleInbound(msg)
|
||||||
|
|
@ -155,9 +167,10 @@ func (c *MQTTChannel) subscribe(client pahomqtt.Client) {
|
||||||
"topic": topic,
|
"topic": topic,
|
||||||
"error": err.Error(),
|
"error": err.Error(),
|
||||||
})
|
})
|
||||||
} else {
|
return err
|
||||||
logger.InfoCF("mqtt", "Subscribed to inbound topic", map[string]any{"topic": topic})
|
|
||||||
}
|
}
|
||||||
|
logger.InfoCF("mqtt", "Subscribed to inbound topic", map[string]any{"topic": topic})
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleInbound processes an inbound MQTT message.
|
// handleInbound processes an inbound MQTT message.
|
||||||
|
|
|
||||||
|
|
@ -517,14 +517,14 @@ type TeamsWebhookTarget struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type MQTTSettings struct {
|
type MQTTSettings struct {
|
||||||
Broker string `json:"broker" yaml:"-" env:"PICOCLAW_CHANNELS_MQTT_BROKER"`
|
Broker string `json:"broker" yaml:"-" env:"PICOCLAW_CHANNELS_MQTT_BROKER"`
|
||||||
AgentID string `json:"agent_id" yaml:"-" env:"PICOCLAW_CHANNELS_MQTT_AGENT_ID"`
|
AgentID string `json:"agent_id" yaml:"-" env:"PICOCLAW_CHANNELS_MQTT_AGENT_ID"`
|
||||||
TopicPrefix string `json:"topic_prefix,omitempty" yaml:"-" env:"PICOCLAW_CHANNELS_MQTT_TOPIC_PREFIX"`
|
TopicPrefix string `json:"topic_prefix,omitempty" yaml:"-" env:"PICOCLAW_CHANNELS_MQTT_TOPIC_PREFIX"`
|
||||||
Username SecureString `json:"username,omitzero" yaml:"username,omitempty" env:"PICOCLAW_CHANNELS_MQTT_USERNAME"`
|
Username SecureString `json:"username,omitzero" yaml:"username,omitempty" env:"PICOCLAW_CHANNELS_MQTT_USERNAME"`
|
||||||
Password SecureString `json:"password,omitzero" yaml:"password,omitempty" env:"PICOCLAW_CHANNELS_MQTT_PASSWORD"`
|
Password SecureString `json:"password,omitzero" yaml:"password,omitempty" env:"PICOCLAW_CHANNELS_MQTT_PASSWORD"`
|
||||||
ClientID string `json:"client_id,omitempty" yaml:"-" env:"PICOCLAW_CHANNELS_MQTT_CLIENT_ID"`
|
ClientID string `json:"client_id,omitempty" yaml:"-" env:"PICOCLAW_CHANNELS_MQTT_CLIENT_ID"`
|
||||||
KeepAlive int `json:"keep_alive,omitempty" yaml:"-" env:"PICOCLAW_CHANNELS_MQTT_KEEP_ALIVE"`
|
KeepAlive int `json:"keep_alive,omitempty" yaml:"-" env:"PICOCLAW_CHANNELS_MQTT_KEEP_ALIVE"`
|
||||||
QoS int `json:"qos,omitempty" yaml:"-" env:"PICOCLAW_CHANNELS_MQTT_QOS"`
|
QoS int `json:"qos,omitempty" yaml:"-" env:"PICOCLAW_CHANNELS_MQTT_QOS"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type HeartbeatConfig struct {
|
type HeartbeatConfig struct {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue