From 96299467958aaffdbef5cb8ae780d3abeddfcaba Mon Sep 17 00:00:00 2001 From: xengineering Date: Sat, 20 Dec 2025 13:27:42 +0100 Subject: Separate MQTT logic This reduces the coupling between the MQTT-related code and everything else to a single `tx` channel of type `MQTTMessage`. This improves the code quality significantly. --- cache.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'cache.go') diff --git a/cache.go b/cache.go index 31c8ba5..e5a4e4f 100644 --- a/cache.go +++ b/cache.go @@ -3,21 +3,19 @@ package main import ( "fmt" "log" - - mqtt "github.com/eclipse/paho.mqtt.golang" ) type States map[string]bool type Cache struct { - Client mqtt.Client + Tx chan MQTTMessage States map[string]bool } -func NewCache(client mqtt.Client) Cache { +func NewCache(tx chan MQTTMessage) Cache { var cache Cache - cache.Client = client + cache.Tx = tx cache.States = make(States) return cache @@ -42,7 +40,10 @@ func (c *Cache) Update(states States) { } else { payload = []byte("closed") } - _ = c.Client.Publish(topic, QOS, RETAINED, payload) + c.Tx <- MQTTMessage{ + Topic: topic, + Payload: payload, + } } c.States[id] = state } -- cgit v1.2.3-70-g09d2