summaryrefslogtreecommitdiff
path: root/main.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 /main.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 'main.go')
-rw-r--r--main.go57
1 files changed, 8 insertions, 49 deletions
diff --git a/main.go b/main.go
index 06ace4a..722bd40 100644
--- a/main.go
+++ b/main.go
@@ -8,19 +8,11 @@ import (
"syscall"
"time"
- mqtt "github.com/eclipse/paho.mqtt.golang"
"xengineering.eu/homematic-go/homematic"
)
const (
OPENCCU = `http://127.0.0.1:8080`
- BROKER = `tcp://127.0.0.1:1883`
- CLIENT_ID = `siaserver`
- TOPIC_PREFIX = `sia`
- QOS = byte(1)
- RETAINED = true
- MQTT_CONNECT_TIMEOUT = time.Second * 5
- MQTT_DISCONNECT_TIMEOUT_US = 500
POLLING_PERIOD = 50 * time.Millisecond
)
@@ -29,13 +21,15 @@ func main() {
defer log.Println("--- Stopped Sia server ---")
go func() {
- req, inventory, client, err := Start()
- defer Stop(client)
+ tx := make(chan MQTTMessage)
+ go MQTTRun(tx)
+
+ req, inventory, err := Start()
if err != nil {
log.Fatalf("Failed startup process: %v", err)
}
- cache := NewCache(client)
+ cache := NewCache(tx)
for {
start := time.Now()
@@ -54,33 +48,9 @@ func main() {
Await(syscall.SIGTERM, syscall.SIGINT)
}
-func ConnectMQTT(broker string, id string) (mqtt.Client, error) {
- opts := mqtt.NewClientOptions()
- opts.AddBroker(broker)
- opts.SetClientID(id)
- opts.SetCleanSession(true)
-
- client := mqtt.NewClient(opts)
-
- token := client.Connect()
-
- success := token.WaitTimeout(MQTT_CONNECT_TIMEOUT)
- if !success {
- return client, fmt.Errorf("Timed out after %v.", MQTT_CONNECT_TIMEOUT)
- }
-
- err := token.Error()
- if err != nil {
- return client, err
- }
-
- return client, nil
-}
-
-func Start() (homematic.Requester, homematic.Devices, mqtt.Client, error) {
+func Start() (homematic.Requester, homematic.Devices, error) {
var req homematic.Requester
var inventory homematic.Devices
- var client mqtt.Client
var err error
req = homematic.NewRequester(OPENCCU)
@@ -88,22 +58,11 @@ func Start() (homematic.Requester, homematic.Devices, mqtt.Client, error) {
inventory, err = req.ListDevices()
if err != nil {
- return req, inventory, client, fmt.Errorf("Failed getting initial device list: %w", err)
+ return req, inventory, fmt.Errorf("Failed getting initial device list: %w", err)
}
log.Printf("Retrieved Homematic inventory with %d devices.", len(inventory))
- client, err = ConnectMQTT(BROKER, CLIENT_ID)
- if err != nil {
- return req, inventory, client, fmt.Errorf("Failed connecting to MQTT broker: %w", err)
- }
- log.Printf("Connected to MQTT broker (%s).", BROKER)
-
- return req, inventory, client, nil
-}
-
-func Stop(c mqtt.Client) {
- c.Disconnect(MQTT_DISCONNECT_TIMEOUT_US)
- log.Println("Disconnected from MQTT broker.")
+ return req, inventory, nil
}
func Poll(req homematic.Requester, inventory homematic.Devices) (States, error) {