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 pathdata_collection.go
125 lines (115 loc) · 3.19 KB
/
data_collection.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
// 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/json"
"errors"
"net/http"
)
type deviceResponse struct {
Totalcount int
PageNo int
Pagesize int
Devices []Device
}
// Subscribe to notifications
func (c *Client) Subscribe(url string) (*Server, error) {
b := struct {
NotifyType string `json:"notifyType"`
CallbackURL string `json:"callbackurl"`
}{
NotifyType: "deviceDataChanged",
CallbackURL: url,
}
body, err := json.Marshal(b)
if err != nil {
return nil, err
}
resp, err := c.request(http.MethodPost, "/iocm/app/sub/v1.1.0/subscribe", bytes.NewBuffer(body))
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusCreated {
return nil, errors.New("invalid response code: " + resp.Status)
}
return &Server{}, nil
}
// RegistrationReply for RegisterDevice
type RegistrationReply struct {
VerifyCode string `json:"verifyCode"`
DeviceID string `json:"deviceId"`
Timeout uint `json:"timeout"`
Psk string `json:"psk"`
}
// RegisterDevice registers a device with a corresponding IMEI number
func (c *Client) RegisterDevice(imei string, timeoutV ...uint) (*RegistrationReply, error) {
type regDevice struct {
VerifyCode string `json:"verifyCode"`
NodeID string `json:"nodeId"`
Timeout uint `json:"timeout"`
EndUserID string `json:"endUserId"`
}
var timeout uint
if len(timeoutV) > 0 {
timeout = timeoutV[0]
}
b := regDevice{
VerifyCode: imei,
NodeID: imei,
Timeout: timeout,
EndUserID: c.cfg.EndUserID,
}
body, err := json.Marshal(b)
if err != nil {
return nil, err
}
resp, err := c.request(http.MethodPost, "/iocm/app/reg/v1.2.0/devices", bytes.NewBuffer(body))
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, errors.New("invalid response code: " + resp.Status)
}
d := RegistrationReply{}
if err := json.NewDecoder(resp.Body).Decode(&d); err != nil {
return nil, err
}
return &d, nil
}
func (c *Client) SetDeviceInfo(deviceID, name string) error {
b := struct {
Name string `json:"name"`
EndUserID string `json:"endUserId"`
Mute string `json:"mute"`
ManufacturerID string `json:"manufacturerId"`
ManufacturerName string `json:"manufacturerName"`
Location string `json:"location"`
DeviceType string `json:"deviceType"`
ProtocolType string `json:"protocolType"`
Model string `json:"model"`
}{
Name: name,
EndUserID: c.cfg.EndUserID,
Mute: "FALSE",
ManufacturerID: c.cfg.ManufacturerID,
ManufacturerName: c.cfg.ManufacturerName,
Location: c.cfg.Location,
DeviceType: c.cfg.DeviceType,
ProtocolType: "CoAP",
Model: c.cfg.Model,
}
body, err := json.Marshal(b)
if err != nil {
return err
}
resp, err := c.request(http.MethodPut, "/iocm/app/dm/v1.2.0/devices/"+deviceID, bytes.NewBuffer(body))
if err != nil {
return err
}
if resp.StatusCode != http.StatusNoContent {
return errors.New("invalid response code: " + resp.Status)
}
return nil
}