Simplify dummy meter to use power instead of current
This commit is contained in:
parent
73753e1212
commit
32bc279dba
3 changed files with 23 additions and 44 deletions
|
|
@ -204,19 +204,20 @@ func (lp *LoadPoint) evChargeStopHandler() {
|
|||
|
||||
// evChargeCurrentHandler updates proxy charge meter's charge current.
|
||||
// If physical charge meter is present this handler is not used.
|
||||
func (lp *LoadPoint) evChargeCurrentHandler(m *wrapper.ChargeMeter) func(para ...interface{}) {
|
||||
return func(para ...interface{}) {
|
||||
current := para[0].(int64)
|
||||
// It assumes that the charge meter cannot consume more than total household consumption
|
||||
func (lp *LoadPoint) evChargeCurrentHandler(m *wrapper.ChargeMeter) func(current int64) {
|
||||
return func(current int64) {
|
||||
power := float64(current*lp.Phases) * lp.Voltage
|
||||
|
||||
if !lp.enabled || lp.status != api.StatusC {
|
||||
current = 0
|
||||
}
|
||||
if current > 0 {
|
||||
power = 0
|
||||
} else if power > 0 {
|
||||
// limit available power to generation plus consumption/ minus delivery
|
||||
availablePower := math.Abs(lp.pvPower) + lp.availableBatteryPower() + lp.gridPower
|
||||
availableCurrent := int64(powerToCurrent(availablePower, lp.Voltage, lp.Phases))
|
||||
current = min(current, availableCurrent)
|
||||
consumedPower := math.Abs(lp.pvPower) + lp.availableBatteryPower() + lp.gridPower
|
||||
power = math.Min(power, consumedPower)
|
||||
}
|
||||
m.SetChargeCurrent(current)
|
||||
|
||||
m.SetPower(power)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -243,13 +244,10 @@ func (lp *LoadPoint) Prepare(uiChan chan<- Param, notificationChan chan<- push.E
|
|||
if mt, ok := lp.charger.(api.Meter); ok {
|
||||
lp.chargeMeter = mt
|
||||
} else {
|
||||
mt := &wrapper.ChargeMeter{
|
||||
Phases: lp.Phases,
|
||||
Voltage: lp.Voltage,
|
||||
}
|
||||
mt := &wrapper.ChargeMeter{}
|
||||
_ = lp.bus.Subscribe(evChargeCurrent, lp.evChargeCurrentHandler(mt))
|
||||
_ = lp.bus.Subscribe(evStopCharge, func() {
|
||||
mt.SetChargeCurrent(0)
|
||||
mt.SetPower(0)
|
||||
})
|
||||
lp.chargeMeter = mt
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,20 +8,14 @@ import (
|
|||
// It uses the charger's actual or max current to calculate power consumption.
|
||||
type ChargeMeter struct {
|
||||
sync.Mutex
|
||||
Phases int64
|
||||
Voltage float64
|
||||
power float64
|
||||
power float64
|
||||
}
|
||||
|
||||
func currentToPower(current, voltage float64, phases int64) float64 {
|
||||
return float64(phases) * current * voltage
|
||||
}
|
||||
|
||||
// SetChargeCurrent updates meter's current power based on actual current
|
||||
func (m *ChargeMeter) SetChargeCurrent(current int64) {
|
||||
// SetPower updates meter's current power
|
||||
func (m *ChargeMeter) SetPower(power float64) {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
m.power = currentToPower(float64(current), m.Voltage, m.Phases)
|
||||
m.power = power
|
||||
}
|
||||
|
||||
// CurrentPower implements the Meter.CurrentPower interface
|
||||
|
|
|
|||
|
|
@ -6,31 +6,18 @@ import (
|
|||
"github.com/golang/mock/gomock"
|
||||
)
|
||||
|
||||
func TestProxyMeterSinglePhase(t *testing.T) {
|
||||
func TestProxyChargeMeter(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
|
||||
tc := []struct {
|
||||
phases int64
|
||||
voltage float64
|
||||
current int64
|
||||
power float64
|
||||
}{
|
||||
{1, 100, 10, 1000},
|
||||
{3, 200, 1, 600},
|
||||
}
|
||||
tc := []float64{600, 1000, 2000}
|
||||
m := ChargeMeter{}
|
||||
|
||||
for _, tc := range tc {
|
||||
m := ChargeMeter{
|
||||
Phases: tc.phases,
|
||||
Voltage: tc.voltage,
|
||||
}
|
||||
for _, f := range tc {
|
||||
m.SetPower(f)
|
||||
|
||||
m.SetChargeCurrent(tc.current)
|
||||
|
||||
if p, err := m.CurrentPower(); p != tc.power || err != nil {
|
||||
if p, err := m.CurrentPower(); p != f || err != nil {
|
||||
t.Errorf("power: %.1f %v", p, err)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue