Implemented additional logic req
All checks were successful
Build Docker image / build (push) Successful in 1m2s

This commit is contained in:
Pieter Hollander 2024-02-17 18:56:09 +01:00
parent 5be69f9baf
commit 8673fabccd
Signed by: pieter
SSH key fingerprint: SHA256:HbX+9cBXsop9SuvL+mELd29sK+7DehFfdVweFVDtMSg

16
main.go
View file

@ -67,6 +67,16 @@ func main() {
opts.SetUsername(os.Getenv("MQTT_USERNAME")) opts.SetUsername(os.Getenv("MQTT_USERNAME"))
opts.SetPassword(os.Getenv("MQTT_PASSWORD")) opts.SetPassword(os.Getenv("MQTT_PASSWORD"))
opts.SetAutoReconnect(true) opts.SetAutoReconnect(true)
opts.SetConnectionLostHandler(func(client mqtt.Client, err error) {
log.Printf("Connection lost: %v", err)
})
opts.SetOnConnectHandler(func(client mqtt.Client) {
topic := os.Getenv("MQTT_TOPIC")
log.Println("Connected to MQTT broker, subscribing to topic...")
if token := client.Subscribe(topic, 0, mqttMessageHandler); token.Wait() && token.Error() != nil {
log.Printf("Error subscribing to MQTT topic %s: %v", topic, token.Error())
}
})
// Connect to MQTT broker // Connect to MQTT broker
client := mqtt.NewClient(opts) client := mqtt.NewClient(opts)
@ -74,12 +84,6 @@ func main() {
log.Fatal(token.Error()) log.Fatal(token.Error())
} }
// Subscribe to MQTT topic
topic := os.Getenv("MQTT_TOPIC")
if token := client.Subscribe(topic, 0, mqttMessageHandler); token.Wait() && token.Error() != nil {
log.Fatal(token.Error())
}
// Keep the program running // Keep the program running
select {} select {}
} }