2018-09-19 07:07:32 +00:00
|
|
|
package arlo
|
2018-09-17 04:44:41 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2018-09-18 18:02:21 +00:00
|
|
|
"net/http"
|
2018-09-17 04:44:41 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
"github.com/r3labs/sse"
|
|
|
|
)
|
|
|
|
|
2018-09-18 18:02:21 +00:00
|
|
|
var (
|
2018-09-19 21:35:05 +00:00
|
|
|
FAILED_TO_PUBLISH = errors.New("failed to publish")
|
|
|
|
FAILED_TO_DECODE_JSON = errors.New("failed to decode json")
|
|
|
|
FAILED_TO_SUBSCRIBE = errors.New("failed to subscribe to seeclient")
|
2018-09-18 18:02:21 +00:00
|
|
|
)
|
2018-09-17 04:44:41 +00:00
|
|
|
|
|
|
|
type EventStream struct {
|
|
|
|
SSEClient *sse.Client
|
2018-09-19 19:12:06 +00:00
|
|
|
Subscriptions map[string]chan *EventStreamResponse
|
2018-09-17 04:44:41 +00:00
|
|
|
Events chan *sse.Event
|
2018-09-19 21:35:05 +00:00
|
|
|
Error chan error
|
|
|
|
Close chan interface{}
|
2018-09-19 07:07:32 +00:00
|
|
|
Connected bool
|
2018-09-17 04:44:41 +00:00
|
|
|
Verbose bool
|
|
|
|
|
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
2018-09-19 07:07:32 +00:00
|
|
|
func NewEventStream(url string, client *http.Client) *EventStream {
|
2018-09-17 04:44:41 +00:00
|
|
|
|
|
|
|
SSEClient := sse.NewClient(url)
|
2018-09-18 18:02:21 +00:00
|
|
|
SSEClient.Connection = client
|
2018-09-17 04:44:41 +00:00
|
|
|
|
|
|
|
return &EventStream{
|
|
|
|
SSEClient: SSEClient,
|
|
|
|
Events: make(chan *sse.Event),
|
2018-09-19 19:12:06 +00:00
|
|
|
Subscriptions: make(map[string]chan *EventStreamResponse),
|
2018-09-19 21:35:05 +00:00
|
|
|
Error: make(chan error),
|
|
|
|
Close: make(chan interface{}),
|
2018-09-17 04:44:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-19 21:35:05 +00:00
|
|
|
func (e *EventStream) Listen() (connected chan bool) {
|
|
|
|
|
|
|
|
connected = make(chan bool)
|
2018-09-17 04:44:41 +00:00
|
|
|
|
|
|
|
go func() {
|
2018-09-18 18:02:21 +00:00
|
|
|
err := e.SSEClient.SubscribeChanRaw(e.Events)
|
2018-09-17 04:44:41 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(FAILED_TO_SUBSCRIBE)
|
2018-09-19 21:35:05 +00:00
|
|
|
e.Error <- FAILED_TO_SUBSCRIBE
|
2018-09-17 04:44:41 +00:00
|
|
|
}
|
|
|
|
|
2018-09-19 21:35:05 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event := <-e.Events:
|
|
|
|
/*
|
|
|
|
fmt.Println("Got event message.")
|
|
|
|
fmt.Printf("EVENT: %s\n", event.Event)
|
|
|
|
fmt.Printf("DATA: %s\n", event.Data)
|
|
|
|
*/
|
2018-09-17 04:44:41 +00:00
|
|
|
|
2018-09-19 21:35:05 +00:00
|
|
|
if event.Data != nil {
|
|
|
|
notifyResponse := &EventStreamResponse{}
|
|
|
|
b := bytes.NewBuffer(event.Data)
|
|
|
|
err := json.NewDecoder(b).Decode(notifyResponse)
|
|
|
|
if err != nil {
|
|
|
|
e.Error <- FAILED_TO_DECODE_JSON
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if notifyResponse.Status == "connected" {
|
|
|
|
connected <- true
|
|
|
|
} else if notifyResponse.Status == "disconnected" {
|
|
|
|
connected <- false
|
|
|
|
} else {
|
|
|
|
if subscriber, ok := e.Subscriptions[notifyResponse.TransId]; ok {
|
|
|
|
e.Lock()
|
|
|
|
subscriber <- notifyResponse
|
|
|
|
e.Unlock()
|
|
|
|
}
|
2018-09-17 04:44:41 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-19 21:35:05 +00:00
|
|
|
case <-e.Close:
|
|
|
|
connected <- false
|
|
|
|
return
|
2018-09-17 04:44:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2018-09-19 21:35:05 +00:00
|
|
|
|
|
|
|
return connected
|
2018-09-17 04:44:41 +00:00
|
|
|
}
|