This repository has been archived by the owner on Jul 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
application.go
111 lines (96 loc) · 2.72 KB
/
application.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package lassie
import (
"encoding/hex"
"fmt"
"net/url"
"golang.org/x/net/websocket"
)
// Application represents an application.
type Application struct {
EUI string `json:"applicationEUI"`
Tags map[string]string `json:"tags"`
}
// CreateApplication creates an application.
func (c *Client) CreateApplication(app Application) (Application, error) {
err := c.create("/applications", &app)
return app, err
}
// UpdateApplication updates an application.
func (c *Client) UpdateApplication(app Application) (Application, error) {
err := c.update("/applications/"+app.EUI, &app)
return app, err
}
// Applications gets all applications.
func (c *Client) Applications() ([]Application, error) {
var apps struct {
Apps []Application `json:"applications"`
}
err := c.get("/applications", &apps)
return apps.Apps, err
}
// Application gets an application.
func (c *Client) Application(eui string) (Application, error) {
var app Application
err := c.get("/applications/"+eui, &app)
return app, err
}
// DeleteApplication deletes an application.
func (c *Client) DeleteApplication(eui string) error {
return c.delete("/applications/" + eui)
}
// ApplicationStream calls handler in its own goroutine for each device message received.
// It blocks until an error occurs, including EOF when the connection is closed.
func (c *Client) ApplicationStream(appeui string, handler func(DeviceData)) error {
url, err := url.Parse(c.addr)
if err != nil {
return err
}
scheme := "wss"
if url.Scheme == "http" {
scheme = "ws"
}
wscfg, err := websocket.NewConfig(fmt.Sprintf("%s://%s/applications/%s/stream", scheme, url.Host, appeui), "http://example.com")
if err != nil {
return err
}
wscfg.Header.Set("X-API-Token", c.token)
ws, err := websocket.DialConfig(wscfg)
if err != nil {
return err
}
defer ws.Close()
for {
var msg struct {
Type string `json:"type"`
Error string `json:"message"`
Data DeviceData `json:"data"`
}
err := websocket.JSON.Receive(ws, &msg)
if err != nil {
return err
}
switch msg.Type {
case "Error":
return fmt.Errorf(msg.Error)
case "DeviceData":
msg.Data.Data, _ = hex.DecodeString(msg.Data.HexData)
go handler(msg.Data)
default:
// Ignore it
}
}
}
// DeviceData represents data received from a device.
type DeviceData struct {
DeviceEUI string `json:"deviceEUI"`
DeviceAddress string `json:"devAddr"`
GatewayEUI string `json:"gatewayEUI"`
ApplicationEUI string `json:"appEUI"`
Timestamp int64 `json:"timestamp"`
HexData string `json:"data"`
Data []byte
Frequency float32 `json:"frequency"`
DataRate string `json:"dataRate"`
RSSI int32 `json:"rssi"`
SNR float32 `json:"snr"`
}