Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor reading phase values #11347

Merged
merged 2 commits into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions charger/hesotec.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,31 +180,26 @@ var _ api.PhaseCurrents = (*Hesotec)(nil)

// Currents implements the api.PhaseCurrents interface
func (wb *Hesotec) Currents() (float64, float64, float64, error) {
b, err := wb.conn.ReadHoldingRegisters(hesotecRegCurrents, 6)
if err != nil {
return 0, 0, 0, err
}

var res [3]float64
for i := range res {
res[i] = float64(binary.BigEndian.Uint32(b[4*i:])) / 1e3
}

return res[0], res[1], res[2], nil
return wb.getPhaseValues(hesotecRegCurrents, 2, 1e3)
}

var _ api.PhaseVoltages = (*Hesotec)(nil)

// Voltages implements the api.PhaseVoltages interface
func (wb *Hesotec) Voltages() (float64, float64, float64, error) {
b, err := wb.conn.ReadHoldingRegisters(hesotecRegVoltages, 3)
return wb.getPhaseValues(hesotecRegVoltages, 1, 1)
}

// getPhaseValues returns 3 sequential phase values
func (wb *Hesotec) getPhaseValues(reg, width uint16, divider float64) (float64, float64, float64, error) {
b, err := wb.conn.ReadHoldingRegisters(reg, 3*width)
if err != nil {
return 0, 0, 0, err
}

var res [3]float64
for i := range res {
res[i] = float64(binary.BigEndian.Uint16(b[2*i:]))
res[i] = float64(binary.BigEndian.Uint32(b[2*int(width)*i:])) / divider
}

return res[0], res[1], res[2], nil
Expand Down
19 changes: 7 additions & 12 deletions charger/phoenix-charx.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,22 +212,17 @@ func (wb *PhoenixCharx) totalEnergy() (float64, error) {

// currents implements the api.PhaseCurrents interface
func (wb *PhoenixCharx) currents() (float64, float64, float64, error) {
b, err := wb.conn.ReadHoldingRegisters(wb.register(charxRegCurrents), 3*2)
if err != nil {
return 0, 0, 0, err
}

var res [3]float64
for i := range res {
res[i] = float64(encoding.Int32(b[4*i:])) / 1e3
}

return res[0], res[1], res[2], nil
return wb.getPhaseValues(charxRegCurrents)
}

// voltages implements the api.PhaseVoltages interface
func (wb *PhoenixCharx) voltages() (float64, float64, float64, error) {
b, err := wb.conn.ReadHoldingRegisters(wb.register(charxRegVoltages), 3*2)
return wb.getPhaseValues(charxRegVoltages)
}

// getPhaseValues returns 3 sequential phase values
func (wb *PhoenixCharx) getPhaseValues(reg uint16) (float64, float64, float64, error) {
b, err := wb.conn.ReadHoldingRegisters(wb.register(reg), 6)
if err != nil {
return 0, 0, 0, err
}
Expand Down
32 changes: 15 additions & 17 deletions meter/tasmota/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,38 +220,36 @@ func (c *Connection) TotalEnergy() (float64, error) {

// Currents implements the api.PhaseCurrents interface
func (c *Connection) Currents() (float64, float64, float64, error) {
s, err := c.statusSnsG.Get()
if err != nil {
return 0, 0, 0, err
}

var res [3]float64
for i, c := range c.channels {
res[i], err = s.StatusSNS.Energy.Current.Channel(c)
if err != nil {
return 0, 0, 0, err
}
}

return res[0], res[1], res[2], err
return c.getPhaseValues(func(s StatusSNSResponse) Channels {
return s.StatusSNS.Energy.Current
})
}

// Voltages implements the api.PhaseVoltages interface
func (c *Connection) Voltages() (float64, float64, float64, error) {
return c.getPhaseValues(func(s StatusSNSResponse) Channels {
return s.StatusSNS.Energy.Voltage
})
}

// getPhaseValues returns 3 sequential phase values
func (c *Connection) getPhaseValues(fun func(StatusSNSResponse) Channels) (float64, float64, float64, error) {
s, err := c.statusSnsG.Get()
if err != nil {
return 0, 0, 0, err
}

all := fun(s)

var res [3]float64
for i, c := range c.channels {
res[i], err = s.StatusSNS.Energy.Voltage.Channel(c)
for i := range res {
res[i], err = all.Channel(c.channels[i])
if err != nil {
return 0, 0, 0, err
}
}

return res[0], res[1], res[2], err
return res[0], res[1], res[2], nil
}

// SmlPower provides the sml sensor power
Expand Down
2 changes: 1 addition & 1 deletion meter/tasmota/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ type StatusSNSResponse struct {
}
}

// Channels is a Tasmota specifc helper type to handle meter value lists and single meter values
// Channels is a Tasmota specific helper type to handle meter value lists and single meter values
type Channels []float64

func (ch *Channels) Channel(channel int) (float64, error) {
Expand Down