summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2025-12-20 12:51:01 +0100
committerxengineering <me@xengineering.eu>2025-12-20 12:51:01 +0100
commitd5b189640ccbd8ed14154fe0f21712194c8e319d (patch)
tree84f3e0d7a2b8656c3a7e72481f758e2249ae0558 /main.go
parentfeced16cf361a085de9a6f97d7491412261cf8b8 (diff)
downloadsia-server-d5b189640ccbd8ed14154fe0f21712194c8e319d.tar
sia-server-d5b189640ccbd8ed14154fe0f21712194c8e319d.tar.zst
sia-server-d5b189640ccbd8ed14154fe0f21712194c8e319d.zip
Refactor with Poll() function
This function allows to reduce the lines of code in main() further.
Diffstat (limited to 'main.go')
-rw-r--r--main.go31
1 files changed, 20 insertions, 11 deletions
diff --git a/main.go b/main.go
index 67af965..f0dfb95 100644
--- a/main.go
+++ b/main.go
@@ -30,17 +30,9 @@ func main() {
log.Fatalf("Failed startup process: %v", err)
}
- for _, device := range inventory {
- if device.Type == `SHUTTER_CONTACT` {
- state, err := req.GetValue(device.Address)
- if err != nil {
- log.Fatalf("Failed to get value: %v", err)
- }
-
- topic := fmt.Sprintf("%s/contact/%s/state", TOPIC_PREFIX, device.Address)
- payload := []byte(fmt.Sprintf("%t", state))
- client.Publish(topic, QOS, RETAINED, payload)
- }
+ err = Poll(req, inventory, client)
+ if err != nil {
+ log.Fatalf("Failed to poll states: %v", err)
}
}
@@ -95,3 +87,20 @@ func Stop(c mqtt.Client) {
c.Disconnect(MQTT_DISCONNECT_TIMEOUT_US)
log.Println("Disconnected from MQTT broker.")
}
+
+func Poll(req homematic.Requester, inventory homematic.Devices, client mqtt.Client) error {
+ for _, device := range inventory {
+ if device.Type == `SHUTTER_CONTACT` {
+ state, err := req.GetValue(device.Address)
+ if err != nil {
+ return fmt.Errorf("Failed to get value: %w", err)
+ }
+
+ topic := fmt.Sprintf("%s/contact/%s/state", TOPIC_PREFIX, device.Address)
+ payload := []byte(fmt.Sprintf("%t", state))
+ _ = client.Publish(topic, QOS, RETAINED, payload)
+ }
+ }
+
+ return nil
+}