arlo-go/events_stream.go

135 lines
3.2 KiB
Go
Raw Normal View History

package arlo
import (
"bytes"
2020-05-27 08:49:40 +00:00
"context"
"encoding/json"
2020-05-27 08:49:40 +00:00
"fmt"
"net/http"
"sync"
"github.com/r3labs/sse"
2020-05-27 08:49:40 +00:00
log "github.com/sirupsen/logrus"
)
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
}
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
}
SSEClient := sse.NewClient(url)
SSEClient.Connection = client
2020-05-27 08:49:40 +00:00
SSEClient.OnDisconnect(func(c *sse.Client) {
e.disconnect()
})
e.SSEClient = SSEClient
return e
}
func (e *eventStream) disconnect() {
e.once.Do(func() {
2020-05-27 19:03:26 +00:00
close(e.disconnectedChan)
})
}
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")
}
2020-05-27 19:03:26 +00:00
connectedChan := make(chan bool)
go func() {
for {
select {
2020-05-27 08:49:40 +00:00
case <-ctx.Done():
e.disconnect()
return
case event, ok := <-e.Events:
if !ok {
return
}
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(&notifyResponse)
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 <- &notifyResponse
}
2020-05-27 19:03:26 +00:00
case <-e.disconnectedChan:
2020-05-27 08:49:40 +00:00
connectedChan <- false
return
}
}
}()
2020-05-27 08:49:40 +00:00
return connectedChan, nil
}
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)
}
2020-05-27 19:03:26 +00:00
e.transactionSubscribersMutex.Unlock()
}
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()
}