-
Notifications
You must be signed in to change notification settings - Fork 1
/
device.go
234 lines (211 loc) · 6.9 KB
/
device.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// December 14, 2016
// Craig Hesling <craig@hesling.com>
package rest
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
)
// DeviceListServiceItem represents the service and service configuration pair
// found in in a Device Node's service list
type DeviceListServiceItem struct {
ServiceID string `json:"service_id"`
ServiceConfig []KeyValuePair `json:"config,omitempty"`
}
// TransducerInfo describes a transducer within a Device.
type TransducerInfo struct {
Name string `json:"name"`
Unit string `json:"unit"`
IsActuable bool `json:"is_actuable"`
}
// TransducerValue holds a transducer description with a single value
// and timestamp.
type TransducerValue struct {
TransducerInfo
Value string `json:"value"`
ValueTimestamp time.Time `json:"timestamp"`
}
// DeviceNode is a container for Device Node object received
// from the RESTful JSON interface
type DeviceNode struct {
NodeDescriptor // Node descriptor of Device Node
Properties map[string]string `json:"properties"`
Transducers []TransducerInfo `json:"transducers"`
Services []DeviceListServiceItem `json:"linked_services"`
}
// Clone creates a new copy of the DeviceNode.
// This is necessary because a DeviceNode has embedded slices.
func (n *DeviceNode) Clone() DeviceNode {
var ret = *n
ret.Transducers = []TransducerInfo{}
ret.Services = []DeviceListServiceItem{}
copy(ret.Transducers, n.Transducers)
copy(ret.Services, n.Services)
return ret
}
// DeviceAll makes an HTTP GET to the framework server requesting
// the a list of all devices
func (host Host) DeviceAll() ([]NodeDescriptor, error) {
var devices []NodeDescriptor
uri := host.uri + rootAPISubPath + deviceSubPath
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return devices, err
}
req.SetBasicAuth(host.user, host.pass)
resp, err := host.client.Do(req)
if err != nil {
// should report auth problems here in future
return devices, err
}
defer resp.Body.Close()
if err := DecodeOCError(resp); err != nil {
return devices, err
}
err = json.NewDecoder(resp.Body).Decode(&devices)
return devices, err
}
// RequestDeviceInfo makes an HTTP GET to the framework server requesting
// the Device Node information for the device with ID deviceID.
func (host Host) RequestDeviceInfo(deviceID string) (DeviceNode, error) {
var deviceNode DeviceNode
uri := host.uri + rootAPISubPath + deviceSubPath + "/" + deviceID
fmt.Println("DevURI:", uri)
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return deviceNode, err
}
req.SetBasicAuth(host.user, host.pass)
resp, err := host.client.Do(req)
if err != nil {
// should report auth problems here in future
return deviceNode, err
}
defer resp.Body.Close()
if err := DecodeOCError(resp); err != nil {
return deviceNode, err
}
err = json.NewDecoder(resp.Body).Decode(&deviceNode)
return deviceNode, err
}
// DeviceTransducerValues makes an HTTP GET to the framework server requesting
// the transducers last value list for the device with ID deviceID.
func (host Host) DeviceTransducerValues(deviceID string) ([]TransducerValue, error) {
var transducers []TransducerValue
uri := host.uri + rootAPISubPath + deviceSubPath + "/" + deviceID + "/transducer"
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return transducers, err
}
req.SetBasicAuth(host.user, host.pass)
resp, err := host.client.Do(req)
if err != nil {
// should report auth problems here in future
return transducers, err
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&transducers)
return transducers, err
}
// DeviceTransducerLastValue makes an HTTP GET to the framework server requesting
// the transducers last value for the device with ID deviceID and transducer
// with with ID or name transducerID.
func (host Host) DeviceTransducerLastValue(deviceID, transducerID string) ([]byte, error) {
var value []byte
uri := host.uri + rootAPISubPath + deviceSubPath + "/" + deviceID + "/transducer/" + transducerID
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return value, err
}
req.SetBasicAuth(host.user, host.pass)
resp, err := host.client.Do(req)
if err != nil {
// should report auth problems here in future
return value, err
}
defer resp.Body.Close()
if err := DecodeOCError(resp); err != nil {
return value, err
}
value, err = ioutil.ReadAll(resp.Body)
return value, err
}
// RequestLinkedService makes an HTTP POST to the framework server to link the
// specified serviceID to device deviceID.
func (host Host) RequestLinkedService(deviceID, serviceID string) (DeviceListServiceItem, error) {
var deviceServiceItem DeviceListServiceItem
uri := host.uri + rootAPISubPath + deviceSubPath + "/" + deviceID + "/service/" + serviceID
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return deviceServiceItem, err
}
req.SetBasicAuth(host.user, host.pass)
resp, err := host.client.Do(req)
if err != nil {
return deviceServiceItem, err
}
defer resp.Body.Close()
if err := DecodeOCError(resp); err != nil {
return deviceServiceItem, err
}
err = json.NewDecoder(resp.Body).Decode(&deviceServiceItem)
return deviceServiceItem, err
}
// LinkService makes an HTTP POST to the framework server to link the
// specified serviceID to device deviceID.
func (host Host) LinkService(deviceID, serviceID string, config []KeyValuePair) error {
uri := host.uri + rootAPISubPath + deviceSubPath + "/" + deviceID + "/service/" + serviceID
body, err := json.Marshal(DeviceListServiceItem{
ServiceID: serviceID,
ServiceConfig: config,
})
if err != nil {
return err
}
req, err := http.NewRequest("POST", uri, bytes.NewReader(body))
if err != nil {
return err
}
req.SetBasicAuth(host.user, host.pass)
resp, err := host.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return DecodeOCError(resp)
}
// DelinkService makes an HTTP DELETE to the framework server to delink the
// specified serviceID from device deviceID.
func (host Host) DelinkService(deviceID, serviceID string) error {
uri := host.uri + rootAPISubPath + deviceSubPath + "/" + deviceID + "/service/" + serviceID
req, err := http.NewRequest("DELETE", uri, nil)
if err != nil {
return err
}
req.SetBasicAuth(host.user, host.pass)
resp, err := host.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return DecodeOCError(resp)
}
// ExecuteCommand makes an HTTP POST to the framework server to execute the
// specified commandID on device deviceID.
func (host Host) ExecuteCommand(deviceID, commandID string) error {
uri := host.uri + rootAPISubPath + deviceSubPath + "/" + deviceID + "/command/" + commandID
req, err := http.NewRequest("POST", uri, bytes.NewReader([]byte("{}")))
if err != nil {
return err
}
req.SetBasicAuth(host.user, host.pass)
resp, err := host.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return DecodeOCError(resp)
}