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. --- main.go | 57 ++++++++------------------------------------------------- 1 file changed, 8 insertions(+), 49 deletions(-) (limited to 'main.go') 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) { -- cgit v1.2.3-70-g09d2