forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnrgconnect.go
211 lines (170 loc) · 5.46 KB
/
nrgconnect.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
package charger
import (
"errors"
"fmt"
"net/http"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
)
// https://www.nrgkick.com/wp-content/uploads/2019/08/20190814_API-Dokumentation_04.pdf
const (
nrgSettings = "settings"
nrgMeasurements = "measurements"
)
// NRGMeasurements is the /api/measurements response
type NRGMeasurements struct {
Message string `json:"omitempty"` // api message if not ok
ChargingEnergy float64
ChargingEnergyOverAll float64
ChargingPower float64
ChargingPowerPhase [3]float64
ChargingCurrentPhase [3]float64
Frequency float64
}
// NRGSettings is the /api/settings request/response
type NRGSettings struct {
Message string `json:",omitempty"` // api message if not ok
Info NRGInfo `json:",omitempty"`
Values NRGValues
}
// NRGInfo is NRGSettings.Info
type NRGInfo struct {
Connected bool `json:",omitempty"`
}
// NRGValues is NRGSettings.Values
type NRGValues struct {
ChargingStatus NRGChargingStatus
ChargingCurrent NRGChargingCurrent
DeviceMetadata NRGDeviceMetadata
}
// NRGChargingStatus is NRGSettings.Values.ChargingStatus
type NRGChargingStatus struct {
Charging *bool `json:",omitempty"` // use pointer to allow omitting false
}
// NRGChargingCurrent is NRGSettings.Values.ChargingCurrent
type NRGChargingCurrent struct {
Value float64 `json:",omitempty"`
}
// NRGDeviceMetadata is NRGSettings.Values.DeviceMetadata
type NRGDeviceMetadata struct {
Password string
}
// NRGKickConnect charger implementation
type NRGKickConnect struct {
*request.Helper
uri string
mac string
password string
}
func init() {
registry.Add("nrgkick-connect", NewNRGKickConnectFromConfig)
}
// NewNRGKickConnectFromConfig creates a NRGKickConnect charger from generic config
func NewNRGKickConnectFromConfig(other map[string]interface{}) (api.Charger, error) {
cc := struct{ URI, Mac, Password string }{}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
return NewNRGKickConnect(cc.URI, cc.Mac, cc.Password)
}
// NewNRGKickConnect creates NRGKickConnect charger
func NewNRGKickConnect(uri, mac, password string) (*NRGKickConnect, error) {
nrg := &NRGKickConnect{
Helper: request.NewHelper(util.NewLogger("nrgconn")),
uri: util.DefaultScheme(uri, "http"),
mac: mac,
password: password,
}
return nrg, nil
}
func (nrg *NRGKickConnect) apiURL(api string) string {
return fmt.Sprintf("%s/api/%s/%s", nrg.uri, api, nrg.mac)
}
func (nrg *NRGKickConnect) putJSON(url string, data interface{}) error {
req, err := request.New(http.MethodPut, url, request.MarshalJSON(data))
if err == nil {
var resp struct {
Message string
}
if err = nrg.DoJSON(req, &resp); err != nil {
if resp.Message != "" {
return fmt.Errorf("response: %s", resp.Message)
}
}
}
return err
}
// Status implements the api.Charger interface
func (nrg *NRGKickConnect) Status() (api.ChargeStatus, error) {
return api.StatusC, nil
}
// Enabled implements the api.Charger interface
func (nrg *NRGKickConnect) Enabled() (bool, error) {
var res NRGSettings
err := nrg.GetJSON(nrg.apiURL(nrgSettings), &res)
if err != nil {
if res.Message != "" {
err = errors.New(res.Message)
}
return false, err
}
return *res.Values.ChargingStatus.Charging, nil
}
// Enable implements the api.Charger interface
func (nrg *NRGKickConnect) Enable(enable bool) error {
settings := NRGSettings{}
settings.Values.DeviceMetadata.Password = nrg.password
settings.Values.ChargingStatus.Charging = &enable
return nrg.putJSON(nrg.apiURL(nrgSettings), settings)
}
// MaxCurrent implements the api.Charger interface
func (nrg *NRGKickConnect) MaxCurrent(current int64) error {
settings := NRGSettings{}
settings.Values.DeviceMetadata.Password = nrg.password
settings.Values.ChargingCurrent.Value = float64(current)
return nrg.putJSON(nrg.apiURL(nrgSettings), settings)
}
var _ api.Meter = (*NRGKickConnect)(nil)
// CurrentPower implements the api.Meter interface
func (nrg *NRGKickConnect) CurrentPower() (float64, error) {
var res NRGMeasurements
err := nrg.GetJSON(nrg.apiURL(nrgMeasurements), &res)
if err != nil && res.Message != "" {
err = errors.New(res.Message)
}
return 1000 * res.ChargingPower, err
}
var _ api.MeterEnergy = (*NRGKickConnect)(nil)
// TotalEnergy implements the api.MeterEnergy interface
func (nrg *NRGKickConnect) TotalEnergy() (float64, error) {
var res NRGMeasurements
err := nrg.GetJSON(nrg.apiURL(nrgMeasurements), &res)
if err != nil && res.Message != "" {
err = errors.New(res.Message)
}
return res.ChargingEnergyOverAll, err
}
var _ api.MeterCurrent = (*NRGKickConnect)(nil)
// Currents implements the api.MeterCurrent interface
func (nrg *NRGKickConnect) Currents() (float64, float64, float64, error) {
var res NRGMeasurements
err := nrg.GetJSON(nrg.apiURL(nrgMeasurements), &res)
if err != nil && res.Message != "" {
err = errors.New(res.Message)
}
if len(res.ChargingCurrentPhase) != 3 {
return 0, 0, 0, fmt.Errorf("unexpected response: %v", res)
}
return res.ChargingCurrentPhase[0],
res.ChargingCurrentPhase[1],
res.ChargingCurrentPhase[2],
err
}
// ChargedEnergy implements the ChargeRater interface
// NOTE: apparently shows energy of a stopped charging session, hence substituted by TotalEnergy
// func (nrg *NRGKickConnect) ChargedEnergy() (float64, error) {
// var res NRGMeasurements
// err := nrg.GetJSON(nrg.apiURL(nrgMeasurements), &res)
// return res.ChargingEnergy, err
// }