forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhardybarth-ecb1.go
236 lines (184 loc) Β· 5.74 KB
/
hardybarth-ecb1.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
package charger
// LICENSE
// Copyright (c) 2019-2022 andig
// This module is NOT covered by the MIT license. All rights reserved.
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import (
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/charger/echarge"
"github.com/evcc-io/evcc/charger/echarge/ecb1"
"github.com/evcc-io/evcc/meter/obis"
"github.com/evcc-io/evcc/provider"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
"github.com/evcc-io/evcc/util/sponsor"
)
// http://apidoc.ecb1.de
// https://github.com/evcc-io/evcc/discussions/778
// https://ee-toolkit.com/electric-car-automated-charging
// HardyBarth charger implementation
type HardyBarth struct {
*request.Helper
uri string
chargecontrol int
meterG func() (ecb1.Meter, error)
}
func init() {
registry.Add("hardybarth-ecb1", NewHardyBarthFromConfig)
}
// NewHardyBarthFromConfig creates a HardyBarth cPH1 charger from generic config
func NewHardyBarthFromConfig(other map[string]interface{}) (api.Charger, error) {
cc := struct {
URI string
ChargeControl int
Meter int
Cache time.Duration
}{
ChargeControl: 1,
Meter: 1,
Cache: time.Second,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
return NewHardyBarth(cc.URI, cc.ChargeControl, cc.Meter, cc.Cache)
}
// NewHardyBarth creates HardyBarth charger
func NewHardyBarth(uri string, chargecontrol, meter int, cache time.Duration) (api.Charger, error) {
log := util.NewLogger("ecb1")
uri = strings.TrimSuffix(uri, "/") + "/api/v1"
wb := &HardyBarth{
Helper: request.NewHelper(log),
uri: util.DefaultScheme(uri, "http"),
chargecontrol: chargecontrol,
}
// cache meter readings
wb.meterG = provider.Cached(func() (ecb1.Meter, error) {
var res struct {
Meter struct {
ecb1.Meter
}
}
uri := fmt.Sprintf("%s/meters/%d", wb.uri, meter)
err := wb.GetJSON(uri, &res)
return res.Meter.Meter, err
}, cache)
if !sponsor.IsAuthorized() {
return nil, api.ErrSponsorRequired
}
uri = fmt.Sprintf("%s/chargecontrols/%d/mode", wb.uri, wb.chargecontrol)
data := url.Values{"mode": {echarge.ModeManual}}
err := wb.post(uri, data)
return wb, err
}
func (wb *HardyBarth) getChargeControl() (ecb1.ChargeControl, error) {
uri := fmt.Sprintf("%s/chargecontrols/%d", wb.uri, wb.chargecontrol)
var res struct {
ChargeControl struct {
ecb1.ChargeControl
}
}
err := wb.GetJSON(uri, &res)
return res.ChargeControl.ChargeControl, err
}
// Status implements the api.Charger interface
func (wb *HardyBarth) Status() (api.ChargeStatus, error) {
resp, err := wb.getChargeControl()
if err != nil {
return api.StatusNone, err
}
if resp.State == "" {
return api.StatusNone, errors.New("invalid state- check controller type (eCB1 vs Salia)")
}
res := api.StatusA
if resp.Connected {
res = api.StatusB
if resp.StateID == 5 {
res = api.StatusC
}
}
return res, nil
}
// Enabled implements the api.Charger interface
func (wb *HardyBarth) Enabled() (bool, error) {
res, err := wb.getChargeControl()
if err == nil && res.Mode != echarge.ModeManual {
err = fmt.Errorf("invalid mode: %s", res.Mode)
}
return res.StateID != 17, err
}
// Enable implements the api.Charger interface
func (wb *HardyBarth) Enable(enable bool) error {
action := "stop"
if enable {
action = "start"
}
uri := fmt.Sprintf("%s/chargecontrols/%d/%s", wb.uri, wb.chargecontrol, action)
req, err := request.New(http.MethodPost, uri, nil, request.JSONEncoding)
if err == nil {
_, err = wb.DoBody(req)
}
return err
}
func (wb *HardyBarth) post(uri string, data url.Values) error {
resp, err := wb.PostForm(uri, data)
if err == nil {
defer resp.Body.Close()
if resp.StatusCode >= http.StatusBadRequest {
err = fmt.Errorf("invalid status: %s", resp.Status)
}
}
return err
}
// MaxCurrent implements the api.Charger interface
func (wb *HardyBarth) MaxCurrent(current int64) error {
uri := fmt.Sprintf("%s/chargecontrols/%d/mode/manual/ampere", wb.uri, wb.chargecontrol)
data := url.Values{"manualmodeamp": {fmt.Sprintf("%d", current)}}
return wb.post(uri, data)
}
var _ api.Meter = (*HardyBarth)(nil)
// CurrentPower implements the api.Meter interface
func (wb *HardyBarth) CurrentPower() (float64, error) {
res, err := wb.meterG()
if err != nil {
return 0, err
}
return res.Data[obis.PowerConsumption], nil
}
var _ api.MeterEnergy = (*HardyBarth)(nil)
// TotalEnergy implements the api.MeterEnergy interface
func (wb *HardyBarth) TotalEnergy() (float64, error) {
res, err := wb.meterG()
if err != nil {
return 0, err
}
return res.Data[obis.EnergyConsumption], nil
}
var _ api.MeterCurrent = (*HardyBarth)(nil)
// Currents implements the api.MeterCurrent interface
func (wb *HardyBarth) Currents() (float64, float64, float64, error) {
res, err := wb.meterG()
if err != nil {
return 0, 0, 0, err
}
return res.Data[obis.CurrentL1], res.Data[obis.CurrentL2], res.Data[obis.CurrentL3], nil
}
// var _ api.Identifier = (*HardyBarth)(nil)
// // Identify implements the api.Identifier interface
// func (wb *HardyBarth) Identify() (string, error) {
// return "", api.ErrNotAvailable
// }