-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.go
140 lines (126 loc) · 3.68 KB
/
agent.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
package dpapi
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
"github.com/waok8s/wao-core/pkg/metrics"
"github.com/waok8s/wao-core/pkg/util"
)
type DeltaPAgent struct {
// address contains scheme, host and port.
// E.g., "http://10.0.0.1:8080"
address string
// sensorName contains sensorName.
// E.g., "101037B"
sensorName string
// nodeName contains nodeName.
// E.g., "node0"
nodeName string
// nodeIP contains node's IPv4 address.
// E.g., "10.0.0.2"
nodeIP string
client *http.Client
editorFns []util.RequestEditorFn
}
var _ metrics.Agent = (*DeltaPAgent)(nil)
// NewDeltaPAgent inits the client.
// At least one of sensorName, nodeName or nodeIP must be specified.
func NewDeltaPAgent(address string, sensorName, nodeName, nodeIP string, insecureSkipVerify bool, timeout time.Duration, editorFns ...util.RequestEditorFn) *DeltaPAgent {
return &DeltaPAgent{
address: address,
sensorName: sensorName,
nodeName: nodeName,
nodeIP: nodeIP,
client: &http.Client{
Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: insecureSkipVerify}},
Timeout: timeout,
},
editorFns: editorFns,
}
}
// Endpoint constructs the API endpoint.
//
// There is 3 types of URL.
// - Get value by sensor name http://hogefuga:12345/api/sensor/2027B30
// - Get value by node name http://hogefuga:12345/api/sensor/by_nodename/node-0
// - Get value by node IP http://hogefuga:12345/api/sensor/by_nodeaddress/10.10.0.1
func (a *DeltaPAgent) Endpoint() (string, error) {
switch {
case a.sensorName != "":
return url.JoinPath(a.address, "api", "sensor", a.sensorName)
case a.nodeName != "":
return url.JoinPath(a.address, "api", "sensor", "by_nodename", a.nodeName)
case a.nodeIP != "":
return url.JoinPath(a.address, "api", "sensor", "by_nodeaddress", a.nodeIP)
default:
return "", fmt.Errorf("could not construct endpoint from %+v", a)
}
}
// differentialPressureAPIResponse holds a response.
//
// e.g.
//
// {
// "code": 200,
// "sensor": [
// {
// "pressure": 0.01,
// "sensorid": "101037B",
// "temperature": 26.02
// }
// ]
// }
type differentialPressureAPIResponse struct {
StatusCode int `json:"code"`
Sensors []sensorValue `json:"sensor"`
}
type sensorValue struct {
SensorID string `json:"sensorid"`
Pressure float64 `json:"pressure"`
Temperature float64 `json:"temperature"`
}
func (a *DeltaPAgent) GetSensorValue(ctx context.Context) (sensorValue, error) {
var v sensorValue
url, err := a.Endpoint()
if err != nil {
return v, fmt.Errorf("unable to get endpoint URL: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return v, fmt.Errorf("unable to create HTTP request: %w", err)
}
for i, f := range a.editorFns {
if err := f(ctx, req); err != nil {
return v, fmt.Errorf("editorFns[%d] got error: %w", i, err)
}
}
resp, err := a.client.Do(req)
if err != nil {
return v, fmt.Errorf("unable to send HTTP request: %w", err)
}
switch resp.StatusCode {
case http.StatusOK:
var apiResp differentialPressureAPIResponse
if err := json.NewDecoder(resp.Body).Decode(&apiResp); err != nil {
return v, fmt.Errorf("could not decode resp: %w", err)
}
if len(apiResp.Sensors) == 0 {
return v, fmt.Errorf("invalid response apiResp=%+v", apiResp)
}
return apiResp.Sensors[0], nil
default:
return v, fmt.Errorf("HTTP status=%s", resp.Status)
}
}
func (a *DeltaPAgent) Fetch(ctx context.Context) (float64, error) {
v, err := a.GetSensorValue(ctx)
if err != nil {
return 0.0, err
}
return v.Pressure, nil
}
func (a *DeltaPAgent) ValueType() metrics.ValueType { return metrics.ValueDeltaPressure }