From 4d51380873888fba3d8354b272a8c63fb6390801 Mon Sep 17 00:00:00 2001 From: Darell Tan Date: Fri, 22 Nov 2024 04:01:07 +0800 Subject: [PATCH] Don't start HAP server without any devices This ensures that when iOS reconnects, it doesn't find an empty server and thinks all the devices have been removed, and removes them all locally. --- bridge.go | 5 +++++ cmd/hapz2m/main.go | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/bridge.go b/bridge.go index 1f8af6c..a3f5a9b 100644 --- a/bridge.go +++ b/bridge.go @@ -212,6 +212,11 @@ func (br *Bridge) WaitConfigured() { } } +// Return number of devices added to the bridge. +func (br *Bridge) NumDevices() int { + return len(br.devices) +} + // Connects to the MQTT server. // Blocks until the connection is established, then auto-reconnect logic takes over func (br *Bridge) ConnectMQTT() error { diff --git a/cmd/hapz2m/main.go b/cmd/hapz2m/main.go index 0b39e2b..1cf2948 100644 --- a/cmd/hapz2m/main.go +++ b/cmd/hapz2m/main.go @@ -123,6 +123,10 @@ func main() { }() br.WaitConfigured() + if br.NumDevices() == 0 { + log.Println("No devices added to bridge. Refusing to start.") + return + } log.Println("hapz2m configured. starting HAP server...")