forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkse.go
246 lines (196 loc) Β· 6.25 KB
/
kse.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
package charger
// LICENSE
// Copyright (c) 2022 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"
"fmt"
"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"
)
// KSE charger implementation
type KSE struct {
log *util.Logger
conn *modbus.Connection
curr uint16
rfid bool
}
const (
kseRegSetMaxCurrent = 0x03 // Externe Stromvorgabe via Bussystem / Ladefreigabe
kseRegChargeMode = 0x0E // Lademodus
kseRegVehicleState = 0x10 // State der Statemachine
kseRegCurrents = 0x14 // Phasenstrom (3)
kseRegCurrentLoadedEnergy = 0x17 // Zwischen anstecken und abstecken geladene Energie (10 Wh)
kseRegActualPower = 0x18 // Aktuelle Ladeleistung (W)
kseRegFirmwareVersion = 0x30 // Firmware Version
kseRegRFIDinstalled = 0x31 // RFID-Leser vorhanden
kseRegNFCTransactionID = 0x67 // Tag ID (8 Bytes)
// kseRegRelayMode = 0x35 // Umschalten 1 phasiges oder 3 phasiges Laden
)
func init() {
registry.Add("kse", NewKSEFromConfig)
}
// NewKSEFromConfig creates a KSE charger from generic config
func NewKSEFromConfig(other map[string]interface{}) (api.Charger, error) {
cc := modbus.Settings{
ID: 100,
Baudrate: 9600,
Comset: "8E1",
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
return NewKSE(cc.URI, cc.Device, cc.Comset, cc.Baudrate, cc.ID)
}
//go:generate go run ../cmd/tools/decorate.go -f decorateKSE -b *KSE -r api.Charger -t "api.Identifier,Identify,func() (string, error)"
// NewKSE creates KSE charger
func NewKSE(uri, device, comset string, baudrate int, slaveID uint8) (api.Charger, error) {
conn, err := modbus.NewConnection(uri, device, comset, baudrate, modbus.Rtu, slaveID)
if err != nil {
return nil, err
}
if !sponsor.IsAuthorized() {
return nil, api.ErrSponsorRequired
}
log := util.NewLogger("kse")
conn.Logger(log.TRACE)
wb := &KSE{
log: log,
conn: conn,
curr: 6, // assume min current
}
// check presence of rfid
b, err := wb.conn.ReadDiscreteInputs(kseRegRFIDinstalled, 1)
if err == nil && b[0] != 0 {
wb.rfid = true
return decorateKSE(wb, wb.identify), err
}
return wb, err
}
// Status implements the api.Charger interface
func (wb *KSE) Status() (api.ChargeStatus, error) {
b, err := wb.conn.ReadInputRegisters(kseRegVehicleState, 1)
if err != nil {
return api.StatusNone, err
}
s := binary.BigEndian.Uint16(b)
switch s {
case 0, 1, 3:
return api.StatusA, nil
case 4:
return api.StatusB, nil
case 5:
return api.StatusC, nil
case 6, 7, 8:
return api.StatusE, nil
default:
return api.StatusNone, fmt.Errorf("invalid status: %d", s)
}
}
// Enabled implements the api.Charger interface
func (wb *KSE) Enabled() (bool, error) {
b, err := wb.conn.ReadHoldingRegisters(kseRegSetMaxCurrent, 1)
if err != nil {
return false, err
}
cur := binary.BigEndian.Uint16(b)
return cur != 0, nil
}
// Enable implements the api.Charger interface
func (wb *KSE) Enable(enable bool) error {
var u uint16
if enable {
u = wb.curr
}
_, err := wb.conn.WriteSingleRegister(kseRegSetMaxCurrent, u)
return err
}
// MaxCurrent implements the api.Charger interface
func (wb *KSE) MaxCurrent(current int64) error {
if current < 6 {
return fmt.Errorf("invalid current %d", current)
}
_, err := wb.conn.WriteSingleRegister(kseRegSetMaxCurrent, uint16(current))
if err == nil {
wb.curr = uint16(current)
}
return err
}
var _ api.Meter = (*KSE)(nil)
// CurrentPower implements the api.Meter interface
func (wb *KSE) CurrentPower() (float64, error) {
b, err := wb.conn.ReadInputRegisters(kseRegActualPower, 1)
if err != nil {
return 0, err
}
return float64(binary.BigEndian.Uint16(b)), err
}
var _ api.ChargeRater = (*KSE)(nil)
// ChargedEnergy implements the api.MeterEnergy interface
func (wb *KSE) ChargedEnergy() (float64, error) {
b, err := wb.conn.ReadInputRegisters(kseRegCurrentLoadedEnergy, 1)
if err != nil {
return 0, err
}
return float64(binary.BigEndian.Uint16(b)) / 100, err
}
var _ api.PhaseCurrents = (*KSE)(nil)
// Currents implements the api.PhaseCurrents interface
func (wb *KSE) Currents() (float64, float64, float64, error) {
b, err := wb.conn.ReadInputRegisters(kseRegCurrents, 3)
if err != nil {
return 0, 0, 0, err
}
var curr [3]float64
for l := 0; l < 3; l++ {
curr[l] = float64(binary.BigEndian.Uint16(b[2*l:])) / 1e3
}
return curr[0], curr[1], curr[2], nil
}
// var _ api.PhaseSwitcher = (*KSE)(nil)
// // Phases1p3p implements the api.PhaseSwitcher interface
// func (wb *KSE) Phases1p3p(phases int) error {
// var b uint16 = 0
// if phases == 1 {
// b = 1 // 1p
// }
// _, err := wb.conn.WriteSingleRegister(kseRegRelayMode, b)
// return err
// }
// Identify implements the api.Identifier interface
func (wb *KSE) identify() (string, error) {
b, err := wb.conn.ReadHoldingRegisters(kseRegNFCTransactionID, 4)
if err != nil {
return "", err
}
return bytesAsString(b), nil
}
var _ api.Diagnosis = (*KSE)(nil)
// Diagnose implements the api.Diagnosis interface
func (wb *KSE) Diagnose() {
if b, err := wb.conn.ReadInputRegisters(kseRegFirmwareVersion, 1); err == nil {
fmt.Printf("\tFirmware:\t%d.%d\n", b[0], b[1])
}
if b, err := wb.conn.ReadInputRegisters(kseRegChargeMode, 1); err == nil {
fmt.Printf("\tCharge Mode:\t%d\n", binary.BigEndian.Uint16(b))
}
if b, err := wb.conn.ReadInputRegisters(kseRegVehicleState, 1); err == nil {
fmt.Printf("\tState:\t%d\n", binary.BigEndian.Uint16(b))
}
if wb.rfid {
if b, err := wb.conn.ReadHoldingRegisters(kseRegNFCTransactionID, 4); err == nil {
fmt.Printf("\tNFC ID:\t%s\n", b)
}
}
}