From 6067145c539b9f62ce2b183c4566ae2b007667b7 Mon Sep 17 00:00:00 2001
From: jkerdreux-imt <jerome.kerdreux@imt-atlantique.fr>
Date: Mon, 18 Nov 2024 17:47:21 +0100
Subject: [PATCH] Refactoring in progress

ignoredTopics are now in Config ;)
It's time to remove the Gateway singleton ?
---
 config.go  | 29 ++++++++++++++++++++++-------
 gateway.go |  3 +--
 z2m.go     | 10 ----------
 3 files changed, 23 insertions(+), 19 deletions(-)

diff --git a/config.go b/config.go
index b5a0469..8d46475 100644
--- a/config.go
+++ b/config.go
@@ -1,22 +1,34 @@
 package main
 
 import (
+	"fmt"
+
 	"gitlab.imt-atlantique.fr/xaal/code/go/core/uuid"
 	"gitlab.imt-atlantique.fr/xaal/code/go/core/xaal"
 	"gopkg.in/ini.v1"
 )
 
 var (
-	mqttTopic    = "zigbee2mqtt"
-	mqttClientID = "z2m-" + uuid.New().String()
+	mqttTopic     = "zigbee2mqtt"
+	mqttClientID  = "z2m-" + uuid.New().String()
+	ignoredTopics = []string{
+		"bridge/groups",
+		"bridge/definitions",
+		"bridge/extensions",
+		"bridge/info",
+		"bridge/state",
+		"bridge/logging",
+		"bridge/config",
+	}
 )
 
 type Config struct {
-	brokerHost string
-	topic      string
-	logLevel   string
-	brokerPort int
-	baseAddr   uuid.UUID
+	brokerHost    string
+	topic         string
+	logLevel      string
+	ignoredTopics []string
+	brokerPort    int
+	baseAddr      uuid.UUID
 }
 
 func parseConfig() (*Config, error) {
@@ -64,5 +76,8 @@ func parseConfig() (*Config, error) {
 		}
 		config.baseAddr = baseAddr
 	}
+	for _, topic := range ignoredTopics {
+		config.ignoredTopics = append(config.ignoredTopics, fmt.Sprintf("%s/%s", config.topic, topic))
+	}
 	return &config, nil
 }
diff --git a/gateway.go b/gateway.go
index 2d73965..6bb8dd6 100644
--- a/gateway.go
+++ b/gateway.go
@@ -60,10 +60,9 @@ func (gw *Gateway) GetZDeviceByTopic(topic string) *Z2MDevice {
 // Else it will find the device with the topic and call the mqttDeviceHandler
 func (gw *Gateway) mqttPublishHander(client MQTT.Client, msg MQTT.Message) {
 	// we ignore some topics
-	if slices.Contains(ignoredTopics, msg.Topic()) {
+	if slices.Contains(gw.config.ignoredTopics, msg.Topic()) {
 		return
 	}
-	// slog.Debug("Received message on", "topic", msg.Topic())
 	// Is it devices definitions ?
 	if msg.Topic() == gw.config.topic+"/bridge/devices" {
 		gw.jsonParseDevices(msg.Payload())
diff --git a/z2m.go b/z2m.go
index e0fa57c..d403d41 100644
--- a/z2m.go
+++ b/z2m.go
@@ -23,16 +23,6 @@ type (
 	AccessLevel int
 )
 
-var ignoredTopics = []string{
-	mqttTopic + "/bridge/groups",
-	mqttTopic + "/bridge/definitions",
-	mqttTopic + "/bridge/extensions",
-	mqttTopic + "/bridge/info",
-	mqttTopic + "/bridge/state",
-	mqttTopic + "/bridge/logging",
-	mqttTopic + "/bridge/config",
-}
-
 // JSON structures for the z2m device from /bridge/devices
 type Z2MDevice struct {
 	Definition struct {
-- 
GitLab