chore: refactor reading phase values (#11347)
This commit is contained in:
parent
f0048cd09f
commit
8ed21de3df
4 changed files with 31 additions and 43 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue