From 67d96e298ce5c23dc20ed31a2f26792bef3f2d7a Mon Sep 17 00:00:00 2001 From: jkerdreux-imt <jerome.kerdreux@imt-atlantique.fr> Date: Mon, 18 Nov 2024 11:02:56 +0100 Subject: [PATCH] Refactoring in progress mqttSetup now use config and take a func parameter for the handler --- main.go | 2 +- mqtt.go | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 8f9ea9b..a2fccbc 100644 --- a/main.go +++ b/main.go @@ -15,7 +15,7 @@ func main() { // spew.Dump(cfg) xaal.SetupLogger() - client := mqttSetup(cfg.brokerHost, cfg.brokerPort, cfg.topic) + client := mqttSetup(cfg, mqttPublishHander) eng := xaal.NewEngine() gw := NewGW(client, cfg) diff --git a/mqtt.go b/mqtt.go index 399ee2d..05288f5 100644 --- a/mqtt.go +++ b/mqtt.go @@ -11,19 +11,19 @@ import ( ) // mqttSetup creates a new MQTT client -func mqttSetup(mqttBroker string, port int, topic string) MQTT.Client { +func mqttSetup(cfg *Config, publishHandler MQTT.MessageHandler) MQTT.Client { // This JS style of creating a client is awfully verbose opts := MQTT.NewClientOptions(). - AddBroker(fmt.Sprintf("tcp://%s:%d", mqttBroker, port)). + AddBroker(fmt.Sprintf("tcp://%s:%d", cfg.brokerHost, cfg.brokerPort)). SetClientID(mqttClientID). - SetDefaultPublishHandler(mqttPublishHander).SetAutoReconnect(true) + SetDefaultPublishHandler(publishHandler).SetAutoReconnect(true) client := MQTT.NewClient(opts) if token := client.Connect(); token.Wait() && token.Error() != nil { panic(token.Error()) } - slog.Debug("Connected to", "Broker", mqttBroker, "Port", port, "Client", mqttClientID) - if token := client.Subscribe(topic+"/#", 0, nil); token.Wait() && token.Error() != nil { + slog.Debug("Connected to", "Broker", cfg.brokerHost, "Port", cfg.brokerPort, "Client", mqttClientID) + if token := client.Subscribe(cfg.topic+"/#", 0, nil); token.Wait() && token.Error() != nil { panic(token.Error()) } return client -- GitLab