chore: speedup mbmd setup

This commit is contained in:
andig 2025-07-13 16:35:56 +02:00
parent 9c7d8cd9b5
commit 217a6ddd3f
2 changed files with 11 additions and 11 deletions

View file

@ -88,7 +88,9 @@ func NewModbusMbmdFromConfig(ctx context.Context, other map[string]interface{})
device: device,
}
m.opPower, err = rs485FindDeviceOp(device, cc.Power)
ops := device.Producer().Produce()
m.opPower, err = rs485FindDeviceOp(ops, cc.Power)
if err != nil {
return nil, fmt.Errorf("invalid measurement for power: %s", cc.Power)
}
@ -96,7 +98,7 @@ func NewModbusMbmdFromConfig(ctx context.Context, other map[string]interface{})
// decorate energy
var totalEnergy func() (float64, error)
if cc.Energy != "" {
m.opEnergy, err = rs485FindDeviceOp(device, cc.Energy)
m.opEnergy, err = rs485FindDeviceOp(ops, cc.Energy)
if err != nil {
return nil, fmt.Errorf("invalid measurement for energy: %s", cc.Energy)
}
@ -105,19 +107,19 @@ func NewModbusMbmdFromConfig(ctx context.Context, other map[string]interface{})
}
// decorate currents
currentsG, err := m.buildPhaseProviders(cc.Currents)
currentsG, err := m.buildPhaseProviders(ops, cc.Currents)
if err != nil {
return nil, fmt.Errorf("currents: %w", err)
}
// decorate voltages
voltagesG, err := m.buildPhaseProviders(cc.Voltages)
voltagesG, err := m.buildPhaseProviders(ops, cc.Voltages)
if err != nil {
return nil, fmt.Errorf("voltages: %w", err)
}
// decorate powers
powersG, err := m.buildPhaseProviders(cc.Powers)
powersG, err := m.buildPhaseProviders(ops, cc.Powers)
if err != nil {
return nil, fmt.Errorf("powers: %w", err)
}
@ -125,7 +127,7 @@ func NewModbusMbmdFromConfig(ctx context.Context, other map[string]interface{})
// decorate soc
var soc func() (float64, error)
if cc.Soc != "" {
m.opSoc, err = rs485FindDeviceOp(device, cc.Soc)
m.opSoc, err = rs485FindDeviceOp(ops, cc.Soc)
if err != nil {
return nil, fmt.Errorf("invalid measurement for soc: %s", cc.Soc)
}
@ -136,7 +138,7 @@ func NewModbusMbmdFromConfig(ctx context.Context, other map[string]interface{})
return decorateModbusMbmd(m, totalEnergy, currentsG, voltagesG, powersG, soc, cc.batteryCapacity.Decorator()), nil
}
func (m *ModbusMbmd) buildPhaseProviders(readings []string) (func() (float64, float64, float64, error), error) {
func (m *ModbusMbmd) buildPhaseProviders(ops []rs485.Operation, readings []string) (func() (float64, float64, float64, error), error) {
if len(readings) == 0 {
return nil, nil
}
@ -147,7 +149,7 @@ func (m *ModbusMbmd) buildPhaseProviders(readings []string) (func() (float64, fl
var phases [3]func() (float64, error)
for idx, reading := range readings {
opCurrent, err := rs485FindDeviceOp(m.device, reading)
opCurrent, err := rs485FindDeviceOp(ops, reading)
if err != nil {
return nil, fmt.Errorf("invalid measurement [%d]: %s", idx, reading)
}

View file

@ -19,14 +19,12 @@ func isRS485(model string) bool {
}
// rs485FindDeviceOp checks is RS485 device supports operation
func rs485FindDeviceOp(device *rs485.RS485, name string) (op rs485.Operation, err error) {
func rs485FindDeviceOp(ops []rs485.Operation, name string) (op rs485.Operation, err error) {
measurement, err := meters.MeasurementString(name)
if err != nil {
return rs485.Operation{}, fmt.Errorf("invalid measurement: %s", name)
}
ops := device.Producer().Produce()
for _, op := range ops {
if op.IEC61850 == measurement {
return op, nil