wip motion detection

This commit is contained in:
Laurent Le Houerou 2020-06-08 20:19:08 +04:00
parent 551716051f
commit 24bac70957
3 changed files with 38 additions and 5 deletions

View File

@ -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 {

View File

@ -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
}

View File

@ -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)
}
}
}