From 48eabc2342d27768056a3d210ff7a48a98716f2e Mon Sep 17 00:00:00 2001 From: Darell Tan Date: Mon, 22 May 2023 22:38:20 +0800 Subject: [PATCH] Mark old last_seen devices as "not responding" Devices that have not received an update since a fixed timeout (24 hrs for now), based on its last_seen time, will be marked as "not responding" in the Home app. With this I can identify which devices are unreachable or dead. Also needed to upgrade hap with the fix for brutella/hap#30, or the entire bridge will stop working. --- bridge.go | 11 +++++++++++ go.mod | 4 ++-- go.sum | 10 ++++------ 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/bridge.go b/bridge.go index 768faf3..8d06eb8 100644 --- a/bridge.go +++ b/bridge.go @@ -410,6 +410,17 @@ func (br *Bridge) AddDevice(dev *Device, acc *accessory.A, mappings []*ExposeMap return nil, 0 } } + + m.Characteristic.ValueRequestFunc = func(req *http.Request) (any, int) { + lastSeenSince := time.Since(brdev.LastSeen) + + errCode := 0 + if lastSeenSince >= Z2M_LAST_SEEN_TIMEOUT { + //log.Printf("dev %s last seen too long ago", name) + errCode = hap.JsonStatusServiceCommunicationFailure + } + return m.Characteristic.Val, errCode + } } br.devices[name] = brdev diff --git a/go.mod b/go.mod index 2a67647..52ed4fc 100644 --- a/go.mod +++ b/go.mod @@ -3,12 +3,12 @@ module hapz2m go 1.20 require ( - github.com/brutella/hap v0.0.26-0.20230424071335-4329b4fdc1c4 + github.com/brutella/hap v0.0.27-0.20230522083132-4ea1de40019c github.com/eclipse/paho.mqtt.golang v1.4.2 ) require ( - github.com/brutella/dnssd v1.2.6 // indirect + github.com/brutella/dnssd v1.2.7 // indirect github.com/go-chi/chi v1.5.4 // indirect github.com/gorilla/websocket v1.4.2 // indirect github.com/miekg/dns v1.1.53 // indirect diff --git a/go.sum b/go.sum index 9d4fdca..9c1985a 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,7 @@ -github.com/brutella/dnssd v1.2.6 h1:/0P13JkHLRzeLQkWRPEn4hJCr4T3NfknIFw3aNPIC34= -github.com/brutella/dnssd v1.2.6/go.mod h1:JoW2sJUrmVIef25G6lrLj7HS6Xdwh6q8WUIvMkkBYXs= -github.com/brutella/hap v0.0.26-0.20230413145315-4d1fbc179bbb h1:Pfh+FPHinTvHjGjvf3xW+IrJl+FL1tAZOlW2l2sWQ2g= -github.com/brutella/hap v0.0.26-0.20230413145315-4d1fbc179bbb/go.mod h1:jq5zNqV0/sUSWnYQ9hBUDDXGfBiZJm1TtnptNw02PfI= -github.com/brutella/hap v0.0.26-0.20230424071335-4329b4fdc1c4 h1:9PsfodvGUvuN15W6lkaeJOl1pegGNcgcgp3F0kRWS5M= -github.com/brutella/hap v0.0.26-0.20230424071335-4329b4fdc1c4/go.mod h1:jq5zNqV0/sUSWnYQ9hBUDDXGfBiZJm1TtnptNw02PfI= +github.com/brutella/dnssd v1.2.7 h1:Uq2NgLzlUz5JWIzcug9xRU6v0UApHrlxbsREA5B1RrY= +github.com/brutella/dnssd v1.2.7/go.mod h1:JoW2sJUrmVIef25G6lrLj7HS6Xdwh6q8WUIvMkkBYXs= +github.com/brutella/hap v0.0.27-0.20230522083132-4ea1de40019c h1:gLVgwrHzMZUn0d1SfAFvsCfHWF12pstyS1erpUE/GZ0= +github.com/brutella/hap v0.0.27-0.20230522083132-4ea1de40019c/go.mod h1:ilKzdnapk5SjRrhedSW1+IMlMCt5P4hR91jlIap33x4= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/eclipse/paho.mqtt.golang v1.4.2 h1:66wOzfUHSSI1zamx7jR6yMEI5EuHnT1G6rNA5PM12m4=