This repository has been archived by the owner on Sep 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdevice.go
193 lines (174 loc) · 4.63 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
// Copyright 2017 The go-oceanconnect authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package oceanconnect
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httputil"
)
// Device struct with device data
type Device struct {
DeviceID string `json:"deviceId"`
GatewayID string `json:"gatewayId"`
NodeType string `json:"nodeType"`
CreateTime OcTime `json:"creationTime"`
LastModifiedTime OcTime `json:"lastModifiedTime"`
DeviceInfo DeviceInfo `json:"deviceInfo"`
Services []Service `json:"services"`
client *Client
}
// Service struct which holds service information data
type Service struct {
ServiceID string
ServiceType string
Data []byte `json:"data"`
EventTime OcTime
ServiceInfo string `json:",omitEmpty"`
}
func (u *Service) UnmarshalJSON(data []byte) error {
srvID := &struct {
ServiceID string
}{}
if err := json.Unmarshal(data, srvID); err != nil {
return err
}
if srvID.ServiceID != "RawData" {
return errors.New("service type not supported: " + srvID.ServiceID)
}
type Alias Service
aux := &struct {
Data struct {
RawData string `json:"rawData"`
} `json:"data"`
*Alias
}{
Alias: (*Alias)(u),
}
if err := json.Unmarshal(data, aux); err != nil {
return err
}
var err error
u.Data, err = base64.StdEncoding.DecodeString(aux.Data.RawData)
return err
}
// DeviceInfo struct with device info data
type DeviceInfo struct {
NodeID string
Name string
Description string
ManufacturerID string
ManufacturerName string
Mac string
Location string
DeviceType string
Model string
Swversion string
FwVersion string
HwVersion string
ProtocolType string
BridgeID string
Status string
StatusDetail string
Mute string
SupportedSecurity string
IsSecurity string
SignalStrength string
SigVersion string
SerialNumber string
}
// deviceHistory struct with response data
type deviceHistory struct {
TotalCount int
PageNo int
PageSize int
DeviceData []DeviceData `json:"deviceDataHistoryDTOs"`
}
// DeviceData struct with response data
type DeviceData struct {
DeviceID string
GatewayID string
Appid string
ServiceIS string
Data []byte `json:"data"`
Timestamp OcTime
}
func (u *DeviceData) UnmarshalJSON(data []byte) error {
type Alias DeviceData
aux := &struct {
Data struct {
RawData string `json:"rawData"`
} `json:"data"`
*Alias
}{
Alias: (*Alias)(u),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
var err error
u.Data, err = base64.StdEncoding.DecodeString(aux.Data.RawData)
return err
}
// GetHistoricalData returns data from specific device
func (d *Device) GetHistoricalData() ([]DeviceData, error) {
resp, err := d.client.request(http.MethodGet, "/iocm/app/data/v1.1.0/deviceDataHistory?deviceId="+d.DeviceID+"&gatewayId="+d.GatewayID, nil)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, errors.New("invalid response code: " + resp.Status)
}
// save device response
dh := deviceHistory{}
if err := json.NewDecoder(resp.Body).Decode(&dh); err != nil {
return nil, err
}
return dh.DeviceData, nil
}
func (d *Device) Command(data []byte, timeoutSec int64) error {
type devCmdBodyRawData struct {
RawData string `json:"rawData"`
}
type devCmdBodyCommand struct {
ServiceID string `json:"serviceId"`
Method string `json:"method"`
Params devCmdBodyRawData `json:"paras"`
}
type devCmdBody struct {
//RequestID string `json:"requestId"`
Command devCmdBodyCommand `json:"command"`
//CallbackURL string `json:"callbackUrl"`
ExpireTime int64 `json:"expireTime"`
}
cmd := devCmdBody{
//RequestID: "1234567890",
Command: devCmdBodyCommand{
ServiceID: "RawData",
Method: "RawData",
Params: devCmdBodyRawData{
RawData: base64.StdEncoding.EncodeToString(data),
},
},
//CallbackURL: "https://www.google.com/",
ExpireTime: timeoutSec,
}
body, err := json.Marshal(cmd)
if err != nil {
return err
}
resp, err := d.client.request(http.MethodPost, "/iocm/app/cmd/v1.2.0/devices/"+d.DeviceID+"/commands", bytes.NewBuffer(body))
if err != nil {
return err
}
rs, _ := httputil.DumpResponse(resp, true)
fmt.Printf("==== response =====\n%s\n", string(rs))
if resp.StatusCode != http.StatusOK {
return errors.New("invalid response code: " + resp.Status)
}
return nil
}