-
Notifications
You must be signed in to change notification settings - Fork 1
/
processdata.go
185 lines (142 loc) · 5.19 KB
/
processdata.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
// Copyright 2022 Ralf Geschke. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package golrackpi
import (
"bytes"
"encoding/json"
"strings"
"io/ioutil"
"net/http"
)
// ProcessData specifies the structure of the response returned by a request to the "processdata" endpoint.
// Furthermore this type is used in the ProcessDataValues function to define an arbitrary number of moduleids and
// also an arbitrary number of their processdataids.
type ProcessData struct {
ModuleId string `json:"moduleid"`
ProcessDataIds []string `json:"processdataids"`
}
// ProcessDataValue specifies the structure of a single processdata value with its Unit, Processdata-ID and Value
type ProcessDataValue struct {
Unit string `json:"unit"`
Id string `json:"id"`
Value interface{} `json:"value"`
}
// ProcessDataValues specifies the structure of the response returned by a request for processdata with moduleid and
// a slice of ProcessDataValue which contains the fields Unid, Id and Value
type ProcessDataValues struct {
ModuleId string `json:"moduleid"`
ProcessData []ProcessDataValue `json:"processdata"`
}
// ProcessData returns a slice of ProcessData type, i.e. a list of modules with a list of their process-data identifiers
func (c *AuthClient) ProcessData() ([]ProcessData, error) {
processData := []ProcessData{}
client := http.Client{}
request, err := http.NewRequest("GET", c.getUrl("/api/v1/processdata"), nil)
if err != nil {
return processData, err
}
request.Header.Add("authorization", "Session "+c.SessionId)
response, err := client.Do(request)
if err != nil {
return processData, err
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return processData, err
}
err = json.Unmarshal(body, &processData)
if err != nil {
return processData, err
}
return processData, nil
}
// ProcessDataModule returns a slice of ProcessDataValues returned by the request to the "processdata/moduleid" endpoint.
// It takes a moduleid and returns all processdata ids and their values according to the moduleid.
func (c *AuthClient) ProcessDataModule(moduleId string) ([]ProcessDataValues, error) {
processDataValues := []ProcessDataValues{}
client := http.Client{}
request, err := http.NewRequest("GET", c.getUrl("/api/v1/processdata/"+moduleId), nil)
if err != nil {
return processDataValues, err
}
request.Header.Add("authorization", "Session "+c.SessionId)
response, err := client.Do(request)
if err != nil {
return processDataValues, err
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return processDataValues, err
}
err = json.Unmarshal(body, &processDataValues)
if err != nil {
return processDataValues, err
}
return processDataValues, nil
}
// ProcessDataModuleValues returns a slice of ProcessDataValues returned by a request of a moduleid and one or more of the processdataids which
// belongs to the moduleid.
func (c *AuthClient) ProcessDataModuleValues(moduleId string, processDataIds ...string) ([]ProcessDataValues, error) {
processDataValues := []ProcessDataValues{}
client := http.Client{}
var processDataString string
if len(processDataIds) > 1 {
processDataString = strings.TrimRight(strings.Join(processDataIds, ","), ",")
} else { // ok, this is not really necessary...
processDataString = processDataIds[0]
}
request, err := http.NewRequest("GET", c.getUrl("/api/v1/processdata/"+moduleId+"/"+processDataString), nil)
if err != nil {
return processDataValues, err
}
request.Header.Add("authorization", "Session "+c.SessionId)
response, err := client.Do(request)
if err != nil {
return processDataValues, err
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return processDataValues, err
}
err = json.Unmarshal(body, &processDataValues)
if err != nil {
return processDataValues, err
}
return processDataValues, nil
}
// ProcessDataValues returns a slice of ProcessDataValues by a request of an arbitrary number of modules with one or more processdataids
// according to the moduleid.
// It takes a slice of ProcessData as argument, so it's possible to submit several moduleids with an arbitrary number of their processdataids
// and get all processdata values with one request to the inverter.
func (c *AuthClient) ProcessDataValues(v []ProcessData) ([]ProcessDataValues, error) {
processDataValues := []ProcessDataValues{}
b, err := json.Marshal(v)
if err != nil {
return processDataValues, err
}
client := http.Client{}
request, err := http.NewRequest("POST", c.getUrl("/api/v1/processdata"), bytes.NewBuffer(b))
if err != nil {
return processDataValues, err
}
request.Header.Add("Content-Type", "application/json")
request.Header.Add("authorization", "Session "+c.SessionId)
response, err := client.Do(request)
if err != nil {
return processDataValues, err
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return processDataValues, err
}
err = json.Unmarshal(body, &processDataValues)
if err != nil {
return processDataValues, err
}
return processDataValues, nil
}