From 7c43f9323766b51549cd5695ca03273bba2b685b Mon Sep 17 00:00:00 2001 From: jkerdreux-imt <jerome.kerdreux@imt-atlantique.fr> Date: Sun, 17 Nov 2024 14:59:57 +0100 Subject: [PATCH] Removing mqttTopic gobal in progress --- main.go | 2 +- mqtt.go | 4 ++-- xaal.go | 4 ++-- z2m.go | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index fd439af..8f9ea9b 100644 --- a/main.go +++ b/main.go @@ -15,7 +15,7 @@ func main() { // spew.Dump(cfg) xaal.SetupLogger() - client := mqttSetup(cfg.brokerHost, cfg.brokerPort) + client := mqttSetup(cfg.brokerHost, cfg.brokerPort, cfg.topic) eng := xaal.NewEngine() gw := NewGW(client, cfg) diff --git a/mqtt.go b/mqtt.go index f29b922..399ee2d 100644 --- a/mqtt.go +++ b/mqtt.go @@ -11,7 +11,7 @@ import ( ) // mqttSetup creates a new MQTT client -func mqttSetup(mqttBroker string, port int) MQTT.Client { +func mqttSetup(mqttBroker string, port int, topic string) MQTT.Client { // This JS style of creating a client is awfully verbose opts := MQTT.NewClientOptions(). AddBroker(fmt.Sprintf("tcp://%s:%d", mqttBroker, port)). @@ -23,7 +23,7 @@ func mqttSetup(mqttBroker string, port int) MQTT.Client { panic(token.Error()) } slog.Debug("Connected to", "Broker", mqttBroker, "Port", port, "Client", mqttClientID) - if token := client.Subscribe(mqttTopic+"/#", 0, nil); token.Wait() && token.Error() != nil { + if token := client.Subscribe(topic+"/#", 0, nil); token.Wait() && token.Error() != nil { panic(token.Error()) } return client diff --git a/xaal.go b/xaal.go index 1cd4615..150d79e 100644 --- a/xaal.go +++ b/xaal.go @@ -420,7 +420,7 @@ func (dev *Lamp) setMode(body xaal.MessageBody) *xaal.MessageBody { func NewButtonRemote(addr uuid.UUID, zDev *Z2MDevice, exp *Expose) XAALDeviceInterface { dev := &ButtonRemote{XAALDevice{schemas.NewButtonRemote(addr), zDev, exp}} dev.setup() - dev.SetMethod("get_buttons", dev.GetButtons) + dev.SetMethod("get_buttons", dev.getButtons) return dev } @@ -434,7 +434,7 @@ func (dev *ButtonRemote) update(payload map[string]interface{}) { } } -func (dev *ButtonRemote) GetButtons(xaal.MessageBody) *xaal.MessageBody { +func (dev *ButtonRemote) getButtons(xaal.MessageBody) *xaal.MessageBody { body := make(xaal.MessageBody) action := dev.Z2MDevice.GetExpose(dev.Expose.Name) if action != nil { diff --git a/z2m.go b/z2m.go index 5619364..c7e19bf 100644 --- a/z2m.go +++ b/z2m.go @@ -70,7 +70,7 @@ type Feature struct { // ============================================================================= // returns the topic for the device -func (zDev *Z2MDevice) GetTopic() string { +func (zDev *Z2MDevice) getTopic() string { return mqttTopic + "/" + zDev.FriendlyName } @@ -146,7 +146,7 @@ func (zDev *Z2MDevice) setupXAALDevices(gw *Gateway) { // Publish the payload to the right topic func (zDev *Z2MDevice) Publish(topic string, payload interface{}) { - topic = zDev.GetTopic() + "/" + topic + topic = zDev.getTopic() + "/" + topic slog.Debug("Sending", "topic", topic, "payload", payload) client := GetGW().client if token := client.Publish(topic, 0, false, payload); token.Wait() && token.Error() != nil { -- GitLab