chore: further reduce code for reading phase values (#10317)
This commit is contained in:
parent
7ad03ecc34
commit
af705d2c7c
4 changed files with 50 additions and 70 deletions
|
|
@ -209,38 +209,33 @@ func (wb *ABB) ChargedEnergy() (float64, error) {
|
|||
return float64(binary.BigEndian.Uint32(b)) / 1e3, err
|
||||
}
|
||||
|
||||
var _ api.PhaseCurrents = (*ABB)(nil)
|
||||
|
||||
// Currents implements the api.PhaseCurrents interface
|
||||
func (wb *ABB) Currents() (float64, float64, float64, error) {
|
||||
b, err := wb.conn.ReadHoldingRegisters(abbRegCurrents, 6)
|
||||
// getPhaseValues returns 3 sequential register values
|
||||
func (wb *ABB) getPhaseValues(reg uint16, divider float64) (float64, float64, float64, error) {
|
||||
b, err := wb.conn.ReadHoldingRegisters(reg, 6)
|
||||
if err != nil {
|
||||
return 0, 0, 0, err
|
||||
}
|
||||
|
||||
var res [3]float64
|
||||
for l := 0; l < 3; l++ {
|
||||
res[l] = float64(binary.BigEndian.Uint32(b[4*l:])) / 1e3
|
||||
for i := 0; i < 3; i++ {
|
||||
res[i] = float64(binary.BigEndian.Uint32(b[4*i:])) / divider
|
||||
}
|
||||
|
||||
return res[0], res[1], res[2], nil
|
||||
}
|
||||
|
||||
var _ api.PhaseCurrents = (*ABB)(nil)
|
||||
|
||||
// Currents implements the api.PhaseCurrents interface
|
||||
func (wb *ABB) Currents() (float64, float64, float64, error) {
|
||||
return wb.getPhaseValues(abbRegCurrents, 1e3)
|
||||
}
|
||||
|
||||
var _ api.PhaseVoltages = (*ABB)(nil)
|
||||
|
||||
// Voltages implements the api.PhaseVoltages interface
|
||||
func (wb *ABB) Voltages() (float64, float64, float64, error) {
|
||||
b, err := wb.conn.ReadHoldingRegisters(abbRegVoltages, 6)
|
||||
if err != nil {
|
||||
return 0, 0, 0, err
|
||||
}
|
||||
|
||||
var res [3]float64
|
||||
for l := 0; l < 3; l++ {
|
||||
res[l] = float64(binary.BigEndian.Uint32(b[4*l:])) / 10
|
||||
}
|
||||
|
||||
return res[0], res[1], res[2], nil
|
||||
return wb.getPhaseValues(abbRegVoltages, 10)
|
||||
}
|
||||
|
||||
// var _ api.PhaseSwitcher = (*ABB)(nil)
|
||||
|
|
|
|||
|
|
@ -280,34 +280,29 @@ func (wb *BenderCC) totalEnergy() (float64, error) {
|
|||
return float64(binary.BigEndian.Uint32(b)) / 1e3, nil
|
||||
}
|
||||
|
||||
// currents implements the api.PhaseCurrents interface
|
||||
func (wb *BenderCC) currents() (float64, float64, float64, error) {
|
||||
b, err := wb.conn.ReadHoldingRegisters(bendRegCurrents, 6)
|
||||
// getPhaseValues returns 3 sequential register values
|
||||
func (wb *BenderCC) getPhaseValues(reg uint16, divider float64) (float64, float64, float64, error) {
|
||||
b, err := wb.conn.ReadHoldingRegisters(reg, 6)
|
||||
if err != nil {
|
||||
return 0, 0, 0, err
|
||||
}
|
||||
|
||||
var res [3]float64
|
||||
for l := 0; l < 3; l++ {
|
||||
res[l] = float64(binary.BigEndian.Uint32(b[4*l:4*(l+1)])) / 1e3
|
||||
for i := 0; i < 3; i++ {
|
||||
res[i] = float64(binary.BigEndian.Uint32(b[4*i:])) / divider
|
||||
}
|
||||
|
||||
return res[0], res[1], res[2], nil
|
||||
}
|
||||
|
||||
// currents implements the api.PhaseCurrents interface
|
||||
func (wb *BenderCC) currents() (float64, float64, float64, error) {
|
||||
return wb.getPhaseValues(bendRegCurrents, 1e3)
|
||||
}
|
||||
|
||||
// voltages implements the api.PhaseVoltages interface
|
||||
func (wb *BenderCC) voltages() (float64, float64, float64, error) {
|
||||
b, err := wb.conn.ReadHoldingRegisters(bendRegVoltages, 6)
|
||||
if err != nil {
|
||||
return 0, 0, 0, err
|
||||
}
|
||||
|
||||
var res [3]float64
|
||||
for l := 0; l < 3; l++ {
|
||||
res[l] = float64(binary.BigEndian.Uint32(b[4*l : 4*(l+1)]))
|
||||
}
|
||||
|
||||
return res[0], res[1], res[2], nil
|
||||
return wb.getPhaseValues(bendRegVoltages, 1)
|
||||
}
|
||||
|
||||
// identify implements the api.Identifier interface
|
||||
|
|
|
|||
|
|
@ -262,38 +262,33 @@ func (wb *HeidelbergEC) TotalEnergy() (float64, error) {
|
|||
return float64(binary.BigEndian.Uint32(b)) / 1e3, nil
|
||||
}
|
||||
|
||||
var _ api.PhaseCurrents = (*HeidelbergEC)(nil)
|
||||
|
||||
// Currents implements the api.PhaseCurrents interface
|
||||
func (wb *HeidelbergEC) Currents() (float64, float64, float64, error) {
|
||||
b, err := wb.conn.ReadInputRegisters(hecRegCurrents, 3)
|
||||
// getPhaseValues returns 3 sequential register values
|
||||
func (wb *HeidelbergEC) getPhaseValues(reg uint16, divider float64) (float64, float64, float64, error) {
|
||||
b, err := wb.conn.ReadInputRegisters(reg, 3)
|
||||
if err != nil {
|
||||
return 0, 0, 0, err
|
||||
}
|
||||
|
||||
var res [3]float64
|
||||
for l := 0; l < 3; l++ {
|
||||
res[l] = float64(binary.BigEndian.Uint16(b[2*l:])) / 10
|
||||
for i := 0; i < 3; i++ {
|
||||
res[i] = float64(binary.BigEndian.Uint16(b[2*i:])) / divider
|
||||
}
|
||||
|
||||
return res[0], res[1], res[2], nil
|
||||
}
|
||||
|
||||
var _ api.PhaseCurrents = (*HeidelbergEC)(nil)
|
||||
|
||||
// Currents implements the api.PhaseCurrents interface
|
||||
func (wb *HeidelbergEC) Currents() (float64, float64, float64, error) {
|
||||
return wb.getPhaseValues(hecRegCurrents, 10)
|
||||
}
|
||||
|
||||
var _ api.PhaseVoltages = (*HeidelbergEC)(nil)
|
||||
|
||||
// Voltages implements the api.PhaseVoltages interface
|
||||
func (wb *HeidelbergEC) Voltages() (float64, float64, float64, error) {
|
||||
b, err := wb.conn.ReadInputRegisters(hecRegVoltages, 3)
|
||||
if err != nil {
|
||||
return 0, 0, 0, err
|
||||
}
|
||||
|
||||
var res [3]float64
|
||||
for l := 0; l < 3; l++ {
|
||||
res[l] = float64(binary.BigEndian.Uint16(b[2*l:]))
|
||||
}
|
||||
|
||||
return res[0], res[1], res[2], nil
|
||||
return wb.getPhaseValues(hecRegVoltages, 1)
|
||||
}
|
||||
|
||||
var _ api.Diagnosis = (*HeidelbergEC)(nil)
|
||||
|
|
|
|||
|
|
@ -210,38 +210,33 @@ func (wb *Vestel) ChargedEnergy() (float64, error) {
|
|||
return float64(binary.BigEndian.Uint32(b)) / 1e3, err
|
||||
}
|
||||
|
||||
var _ api.PhaseCurrents = (*Vestel)(nil)
|
||||
|
||||
// Currents implements the api.PhaseCurrents interface
|
||||
func (wb *Vestel) Currents() (float64, float64, float64, error) {
|
||||
// getPhaseValues returns 3 sequential register values
|
||||
func (wb *Vestel) getPhaseValues(regs []uint16, divider float64) (float64, float64, float64, error) {
|
||||
var res [3]float64
|
||||
for i, regCurrent := range vestelRegCurrents {
|
||||
b, err := wb.conn.ReadInputRegisters(regCurrent, 1)
|
||||
for i, reg := range regs {
|
||||
b, err := wb.conn.ReadInputRegisters(reg, 1)
|
||||
if err != nil {
|
||||
return 0, 0, 0, err
|
||||
}
|
||||
|
||||
res[i] = float64(binary.BigEndian.Uint16(b)) / 1e3
|
||||
res[i] = float64(binary.BigEndian.Uint16(b)) / divider
|
||||
}
|
||||
|
||||
return res[0], res[1], res[2], nil
|
||||
}
|
||||
|
||||
var _ api.PhaseCurrents = (*Vestel)(nil)
|
||||
|
||||
// Currents implements the api.PhaseCurrents interface
|
||||
func (wb *Vestel) Currents() (float64, float64, float64, error) {
|
||||
return wb.getPhaseValues(vestelRegCurrents, 1e3)
|
||||
}
|
||||
|
||||
var _ api.PhaseVoltages = (*Vestel)(nil)
|
||||
|
||||
// Voltages implements the api.PhaseVoltages interface
|
||||
func (wb *Vestel) Voltages() (float64, float64, float64, error) {
|
||||
var res [3]float64
|
||||
for i, regVoltage := range vestelRegVoltages {
|
||||
b, err := wb.conn.ReadInputRegisters(regVoltage, 1)
|
||||
if err != nil {
|
||||
return 0, 0, 0, err
|
||||
}
|
||||
|
||||
res[i] = float64(binary.BigEndian.Uint16(b))
|
||||
}
|
||||
|
||||
return res[0], res[1], res[2], nil
|
||||
return wb.getPhaseValues(vestelRegVoltages, 1)
|
||||
}
|
||||
|
||||
var _ api.Diagnosis = (*Vestel)(nil)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue