KSE: Fix registers and detect RFID (#3892)

This commit is contained in:
premultiply 2022-07-19 01:15:50 +02:00 • committed by GitHub
parent bc5522a456
commit 97262a65b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 11 deletions

View file

@ -33,6 +33,7 @@ type KSE struct {
log *util.Logger
conn *modbus.Connection
curr uint16
rfid bool
}
const (
@ -67,6 +68,8 @@ func NewKSEFromConfig(other map[string]interface{}) (api.Charger, error) {
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)
@ -87,6 +90,13 @@ func NewKSE(uri, device, comset string, baudrate int, slaveID uint8) (api.Charge
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
}
@ -155,7 +165,7 @@ var _ api.Meter = (*KSE)(nil)
// CurrentPower implements the api.Meter interface
func (wb *KSE) CurrentPower() (float64, error) {
b, err := wb.conn.ReadHoldingRegisters(kseRegActualPower, 1)
b, err := wb.conn.ReadInputRegisters(kseRegActualPower, 1)
if err != nil {
return 0, err
}
@ -167,7 +177,7 @@ var _ api.ChargeRater = (*KSE)(nil)
// ChargedEnergy implements the api.MeterEnergy interface
func (wb *KSE) ChargedEnergy() (float64, error) {
b, err := wb.conn.ReadHoldingRegisters(kseRegCurrentLoadedEnergy, 1)
b, err := wb.conn.ReadInputRegisters(kseRegCurrentLoadedEnergy, 1)
if err != nil {
return 0, err
}
@ -179,7 +189,7 @@ var _ api.MeterCurrent = (*KSE)(nil)
// Currents implements the api.MeterCurrent interface
func (wb *KSE) Currents() (float64, float64, float64, error) {
b, err := wb.conn.ReadHoldingRegisters(kseRegCurrents, 3)
b, err := wb.conn.ReadInputRegisters(kseRegCurrents, 3)
if err != nil {
return 0, 0, 0, err
}
@ -205,10 +215,8 @@ func (wb *KSE) Phases1p3p(phases int) error {
return err
}
var _ api.Identifier = (*BenderCC)(nil)
// Identify implements the api.Identifier interface
func (wb *KSE) Identify() (string, error) {
func (wb *KSE) identify() (string, error) {
id, err := wb.conn.ReadHoldingRegisters(kseRegNFCTransactionID, 4)
if err != nil {
return "", err
@ -230,10 +238,9 @@ func (wb *KSE) Diagnose() {
if b, err := wb.conn.ReadInputRegisters(kseRegVehicleState, 1); err == nil {
fmt.Printf("\tState:\t%d\n", binary.BigEndian.Uint16(b))
}
if b, err := wb.conn.ReadDiscreteInputs(kseRegRFIDinstalled, 1); err == nil {
fmt.Printf("\tRFID:\t%t\n", binary.BigEndian.Uint16(b) != 0)
}
if b, err := wb.conn.ReadHoldingRegisters(kseRegNFCTransactionID, 4); err == nil {
fmt.Printf("\tNFC ID:\t%s\n", b)
if wb.rfid {
if b, err := wb.conn.ReadHoldingRegisters(kseRegNFCTransactionID, 4); err == nil {
fmt.Printf("\tNFC ID:\t%s\n", b)
}
}
}

35
charger/kse_decorators.go Normal file
View file

@ -0,0 +1,35 @@
package charger
// Code generated by github.com/evcc-io/evcc/cmd/tools/decorate.go. DO NOT EDIT.
import (
"github.com/evcc-io/evcc/api"
)
func decorateKSE(base *KSE, identifier func() (string, error)) api.Charger {
switch {
case identifier == nil:
return base
case identifier != nil:
return &struct {
*KSE
api.Identifier
}{
KSE: base,
Identifier: &decorateKSEIdentifierImpl{
identifier: identifier,
},
}
}
return nil
}
type decorateKSEIdentifierImpl struct {
identifier func() (string, error)
}
func (impl *decorateKSEIdentifierImpl) Identify() (string, error) {
return impl.identifier()
}