-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapi.go
413 lines (347 loc) · 9.27 KB
/
api.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package growatt
import (
"bytes"
"crypto/md5"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"time"
)
const (
apiBasePath = "https://server-api.growatt.com"
apiLoginPath = "/newTwoLoginAPI.do"
apiCookieSessionID = "JSESSIONID"
apiCookieServerID = "SERVERID"
)
var (
// ErrAPILogin is the error representing API login failures
ErrAPILogin = errors.New("API Error: login failed")
)
// API represents the API client structure
type API struct {
username string
password string
sessionID string
serverID string
UserID int
}
// NewAPI returns a new API struct configured with authentication details
func NewAPI(username, password string) *API {
return &API{
username: username,
password: password,
}
}
// GetPlantEnergy returns detailed energy information for plant with given PlantID
// for given period of time
// Based on the format of the given date, the timespan will be either:
// - total (empty string, "")
// - year (eg. "2019")
// - month (eg. "2019-01")
// - day (eg. "2019-01-01")
// any other format will result in error
func (a API) GetPlantEnergy(plantID int, date string) ([]TimeEnergy, error) {
var timespan int
var dateLayout string
dateFormat := "%s"
switch {
case regexp.MustCompile(`^\d+$`).MatchString(date): // year
timespan = 3
dateFormat = fmt.Sprintf("%s-%%s", date)
dateLayout = "2006-01"
case regexp.MustCompile(`^\d+-\d+$`).MatchString(date): // month
timespan = 2
dateFormat = fmt.Sprintf("%s-%%s", date)
dateLayout = "2006-01-02"
case regexp.MustCompile(`^\d+-\d+-\d+$`).MatchString(date): // day
timespan = 1
dateLayout = "2006-01-02 15:04"
case date == "":
timespan = 4
dateLayout = "2006"
default:
return nil, fmt.Errorf("could not parse timespan %s", date)
}
val := url.Values{}
val.Add("plantId", fmt.Sprintf("%d", plantID))
val.Add("type", fmt.Sprintf("%d", timespan))
val.Add("date", date)
data, err := a.call("GET", "newPlantDetailAPI.do", val.Encode())
if err != nil {
return nil, err
}
var r struct {
X map[string]interface{} `json:"-"` // we don't know all the keys upfront
}
if err := json.Unmarshal(data, &r.X); err != nil {
return nil, err
}
if _, ok := r.X["back"]; !ok {
return nil, errors.New("could not unmarshal energy data")
}
energyData, ok := r.X["back"].(map[string]interface{})
if !ok {
return nil, errors.New("could not convert energy data")
}
list, ok := energyData["data"].(map[string]interface{})
if !ok {
return nil, errors.New("could not convert energy data")
}
var result []TimeEnergy
for k, v := range list {
normalizedDate := fmt.Sprintf(dateFormat, k)
ts, err := time.Parse(dateLayout, normalizedDate)
if err != nil {
continue
}
pwr, err := strconv.ParseFloat(v.(string), 64)
if err != nil {
continue
}
result = append(result, TimeEnergy{
Timestamp: ts,
Power: pwr,
})
}
return result, nil
}
// GetPlantList get's a list of plants from the API
func (a API) GetPlantList() ([]Plant, error) {
data, err := a.call("GET", "/PlantListAPI.do", "")
if err != nil {
return nil, err
}
var r struct {
Back struct {
Data []plantData `json:"data"`
/*
There is more JSON data being returned, but we're currently not interested
in that data in this context
TotalData struct {
CurrentPowerSum string `json:"currentPowerSum"`
CO2Sum string `json:"CO2Sum"`
IsHaveStorage string `json:"isHaveStorage"`
ETotalMoneyText string `json:"eTotalMoneyText"`
TodayEnergySum string `json:"todayEnergySum"`
TotalEnergySum string `json:"totalEnergySum"`
} `json:"totalData"`
Success bool `json:"success"`
*/
} `json:"back"`
}
if err := json.Unmarshal(data, &r); err != nil {
return nil, err
}
var plants []Plant
for _, p := range r.Back.Data {
plants = append(plants, parsePlantData(p))
}
return plants, nil
}
// GetPlantInverterList returns a list of Inverter objects for given plant
func (a API) GetPlantInverterList(plantID int) ([]Inverter, error) {
val := url.Values{}
val.Add("op", "getAllDeviceListThree")
val.Add("plantId", fmt.Sprintf("%d", plantID))
val.Add("pageNum", "1")
val.Add("pageSize", "1")
data, err := a.call("GET", "/newPlantAPI.do", val.Encode())
if err != nil {
return nil, err
}
var r struct {
Devices []inverterData `json:"deviceList"`
}
if err := json.Unmarshal(data, &r); err != nil {
return nil, err
}
var result []Inverter
for _, d := range r.Devices {
result = append(result, parseInvertedData(d))
}
return result, nil
}
// GetInverterEnergy returns detailed energy information for an inverter with
// given serial number for the given date. While GetPlantEnergy does support to
// return result for multiple timespans (day, month, year) GetInverterEnergy only
// supports results for a timespan of a day
func (a API) GetInverterEnergy(inverterSerial string, date time.Time) ([]TimeEnergy, error) {
// prepare request
val := url.Values{}
val.Add("op", "getInverterData")
val.Add("id", inverterSerial)
// Not really sure what type 1 is, but different types seem to be returning
// values in different units. It's unlike the type of GetPlantEnergy which
// determines the timespan over which you want details
val.Add("type", "1")
val.Add("date", date.Format("2006-01-02"))
data, err := a.call("GET", "newInverterAPI.do", val.Encode())
if err != nil {
return nil, err
}
var r struct {
X map[string]interface{} `json:"-"` // we don't know the JSON keys
}
if err := json.Unmarshal(data, &r.X); err != nil {
return nil, err
}
pacData, ok := r.X["invPacData"].(map[string]interface{})
if !ok {
return nil, errors.New("could not convert energy data")
}
var result []TimeEnergy
for k, v := range pacData {
ts, err := time.Parse("2006-01-02 15:04", k)
if err != nil {
continue
}
result = append(result, TimeEnergy{
Timestamp: ts,
Power: v.(float64),
})
}
return result, nil
}
func (a *API) call(method, path, data string) ([]byte, error) {
// if API hasn't logged in yet, do so now
if !a.isLoggedIn() && path != apiLoginPath {
if err := a.login(); err != nil {
return nil, err
}
}
// prepare HTTP request
var url string
var req *http.Request
var err error
switch method {
case "POST":
url = getAPIURL(path, "")
req, err = http.NewRequest(method, url, strings.NewReader(data))
case "GET":
url = getAPIURL(path, data)
req, err = http.NewRequest(method, url, nil)
}
// we couldn't prepare the request
if err != nil {
return nil, err
}
// make sure to set proper content-type when sending data
if method == "POST" {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
// add SessionID and ServerID cookie if we're not currently trying to login
if path != apiLoginPath {
req.AddCookie(&http.Cookie{
Name: apiCookieSessionID,
Value: a.sessionID,
})
req.AddCookie(&http.Cookie{
Name: apiCookieServerID,
Value: a.serverID,
})
}
// do HTTP request
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
switch resp.StatusCode {
case 200: // do nothing, this is OK
default:
return nil, fmt.Errorf("API HTTP error: %s", resp.Status)
}
// read entire response body
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var aErr struct {
Back struct {
ErrCode string `json:"errCode"`
Succces bool `json:"success"`
} `json:"back"`
}
// if we succeed in unmarshalling the response into an API error, we'll just
// return that error
if err := json.Unmarshal(b, &aErr); err == nil && aErr.Back.ErrCode != "" {
switch aErr.Back.ErrCode {
case "502":
return nil, ErrAPILogin
}
return nil, fmt.Errorf("API Error %s", aErr.Back.ErrCode)
}
// if this is a login attempt, record given sessionID and serverID
if path == apiLoginPath {
for _, c := range resp.Cookies() {
switch c.Name {
case apiCookieSessionID:
a.sessionID = c.Value
case apiCookieServerID:
a.serverID = c.Value
}
}
}
return b, nil
}
func (a *API) login() error {
// prepare form data
val := url.Values{}
val.Add("userName", a.username)
val.Add("password", hashPassword(a.password))
// do login request
data, err := a.call("POST", apiLoginPath, val.Encode())
if err != nil {
return err
}
var r struct {
Back struct {
User struct {
ID int `json:"id"`
} `json:"user"`
Success bool `json:"success"`
Message string `json:"msg"`
} `json:"back"`
}
if err := json.Unmarshal(data, &r); err != nil {
return err
}
if !r.Back.Success {
return fmt.Errorf("failed to login: %s", r.Back.Message)
}
// record relevant response meta data
a.UserID = r.Back.User.ID
return nil
}
func (a *API) isLoggedIn() bool {
return (len(a.sessionID) > 0) && (len(a.serverID) > 0)
}
func getAPIURL(path, query string) string {
glue := ""
if !strings.HasPrefix(path, "/") {
glue = "/"
}
if query != "" {
query = fmt.Sprintf("?%s", query)
}
return fmt.Sprintf("%s%s%s%s", apiBasePath, glue, path, query)
}
func hashPassword(passwd string) string {
var buf bytes.Buffer
// hash password as MD5
hash := md5.Sum([]byte(passwd))
// go over each byte and add 12 (hex C) if the byte is less than 16
for _, b := range hash {
if b <= 0xf {
b += 0xc0
}
buf.Write([]byte{b})
}
return fmt.Sprintf("%x", buf.String())
}