add output for mqtt client

This commit is contained in:
Joel Wetzell
2025-11-22 22:46:00 -06:00
parent 532a37816f
commit 156c6fbe5b
2 changed files with 110 additions and 1 deletions

View File

@@ -5,6 +5,7 @@ import (
"log/slog"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/jwetzell/showbridge-go/internal/processing"
)
type MQTTClient struct {
@@ -104,5 +105,23 @@ func (mc *MQTTClient) Run() error {
}
func (mc *MQTTClient) Output(payload any) error {
return fmt.Errorf("net.mqtt.client output is not implemented")
payloadMessage, ok := payload.(processing.MQTTMessage)
if !ok {
return fmt.Errorf("net.mqtt.client is only able to output MQTTMessage")
}
if mc.client == nil {
return fmt.Errorf("net.mqtt.client client is not setup")
}
if !mc.client.IsConnected() {
return fmt.Errorf("net.mqtt.client is not connected")
}
token := mc.client.Publish(payloadMessage.Topic, payloadMessage.QoS, payloadMessage.Retained, payloadMessage.Payload)
token.Wait()
return token.Error()
}