From e8b77963da4537224b57c53726c96296bd9dc424 Mon Sep 17 00:00:00 2001 From: Simon Thelen Date: Fri, 30 Aug 2024 08:54:53 +0200 Subject: [PATCH] Add functions to read currently stored meter readings and use in tests --- usecases/api/mu_mpc.go | 39 +++++++++ usecases/mu/mpc/public.go | 145 +++++++++++++++++++++++++++++++++ usecases/mu/mpc/public_test.go | 94 ++++++++++++++++++--- 3 files changed, 268 insertions(+), 10 deletions(-) diff --git a/usecases/api/mu_mpc.go b/usecases/api/mu_mpc.go index c4d38947..8329b323 100644 --- a/usecases/api/mu_mpc.go +++ b/usecases/api/mu_mpc.go @@ -17,6 +17,13 @@ type MuMPCInterface interface { // - power: the active power SetPower(power float64) error + // return the momentary active power consumption or production + // + // possible errors: + // - ErrDataNotAvailable if no such value is (yet) available + // - and others + Power() (float64, error) + // set the momentary active phase specific power consumption or production per phase // // parameters: @@ -25,6 +32,13 @@ type MuMPCInterface interface { // - phaseC: the active power of phase C SetPowerPerPhase(phaseA, phaseB, phaseC float64) error + // return the momentary active phase specific power consumption or production per phase + // + // possible errors: + // - ErrDataNotAvailable if no such values are (yet) available + // - and others + PowerPerPhase() ([]float64, error) + // Scenario 2 // set the total consumption energy @@ -33,12 +47,24 @@ type MuMPCInterface interface { // - consumed: the total consumption energy SetEnergyConsumed(consumed float64) error + // return the total feed in energy + // + // return values: + // - negative values are used for production + EnergyConsumed() (float64, error) + // set the total feed in energy // // parameters: // - produced: the total feed in energy SetEnergyProduced(produced float64) error + // return the total feed in energy + // + // return values: + // - negative values are used for production + EnergyProduced() (float64, error) + // Scenario 3 // set the momentary phase specific current consumption or production @@ -49,6 +75,13 @@ type MuMPCInterface interface { // - phaseC: the current of phase C SetCurrentPerPhase(phaseA, phaseB, phaseC float64) error + // return the momentary phase specific current consumption or production + // + // return values + // - positive values are used for consumption + // - negative values are used for production + CurrentPerPhase() ([]float64, error) + // Scenario 4 // set the phase specific voltage details @@ -59,6 +92,9 @@ type MuMPCInterface interface { // - phaseC: the voltage of phase C SetVoltagePerPhase(phaseA, phaseB, phaseC float64) error + // return the phase specific voltage details + VoltagePerPhase() ([]float64, error) + // Scenario 5 // set frequency @@ -66,4 +102,7 @@ type MuMPCInterface interface { // parameters: // - frequency: the frequency SetFrequency(frequency float64) error + + // return frequency + Frequency() (float64, error) } diff --git a/usecases/mu/mpc/public.go b/usecases/mu/mpc/public.go index 46f95935..284e4092 100644 --- a/usecases/mu/mpc/public.go +++ b/usecases/mu/mpc/public.go @@ -24,6 +24,19 @@ func (e *MPC) SetPower(power float64) error { return nil } +// get the momentary active power consumption or production +// +// possible errors: +// - ErrMissingData if the id is not available +// - and others +func (e *MPC) Power() (float64, error) { + if e.acPowerTotal == nil { + return 0, api.ErrMissingData + } + + return e.getMeasurementDataForId(e.acPowerTotal) +} + // set the momentary active power consumption or production per phase // // possible errors: @@ -52,6 +65,34 @@ func (e *MPC) SetPowerPerPhase(phaseA, phaseB, phaseC float64) error { return nil } +// get the momentary active power consumption or production per phase +// +// possible errors: +// - ErrMissingData if the id is not available +// - and others +func (e *MPC) PowerPerPhase() ([]float64, error) { + if e.acPower[0] == nil || e.acPower[1] == nil || e.acPower[2] == nil { + return nil, api.ErrMissingData + } + + phaseA, err := e.getMeasurementDataForId(e.acPower[0]) + if err != nil { + return nil, err + } + + phaseB, err := e.getMeasurementDataForId(e.acPower[1]) + if err != nil { + return nil, err + } + + phaseC, err := e.getMeasurementDataForId(e.acPower[2]) + if err != nil { + return nil, err + } + + return []float64{phaseA, phaseB, phaseC}, nil +} + // Scenario 2 // set the total consumption energy @@ -70,6 +111,21 @@ func (e *MPC) SetEnergyConsumed(energy float64) error { return nil } +// get the total feed in energy +// +// - negative values are used for production +// +// possible errors: +// - ErrMissingData if the id is not available +// - and others +func (e *MPC) EnergyConsumed() (float64, error) { + if e.acEnergyConsumed == nil { + return 0, api.ErrMissingData + } + + return e.getMeasurementDataForId(e.acEnergyConsumed) +} + // set the total feed in energy // // - negative values are used for production @@ -86,6 +142,21 @@ func (e *MPC) SetEnergyProduced(energy float64) error { return nil } +// get the total feed in energy +// +// - negative values are used for production +// +// possible errors: +// - ErrMissingData if the id is not available +// - and others +func (e *MPC) EnergyProduced() (float64, error) { + if e.acEnergyProduced == nil { + return 0, api.ErrMissingData + } + + return e.getMeasurementDataForId(e.acEnergyProduced) +} + // Scenario 3 // set the momentary phase specific current consumption or production @@ -115,6 +186,37 @@ func (e *MPC) SetCurrentPerPhase(phaseA, phaseB, phaseC float64) error { return nil } +// get the momentary phase specific current consumption or production +// +// - positive values are used for consumption +// - negative values are used for production +// +// possible errors: +// - ErrMissingData if the id is not available +// - and others +func (e *MPC) CurrentPerPhase() ([]float64, error) { + if e.acCurrent[0] == nil || e.acCurrent[1] == nil || e.acCurrent[2] == nil { + return nil, api.ErrMissingData + } + + phaseA, err := e.getMeasurementDataForId(e.acCurrent[0]) + if err != nil { + return nil, err + } + + phaseB, err := e.getMeasurementDataForId(e.acCurrent[1]) + if err != nil { + return nil, err + } + + phaseC, err := e.getMeasurementDataForId(e.acCurrent[2]) + if err != nil { + return nil, err + } + + return []float64{phaseA, phaseB, phaseC}, nil +} + // Scenario 4 // set the phase specific voltage details @@ -143,6 +245,36 @@ func (e *MPC) SetVoltagePerPhase(phaseA, phaseB, phaseC float64) error { return nil } +// get the phase specific voltage details +// +// possible errors: +// - ErrMissingData if the id is not available +// - and others +func (e *MPC) VoltagePerPhase() ([]float64, error) { + for _, id := range e.acVoltage { + if id == nil { + return nil, api.ErrMissingData + } + } + + phaseA, err := e.getMeasurementDataForId(e.acVoltage[0]) + if err != nil { + return nil, err + } + + phaseB, err := e.getMeasurementDataForId(e.acVoltage[1]) + if err != nil { + return nil, err + } + + phaseC, err := e.getMeasurementDataForId(e.acVoltage[2]) + if err != nil { + return nil, err + } + + return []float64{phaseA, phaseB, phaseC}, nil +} + // Scenario 5 // SetFrequency set frequency @@ -158,3 +290,16 @@ func (e *MPC) SetFrequency(frequency float64) error { return nil } + +// get frequency +// +// possible errors: +// - ErrMissingData if the id is not available +// - and others +func (e *MPC) Frequency() (float64, error) { + if e.acFrequency == nil { + return 0, api.ErrMissingData + } + + return e.getMeasurementDataForId(e.acFrequency) +} diff --git a/usecases/mu/mpc/public_test.go b/usecases/mu/mpc/public_test.go index decee094..d1112f85 100644 --- a/usecases/mu/mpc/public_test.go +++ b/usecases/mu/mpc/public_test.go @@ -1,40 +1,114 @@ package mpc -import ( - "github.com/stretchr/testify/assert" -) +import "github.com/stretchr/testify/assert" func (s *MuMPCSuite) Test_Power() { - err := s.sut.SetPower(5.0) + power, err := s.sut.Power() + assert.Equal(s.T(), 0.0, power) + assert.Nil(s.T(), err) + + err = s.sut.SetPower(17.0) + assert.Nil(s.T(), err) + + power, err = s.sut.Power() + assert.Equal(s.T(), 17.0, power) assert.Nil(s.T(), err) } func (s *MuMPCSuite) Test_PowerPerPhase() { - err := s.sut.SetPowerPerPhase(5.0, 5.0, 5.0) + phases, err := s.sut.PowerPerPhase() + + assert.Equal(s.T(), 3, len(phases)) + assert.Equal(s.T(), 0.0, phases[0]) + assert.Equal(s.T(), 0.0, phases[1]) + assert.Equal(s.T(), 0.0, phases[2]) + assert.Nil(s.T(), err) + + err = s.sut.SetPowerPerPhase(17.0, 18.0, 19.0) assert.Nil(s.T(), err) + + phases, err = s.sut.PowerPerPhase() + assert.Equal(s.T(), 3, len(phases)) + assert.Equal(s.T(), 17.0, phases[0]) + assert.Equal(s.T(), 18.0, phases[1]) + assert.Equal(s.T(), 19.0, phases[2]) } func (s *MuMPCSuite) Test_EnergyConsumed() { - err := s.sut.SetEnergyConsumed(5.0) + energy, err := s.sut.EnergyConsumed() + assert.Equal(s.T(), 0.0, energy) + assert.Nil(s.T(), err) + + err = s.sut.SetEnergyConsumed(17.0) + assert.Nil(s.T(), err) + + energy, err = s.sut.EnergyConsumed() + assert.Equal(s.T(), 17.0, energy) assert.Nil(s.T(), err) } func (s *MuMPCSuite) Test_EnergyProduced() { - err := s.sut.SetEnergyProduced(5.0) + energy, err := s.sut.EnergyProduced() + assert.Equal(s.T(), 0.0, energy) + assert.Nil(s.T(), err) + + err = s.sut.SetEnergyProduced(17.0) + assert.Nil(s.T(), err) + + energy, err = s.sut.EnergyProduced() + assert.Equal(s.T(), 17.0, energy) assert.Nil(s.T(), err) } func (s *MuMPCSuite) Test_CurrentPerPhase() { - err := s.sut.SetCurrentPerPhase(5.0, 5.0, 5.0) + current, err := s.sut.CurrentPerPhase() + + assert.Nil(s.T(), err) + assert.Equal(s.T(), 3, len(current)) + assert.Equal(s.T(), 0.0, current[0]) + assert.Equal(s.T(), 0.0, current[1]) + assert.Equal(s.T(), 0.0, current[2]) + + err = s.sut.SetCurrentPerPhase(17.0, 18.0, 19.0) assert.Nil(s.T(), err) + + current, err = s.sut.CurrentPerPhase() + assert.Equal(s.T(), 3, len(current)) + assert.Equal(s.T(), 17.0, current[0]) + assert.Equal(s.T(), 18.0, current[1]) + assert.Equal(s.T(), 19.0, current[2]) } func (s *MuMPCSuite) Test_VoltagePerPhase() { - err := s.sut.SetVoltagePerPhase(5.0, 5.0, 5.0) + voltages, err := s.sut.VoltagePerPhase() + assert.Nil(s.T(), err) + + assert.Equal(s.T(), 3, len(voltages)) + assert.Equal(s.T(), 0.0, voltages[0]) + assert.Equal(s.T(), 0.0, voltages[1]) + assert.Equal(s.T(), 0.0, voltages[2]) + + err = s.sut.SetVoltagePerPhase(1.0, 2.0, 3.0) assert.Nil(s.T(), err) + + voltages, err = s.sut.VoltagePerPhase() + assert.Nil(s.T(), err) + + assert.Equal(s.T(), 3, len(voltages)) + assert.Equal(s.T(), 1.0, voltages[0]) + assert.Equal(s.T(), 2.0, voltages[1]) + assert.Equal(s.T(), 3.0, voltages[2]) } func (s *MuMPCSuite) Test_Frequency() { - err := s.sut.SetFrequency(5.0) + frequency, err := s.sut.Frequency() + assert.Nil(s.T(), err) + assert.Equal(s.T(), 0.0, frequency) + + err = s.sut.SetFrequency(50.0) + assert.Nil(s.T(), err) + + frequency, err = s.sut.Frequency() assert.Nil(s.T(), err) + assert.Equal(s.T(), 50.0, frequency) }