Skip to content

Commit

Permalink
chore: align implementation of phase-value returns (evcc-io#10313)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Oct 14, 2023
1 parent e22564d commit 3b11d0e
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 59 deletions.
12 changes: 6 additions & 6 deletions charger/abb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions charger/abl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions charger/bender.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions charger/heidelberg-ec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions charger/openwb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions charger/smaevcharger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 13 additions & 11 deletions charger/vestel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions charger/wallbe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions charger/webasto-next.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 10 additions & 10 deletions meter/sma.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 3b11d0e

Please sign in to comment.