-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
248 lines (204 loc) · 6.68 KB
/
client.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package homehub
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
)
type client struct {
authData authData
}
func newClient(URL string, username string, password string) *client {
auth := authData{
url: URL,
userName: username,
password: hexmd5(password),
}
return &client{auth}
}
func (c *client) login() (err error) {
req := newLoginRequest(&c.authData)
resp, err := req.send()
if err == nil {
c.authData.sessionID = strconv.Itoa(resp.ResponseBody.Reply.ResponseActions[0].ResponseCallbacks[0].Parameters.ID)
c.authData.nonce = resp.ResponseBody.Reply.ResponseActions[0].ResponseCallbacks[0].Parameters.Nonce
return nil
}
return err
}
func (c *client) doReboot(xpath string) (err error) {
req := newRebootRequest(&c.authData, xpath)
_, err = req.send()
return err
}
func (c *client) getBandwidthUsage(xpath string) (result string, err error) {
bandwidthMonitorRequest := newBandwidthMonitorRequest(&c.authData, xpath)
req := newHubResourceRequest(&c.authData, c.authData.url, bandwidthMonitorRequest)
resp, err := req.send()
if err != nil {
return "", err
}
return resp.body, nil
}
func (c *client) getEventLog(xpath string) (result string, err error) {
eventLogRequest := newEventLogRequest(&c.authData, xpath)
req := newHubResourceRequest(&c.authData, c.authData.url, eventLogRequest)
resp, err := req.send()
if err != nil {
return "", err
}
return resp.body, nil
}
func (c *client) getXPathValueString(xpath string) (result string, err error) {
resp, err := c.doXPathRequest(xpath)
if err == nil {
if resp.ResponseBody.Reply != nil {
params := resp.ResponseBody.Reply.ResponseActions[0].ResponseCallbacks[0].Parameters
vo := reflect.ValueOf(params.Value)
if getTypeMapping(params.Capability.Type) != "string" {
return "", errors.New("Expected response value to be of type string but was " + getTypeMapping(params.Capability.Type))
}
return vo.String(), nil
}
}
return "", err
}
func (c *client) getXPathValueInt(xpath string) (result int, err error) {
resp, err := c.doXPathRequest(xpath)
if err == nil {
if resp.ResponseBody.Reply != nil {
params := resp.ResponseBody.Reply.ResponseActions[0].ResponseCallbacks[0].Parameters
vo := reflect.ValueOf(params.Value)
if getTypeMapping(params.Capability.Type) != "int" {
return -1, errors.New("Expected response value to be of type int but was " + getTypeMapping(params.Capability.Type))
}
if vo.Type().String() == "float64" {
return int(vo.Float()), nil
} else if vo.Type().String() == "string" {
i, _ := strconv.Atoi(vo.String())
return i, nil
}
return int(vo.Int()), nil
}
}
return -1, err
}
func (c *client) getXPathValueInt64(xpath string) (result int64, err error) {
resp, err := c.doXPathRequest(xpath)
if err == nil {
if resp.ResponseBody.Reply != nil {
params := resp.ResponseBody.Reply.ResponseActions[0].ResponseCallbacks[0].Parameters
vo := reflect.ValueOf(params.Value)
if getTypeMapping(params.Capability.Type) != "int64" {
return -1, errors.New("Expected response value to be of type int64 but was " + getTypeMapping(params.Capability.Type))
}
if vo.Type().String() == "float64" {
return int64(vo.Float()), nil
} else if vo.Type().String() == "string" {
i, _ := strconv.Atoi(vo.String())
return int64(i), nil
}
return int64(vo.Int()), nil
}
}
return -1, err
}
func (c *client) getXPathValueBool(xpath string) (result bool, err error) {
resp, err := c.doXPathRequest(xpath)
if err == nil {
if resp.ResponseBody.Reply != nil {
params := resp.ResponseBody.Reply.ResponseActions[0].ResponseCallbacks[0].Parameters
vo := reflect.ValueOf(params.Value)
if getTypeMapping(params.Capability.Type) != "bool" {
return false, errors.New("Expected response value to be of type bool but was " + getTypeMapping(params.Capability.Type))
}
if vo.Type().String() == "string" {
b, _ := strconv.ParseBool(vo.String())
return b, nil
}
return vo.Bool(), nil
}
}
return false, err
}
func (c *client) getXPathValueType(xpath string, valueType reflect.Type) (result interface{}, err error) {
req := newXPathRequest(&c.authData, xpath, methodGetValue, nil)
resp, err := req.send()
if err != nil {
return nil, err
}
return getMapValue(resp, valueType), nil
}
func (c *client) getXPathValues(xpath string, valueType reflect.Type) (values []interface{}, err error) {
req := newXPathRequest(&c.authData, xpath, methodGetValue, nil)
resp, err := req.send()
if err == nil {
var values []interface{}
if resp.ResponseBody.Reply != nil {
for _, action := range resp.ResponseBody.Reply.ResponseActions {
c := action.ResponseCallbacks[0]
if c.XPath == xpath {
p := c.Parameters
if strings.HasPrefix(fmt.Sprintf("%s", p.Value), "[") {
v := reflect.New(valueType).Interface()
x, _ := json.Marshal(p.Value)
json.Unmarshal(x, v)
array := reflect.ValueOf(v).Elem()
for i := 0; i < array.Len(); i++ {
values = append(values, array.Index(i).Interface())
}
}
}
}
}
return values, nil
}
return nil, err
}
func (c *client) setXPathValue(xpath string, value interface{}) (err error) {
req := newXPathRequest(&c.authData, xpath, methodSetValue, value)
_, err = req.send()
return err
}
func (c *client) setXPathValues(actions []action) (err error) {
req := newMultiXPathRequest(&c.authData, actions)
_, err = req.send()
return err
}
func (c *client) addChildXPathValue(xpath string, value interface{}) (result int, err error) {
req := newXPathRequest(&c.authData, xpath, methodAddChild, value)
resp, err := req.send()
if err == nil {
responseXPath := resp.ResponseBody.Reply.ResponseActions[0].ResponseCallbacks[0].XPath
re := regexp.MustCompile("'(.*)'")
matches := re.FindAllStringSubmatch(responseXPath, -1)
if len(matches) > 0 {
return strconv.Atoi(matches[0][1])
}
}
return -1, err
}
func (c *client) deleteChildXPathValue(xpath string) (err error) {
req := newXPathRequest(&c.authData, xpath, methodDeleteValue, nil)
_, err = req.send()
return err
}
func (c *client) doXPathRequest(xpath string) (response *response, err error) {
return newXPathRequest(&c.authData, xpath, methodGetValue, nil).send()
}
func getMapValue(response *response, valueType reflect.Type) (result interface{}) {
var xPathValueType interface{}
if response.ResponseBody.Reply != nil {
params := response.ResponseBody.Reply.ResponseActions[0].ResponseCallbacks[0].Parameters
if strings.HasPrefix(fmt.Sprintf("%s", params.Value), "map[") {
v := reflect.New(valueType).Interface()
x, _ := json.Marshal(params.Value)
json.Unmarshal(x, v)
xPathValueType = reflect.ValueOf(v).Interface()
}
}
return xPathValueType
}