-
Notifications
You must be signed in to change notification settings - Fork 1
/
doc.go
79 lines (60 loc) · 2.33 KB
/
doc.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
// Copyright 2018 DataArt. All rights reserved.
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.
/*
Package devicehive-go provides access to DeviceHive API through WebSocket or REST.
Error handling is omitted to simplify examples:
client, _ := devicehive_go.ConnectWithCreds("ws://devicehive-address.com/api/websocket", "login", "password")
// or
client, _ := devicehive_go.ConnectWithCreds("http://devicehive-address.com/api/rest", "login", "password")
device, _ := client.PutDevice("my-device", "", nil, 0, 0, false)
subscription, _ := device.SubscribeInsertCommands(nil, time.Time{})
done := make(chan struct{})
go func() {
for command := range subscription.CommandsChan {
fmt.Printf("Received command with id %d\n", command.Id)
close(done)
}
}()
command, _ := device.SendCommand("my-command", nil, 120, time.Time{}, "", nil)
fmt.Printf("Command with id %d has been sent\n", command.Id)
<-done
In addition there is an ability to connect with tokens.
client, err := devicehive_go.ConnectWithToken("ws://devicehive-address.com/api/websocket", "some.JWT.accessToken", "some.JWT.refreshToken")
The client will be automatically reauthenticated by credentials or refresh token in case of access token expiration.
The SDK has an ability to send requests in non-blocking manner, writing each response and error to separate channels that you can read in a separate go routine. This API is called WebSocket low-level API.
WS low-level API usage example:
wsclient, err := devicehive_go.WSConnect("ws://devicehive-address.com/api/websocket")
...
done := make(chan struct{})
go func() {
for {
select {
case d := <- wsclient.DataChan:
res := make(map[string]interface{})
action := ""
status := ""
json.Unmarshal(d, &res) // If message was written to DataChan it must be valid JSON
if a, ok := res["action"].(string); ok {
action = a
}
if s, ok := res["status"].(string); ok {
status = s
}
if action == "authenticate" && status == "success" {
wsclient.SubscribeCommands(nil)
} else {
fmt.Println(string(d))
}
case err := <- wsclient.ErrorChan:
fmt.Println("Error", err)
}
}
close(done)
}()
err = wsclient.Authenticate("some.JWT.accessToken")
...
<-done
fmt.Println("Done")
*/
package devicehive_go