forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalfen.go
251 lines (200 loc) Β· 5.64 KB
/
alfen.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
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 (
"encoding/binary"
"fmt"
"math"
"sync"
"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"
"github.com/volkszaehler/mbmd/meters/rs485"
)
// https://github.com/evcc-io/evcc/discussions/1965
// Alfen charger implementation
type Alfen struct {
log *util.Logger
conn *modbus.Connection
mu sync.Mutex
curr float64
enabled bool
}
const (
alfenRegCurrents = 320 // 3 registers
alfenRegPower = 344
alfenRegEnergy = 374 // 390
alfenRegStatus = 1201 // 5 registers
alfenRegAmpsConfig = 1210
alfenRegPhases = 1215
)
func init() {
registry.Add("alfen", NewAlfenFromConfig)
}
// NewAlfenFromConfig creates a Alfen charger from generic config
func NewAlfenFromConfig(other map[string]interface{}) (api.Charger, error) {
cc := modbus.TcpSettings{
ID: 1,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
return NewAlfen(cc.URI, cc.ID)
}
// NewAlfen creates Alfen charger
func NewAlfen(uri string, slaveID uint8) (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("alfen")
conn.Logger(log.TRACE)
wb := &Alfen{
log: log,
conn: conn,
}
go wb.heartbeat()
return wb, err
}
func (wb *Alfen) heartbeat() {
for range time.NewTicker(25 * time.Second).C {
wb.mu.Lock()
var curr float64
if wb.enabled {
curr = wb.curr
}
wb.mu.Unlock()
if err := wb.setCurrent(curr); err != nil {
wb.log.ERROR.Println("heartbeat:", err)
}
}
}
// Status implements the api.Charger interface
func (wb *Alfen) Status() (api.ChargeStatus, error) {
b, err := wb.conn.ReadHoldingRegisters(alfenRegStatus, 5)
if err != nil {
return api.StatusNone, err
}
switch r := rune(b[0]); r {
case 'A', 'B', 'D', 'E', 'F':
return api.ChargeStatus(r), nil
case 'C':
// C1 is "connected"
if rune(b[1]) == '1' {
return api.StatusB, nil
}
return api.StatusC, nil
default:
return api.StatusNone, fmt.Errorf("invalid status: %0x", b[:1])
}
}
// Enabled implements the api.Charger interface
func (wb *Alfen) Enabled() (bool, error) {
b, err := wb.conn.ReadHoldingRegisters(alfenRegAmpsConfig, 2)
if err != nil {
return false, err
}
return math.Float32frombits(binary.BigEndian.Uint32(b)) > 0, nil
}
// Enable implements the api.Charger interface
func (wb *Alfen) Enable(enable bool) error {
var curr float64
if enable {
wb.mu.Lock()
curr = wb.curr
wb.mu.Unlock()
}
err := wb.setCurrent(curr)
if err == nil {
wb.mu.Lock()
wb.enabled = enable
wb.mu.Unlock()
}
return err
}
// MaxCurrent implements the api.Charger interface
func (wb *Alfen) MaxCurrent(current int64) error {
return wb.MaxCurrentMillis(float64(current))
}
var _ api.ChargerEx = (*Alfen)(nil)
// setCurrent sets the current in milliamps without modifying the stored current value
func (wb *Alfen) setCurrent(current float64) error {
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, math.Float32bits(float32(current)))
_, err := wb.conn.WriteMultipleRegisters(alfenRegAmpsConfig, 2, b)
return err
}
// MaxCurrent implements the api.ChargerEx interface
func (wb *Alfen) MaxCurrentMillis(current float64) error {
err := wb.setCurrent(current)
if err == nil {
wb.mu.Lock()
wb.curr = current
wb.mu.Unlock()
}
return err
}
var _ api.Meter = (*Alfen)(nil)
// CurrentPower implements the api.Meter interface
func (wb *Alfen) CurrentPower() (float64, error) {
b, err := wb.conn.ReadHoldingRegisters(alfenRegPower, 2)
if err != nil {
return 0, err
}
return rs485.RTUIeee754ToFloat64(b), err
}
var _ api.MeterEnergy = (*Alfen)(nil)
// TotalEnergy implements the api.MeterEnergy interface
func (wb *Alfen) TotalEnergy() (float64, error) {
b, err := wb.conn.ReadHoldingRegisters(alfenRegEnergy, 4)
if err != nil {
return 0, err
}
return math.Float64frombits(binary.BigEndian.Uint64(b)) / 1e3, err
}
var _ api.MeterCurrent = (*Alfen)(nil)
// Currents implements the api.MeterCurrent interface
func (wb *Alfen) Currents() (float64, float64, float64, error) {
b, err := wb.conn.ReadHoldingRegisters(alfenRegCurrents, 6)
if err != nil {
return 0, 0, 0, err
}
var res [3]float64
for i := 0; i < 3; i++ {
f := rs485.RTUIeee754ToFloat64(b[4*i:])
if math.IsNaN(f) {
f = 0
}
res[i] = f
}
return res[0], res[1], res[2], nil
}
var _ api.PhaseSwitcher = (*Alfen)(nil)
// Phases1p3p implements the api.PhaseSwitcher interface
func (c *Alfen) Phases1p3p(phases int) error {
_, err := c.conn.WriteSingleRegister(alfenRegPhases, uint16(phases))
return err
}
// var _ api.Diagnosis = (*Alfen)(nil)
// // Diagnose implements the api.Diagnosis interface
// func (wb *Alfen) Diagnose() {
// b, err := wb.conn.ReadHoldingRegisters(ablRegFirmware, 2)
// if err == nil {
// fmt.Printf("Firmware: %0 x\n", b)
// }
// }