forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphoenix-charx.go
279 lines (221 loc) Β· 6.85 KB
/
phoenix-charx.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
package charger
import (
"encoding/binary"
"fmt"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/modbus"
"github.com/volkszaehler/mbmd/encoding"
)
const (
// holding and input return same values
charxRegName = 100
charxRegSwVersion = 110
charxRegNumControllers = 114
// per-unit registers
charxOffset = 1000
charxRegMeter = 112
charxRegVoltages = 232 // mV
charxRegCurrents = 238 // mA
charxRegPower = 244 // mW
charxRegEnergy = 250 // Wh
charxRegSoc = 264 // %
charxRegEvid = 265 // 10
charxRegRfid = 275 // 10
charxRegChargeTime = 287 // s
charxRegChargeEnergy = 289 // Wh
charxRegStatus = 299 // IEC 61851-1
charxRegEnable = 300
charxRegMaxCurrent = 301 // A
)
// PhoenixCharx is an api.Charger implementation for Phoenix CHARX controller
type PhoenixCharx struct {
conn *modbus.Connection
connector uint16
}
func init() {
registry.Add("phoenix-charx", NewPhoenixCharxFromConfig)
}
//go:generate go run ../cmd/tools/decorate.go -f decoratePhoenixCharx -b *PhoenixCharx -r api.Charger -t "api.Meter,CurrentPower,func() (float64, error)" -t "api.MeterEnergy,TotalEnergy,func() (float64, error)" -t "api.PhaseCurrents,Currents,func() (float64, float64, float64, error)" -t "api.PhaseVoltages,Voltages,func() (float64, float64, float64, error)"
// NewPhoenixCharxFromConfig creates a Phoenix charger from generic config
func NewPhoenixCharxFromConfig(other map[string]interface{}) (api.Charger, error) {
cc := struct {
modbus.TcpSettings `mapstructure:",squash"`
Connector uint16
}{
TcpSettings: modbus.TcpSettings{
ID: 1, // default
},
Connector: 1,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
wb, err := NewPhoenixCharx(cc.URI, cc.ID, cc.Connector)
if err != nil {
return nil, err
}
meter, err := wb.meter()
if err != nil {
return nil, err
}
if meter > 0 && meter != 65535 {
return decoratePhoenixCharx(wb, wb.currentPower, wb.totalEnergy, wb.currents, wb.voltages), nil
}
return wb, nil
}
// NewPhoenixCharx creates a Phoenix charger
func NewPhoenixCharx(uri string, id uint8, connector uint16) (*PhoenixCharx, error) {
conn, err := modbus.NewConnection(uri, "", "", 0, modbus.Tcp, id)
if err != nil {
return nil, err
}
log := util.NewLogger("charx")
conn.Logger(log.TRACE)
wb := &PhoenixCharx{
conn: conn,
connector: connector,
}
controllers, err := wb.controllers()
if err != nil {
return nil, err
}
if connector > controllers {
return nil, fmt.Errorf("invalid connector: %d", connector)
}
return wb, err
}
func (wb *PhoenixCharx) controllers() (uint16, error) {
b, err := wb.conn.ReadHoldingRegisters(charxRegNumControllers, 1)
if err != nil {
return 0, err
}
return binary.BigEndian.Uint16(b), nil
}
func (wb *PhoenixCharx) register(reg uint16) uint16 {
return wb.connector*charxOffset + reg
}
func (wb *PhoenixCharx) meter() (uint16, error) {
b, err := wb.conn.ReadHoldingRegisters(wb.register(charxRegMeter), 1)
if err != nil {
return 0, err
}
return encoding.Uint16(b), nil
}
// Status implements the api.Charger interface
func (wb *PhoenixCharx) Status() (api.ChargeStatus, error) {
b, err := wb.conn.ReadHoldingRegisters(wb.register(charxRegStatus), 1)
if err != nil {
return api.StatusNone, err
}
// TODO check IEC 61851-1 C1 state
state := string(b[0])
return api.ChargeStatusString(state)
}
// Enabled implements the api.Charger interface
func (wb *PhoenixCharx) Enabled() (bool, error) {
b, err := wb.conn.ReadHoldingRegisters(wb.register(charxRegEnable), 1)
if err != nil {
return false, err
}
return encoding.Uint16(b) == 1, nil
}
// Enable implements the api.Charger interface
func (wb *PhoenixCharx) Enable(enable bool) error {
b := make([]byte, 2)
if enable {
binary.BigEndian.PutUint16(b, 1)
}
_, err := wb.conn.WriteMultipleRegisters(wb.register(charxRegEnable), 1, b)
return err
}
// MaxCurrent implements the api.Charger interface
func (wb *PhoenixCharx) MaxCurrent(current int64) error {
if current < 6 {
return fmt.Errorf("invalid current %d", current)
}
b := make([]byte, 2)
binary.BigEndian.PutUint16(b, uint16(current))
_, err := wb.conn.WriteMultipleRegisters(wb.register(charxRegMaxCurrent), 1, b)
return err
}
var _ api.ChargeTimer = (*PhoenixCharx)(nil)
// ChargingTime implements the api.ChargeTimer interface
func (wb *PhoenixCharx) ChargingTime() (time.Duration, error) {
b, err := wb.conn.ReadHoldingRegisters(wb.register(charxRegChargeTime), 2)
if err != nil {
return 0, err
}
return time.Duration(encoding.Uint16(b)) * time.Second, nil
}
// currentPower implements the api.Meter interface
func (wb *PhoenixCharx) currentPower() (float64, error) {
b, err := wb.conn.ReadHoldingRegisters(wb.register(charxRegPower), 2)
if err != nil {
return 0, err
}
return float64(encoding.Int32(b)) / 1e3, nil
}
// totalEnergy implements the api.MeterEnergy interface
func (wb *PhoenixCharx) totalEnergy() (float64, error) {
b, err := wb.conn.ReadHoldingRegisters(wb.register(charxRegEnergy), 4)
if err != nil {
return 0, err
}
return float64(encoding.Int64(b)) / 1e3, nil
}
// currents implements the api.PhaseCurrents interface
func (wb *PhoenixCharx) currents() (float64, float64, float64, error) {
return wb.getPhaseValues(charxRegCurrents)
}
// voltages implements the api.PhaseVoltages interface
func (wb *PhoenixCharx) voltages() (float64, float64, float64, error) {
return wb.getPhaseValues(charxRegVoltages)
}
// getPhaseValues returns 3 sequential phase values
func (wb *PhoenixCharx) getPhaseValues(reg uint16) (float64, float64, float64, error) {
b, err := wb.conn.ReadHoldingRegisters(wb.register(reg), 6)
if err != nil {
return 0, 0, 0, err
}
var res [3]float64
for i := range res {
res[i] = float64(encoding.Int32(b[4*i:])) / 1e3
}
return res[0], res[1], res[2], nil
}
var _ api.Identifier = (*PhoenixCharx)(nil)
// Identify implements the api.Identifier interface
func (wb *PhoenixCharx) Identify() (string, error) {
b, err := wb.conn.ReadHoldingRegisters(wb.register(charxRegEvid), 10)
if err != nil {
return "", err
}
if res := bytesAsString(b); res != "" {
return res, nil
}
b, err = wb.conn.ReadHoldingRegisters(wb.register(charxRegRfid), 10)
if err != nil {
return "", err
}
return bytesAsString(b), nil
}
var _ api.Diagnosis = (*PhoenixCharx)(nil)
// Diagnose implements the api.Diagnosis interface
func (wb *PhoenixCharx) Diagnose() {
if b, err := wb.conn.ReadHoldingRegisters(charxRegName, 10); err == nil {
fmt.Printf("Name: %s\n", b)
}
if b, err := wb.conn.ReadHoldingRegisters(charxRegSwVersion, 4); err == nil {
fmt.Printf("Software version: %s\n", b)
}
controllers, err := wb.controllers()
if err == nil {
fmt.Printf("Controllers: %d\n", controllers)
}
meter, err := wb.meter()
if err == nil {
fmt.Printf("Meter: %d\n", meter)
}
}