forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphoenix-ev-ser.go
98 lines (76 loc) · 2.26 KB
/
phoenix-ev-ser.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
package charger
import (
"fmt"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/modbus"
)
const (
phxEVSerRegEnable = 20000 // Coil
phxEVSerRegMaxCurrent = 22000 // Holding
phxEVSerRegStatus = 24000 // Input
)
// PhoenixEVSer is an api.Charger implementation for Phoenix EV-CC-AC1-M wallboxes.
// It uses Modbus RTU to communicate with the wallbox at configurable modbus client.
type PhoenixEVSer struct {
conn *modbus.Connection
}
func init() {
registry.Add("phoenix-ev-ser", NewPhoenixEVSerFromConfig)
}
// NewPhoenixEVSerFromConfig creates a Phoenix charger from generic config
func NewPhoenixEVSerFromConfig(other map[string]interface{}) (api.Charger, error) {
cc := modbus.Settings{
ID: 1,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
return NewPhoenixEVSer(cc.URI, cc.Device, cc.Comset, cc.Baudrate, cc.Protocol(), cc.ID)
}
// NewPhoenixEVSer creates a Phoenix charger
func NewPhoenixEVSer(uri, device, comset string, baudrate int, proto modbus.Protocol, id uint8) (*PhoenixEVSer, error) {
conn, err := modbus.NewConnection(uri, device, comset, baudrate, proto, id)
if err != nil {
return nil, err
}
log := util.NewLogger("ev-ser")
conn.Logger(log.TRACE)
wb := &PhoenixEVSer{
conn: conn,
}
return wb, nil
}
// Status implements the api.Charger interface
func (wb *PhoenixEVSer) Status() (api.ChargeStatus, error) {
b, err := wb.conn.ReadInputRegisters(phxEVSerRegStatus, 1)
if err != nil {
return api.StatusNone, err
}
return api.ChargeStatusString(string(b[0]))
}
// Enabled implements the api.Charger interface
func (wb *PhoenixEVSer) Enabled() (bool, error) {
b, err := wb.conn.ReadCoils(phxEVSerRegEnable, 1)
if err != nil {
return false, err
}
return b[0] == 1, nil
}
// Enable implements the api.Charger interface
func (wb *PhoenixEVSer) Enable(enable bool) error {
var u uint16
if enable {
u = modbus.CoilOn
}
_, err := wb.conn.WriteSingleCoil(phxEVSerRegEnable, u)
return err
}
// MaxCurrent implements the api.Charger interface
func (wb *PhoenixEVSer) MaxCurrent(current int64) error {
if current < 6 {
return fmt.Errorf("invalid current %d", current)
}
_, err := wb.conn.WriteSingleRegister(phxEVSerRegMaxCurrent, uint16(current))
return err
}