From 3b11d0e2532b1827423487230abec2f595065655 Mon Sep 17 00:00:00 2001 From: andig Date: Sat, 14 Oct 2023 16:35:11 +0200 Subject: [PATCH] chore: align implementation of phase-value returns (#10313) --- charger/abb.go | 12 ++++++------ charger/abl.go | 10 +++++----- charger/bender.go | 12 ++++++------ charger/heidelberg-ec.go | 12 ++++++------ charger/openwb.go | 6 +++--- charger/smaevcharger.go | 8 ++++---- charger/vestel.go | 24 +++++++++++++----------- charger/wallbe.go | 10 +++++----- charger/webasto-next.go | 6 +++--- meter/sma.go | 20 ++++++++++---------- 10 files changed, 61 insertions(+), 59 deletions(-) diff --git a/charger/abb.go b/charger/abb.go index b2ff703f8..8fa80baa4 100644 --- a/charger/abb.go +++ b/charger/abb.go @@ -218,12 +218,12 @@ func (wb *ABB) Currents() (float64, float64, float64, error) { return 0, 0, 0, err } - var curr [3]float64 + var res [3]float64 for l := 0; l < 3; l++ { - curr[l] = float64(binary.BigEndian.Uint32(b[4*l:])) / 1e3 + res[l] = float64(binary.BigEndian.Uint32(b[4*l:])) / 1e3 } - return curr[0], curr[1], curr[2], nil + return res[0], res[1], res[2], nil } var _ api.PhaseVoltages = (*ABB)(nil) @@ -235,12 +235,12 @@ func (wb *ABB) Voltages() (float64, float64, float64, error) { return 0, 0, 0, err } - var volt [3]float64 + var res [3]float64 for l := 0; l < 3; l++ { - volt[l] = float64(binary.BigEndian.Uint32(b[4*l:])) / 10 + res[l] = float64(binary.BigEndian.Uint32(b[4*l:])) / 10 } - return volt[0], volt[1], volt[2], nil + return res[0], res[1], res[2], nil } // var _ api.PhaseSwitcher = (*ABB)(nil) diff --git a/charger/abl.go b/charger/abl.go index 84012f2c3..ea7b70ca0 100644 --- a/charger/abl.go +++ b/charger/abl.go @@ -236,17 +236,17 @@ func (wb *ABLeMH) currents() (float64, float64, float64, error) { return 0, 0, 0, err } - var currents []float64 - for i := 2; i < 5; i++ { - u := binary.BigEndian.Uint16(b[2*i:]) + var res [3]float64 + for i := 0; i < 3; i++ { + u := binary.BigEndian.Uint16(b[2*(2+i):]) if u == ablAmpsDisabled || u == 1 { u = 0 } - currents = append(currents, float64(u)/10) + res[i] = float64(u) / 10 } - return currents[2], currents[1], currents[0], nil + return res[2], res[1], res[0], nil } var _ api.Diagnosis = (*ABLeMH)(nil) diff --git a/charger/bender.go b/charger/bender.go index 45ea2d405..057ca532e 100644 --- a/charger/bender.go +++ b/charger/bender.go @@ -287,12 +287,12 @@ func (wb *BenderCC) currents() (float64, float64, float64, error) { return 0, 0, 0, err } - var curr [3]float64 + var res [3]float64 for l := 0; l < 3; l++ { - curr[l] = float64(binary.BigEndian.Uint32(b[4*l:4*(l+1)])) / 1e3 + res[l] = float64(binary.BigEndian.Uint32(b[4*l:4*(l+1)])) / 1e3 } - return curr[0], curr[1], curr[2], nil + return res[0], res[1], res[2], nil } // voltages implements the api.PhaseVoltages interface @@ -302,12 +302,12 @@ func (wb *BenderCC) voltages() (float64, float64, float64, error) { return 0, 0, 0, err } - var volt [3]float64 + var res [3]float64 for l := 0; l < 3; l++ { - volt[l] = float64(binary.BigEndian.Uint32(b[4*l : 4*(l+1)])) + res[l] = float64(binary.BigEndian.Uint32(b[4*l : 4*(l+1)])) } - return volt[0], volt[1], volt[2], nil + return res[0], res[1], res[2], nil } // identify implements the api.Identifier interface diff --git a/charger/heidelberg-ec.go b/charger/heidelberg-ec.go index ef6f8b058..8b007961b 100644 --- a/charger/heidelberg-ec.go +++ b/charger/heidelberg-ec.go @@ -271,12 +271,12 @@ func (wb *HeidelbergEC) Currents() (float64, float64, float64, error) { return 0, 0, 0, err } - var curr [3]float64 + var res [3]float64 for l := 0; l < 3; l++ { - curr[l] = float64(binary.BigEndian.Uint16(b[2*l:])) / 10 + res[l] = float64(binary.BigEndian.Uint16(b[2*l:])) / 10 } - return curr[0], curr[1], curr[2], nil + return res[0], res[1], res[2], nil } var _ api.PhaseVoltages = (*HeidelbergEC)(nil) @@ -288,12 +288,12 @@ func (wb *HeidelbergEC) Voltages() (float64, float64, float64, error) { return 0, 0, 0, err } - var volt [3]float64 + var res [3]float64 for l := 0; l < 3; l++ { - volt[l] = float64(binary.BigEndian.Uint16(b[2*l:])) + res[l] = float64(binary.BigEndian.Uint16(b[2*l:])) } - return volt[0], volt[1], volt[2], nil + return res[0], res[1], res[2], nil } var _ api.Diagnosis = (*HeidelbergEC)(nil) diff --git a/charger/openwb.go b/charger/openwb.go index 8f67e0ca3..ff257ca31 100644 --- a/charger/openwb.go +++ b/charger/openwb.go @@ -214,17 +214,17 @@ var _ api.PhaseCurrents = (*OpenWB)(nil) // Currents implements the api.PhaseCurrents interface func (m *OpenWB) Currents() (float64, float64, float64, error) { - var currents []float64 + var res []float64 for _, currentG := range m.currentsG { c, err := currentG() if err != nil { return 0, 0, 0, err } - currents = append(currents, c) + res = append(res, c) } - return currents[0], currents[1], currents[2], nil + return res[0], res[1], res[2], nil } var _ api.Authorizer = (*OpenWB)(nil) diff --git a/charger/smaevcharger.go b/charger/smaevcharger.go index 37502204c..f557b81c6 100644 --- a/charger/smaevcharger.go +++ b/charger/smaevcharger.go @@ -252,18 +252,18 @@ var _ api.PhaseCurrents = (*Smaevcharger)(nil) // Currents implements the api.PhaseCurrents interface func (wb *Smaevcharger) Currents() (float64, float64, float64, error) { - var curr []float64 + var res [3]float64 - for _, phase := range []string{"A", "B", "C"} { + for i, phase := range []string{"A", "B", "C"} { val, err := wb.getMeasurement("Measurement.GridMs.A.phs" + phase) if err != nil { return 0, 0, 0, err } - curr = append(curr, -val) + res[i] = -val } - return curr[0], curr[1], curr[2], nil + return res[0], res[1], res[2], nil } // reset cache diff --git a/charger/vestel.go b/charger/vestel.go index b4e1a223e..38bb75d2d 100644 --- a/charger/vestel.go +++ b/charger/vestel.go @@ -42,11 +42,13 @@ const ( vestelRegSessionEnergy = 1502 vestelRegFailsafeTimeout = 2002 vestelRegAlive = 6000 - //vestelRegChargepointState = 1000 + // vestelRegChargepointState = 1000 ) -var vestelRegCurrents = []uint16{1008, 1010, 1012} // non-continuous uint16 registers! -var vestelRegVoltages = []uint16{1014, 1016, 1018} // non-continuous uint16 registers! +var ( + vestelRegCurrents = []uint16{1008, 1010, 1012} // non-continuous uint16 registers! + vestelRegVoltages = []uint16{1014, 1016, 1018} // non-continuous uint16 registers! +) // Vestel is an api.Charger implementation for Vestel/Hymes wallboxes with Ethernet (SW modells). // It uses Modbus TCP to communicate with the wallbox at modbus client id 255. @@ -212,34 +214,34 @@ var _ api.PhaseCurrents = (*Vestel)(nil) // Currents implements the api.PhaseCurrents interface func (wb *Vestel) Currents() (float64, float64, float64, error) { - var currents []float64 - for _, regCurrent := range vestelRegCurrents { + var res [3]float64 + for i, regCurrent := range vestelRegCurrents { b, err := wb.conn.ReadInputRegisters(regCurrent, 1) if err != nil { return 0, 0, 0, err } - currents = append(currents, float64(binary.BigEndian.Uint16(b))/1e3) + res[i] = float64(binary.BigEndian.Uint16(b)) / 1e3 } - return currents[0], currents[1], currents[2], nil + return res[0], res[1], res[2], nil } var _ api.PhaseVoltages = (*Vestel)(nil) // Voltages implements the api.PhaseVoltages interface func (wb *Vestel) Voltages() (float64, float64, float64, error) { - var voltages []float64 - for _, regVoltage := range vestelRegVoltages { + var res [3]float64 + for i, regVoltage := range vestelRegVoltages { b, err := wb.conn.ReadInputRegisters(regVoltage, 1) if err != nil { return 0, 0, 0, err } - voltages = append(voltages, float64(binary.BigEndian.Uint16(b))) + res[i] = float64(binary.BigEndian.Uint16(b)) } - return voltages[0], voltages[1], voltages[2], nil + return res[0], res[1], res[2], nil } var _ api.Diagnosis = (*Vestel)(nil) diff --git a/charger/wallbe.go b/charger/wallbe.go index 8b5a1f4dc..9f43c1596 100644 --- a/charger/wallbe.go +++ b/charger/wallbe.go @@ -195,17 +195,17 @@ func (wb *Wallbe) totalEnergy() (float64, error) { // currents implements the api.PhaseCurrents interface func (wb *Wallbe) currents() (float64, float64, float64, error) { - var currents []float64 - for _, regCurrent := range wbRegCurrents { - b, err := wb.conn.ReadInputRegisters(regCurrent, 2) + var res [3]float64 + for i, reg := range wbRegCurrents { + b, err := wb.conn.ReadInputRegisters(reg, 2) if err != nil { return 0, 0, 0, err } - currents = append(currents, rs485.RTUInt32ToFloat64Swapped(b)) + res[i] = rs485.RTUInt32ToFloat64Swapped(b) } - return currents[0], currents[1], currents[2], nil + return res[0], res[1], res[2], nil } var _ api.Diagnosis = (*Wallbe)(nil) diff --git a/charger/webasto-next.go b/charger/webasto-next.go index 6b2c70305..578714055 100644 --- a/charger/webasto-next.go +++ b/charger/webasto-next.go @@ -217,17 +217,17 @@ var _ api.PhaseCurrents = (*WebastoNext)(nil) // Currents implements the api.PhaseCurrents interface func (wb *WebastoNext) Currents() (float64, float64, float64, error) { - var curr [3]float64 + var res [3]float64 for l := uint16(0); l < 3; l++ { b, err := wb.conn.ReadInputRegisters(tqRegCurrents+2*l, 1) if err != nil { return 0, 0, 0, err } - curr[l] = float64(binary.BigEndian.Uint16(b)) / 1e3 + res[l] = float64(binary.BigEndian.Uint16(b)) / 1e3 } - return curr[0], curr[1], curr[2], nil + return res[0], res[1], res[2], nil } var _ api.Identifier = (*WebastoNext)(nil) diff --git a/meter/sma.go b/meter/sma.go index dd9e806b0..3a8e35ad8 100644 --- a/meter/sma.go +++ b/meter/sma.go @@ -125,12 +125,12 @@ func (sm *SMA) Currents() (float64, float64, float64, error) { } } - var currents [3]float64 + var res [3]float64 for i, id := range []sunny.ValueID{sunny.CurrentL1, sunny.CurrentL2, sunny.CurrentL3} { - currents[i] = util.SignFromPower(sma.AsFloat(values[id]), powers[i]) + res[i] = util.SignFromPower(sma.AsFloat(values[id]), powers[i]) } - return currents[0], currents[1], currents[2], err + return res[0], res[1], res[2], err } var _ api.PhaseVoltages = (*SMA)(nil) @@ -139,12 +139,12 @@ var _ api.PhaseVoltages = (*SMA)(nil) func (sm *SMA) Voltages() (float64, float64, float64, error) { values, err := sm.device.Values() - var voltages [3]float64 + var res [3]float64 for i, id := range []sunny.ValueID{sunny.VoltageL1, sunny.VoltageL2, sunny.VoltageL3} { - voltages[i] = sma.AsFloat(values[id]) + res[i] = sma.AsFloat(values[id]) } - return voltages[0], voltages[1], voltages[2], err + return res[0], res[1], res[2], err } var _ api.PhasePowers = (*SMA)(nil) @@ -153,15 +153,15 @@ var _ api.PhasePowers = (*SMA)(nil) func (sm *SMA) Powers() (float64, float64, float64, error) { values, err := sm.device.Values() - var powers [3]float64 + var res [3]float64 for i, id := range []sunny.ValueID{sunny.ActivePowerPlusL1, sunny.ActivePowerPlusL2, sunny.ActivePowerPlusL3} { - powers[i] = sma.AsFloat(values[id]) + res[i] = sma.AsFloat(values[id]) } for i, id := range []sunny.ValueID{sunny.ActivePowerMinusL1, sunny.ActivePowerMinusL2, sunny.ActivePowerMinusL3} { - powers[i] -= sma.AsFloat(values[id]) + res[i] -= sma.AsFloat(values[id]) } - return powers[0], powers[1], powers[2], err + return res[0], res[1], res[2], err } // soc implements the api.Battery interface