diff options
| author | xengineering <me@xengineering.eu> | 2025-12-20 13:47:56 +0100 |
|---|---|---|
| committer | xengineering <me@xengineering.eu> | 2025-12-20 13:47:56 +0100 |
| commit | 6978bf0635372b0330629469fb86ddb640cdd863 (patch) | |
| tree | 5c35b936a96c7383b85786fac39ea68cf3b96b98 /main.go | |
| parent | 58d7c51baa053be8d6d4ec5f409fceef1c7c11b5 (diff) | |
| download | sia-server-6978bf0635372b0330629469fb86ddb640cdd863.tar sia-server-6978bf0635372b0330629469fb86ddb640cdd863.tar.zst sia-server-6978bf0635372b0330629469fb86ddb640cdd863.zip | |
Separate Homematic code
Similar to a previous refactoring of the MQTT-related code this removes
all Homematic logic to a dedicated file.
The only connection to the outside is the `tx chan MQTTMessage` channel
and the `HomematicRun()` function.
This makes the code more modular.
Diffstat (limited to 'main.go')
| -rw-r--r-- | main.go | 75 |
1 files changed, 3 insertions, 72 deletions
@@ -1,86 +1,24 @@ package main import ( - "fmt" "log" "os" "os/signal" "syscall" - "time" - - "xengineering.eu/homematic-go/homematic" -) - -const ( - OPENCCU = `http://127.0.0.1:8080` - POLLING_PERIOD = 50 * time.Millisecond ) func main() { log.Println("+++ Started Sia server +++") defer log.Println("--- Stopped Sia server ---") - go func() { - tx := make(chan MQTTMessage) - go MQTTRun(tx) - - req, inventory, err := Start() - if err != nil { - log.Fatalf("Failed startup process: %v", err) - } - - cache := NewCache(tx) - - for { - start := time.Now() + tx := make(chan MQTTMessage) - states, err := Poll(req, inventory) - if err != nil { - log.Fatalf("Failed to poll states: %v", err) - } - - cache.Update(states) - - WaitUntil(start.Add(POLLING_PERIOD)) - } - }() + go MQTTRun(tx) + go HomematicRun(tx) Await(syscall.SIGTERM, syscall.SIGINT) } -func Start() (homematic.Requester, homematic.Devices, error) { - var req homematic.Requester - var inventory homematic.Devices - var err error - - req = homematic.NewRequester(OPENCCU) - log.Printf("Created Homematic requester (%s).", OPENCCU) - - inventory, err = req.ListDevices() - if err != nil { - return req, inventory, fmt.Errorf("Failed getting initial device list: %w", err) - } - log.Printf("Retrieved Homematic inventory with %d devices.", len(inventory)) - - return req, inventory, nil -} - -func Poll(req homematic.Requester, inventory homematic.Devices) (States, error) { - states := make(States) - - for _, device := range inventory { - if device.Type == `SHUTTER_CONTACT` { - state, err := req.GetValue(device.Address) - if err != nil { - return states, fmt.Errorf("Failed to get value: %w", err) - } - states[device.Address] = state - } - } - - return states, nil -} - func Await(signals ...syscall.Signal) { listener := make(chan os.Signal) for _, s := range signals { @@ -89,10 +27,3 @@ func Await(signals ...syscall.Signal) { sig := <-listener log.Printf("Received OS signal '%v'\n", sig) } - -func WaitUntil(deadline time.Time) { - duration := time.Until(deadline) - if duration > 0 { - time.Sleep(duration) - } -} |
