2018-09-19 07:07:32 +00:00
|
|
|
package arlo
|
2018-09-17 04:44:41 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-05-27 08:49:40 +00:00
|
|
|
"context"
|
2018-09-17 04:44:41 +00:00
|
|
|
"encoding/json"
|
2020-05-27 08:49:40 +00:00
|
|
|
"fmt"
|
2018-09-18 18:02:21 +00:00
|
|
|
"net/http"
|
2018-09-17 04:44:41 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/r3labs/sse"
|
2020-05-27 08:49:40 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2018-09-17 04:44:41 +00:00
|
|
|
)
|
|
|
|
|
2018-09-22 14:15:22 +00:00
|
|
|
type eventStream struct {
|
2020-05-27 08:49:40 +00:00
|
|
|
SSEClient *sse.Client
|
|
|
|
Events chan *sse.Event
|
|
|
|
Error chan error
|
|
|
|
Verbose bool
|
2020-05-27 19:03:26 +00:00
|
|
|
disconnectedChan chan interface{}
|
2020-05-27 08:49:40 +00:00
|
|
|
once *sync.Once
|
|
|
|
|
2020-05-27 19:03:26 +00:00
|
|
|
transactionSubscribers map[string]chan *EventStreamResponse
|
|
|
|
transactionSubscribersMutex sync.RWMutex
|
2018-09-22 14:15:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newEventStream(url string, client *http.Client) *eventStream {
|
2020-05-27 08:49:40 +00:00
|
|
|
e := &eventStream{
|
2020-05-27 19:03:26 +00:00
|
|
|
Events: make(chan *sse.Event),
|
|
|
|
transactionSubscribers: make(map[string]chan *EventStreamResponse),
|
|
|
|
transactionSubscribersMutex: sync.RWMutex{},
|
|
|
|
disconnectedChan: make(chan interface{}),
|
|
|
|
once: new(sync.Once),
|
2020-05-27 08:49:40 +00:00
|
|
|
}
|
2018-09-17 04:44:41 +00:00
|
|
|
|
|
|
|
SSEClient := sse.NewClient(url)
|
2018-09-18 18:02:21 +00:00
|
|
|
SSEClient.Connection = client
|
2020-05-27 08:49:40 +00:00
|
|
|
SSEClient.OnDisconnect(func(c *sse.Client) {
|
|
|
|
e.disconnect()
|
|
|
|
})
|
|
|
|
e.SSEClient = SSEClient
|
|
|
|
return e
|
2018-09-17 04:44:41 +00:00
|
|
|
}
|
|
|
|
|
2018-09-22 14:15:22 +00:00
|
|
|
func (e *eventStream) disconnect() {
|
|
|
|
e.once.Do(func() {
|
2020-05-27 19:03:26 +00:00
|
|
|
close(e.disconnectedChan)
|
2018-09-22 14:15:22 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-27 08:49:40 +00:00
|
|
|
func (e *eventStream) listen(ctx context.Context) (chan bool, error) {
|
2020-05-27 19:03:26 +00:00
|
|
|
|
2020-05-27 08:49:40 +00:00
|
|
|
err := e.SSEClient.SubscribeChanRaw(e.Events)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to subscribe to seeclient")
|
|
|
|
}
|
2018-09-17 04:44:41 +00:00
|
|
|
|
2020-05-27 19:03:26 +00:00
|
|
|
connectedChan := make(chan bool)
|
|
|
|
|
2018-09-17 04:44:41 +00:00
|
|
|
go func() {
|
2018-09-19 21:35:05 +00:00
|
|
|
for {
|
|
|
|
select {
|
2020-05-27 08:49:40 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
e.disconnect()
|
|
|
|
return
|
|
|
|
case event, ok := <-e.Events:
|
|
|
|
if !ok {
|
|
|
|
return
|
2018-09-17 04:44:41 +00:00
|
|
|
}
|
2020-05-27 08:49:40 +00:00
|
|
|
|
|
|
|
if event == nil || event.Data == nil {
|
|
|
|
log.Warn("EventStream > nil event or nil data in event")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("DATA : %s\n", event.Data)
|
|
|
|
|
|
|
|
var notifyResponse EventStreamResponse
|
|
|
|
err := json.NewDecoder(bytes.NewBuffer(event.Data)).Decode(¬ifyResponse)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("EventStream > failed to decode event: %s", event.Data)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
bytesProperties, err := json.Marshal(notifyResponse.EventStreamPayload.Properties)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("EventStream > failed to marshal raw properties: %s", event.Data)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
notifyResponse.RawProperties = bytesProperties
|
|
|
|
|
|
|
|
if notifyResponse.Status == "connected" {
|
|
|
|
connectedChan <- true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if notifyResponse.Status == "disconnected" {
|
|
|
|
e.disconnect()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if notifyResponse.Action == "logout" {
|
|
|
|
log.Warn("EventStream > logged out")
|
|
|
|
e.disconnect()
|
|
|
|
continue
|
|
|
|
}
|
2020-05-27 19:03:26 +00:00
|
|
|
e.transactionSubscribersMutex.RLock()
|
|
|
|
subscriber, ok := e.transactionSubscribers[notifyResponse.TransId]
|
|
|
|
e.transactionSubscribersMutex.RUnlock()
|
2020-05-27 08:49:40 +00:00
|
|
|
if ok {
|
|
|
|
subscriber <- ¬ifyResponse
|
|
|
|
}
|
|
|
|
|
2020-05-27 19:03:26 +00:00
|
|
|
case <-e.disconnectedChan:
|
2020-05-27 08:49:40 +00:00
|
|
|
connectedChan <- false
|
2018-09-19 21:35:05 +00:00
|
|
|
return
|
2018-09-17 04:44:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2018-09-19 21:35:05 +00:00
|
|
|
|
2020-05-27 08:49:40 +00:00
|
|
|
return connectedChan, nil
|
2018-09-17 04:44:41 +00:00
|
|
|
}
|
2018-09-22 14:15:22 +00:00
|
|
|
|
2020-05-27 08:49:40 +00:00
|
|
|
func (e *eventStream) unsubscribe(transId string) {
|
2020-05-27 19:03:26 +00:00
|
|
|
e.transactionSubscribersMutex.Lock()
|
|
|
|
if c, ok := e.transactionSubscribers[transId]; ok {
|
2020-05-27 08:49:40 +00:00
|
|
|
close(c)
|
2020-05-27 19:03:26 +00:00
|
|
|
delete(e.transactionSubscribers, transId)
|
2018-09-22 14:15:22 +00:00
|
|
|
}
|
2020-05-27 19:03:26 +00:00
|
|
|
e.transactionSubscribersMutex.Unlock()
|
2018-09-22 14:15:22 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 19:03:26 +00:00
|
|
|
func (e *eventStream) subscribe(transId string, subscriber chan *EventStreamResponse) {
|
|
|
|
e.transactionSubscribersMutex.Lock()
|
|
|
|
e.transactionSubscribers[transId] = subscriber
|
|
|
|
e.transactionSubscribersMutex.Unlock()
|
2018-09-22 14:15:22 +00:00
|
|
|
}
|