This repository has been archived by the owner on Apr 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
outputstream.go
89 lines (74 loc) · 2.14 KB
/
outputstream.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package nbiot
import (
"fmt"
"net/http"
"net/url"
"github.com/gorilla/websocket"
)
// OutputStream provides a stream of OutputDataMessages.
type OutputStream struct {
ws *websocket.Conn
}
// OutputDataMessage represents a message sent by a device.
type OutputDataMessage struct {
Device Device `json:"device"`
Payload []byte `json:"payload"`
Received int64 `json:"received,string"`
Transport string `json:"transport"`
CoAPMetaData struct {
Method string `json:"method"`
Path string `json:"path"`
} `json:"coapMetaData"`
UDPMetaData struct {
LocalPort int `json:"localPort"`
RemotePort int `json:"remotePort"`
} `json:"udpMetaData"`
}
// CollectionOutputStream streams messages from all devices in a collection.
func (c *Client) CollectionOutputStream(collectionID string) (*OutputStream, error) {
return c.outputStream(fmt.Sprintf("/collections/%s", collectionID))
}
// DeviceOutputStream streams messages from one device.
func (c *Client) DeviceOutputStream(collectionID, deviceID string) (*OutputStream, error) {
return c.outputStream(fmt.Sprintf("/collections/%s/devices/%s", collectionID, deviceID))
}
func (c *Client) outputStream(path string) (*OutputStream, error) {
url, err := url.Parse(c.addr)
if err != nil {
return nil, err
}
scheme := "wss"
if url.Scheme == "http" {
scheme = "ws"
}
urlStr := fmt.Sprintf("%s://%s%s/from", scheme, url.Host, path)
header := http.Header{}
header.Add("X-API-Token", c.token)
dialer := websocket.Dialer{}
ws, resp, err := dialer.Dial(urlStr, header)
if err != nil {
return nil, fmt.Errorf("handshake failed with status %d", resp.StatusCode)
}
return &OutputStream{ws}, nil
}
// Recv blocks until a new message is received.
// It returns io.EOF if the stream is closed by the server.
func (s *OutputStream) Recv() (OutputDataMessage, error) {
for {
var msg struct {
Type string `json:"type"`
OutputDataMessage
}
err := s.ws.ReadJSON(&msg)
if err != nil {
return OutputDataMessage{}, err
}
if msg.Type == "data" {
return msg.OutputDataMessage, nil
}
}
}
// Close closes the output stream.
func (s *OutputStream) Close() {
s.ws.Close()
}