Skip to content
Snippets Groups Projects
Commit 67d96e29 authored by KERDREUX Jerome's avatar KERDREUX Jerome
Browse files

Refactoring in progress

mqttSetup now use config and take a func parameter for the handler
parent 7c43f932
No related branches found
No related tags found
No related merge requests found
......@@ -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)
......
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment