summaryrefslogtreecommitdiff
path: root/cache.go
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2025-12-20 13:27:42 +0100
committerxengineering <me@xengineering.eu>2025-12-20 13:27:42 +0100
commit96299467958aaffdbef5cb8ae780d3abeddfcaba (patch)
tree95e3ff2ccb6bdaffe0f1afedebb6794b72cd6865 /cache.go
parentfaf9fb7a8c5e8b31e9c1104b42d7e550e986ca61 (diff)
downloadsia-server-96299467958aaffdbef5cb8ae780d3abeddfcaba.tar
sia-server-96299467958aaffdbef5cb8ae780d3abeddfcaba.tar.zst
sia-server-96299467958aaffdbef5cb8ae780d3abeddfcaba.zip
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.
Diffstat (limited to 'cache.go')
-rw-r--r--cache.go13
1 files changed, 7 insertions, 6 deletions
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
}