feat(hksv): add motion detection API and enhance server handling for consumers

This commit is contained in:
Sergey Krashevich
2026-03-07 18:54:50 +03:00
parent 9c901dc995
commit e3d1085a6d
6 changed files with 143 additions and 38 deletions
+3
View File
@@ -465,6 +465,9 @@ detector.Stop()
### Motion Control
```go
// Check current motion status
detected := srv.MotionDetected()
// Trigger motion detected (for "api" mode or external sensors)
srv.SetMotionDetected(true)
+19 -3
View File
@@ -107,8 +107,8 @@ type Config struct {
// Dependencies (injected by host)
Streams StreamProvider
Store PairingStore // optional, nil = no persistence
Snapshots SnapshotProvider // optional, nil = no snapshots
Store PairingStore // optional, nil = no persistence
Snapshots SnapshotProvider // optional, nil = no snapshots
LiveStream LiveStreamHandler // optional, nil = HKSV only (no live streaming)
Logger zerolog.Logger
@@ -497,8 +497,11 @@ func (s *Server) SetCharacteristic(conn net.Conn, aid uint8, iid uint64, value a
resp, err := s.liveStream.SetupEndpoints(conn, &offer)
if err != nil {
s.log.Error().Err(err).Msg("[hksv] setup endpoints failed")
return
}
_ = resp // stored by the handler
// Keep the latest response in characteristic value for write-response (r=true)
// and subsequent GET /characteristics reads.
char.Value = resp
case camera.TypeSelectedStreamConfiguration:
if s.liveStream == nil {
@@ -608,6 +611,19 @@ func (s *Server) SetMotionDetected(detected bool) {
s.log.Debug().Str("stream", s.stream).Bool("motion", detected).Msg("[hksv] motion")
}
// MotionDetected returns the current motion detected state.
func (s *Server) MotionDetected() bool {
if s.accessory == nil {
return false
}
char := s.accessory.GetCharacter("22") // MotionDetected
if char == nil {
return false
}
v, _ := char.Value.(bool)
return v
}
// TriggerDoorbell triggers a doorbell press event.
func (s *Server) TriggerDoorbell() {
if s.accessory == nil {
+22 -20
View File
@@ -77,6 +77,16 @@ func ServerHandler(server Server) HandlerFunc {
}
var writeResponses []hap.JSONCharacter
findChar := func(aid uint8, iid uint64) *hap.Character {
accs := server.GetAccessories(conn)
for _, acc := range accs {
if acc.AID != aid {
continue
}
return acc.GetCharacterByID(iid)
}
return nil
}
for _, c := range v.Value {
if c.Value != nil {
@@ -84,31 +94,23 @@ func ServerHandler(server Server) HandlerFunc {
}
if c.Event != nil {
// subscribe/unsubscribe to events
accs := server.GetAccessories(conn)
for _, acc := range accs {
if char := acc.GetCharacterByID(c.IID); char != nil {
if ev, ok := c.Event.(bool); ok && ev {
char.AddListener(conn)
} else {
char.RemoveListener(conn)
}
break
if char := findChar(c.AID, c.IID); char != nil {
if ev, ok := c.Event.(bool); ok && ev {
char.AddListener(conn)
} else {
char.RemoveListener(conn)
}
}
}
if c.R != nil && *c.R {
// write-response: return updated value
accs := server.GetAccessories(conn)
for _, acc := range accs {
if char := acc.GetCharacterByID(c.IID); char != nil {
writeResponses = append(writeResponses, hap.JSONCharacter{
AID: c.AID,
IID: c.IID,
Status: 0,
Value: char.Value,
})
break
}
if char := findChar(c.AID, c.IID); char != nil {
writeResponses = append(writeResponses, hap.JSONCharacter{
AID: c.AID,
IID: c.IID,
Status: 0,
Value: char.Value,
})
}
}
}