-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
68 lines (65 loc) · 1.66 KB
/
controller.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
package abb
import (
"encoding/xml"
"fmt"
"io"
"net/http"
)
func (c *Client) GetControllerResources() (*ControllerResources, error) {
var ControllerResources ControllerResources
c.Client = c.DigestAuthenticate()
req, err := http.NewRequest("GET", "http://"+c.Host+"/ctrl", nil)
if err != nil {
return nil, err
}
resp, err := c.Client.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("HTTP Status Code: %d", resp.StatusCode)
}
resources_raw, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = xml.Unmarshal(resources_raw, &ControllerResources)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return &ControllerResources, nil
}
// Returns the actions that can be performed on the controller
func (c *Client) GetControllerActions() (*ControllerActions, error) {
var actions ControllerActionsHTML
var actions_struct ControllerActions
c.Client = c.DigestAuthenticate()
req, err := http.NewRequest("GET", "http://"+c.Host+"/ctrl", nil)
if err != nil {
return nil, err
}
q := req.URL.Query()
q.Add("action", "show")
req.URL.RawQuery = q.Encode()
resp, err := c.Client.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("HTTP Status Code: %d", resp.StatusCode)
}
actions_raw, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = xml.Unmarshal(actions_raw, &actions)
if err != nil {
return nil, err
}
defer resp.Body.Close()
for _, option := range actions.Body.Div.Select.Options {
actions_struct.Actions = append(actions_struct.Actions, option.Value)
}
return &actions_struct, nil
}