forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamperfied.go
358 lines (286 loc) Β· 9.05 KB
/
amperfied.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
package charger
// LICENSE
// Copyright (c) 2023 premultiply
// 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 (
"encoding/binary"
"encoding/hex"
"fmt"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/modbus"
"github.com/evcc-io/evcc/util/sponsor"
)
// Amperfied charger implementation
type Amperfied struct {
log *util.Logger
conn *modbus.Connection
current uint16
wakeup bool
}
const (
ampRegChargingState = 5 // Input
ampRegCurrents = 6 // Input 6,7,8
ampRegTemperature = 9 // Input
ampRegVoltages = 10 // Input 10,11,12
ampRegPower = 14 // Input
ampRegEnergy = 17 // Input
ampRegTimeoutConfig = 257 // Holding
ampRegRemoteLock = 259 // Holding
ampRegAmpsConfig = 261 // Holding
ampRegFailSafeConfig = 262 // Holding
ampRegPhaseSwitchControl = 501 // Holding
ampRegPhaseSwitchState = 5001 // Input
ampRegRfidUID = 2002 // Input
)
func init() {
registry.Add("amperfied", NewAmperfiedFromConfig)
}
//go:generate go run ../cmd/tools/decorate.go -f decorateAmperfied -b *Amperfied -r api.Charger -t "api.PhaseSwitcher,Phases1p3p,func(int) error" -t "api.PhaseGetter,GetPhases,func() (int, error)"
// NewAmperfiedFromConfig creates a Amperfied charger from generic config
func NewAmperfiedFromConfig(other map[string]interface{}) (api.Charger, error) {
cc := struct {
modbus.TcpSettings `mapstructure:",squash"`
Phases1p3p bool
}{
TcpSettings: modbus.TcpSettings{
ID: 255,
},
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
return NewAmperfied(cc.URI, cc.ID, cc.Phases1p3p)
}
// NewAmperfied creates Amperfied charger
func NewAmperfied(uri string, slaveID uint8, phases bool) (api.Charger, error) {
conn, err := modbus.NewConnection(uri, "", "", 0, modbus.Tcp, slaveID)
if err != nil {
return nil, err
}
if !sponsor.IsAuthorized() {
return nil, api.ErrSponsorRequired
}
log := util.NewLogger("amperfied")
conn.Logger(log.TRACE)
wb := &Amperfied{
log: log,
conn: conn,
current: 60, // assume min current
}
// get failsafe timeout from charger
b, err := wb.conn.ReadHoldingRegisters(ampRegTimeoutConfig, 1)
if err != nil {
return nil, fmt.Errorf("failsafe timeout: %w", err)
}
if u := binary.BigEndian.Uint16(b); u > 0 {
go wb.heartbeat(time.Duration(u) * time.Millisecond / 2)
}
var phases1p3p func(int) error
var phasesG func() (int, error)
if phases {
phases1p3p = wb.phases1p3p
phasesG = wb.getPhases
}
return decorateAmperfied(wb, phases1p3p, phasesG), nil
}
func (wb *Amperfied) heartbeat(timeout time.Duration) {
for range time.Tick(timeout) {
if _, err := wb.Status(); err != nil {
wb.log.ERROR.Println("heartbeat:", err)
}
}
}
func (wb *Amperfied) set(reg, val uint16) error {
b := make([]byte, 2)
binary.BigEndian.PutUint16(b, val)
_, err := wb.conn.WriteMultipleRegisters(reg, 1, b)
return err
}
// Status implements the api.Charger interface
func (wb *Amperfied) Status() (api.ChargeStatus, error) {
b, err := wb.conn.ReadInputRegisters(ampRegChargingState, 1)
if err != nil {
return api.StatusNone, err
}
sb := binary.BigEndian.Uint16(b)
if sb != 10 {
wb.wakeup = false
}
switch sb {
case 2, 3:
return api.StatusA, nil
case 4, 5:
return api.StatusB, nil
case 6, 7:
return api.StatusC, nil
case 8:
return api.StatusD, nil
case 9:
return api.StatusE, nil
case 10:
// ensure RemoteLock is disabled after wake-up
b, err := wb.conn.ReadHoldingRegisters(ampRegRemoteLock, 1)
if err != nil {
return api.StatusNone, err
}
// unlock
if binary.BigEndian.Uint16(b) != 1 {
if err := wb.set(ampRegRemoteLock, 1); err != nil {
return api.StatusNone, err
}
}
// keep status B2 during wakeup
if wb.wakeup {
return api.StatusB, nil
}
return api.StatusF, nil
default:
return api.StatusNone, fmt.Errorf("invalid status: %d", sb)
}
}
// Enabled implements the api.Charger interface
func (wb *Amperfied) Enabled() (bool, error) {
b, err := wb.conn.ReadHoldingRegisters(ampRegAmpsConfig, 1)
if err != nil {
return false, err
}
cur := binary.BigEndian.Uint16(b)
enabled := cur != 0
if enabled {
wb.current = cur
}
return enabled, nil
}
// Enable implements the api.Charger interface
func (wb *Amperfied) Enable(enable bool) error {
var cur uint16
if enable {
cur = wb.current
}
b := make([]byte, 2)
binary.BigEndian.PutUint16(b, cur)
_, err := wb.conn.WriteMultipleRegisters(ampRegAmpsConfig, 1, b)
return err
}
// MaxCurrent implements the api.Charger interface
func (wb *Amperfied) MaxCurrent(current int64) error {
return wb.MaxCurrentMillis(float64(current))
}
var _ api.ChargerEx = (*Amperfied)(nil)
// MaxCurrentMillis implements the api.ChargerEx interface
func (wb *Amperfied) MaxCurrentMillis(current float64) error {
if current < 6 {
return fmt.Errorf("invalid current %.1f", current)
}
cur := uint16(10 * current)
b := make([]byte, 2)
binary.BigEndian.PutUint16(b, cur)
_, err := wb.conn.WriteMultipleRegisters(ampRegAmpsConfig, 1, b)
if err == nil {
wb.current = cur
}
return err
}
var _ api.Meter = (*Amperfied)(nil)
// CurrentPower implements the api.Meter interface
func (wb *Amperfied) CurrentPower() (float64, error) {
b, err := wb.conn.ReadInputRegisters(ampRegPower, 1)
if err != nil {
return 0, err
}
return float64(binary.BigEndian.Uint16(b)), nil
}
var _ api.MeterEnergy = (*Amperfied)(nil)
// TotalEnergy implements the api.MeterEnergy interface
func (wb *Amperfied) TotalEnergy() (float64, error) {
b, err := wb.conn.ReadInputRegisters(ampRegEnergy, 2)
if err != nil {
return 0, err
}
return float64(binary.BigEndian.Uint32(b)) / 1e3, nil
}
// getPhaseValues returns 3 sequential register values
func (wb *Amperfied) getPhaseValues(reg uint16, divider float64) (float64, float64, float64, error) {
b, err := wb.conn.ReadInputRegisters(reg, 3)
if err != nil {
return 0, 0, 0, err
}
var res [3]float64
for i := range res {
res[i] = float64(binary.BigEndian.Uint16(b[2*i:])) / divider
}
return res[0], res[1], res[2], nil
}
var _ api.PhaseCurrents = (*Amperfied)(nil)
// Currents implements the api.PhaseCurrents interface
func (wb *Amperfied) Currents() (float64, float64, float64, error) {
return wb.getPhaseValues(ampRegCurrents, 10)
}
var _ api.PhaseVoltages = (*Amperfied)(nil)
// Voltages implements the api.PhaseVoltages interface
func (wb *Amperfied) Voltages() (float64, float64, float64, error) {
return wb.getPhaseValues(ampRegVoltages, 1)
}
var _ api.Identifier = (*Amperfied)(nil)
// identify implements the api.Identifier interface
func (wb *Amperfied) Identify() (string, error) {
b, err := wb.conn.ReadInputRegisters(ampRegRfidUID, 6)
if err != nil {
return "", err
}
return hex.EncodeToString(b), nil
}
var _ api.Diagnosis = (*Amperfied)(nil)
// Diagnose implements the api.Diagnosis interface
func (wb *Amperfied) Diagnose() {
if b, err := wb.conn.ReadInputRegisters(ampRegTemperature, 1); err == nil {
fmt.Printf("Temperature:\t%.1fC\n", float64(int16(binary.BigEndian.Uint16(b)))/10)
}
if b, err := wb.conn.ReadHoldingRegisters(ampRegTimeoutConfig, 1); err == nil {
fmt.Printf("Timeout:\t%d\n", binary.BigEndian.Uint16(b))
}
if b, err := wb.conn.ReadHoldingRegisters(ampRegRemoteLock, 1); err == nil {
fmt.Printf("Remote Lock:\t%d\n", binary.BigEndian.Uint16(b))
}
if b, err := wb.conn.ReadHoldingRegisters(ampRegFailSafeConfig, 1); err == nil {
fmt.Printf("FailSafe:\t%d\n", binary.BigEndian.Uint16(b))
}
}
var _ api.Resurrector = (*Amperfied)(nil)
// WakeUp implements the api.Resurrector interface
func (wb *Amperfied) WakeUp() error {
// force status F by locking
if err := wb.set(ampRegRemoteLock, 0); err == nil {
// Takes at least ~10 sec to return to normal operation
// after locking even if unlocking immediately.
wb.wakeup = true
}
// return to normal operation by unlocking after ~10 sec
return wb.set(ampRegRemoteLock, 1)
}
// phases1p3p implements the api.PhaseSwitcher interface
func (wb *Amperfied) phases1p3p(phases int) error {
b := make([]byte, 2)
binary.BigEndian.PutUint16(b, uint16(phases))
_, err := wb.conn.WriteMultipleRegisters(ampRegPhaseSwitchControl, 1, b)
return err
}
// getPhases implements the api.PhaseGetter interface
func (wb *Amperfied) getPhases() (int, error) {
b, err := wb.conn.ReadInputRegisters(ampRegPhaseSwitchState, 1)
if err != nil {
return 0, err
}
return int(binary.BigEndian.Uint16(b)), nil
}