-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.go
64 lines (56 loc) · 1.38 KB
/
stats.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
package solarman
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
)
func (c *Client) CurrentData(inverterSN string) (CurrentData, error) {
req, err := http.NewRequest(
http.MethodPost,
fmt.Sprintf(
baseURL+"/device/v1.0/currentData?appId=%s&language=en&=",
c.appID,
),
strings.NewReader(fmt.Sprintf(`{"deviceSn":%q}`, inverterSN)),
)
if err != nil {
return CurrentData{}, err
}
req.Header.Add("Content-Type", "application/json")
resp, err := c.c.Do(req)
if err != nil {
return CurrentData{}, err
}
defer func() {
_ = resp.Body.Close()
}()
bts, err := io.ReadAll(resp.Body)
if err != nil {
return CurrentData{}, err
}
var data CurrentData
if err := json.Unmarshal(bts, &data); err != nil {
return CurrentData{}, err
}
return data, nil
}
type CurrentData struct {
Code any `json:"code"`
Msg any `json:"msg"`
Success bool `json:"success"`
RequestID string `json:"requestId"`
DeviceSn string `json:"deviceSn"`
DeviceID int `json:"deviceId"`
DeviceType string `json:"deviceType"`
DeviceState int `json:"deviceState"`
CollectionTime int `json:"collectionTime"`
DataList []DataList `json:"dataList"`
}
type DataList struct {
Key string `json:"key"`
Value string `json:"value"`
Unit any `json:"unit"`
Name string `json:"name"`
}