graphicek/server/integrations/mqtt.go

28 lines
649 B
Go
Raw Normal View History

2024-04-01 19:42:20 +02:00
package integrations
2024-04-01 17:26:30 +02:00
import (
MQTT "github.com/eclipse/paho.mqtt.golang"
)
2024-04-01 19:42:20 +02:00
type MQTTIntegration struct{}
2024-04-01 17:26:30 +02:00
2024-04-01 19:42:20 +02:00
func (s *MQTTIntegration) Publish(server string, username string, password string, clientId string, retain bool, qos byte, topic string, message string) error {
2024-04-01 17:26:30 +02:00
opts := MQTT.NewClientOptions()
opts.AddBroker(server)
opts.SetUsername(username)
opts.SetPassword(password)
opts.SetClientID(clientId)
client := MQTT.NewClient(opts)
if token := client.Connect(); token.Wait() && token.Error() != nil {
return token.Error()
}
token := client.Publish(topic, qos, retain, message)
token.Wait()
client.Disconnect(250)
return nil
}