forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdadapower.go
244 lines (194 loc) Β· 6.31 KB
/
dadapower.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
package charger
import (
"encoding/binary"
"errors"
"fmt"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/modbus"
)
const (
dadapowerRegFailsafeTimeout = 102
dadapowerRegModel = 105
dadapowerRegSerial = 106 // 6
dadapowerRegFirmware = 112 // 6
dadapowerRegChargingAllowed = 1000
dadapowerRegChargeCurrentLimit = 1001
dadapowerRegActivePhases = 1002
dadapowerRegActiveEnergy = 1009
dadapowerRegPlugState = 1016
dadapowerRegEnergyImportSession = 1017
dadapowerRegEnergyImportTotal = 1025
dadapowerRegIdentification = 1040 // 20
)
var dadapowerRegCurrents = []uint16{1006, 1007, 1008}
// Dadapower charger implementation
type Dadapower struct {
log *util.Logger
conn *modbus.Connection
regOffset uint16
}
func init() {
registry.Add("dadapower", NewDadapowerFromConfig)
}
// NewDadapowerFromConfig creates a Dadapower charger from generic config
func NewDadapowerFromConfig(other map[string]interface{}) (api.Charger, error) {
cc := modbus.TcpSettings{}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
return NewDadapower(cc.URI, cc.ID)
}
// NewDadapower creates a Dadapower charger
func NewDadapower(uri string, id uint8) (*Dadapower, error) {
conn, err := modbus.NewConnection(uri, "", "", 0, modbus.Tcp, id)
if err != nil {
return nil, err
}
log := util.NewLogger("dadapower")
conn.Logger(log.TRACE)
wb := &Dadapower{
log: log,
conn: conn,
}
// 5min failsafe timeout
if _, err := wb.conn.WriteSingleRegister(dadapowerRegFailsafeTimeout, 5*60); err != nil {
return nil, fmt.Errorf("could not set failsafe timeout: %v", err)
}
// The charging station may have multiple charging ports - use offset for register addresses for each port
if id > 1 {
wb.regOffset = (uint16(id) - 1) * 1000
}
go wb.heartbeat()
return wb, nil
}
func (wb *Dadapower) heartbeat() {
for range time.NewTicker(time.Minute).C {
if _, err := wb.conn.ReadInputRegisters(dadapowerRegFailsafeTimeout, 1); err != nil {
wb.log.ERROR.Println("heartbeat:", err)
}
}
}
// Status implements the api.Charger interface
func (wb *Dadapower) Status() (api.ChargeStatus, error) {
b, err := wb.conn.ReadInputRegisters(dadapowerRegPlugState+wb.regOffset, 1)
if err != nil {
return api.StatusNone, err
}
switch binary.BigEndian.Uint16(b) {
case 0x0A: // ready
return api.StatusA, nil
case 0x0B: // EV is present
return api.StatusB, nil
case 0x0C: // charging
return api.StatusC, nil
case 0x0D: // charging with ventilation
return api.StatusD, nil
case 0x0E: // failure (e.g. diode check, RCD failure)
return api.StatusE, nil
default:
return api.StatusNone, errors.New("invalid response")
}
}
// Enabled implements the api.Charger interface
func (wb *Dadapower) Enabled() (bool, error) {
b, err := wb.conn.ReadInputRegisters(dadapowerRegChargingAllowed+wb.regOffset, 1)
if err != nil {
return false, err
}
return binary.BigEndian.Uint16(b) > 0, nil
}
// Enable implements the api.Charger interface
func (wb *Dadapower) Enable(enable bool) error {
var u uint16
if enable {
u = 1
}
_, err := wb.conn.WriteSingleRegister(dadapowerRegChargingAllowed+wb.regOffset, u)
return err
}
var _ api.ChargerEx = (*Dadapower)(nil)
// MaxCurrentMillis implements the api.ChargerEx interface
func (wb *Dadapower) MaxCurrentMillis(current float64) error {
if current < 6 {
return fmt.Errorf("invalid current %v", current)
}
_, err := wb.conn.WriteSingleRegister(dadapowerRegChargeCurrentLimit+wb.regOffset, uint16(current*100))
return err
}
// MaxCurrent implements the api.Charger interface
func (wb *Dadapower) MaxCurrent(current int64) error {
return wb.MaxCurrentMillis(float64(current))
}
var _ api.Meter = (*Dadapower)(nil)
// CurrentPower implements the api.Meter interface
func (wb *Dadapower) CurrentPower() (float64, error) {
b, err := wb.conn.ReadInputRegisters(dadapowerRegActiveEnergy+wb.regOffset, 2)
if err != nil {
return 0, err
}
return float64(binary.BigEndian.Uint32(b)), err
}
var _ api.MeterEnergy = (*Dadapower)(nil)
// TotalEnergy implements the api.MeterEnergy interface
func (wb *Dadapower) TotalEnergy() (float64, error) {
b, err := wb.conn.ReadInputRegisters(dadapowerRegEnergyImportTotal+wb.regOffset, 4)
if err != nil {
return 0, err
}
return float64(binary.BigEndian.Uint64(b)) / 1e3, err
}
var _ api.ChargeRater = (*Dadapower)(nil)
// ChargedEnergy implements the api.ChargeRater interface
func (wb *Dadapower) ChargedEnergy() (float64, error) {
b, err := wb.conn.ReadInputRegisters(dadapowerRegEnergyImportSession+wb.regOffset, 4)
if err != nil {
return 0, err
}
return float64(binary.BigEndian.Uint64(b)) / 1e3, err
}
var _ api.PhaseCurrents = (*Dadapower)(nil)
// Currents implements the api.PhaseCurrents interface
func (wb *Dadapower) Currents() (float64, float64, float64, error) {
var currents []float64
for _, regCurrent := range dadapowerRegCurrents {
b, err := wb.conn.ReadInputRegisters(regCurrent+wb.regOffset, 1)
if err != nil {
return 0, 0, 0, err
}
currents = append(currents, float64(binary.BigEndian.Uint16(b))/100)
}
return currents[0], currents[1], currents[2], nil
}
var _ api.PhaseSwitcher = (*Dadapower)(nil)
// Phases1p3p implements the api.PhaseSwitcher interface
func (wb *Dadapower) Phases1p3p(phases int) error {
_, err := wb.conn.WriteSingleRegister(dadapowerRegActivePhases+wb.regOffset, uint16(phases))
return err
}
var _ api.Identifier = (*Dadapower)(nil)
// Identify implements the api.Identifier interface
func (wb *Dadapower) Identify() (string, error) {
u, err := wb.conn.ReadInputRegisters(dadapowerRegIdentification+wb.regOffset, 20)
if err != nil {
return "", err
}
return bytesAsString(u), nil
}
var _ api.Diagnosis = (*Dadapower)(nil)
// Diagnose implements the api.Diagnosis interface
func (wb *Dadapower) Diagnose() {
if b, err := wb.conn.ReadInputRegisters(dadapowerRegModel, 1); err == nil {
fmt.Printf("Model:\t%d\n", b[1])
}
if b, err := wb.conn.ReadInputRegisters(dadapowerRegSerial, 6); err == nil {
fmt.Printf("Serial:\t%s\n", b)
}
if b, err := wb.conn.ReadInputRegisters(dadapowerRegFirmware, 6); err == nil {
fmt.Printf("Firmware:\t%s\n", b)
}
if b, err := wb.conn.ReadInputRegisters(dadapowerRegIdentification, 20); err == nil {
fmt.Printf("Identification:\t%s\n", b)
}
}