Skip to content

Commit

Permalink
chore: cleanup Menneckes naming (#12202)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Feb 14, 2024
1 parent 8e0079f commit f564a9b
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 72 deletions.
72 changes: 36 additions & 36 deletions charger/amtron.go → charger/menneckes-hcc3.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,28 @@ import (

// https://update.mennekes.de/hcc3/1.13/Description%20Modbus_AMTRON%20HCC3_v01_2021-06-25_en.pdf

// Amtron Xtra/Premium charger implementation
type Amtron struct {
// MenneckesHcc3 Xtra/Premium charger implementation
type MenneckesHcc3 struct {
conn *modbus.Connection
curr uint16
}

const (
amtronRegStatus = 0x0302
amtronRegPhases = 0x0308
amtronRegSerial = 0x030B
amtronRegEnergy = 0x030D
amtronRegName = 0x0311
amtronRegPower = 0x030F
amtronRegAmpsConfig = 0x0400
menneckesHcc3RegStatus = 0x0302
menneckesHcc3RegPhases = 0x0308
menneckesHcc3RegSerial = 0x030B
menneckesHcc3RegEnergy = 0x030D
menneckesHcc3RegName = 0x0311
menneckesHcc3RegPower = 0x030F
menneckesHcc3RegAmpsConfig = 0x0400
)

func init() {
registry.Add("amtron", NewAmtronFromConfig)
registry.Add("menneckes-hcc3", NewMenneckesHcc3FromConfig)
}

// NewAmtronFromConfig creates a Mennekes Amtron charger from generic config
func NewAmtronFromConfig(other map[string]interface{}) (api.Charger, error) {
// NewMenneckesHcc3FromConfig creates a Mennekes menneckesHcc3 charger from generic config
func NewMenneckesHcc3FromConfig(other map[string]interface{}) (api.Charger, error) {
cc := modbus.TcpSettings{
ID: 255,
}
Expand All @@ -62,11 +62,11 @@ func NewAmtronFromConfig(other map[string]interface{}) (api.Charger, error) {
return nil, err
}

return NewAmtron(cc.URI, cc.ID)
return NewMenneckesHcc3(cc.URI, cc.ID)
}

// NewAmtron creates Amtron charger
func NewAmtron(uri string, slaveID uint8) (api.Charger, error) {
// NewMenneckesHcc3 creates Menneckes HCC3 charger
func NewMenneckesHcc3(uri string, slaveID uint8) (api.Charger, error) {
uri = util.DefaultPort(uri, 502)

conn, err := modbus.NewConnection(uri, "", "", 0, modbus.Tcp, slaveID)
Expand All @@ -78,10 +78,10 @@ func NewAmtron(uri string, slaveID uint8) (api.Charger, error) {
return nil, api.ErrSponsorRequired
}

log := util.NewLogger("amtron")
log := util.NewLogger("menneckes-hcc3")
conn.Logger(log.TRACE)

wb := &Amtron{
wb := &MenneckesHcc3{
conn: conn,
curr: 6,
}
Expand All @@ -90,8 +90,8 @@ func NewAmtron(uri string, slaveID uint8) (api.Charger, error) {
}

// Status implements the api.Charger interface
func (wb *Amtron) Status() (api.ChargeStatus, error) {
b, err := wb.conn.ReadInputRegisters(amtronRegStatus, 1)
func (wb *MenneckesHcc3) Status() (api.ChargeStatus, error) {
b, err := wb.conn.ReadInputRegisters(menneckesHcc3RegStatus, 1)
if err != nil {
return api.StatusNone, err
}
Expand All @@ -111,8 +111,8 @@ func (wb *Amtron) Status() (api.ChargeStatus, error) {
}

// Enabled implements the api.Charger interface
func (wb *Amtron) Enabled() (bool, error) {
b, err := wb.conn.ReadHoldingRegisters(amtronRegAmpsConfig, 1)
func (wb *MenneckesHcc3) Enabled() (bool, error) {
b, err := wb.conn.ReadHoldingRegisters(menneckesHcc3RegAmpsConfig, 1)
if err != nil {
return false, err
}
Expand All @@ -123,69 +123,69 @@ func (wb *Amtron) Enabled() (bool, error) {
}

// Enable implements the api.Charger interface
func (wb *Amtron) Enable(enable bool) error {
func (wb *MenneckesHcc3) Enable(enable bool) error {
var u uint16
if enable {
u = wb.curr
}

_, err := wb.conn.WriteSingleRegister(amtronRegAmpsConfig, u)
_, err := wb.conn.WriteSingleRegister(menneckesHcc3RegAmpsConfig, u)
return err
}

// MaxCurrent implements the api.Charger interface
func (wb *Amtron) MaxCurrent(current int64) error {
func (wb *MenneckesHcc3) MaxCurrent(current int64) error {
if current < 6 {
return fmt.Errorf("invalid current %d", current)
}

cur := uint16(current)

_, err := wb.conn.WriteSingleRegister(amtronRegAmpsConfig, cur)
_, err := wb.conn.WriteSingleRegister(menneckesHcc3RegAmpsConfig, cur)
if err == nil {
wb.curr = cur
}

return err
}

var _ api.Meter = (*Amtron)(nil)
var _ api.Meter = (*MenneckesHcc3)(nil)

// CurrentPower implements the api.Meter interface
func (wb *Amtron) CurrentPower() (float64, error) {
b, err := wb.conn.ReadInputRegisters(amtronRegPower, 2)
func (wb *MenneckesHcc3) CurrentPower() (float64, error) {
b, err := wb.conn.ReadInputRegisters(menneckesHcc3RegPower, 2)
if err != nil {
return 0, err
}

return rs485.RTUUint32ToFloat64Swapped(b), nil
}

var _ api.ChargeRater = (*Amtron)(nil)
var _ api.ChargeRater = (*MenneckesHcc3)(nil)

// ChargedEnergy implements the api.MeterEnergy interface
func (wb *Amtron) ChargedEnergy() (float64, error) {
b, err := wb.conn.ReadInputRegisters(amtronRegEnergy, 2)
func (wb *MenneckesHcc3) ChargedEnergy() (float64, error) {
b, err := wb.conn.ReadInputRegisters(menneckesHcc3RegEnergy, 2)
if err != nil {
return 0, err
}

return rs485.RTUUint32ToFloat64Swapped(b) / 1e3, nil
}

var _ api.Diagnosis = (*Amtron)(nil)
var _ api.Diagnosis = (*MenneckesHcc3)(nil)

// Diagnose implements the api.Diagnosis interface
func (wb *Amtron) Diagnose() {
if b, err := wb.conn.ReadInputRegisters(amtronRegName, 11); err == nil {
func (wb *MenneckesHcc3) Diagnose() {
if b, err := wb.conn.ReadInputRegisters(menneckesHcc3RegName, 11); err == nil {
fmt.Printf("Name: %s\n", encoding.StringLsbFirst(b))
}

if b, err := wb.conn.ReadInputRegisters(amtronRegPhases, 1); err == nil {
if b, err := wb.conn.ReadInputRegisters(menneckesHcc3RegPhases, 1); err == nil {
fmt.Printf("Phases: %d\n", binary.BigEndian.Uint16(b))
}

if b, err := wb.conn.ReadInputRegisters(amtronRegSerial, 2); err == nil {
if b, err := wb.conn.ReadInputRegisters(menneckesHcc3RegSerial, 2); err == nil {
fmt.Printf("Serial: %d\n", binary.LittleEndian.Uint32(b))
}
}
64 changes: 32 additions & 32 deletions charger/mennekes.go → charger/mennekes-compact.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (
"github.com/volkszaehler/mbmd/encoding"
)

// Mennekes is an api.Charger implementation
type Mennekes struct {
// MennekesCompact is an api.Charger implementation
type MennekesCompact struct {
log *util.Logger
conn *modbus.Connection
}
Expand Down Expand Up @@ -63,11 +63,11 @@ const (
)

func init() {
registry.Add("mennekes", NewMennekesFromConfig)
registry.Add("mennekes-compact", NewMennekesCompactFromConfig)
}

// NewMennekesFromConfig creates a new Mennekes ModbusTCP charger
func NewMennekesFromConfig(other map[string]interface{}) (api.Charger, error) {
// NewMennekesCompactFromConfig creates a new Mennekes ModbusTCP charger
func NewMennekesCompactFromConfig(other map[string]interface{}) (api.Charger, error) {
cc := struct {
modbus.Settings `mapstructure:",squash"`
Timeout time.Duration
Expand All @@ -83,11 +83,11 @@ func NewMennekesFromConfig(other map[string]interface{}) (api.Charger, error) {
return nil, err
}

return NewMennekes(cc.URI, cc.Device, cc.Comset, cc.Baudrate, modbus.ProtocolFromRTU(cc.RTU), cc.ID, cc.Timeout)
return NewMennekesCompact(cc.URI, cc.Device, cc.Comset, cc.Baudrate, modbus.ProtocolFromRTU(cc.RTU), cc.ID, cc.Timeout)
}

// NewMennekes creates Mennekes charger
func NewMennekes(uri, device, comset string, baudrate int, proto modbus.Protocol, slaveID uint8, timeout time.Duration) (api.Charger, error) {
// NewMennekesCompact creates Mennekes charger
func NewMennekesCompact(uri, device, comset string, baudrate int, proto modbus.Protocol, slaveID uint8, timeout time.Duration) (api.Charger, error) {
conn, err := modbus.NewConnection(uri, device, comset, baudrate, proto, slaveID)
if err != nil {
return nil, err
Expand All @@ -104,7 +104,7 @@ func NewMennekes(uri, device, comset string, baudrate int, proto modbus.Protocol
log := util.NewLogger("mennekes")
conn.Logger(log.TRACE)

wb := &Mennekes{
wb := &MennekesCompact{
log: log,
conn: conn,
}
Expand All @@ -115,7 +115,7 @@ func NewMennekes(uri, device, comset string, baudrate int, proto modbus.Protocol
return wb, err
}

func (wb *Mennekes) heartbeat(timeout time.Duration) {
func (wb *MennekesCompact) heartbeat(timeout time.Duration) {
for range time.Tick(timeout) {
if _, err := wb.conn.WriteSingleRegister(mennekesRegHeartbeat, mennekesHeartbeatToken); err != nil {
wb.log.ERROR.Println("heartbeat:", err)
Expand All @@ -124,7 +124,7 @@ func (wb *Mennekes) heartbeat(timeout time.Duration) {
}

// Status implements the api.Charger interface
func (wb *Mennekes) Status() (api.ChargeStatus, error) {
func (wb *MennekesCompact) Status() (api.ChargeStatus, error) {
b, err := wb.conn.ReadHoldingRegisters(mennekesRegEvseState, 1)
if err != nil {
return api.StatusNone, err
Expand All @@ -146,7 +146,7 @@ func (wb *Mennekes) Status() (api.ChargeStatus, error) {
}

// Enabled implements the api.Charger interface
func (wb *Mennekes) Enabled() (bool, error) {
func (wb *MennekesCompact) Enabled() (bool, error) {
b, err := wb.conn.ReadHoldingRegisters(mennekesRegChargingReleaseEM, 1)
if err != nil {
return false, err
Expand All @@ -156,7 +156,7 @@ func (wb *Mennekes) Enabled() (bool, error) {
}

// Enable implements the api.Charger interface
func (wb *Mennekes) Enable(enable bool) error {
func (wb *MennekesCompact) Enable(enable bool) error {
var u uint16
if enable {
u = mennekesAllowed
Expand All @@ -166,14 +166,14 @@ func (wb *Mennekes) Enable(enable bool) error {
}

// MaxCurrent implements the api.Charger interface
func (wb *Mennekes) MaxCurrent(current int64) error {
func (wb *MennekesCompact) MaxCurrent(current int64) error {
return wb.MaxCurrentMillis(float64(current))
}

var _ api.ChargerEx = (*Mennekes)(nil)
var _ api.ChargerEx = (*MennekesCompact)(nil)

// MaxCurrentMillis implements the api.ChargerEx interface
func (wb *Mennekes) MaxCurrentMillis(current float64) error {
func (wb *MennekesCompact) MaxCurrentMillis(current float64) error {
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, math.Float32bits(float32(current)))

Expand All @@ -182,7 +182,7 @@ func (wb *Mennekes) MaxCurrentMillis(current float64) error {
}

// CurrentPower implements the api.Meter interface
func (wb *Mennekes) CurrentPower() (float64, error) {
func (wb *MennekesCompact) CurrentPower() (float64, error) {
b, err := wb.conn.ReadHoldingRegisters(mennekesRegPower, 2)
if err != nil {
return 0, err
Expand All @@ -191,10 +191,10 @@ func (wb *Mennekes) CurrentPower() (float64, error) {
return float64(encoding.Float32(b)), nil
}

var _ api.MeterEnergy = (*Mennekes)(nil)
var _ api.MeterEnergy = (*MennekesCompact)(nil)

// TotalEnergy implements the api.MeterEnergy interface
func (wb *Mennekes) TotalEnergy() (float64, error) {
func (wb *MennekesCompact) TotalEnergy() (float64, error) {
b, err := wb.conn.ReadHoldingRegisters(mennekesRegChargedEnergyTotal, 2)
if err != nil {
return 0, err
Expand All @@ -203,22 +203,22 @@ func (wb *Mennekes) TotalEnergy() (float64, error) {
return float64(encoding.Float32(b)), nil
}

var _ api.PhaseCurrents = (*Mennekes)(nil)
var _ api.PhaseCurrents = (*MennekesCompact)(nil)

// Currents implements the api.PhaseCurrents interface
func (wb *Mennekes) Currents() (float64, float64, float64, error) {
func (wb *MennekesCompact) Currents() (float64, float64, float64, error) {
return wb.getPhaseValues(mennekesRegCurrents)
}

var _ api.PhaseVoltages = (*Mennekes)(nil)
var _ api.PhaseVoltages = (*MennekesCompact)(nil)

// Voltages implements the api.PhaseVoltages interface
func (wb *Mennekes) Voltages() (float64, float64, float64, error) {
func (wb *MennekesCompact) Voltages() (float64, float64, float64, error) {
return wb.getPhaseValues(mennekesRegVoltages)
}

// getPhaseValues returns 3 sequential phase values
func (wb *Mennekes) getPhaseValues(reg uint16) (float64, float64, float64, error) {
func (wb *MennekesCompact) getPhaseValues(reg uint16) (float64, float64, float64, error) {
b, err := wb.conn.ReadHoldingRegisters(reg, 6)
if err != nil {
return 0, 0, 0, err
Expand All @@ -233,10 +233,10 @@ func (wb *Mennekes) getPhaseValues(reg uint16) (float64, float64, float64, error
}

/*
var _ api.ChargeRater = (*Mennekes)(nil)
var _ api.ChargeRater = (*MennekesCompact)(nil)
// ChargedEnergy implements the api.MeterEnergy interface
func (wb *Mennekes) ChargedEnergy() (float64, error) {
func (wb *MennekesCompact) ChargedEnergy() (float64, error) {
b, err := wb.conn.ReadHoldingRegisters(mennekesRegChargedEnergySession, 2)
if err != nil {
return 0, err
Expand All @@ -245,10 +245,10 @@ func (wb *Mennekes) ChargedEnergy() (float64, error) {
return float64(encoding.Float32(b)), err
}
var _ api.ChargeTimer = (*Mennekes)(nil)
var _ api.ChargeTimer = (*MennekesCompact)(nil)
// ChargingTime implements the api.ChargeTimer interface
func (wb *Mennekes) ChargingTime() (time.Duration, error) {
func (wb *MennekesCompact) ChargingTime() (time.Duration, error) {
b, err := wb.conn.ReadHoldingRegisters(mennekesRegDurationSession, 2)
if err != nil {
return 0, err
Expand All @@ -258,10 +258,10 @@ func (wb *Mennekes) ChargingTime() (time.Duration, error) {
}
*/

var _ api.PhaseSwitcher = (*Mennekes)(nil)
var _ api.PhaseSwitcher = (*MennekesCompact)(nil)

// Phases1p3p implements the api.PhaseSwitcher interface
func (wb *Mennekes) Phases1p3p(phases int) error {
func (wb *MennekesCompact) Phases1p3p(phases int) error {
var u uint16
if phases == 1 {
u = 1
Expand All @@ -271,10 +271,10 @@ func (wb *Mennekes) Phases1p3p(phases int) error {
return err
}

var _ api.Diagnosis = (*Mennekes)(nil)
var _ api.Diagnosis = (*MennekesCompact)(nil)

// Diagnose implements the api.Diagnosis interface
func (wb *Mennekes) Diagnose() {
func (wb *MennekesCompact) Diagnose() {
if b, err := wb.conn.ReadHoldingRegisters(mennekesRegModbusVersion, 1); err == nil {
fmt.Printf("\tModbus: %03X\n", encoding.Uint16(b))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
template: amtron
template: menneckes-hcc3
covers: ["amtron"]
products:
- brand: Mennekes
description:
Expand All @@ -10,5 +11,5 @@ params:
choice: ["tcpip"]
id: 255
render: |
type: amtron
type: menneckes-hcc3
{{- include "modbus" . }}
Loading

0 comments on commit f564a9b

Please sign in to comment.