-
-
Notifications
You must be signed in to change notification settings - Fork 541
/
api.go
144 lines (113 loc) Β· 2.98 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
package bosch
import (
"fmt"
"net/http"
"net/http/cookiejar"
"strconv"
"strings"
"sync"
"time"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
"github.com/evcc-io/evcc/util/transport"
)
type API struct {
*request.Helper
uri string
status StatusResponse
login LoginResponse
updated time.Time
cache time.Duration
}
var Instances = new(sync.Map)
func NewLocal(log *util.Logger, uri string, cache time.Duration) *API {
api := &API{
Helper: request.NewHelper(log),
uri: util.DefaultScheme(strings.TrimSuffix(uri, "/"), "http"),
cache: cache,
}
// ignore the self signed certificate
api.Client.Transport = request.NewTripper(log, transport.Insecure())
// create cookie jar to save login tokens
api.Client.Jar, _ = cookiejar.New(nil)
return api
}
func (c *API) Login() (err error) {
req, err := request.New(http.MethodGet, c.uri, nil, nil)
if err != nil {
return err
}
body, err := c.DoBody(req)
if err != nil {
return err
}
return c.extractWuiSidFromBody(string(body))
}
func (c *API) Status() (StatusResponse, error) {
var err error
if time.Since(c.updated) > c.cache {
if err = c.updateValues(); err == nil {
c.updated = time.Now()
}
}
return c.status, err
}
func (c *API) extractWuiSidFromBody(body string) error {
index := strings.Index(body, "WUI_SID=")
if index < 0 || len(body) < index+9+15 {
c.login.wuSid = ""
return fmt.Errorf("error while extracting wui sid. body was= %s", body)
}
c.login.wuSid = body[index+9 : index+9+15]
return nil
}
func (c *API) updateValues() error {
data := "action=get.hyb.overview&flow=1"
uri := c.uri + "/cgi-bin/ipcclient.fcgi?" + c.login.wuSid
req, err := request.New(http.MethodPost, uri, strings.NewReader(data), map[string]string{
"Content-Type": "text/plain",
})
if err != nil {
return err
}
body, err := c.DoBody(req)
if err != nil {
return err
}
return c.extractValues(string(body))
}
func (c *API) extractValues(body string) error {
if strings.Contains(body, "session invalid") {
return c.Login()
}
values := strings.Split(body, "|")
if len(values) < 14 {
return fmt.Errorf("extractValues: response has not enough values")
}
soc, err := strconv.Atoi(values[3])
if err == nil {
c.status.CurrentBatterySoc = float64(soc)
c.status.SellToGrid, err = parseWattValue(values[11])
}
if err == nil {
c.status.BuyFromGrid, err = parseWattValue(values[14])
}
if err == nil {
c.status.PvPower, err = parseWattValue(values[2])
}
if err == nil {
c.status.BatteryChargePower, err = parseWattValue(values[10])
}
if err == nil {
c.status.BatteryDischargePower, err = parseWattValue(values[13])
}
return err
}
func parseWattValue(inputString string) (float64, error) {
if len(strings.TrimSpace(inputString)) == 0 || strings.Contains(inputString, "nbsp;") {
return 0.0, nil
}
num := strings.TrimSpace(strings.ReplaceAll(strings.ReplaceAll(inputString, "kW", " "), "von", " "))
res, err := strconv.ParseFloat(num, 64)
return res * 1000.0, err
}