From 24bac709573fdcb625df46d5c7f339d1e188cce6 Mon Sep 17 00:00:00 2001 From: Laurent Le Houerou Date: Mon, 8 Jun 2020 20:19:08 +0400 Subject: [PATCH] wip motion detection --- basestation.go | 4 +++- camera.go | 25 +++++++++++++++++++++++++ cmd/main.go | 14 ++++++++++---- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/basestation.go b/basestation.go index 577f951..0da5da9 100644 --- a/basestation.go +++ b/basestation.go @@ -158,7 +158,9 @@ func (b *Basestation) IsConnected() bool { } func (b *Basestation) Subscribe(ctx context.Context) error { - b.eventStream = newEventStream(BaseUrl+fmt.Sprintf(NotifyResponsesPushServiceUri, b.arlo.Account.Token), &http.Client{Jar: b.arlo.client.GetClient().Jar}) + b.eventStream = newEventStream( + BaseUrl+fmt.Sprintf(NotifyResponsesPushServiceUri, b.arlo.Account.Token), + &http.Client{Jar: b.arlo.client.GetClient().Jar}) connectedChan, err := b.eventStream.listen(ctx) if err != nil { diff --git a/camera.go b/camera.go index f4fff10..194c112 100644 --- a/camera.go +++ b/camera.go @@ -2,7 +2,10 @@ package arlo import ( "context" + "encoding/json" "fmt" + + log "github.com/sirupsen/logrus" ) // A Camera is a Device of type "camera". @@ -268,3 +271,25 @@ func (c *Camera) SetAlertNotificationMethods(ctx context.Context, action string, } return b.makeEventStreamRequest(ctx, payload) } + +func (c *Camera) SubscribeToMotionDetection(ctx context.Context) (chan bool, error) { + b := c.arlo.Basestations.Find(c.ParentId) + if b == nil { + return nil, fmt.Errorf("basestation (%s) not found for camera (%s)", c.ParentId, c.DeviceId) + } + out := make(chan bool) + respChan := b.eventStream.subscribeResource(fmt.Sprintf("cameras/%s", c.DeviceId)) + go func() { + for msg := range respChan { + var state CameraState + err := json.Unmarshal(msg.RawProperties, &state) + if err != nil { + log.Errorf("unmarshalling properties: %v", err) + continue + } + out <- state.MotionDetected + } + }() + + return out, nil +} diff --git a/cmd/main.go b/cmd/main.go index 54a08f3..2ae037d 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -17,10 +17,16 @@ func main() { } for _, device := range a.Cameras { - log.Infof("%s", device.DeviceName) - err := device.EnableMotionAlerts(ctx, 70, nil) - if err != nil { - log.Errorf("setting camera %s on: %v", device.DeviceName, err) + if device.DeviceName == "Salon" { + motionChan, err := device.SubscribeToMotionDetection(ctx) + if err != nil { + log.Errorf("subscribing to motion: %v", err) + return + } + for b := range motionChan { + log.Infof("motion salon %t", b) + + } } }