diff --git a/Gopkg.lock b/Gopkg.lock index b3e1d27..1cb5a7d 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -11,11 +11,11 @@ [[projects]] branch = "master" - digest = "1:63eb8e7863bcc41b162d76c3e1b5dd4991fd01f29585017b3a8bab7a5928edd3" + digest = "1:b44621ef75eb48a8c09e7d6ce4d3f6681534307189c9bc0267c9532094c20a2a" name = "github.com/r3labs/sse" packages = ["."] pruneopts = "" - revision = "ad82e5b42970ce737f0ce61490a1607331299496" + revision = "507597500aa545836a85909d89a59e50d260f366" [[projects]] branch = "master" diff --git a/account.go b/account.go index 0adf1d9..9574881 100644 --- a/account.go +++ b/account.go @@ -1,4 +1,4 @@ -package arlo_golang +package arlo import ( "fmt" diff --git a/arlo.go b/arlo.go index f58ba12..6c4ad03 100644 --- a/arlo.go +++ b/arlo.go @@ -1,4 +1,4 @@ -package arlo_golang +package arlo import ( "github.com/jeffreydwalter/arlo-golang/internal/request" diff --git a/arlo_test.go b/arlo_test.go index 7c06dbd..d0aa6f2 100644 --- a/arlo_test.go +++ b/arlo_test.go @@ -1 +1 @@ -package arlo_golang +package arlo diff --git a/basestation.go b/basestation.go index 5eb0ab5..7e59a8f 100644 --- a/basestation.go +++ b/basestation.go @@ -1,9 +1,8 @@ -package arlo_golang +package arlo import ( "fmt" - "github.com/jeffreydwalter/arlo-golang/internal/util" "github.com/pkg/errors" ) @@ -43,7 +42,7 @@ type Basestation struct { type Basestations []Basestation func (b *Basestation) Subscribe() (*Status, error) { - b.eventStream = NewEventStream(BaseUrl+fmt.Sprintf(SubscribeUri, b.arlo.Account.Token), b.arlo.client.HttpClient, util.HeaderToMap(*b.arlo.client.BaseHttpHeader)) + b.eventStream = NewEventStream(BaseUrl+fmt.Sprintf(SubscribeUri, b.arlo.Account.Token), b.arlo.client.HttpClient) b.eventStream.Listen() transId := GenTransId() @@ -71,22 +70,6 @@ func (b *Basestation) Subscribe() (*Status, error) { return &status, nil } -/* - This is an example of the json you would pass in the body to UpdateFriends(): -{ - "firstName":"Some", - "lastName":"Body", - "devices":{ - "XXXXXXXXXXXXX":"Camera 1", - "XXXXXXXXXXXXX":"Camera 2 ", - "XXXXXXXXXXXXX":"Camera 3" - }, - "lastModified":1463977440911, - "adminUser":true, - "email":"user@example.com", - "id":"XXX-XXXXXXX" -} -*/ func (b *Basestation) GetState() (*NotifyResponse, error) { transId := GenTransId() @@ -100,11 +83,7 @@ func (b *Basestation) GetState() (*NotifyResponse, error) { To: b.DeviceId, } - //fmt.Printf("BODY: %+v\n", body) - //fmt.Printf("HEADERS: %+v\n", a.client.BaseHttpHeader) - - fmt.Println("Subscribing to the eventstream.") - b.eventStream.Subscriptions[transId] = new(Subscriber) + b.eventStream.Subscriptions[transId] = make(chan *NotifyResponse) resp, err := b.arlo.client.Post(fmt.Sprintf(NotifyUri, b.DeviceId), body, nil) if err != nil { @@ -120,6 +99,37 @@ func (b *Basestation) GetState() (*NotifyResponse, error) { return nil, errors.New("failed to get basestation status") } - notifyResponse := <-*b.eventStream.Subscriptions[transId] - return ¬ifyResponse, nil + return <-b.eventStream.Subscriptions[transId], nil +} + +func (b *Basestation) GetCameraState() (*NotifyResponse, error) { + + transId := GenTransId() + + body := NotifyPayload{ + Action: "get", + Resource: "cameras", + PublishResponse: false, + TransId: transId, + From: fmt.Sprintf("%s_%s", b.UserId, TransIdPrefix), + To: b.DeviceId, + } + + b.eventStream.Subscriptions[transId] = make(chan *NotifyResponse) + + resp, err := b.arlo.client.Post(fmt.Sprintf(NotifyUri, b.DeviceId), body, nil) + if err != nil { + return nil, errors.WithMessage(err, "failed to get basestation state") + } + + var status Status + if err := resp.Decode(&status); err != nil { + return nil, err + } + + if !status.Success { + return nil, errors.New("failed to get basestation status") + } + + return <-b.eventStream.Subscriptions[transId], nil } diff --git a/camera.go b/camera.go index 3d7140c..55bfb69 100644 --- a/camera.go +++ b/camera.go @@ -1,4 +1,4 @@ -package arlo_golang +package arlo // A Camera is a Device of type "camera". // This type is here just for semantics. Some methods explicitly require a device of a certain type. diff --git a/const.go b/const.go index 9bc02a1..8f4f6a8 100644 --- a/const.go +++ b/const.go @@ -1,4 +1,4 @@ -package arlo_golang +package arlo const ( TransIdPrefix = "web" diff --git a/devices.go b/devices.go index 62fdc8f..1415b9f 100644 --- a/devices.go +++ b/devices.go @@ -1,4 +1,4 @@ -package arlo_golang +package arlo import ( "fmt" diff --git a/events_stream.go b/events_stream.go index f002ee5..73e876a 100644 --- a/events_stream.go +++ b/events_stream.go @@ -1,4 +1,4 @@ -package arlo_golang +package arlo import ( "bytes" @@ -18,31 +18,27 @@ var ( FAILED_TO_SUBSCRIBE = errors.New("Failed to subscribe to SSEClient") ) -type Subscriber chan NotifyResponse - type EventStream struct { + SSEClient *sse.Client + Subscriptions map[string]chan *NotifyResponse + Events chan *sse.Event + ErrorChan chan error Registered bool Connected bool - SSEClient *sse.Client - Events chan *sse.Event - Subscriptions map[string]*Subscriber - ErrorChan chan error - Responses map[string]NotifyResponse Verbose bool sync.Mutex } -func NewEventStream(url string, client *http.Client, headers map[string]string) *EventStream { +func NewEventStream(url string, client *http.Client) *EventStream { SSEClient := sse.NewClient(url) SSEClient.Connection = client - SSEClient.Headers = headers return &EventStream{ SSEClient: SSEClient, Events: make(chan *sse.Event), - Subscriptions: map[string]*Subscriber{}, + Subscriptions: make(map[string]chan *NotifyResponse), ErrorChan: make(chan error, 1), } } @@ -59,9 +55,11 @@ func (e *EventStream) Listen() { go func() { for event := range e.Events { - fmt.Println("Got event message.") - fmt.Printf("EVENT: %X\n", event.Event) - fmt.Printf("DATA: %X\n", event.Data) + /* + fmt.Println("Got event message.") + fmt.Printf("EVENT: %s\n", event.Event) + fmt.Printf("DATA: %s\n", event.Data) + */ if event.Data != nil { notifyResponse := &NotifyResponse{} @@ -72,28 +70,19 @@ func (e *EventStream) Listen() { break } - fmt.Printf("%s\n", notifyResponse) if notifyResponse.Status == "connected" { - fmt.Println("Connected.") e.Connected = true } else if notifyResponse.Status == "disconnected" { - fmt.Println("Disconnected.") e.Connected = false } else { - fmt.Printf("Message for transId: %s\n", notifyResponse.TransId) if subscriber, ok := e.Subscriptions[notifyResponse.TransId]; ok { e.Lock() - *subscriber <- *notifyResponse - close(*subscriber) + subscriber <- notifyResponse + close(subscriber) delete(e.Subscriptions, notifyResponse.TransId) e.Unlock() - } else { - // Throw away the message. - fmt.Println("Throwing away message.") } } - } else { - fmt.Printf("Event data was nil.\n") } } }() diff --git a/library.go b/library.go index 7bc36e1..2e4d099 100644 --- a/library.go +++ b/library.go @@ -1,4 +1,4 @@ -package arlo_golang +package arlo import ( "time" diff --git a/responses.go b/responses.go index fe536a2..5b4607d 100644 --- a/responses.go +++ b/responses.go @@ -1,4 +1,4 @@ -package arlo_golang +package arlo // UpdateResponse is an intermediate struct used when parsing data from the UpdateProfile() call. type Status struct { diff --git a/types.go b/types.go index 4805aea..5557e8e 100644 --- a/types.go +++ b/types.go @@ -1,4 +1,4 @@ -package arlo_golang +package arlo /* // Credentials is the login credential data.