Skip to content

Commit

Permalink
Update pv timer handling (evcc-io#2168)
Browse files Browse the repository at this point in the history
Co-authored-by: rivengh <75251226+rivengh@users.noreply.github.com>
  • Loading branch information
andig and rivengh authored Dec 31, 2021
1 parent 78e606f commit bac4e07
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
38 changes: 23 additions & 15 deletions core/loadpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,9 @@ func (lp *LoadPoint) evChargeStopHandler() {

// soc update reset
lp.socUpdated = time.Time{}

// reset pv enable/disable timer
lp.resetPVTimerIfRunning()
}

// evVehicleConnectHandler sends external start event
Expand Down Expand Up @@ -879,6 +882,22 @@ func (lp *LoadPoint) elapsePVTimer() {
lp.publishTimer(pvTimer, 0, timerInactive)
}

// resetPVTimerIfRunning resets the pv enable/disable timer to disabled state
func (lp *LoadPoint) resetPVTimerIfRunning(typ ...string) {
if lp.pvTimer.IsZero() {
return
}

msg := "pv timer reset"
if len(typ) == 1 {
msg = fmt.Sprintf("pv %s timer reset", typ)
}
lp.log.DEBUG.Printf(msg)

lp.pvTimer = time.Time{}
lp.publishTimer(pvTimer, 0, timerInactive)
}

// scalePhasesIfAvailable scales if api.ChargePhases is available
func (lp *LoadPoint) scalePhasesIfAvailable(phases int) error {
err := lp.scalePhases(phases)
Expand Down Expand Up @@ -1043,7 +1062,7 @@ func (lp *LoadPoint) publishTimer(name string, delay time.Duration, action strin
if action == timerInactive {
lp.log.DEBUG.Printf("%s action: %s", name, action)
} else {
lp.log.DEBUG.Printf("%s action: %s in %v", name, action, remaining)
lp.log.DEBUG.Printf("%s action: %s in %v", name, action, remaining.Round(time.Second))
}
}

Expand Down Expand Up @@ -1099,10 +1118,7 @@ func (lp *LoadPoint) pvMaxCurrent(mode api.ChargeMode, sitePower float64, batter
}
} else {
// reset timer
lp.log.DEBUG.Printf("pv disable timer reset: %v", lp.Disable.Delay)
lp.pvTimer = lp.clock.Now()

lp.publishTimer(pvTimer, 0, timerInactive)
lp.resetPVTimerIfRunning("disable")
}

// lp.log.DEBUG.Println("pv disable timer: keep enabled")
Expand Down Expand Up @@ -1134,23 +1150,15 @@ func (lp *LoadPoint) pvMaxCurrent(mode api.ChargeMode, sitePower float64, batter
}
} else {
// reset timer
lp.log.DEBUG.Printf("pv enable timer reset: %v", lp.Enable.Delay)
lp.pvTimer = lp.clock.Now()

lp.publishTimer(pvTimer, 0, timerInactive)
lp.resetPVTimerIfRunning("enable")
}

// lp.log.DEBUG.Println("pv enable timer: keep disabled")
return 0
}

// reset timer to disabled state
if !lp.pvTimer.IsZero() {
lp.log.DEBUG.Printf("pv timer reset")
lp.pvTimer = time.Time{}

lp.publishTimer(pvTimer, 0, timerInactive)
}
lp.resetPVTimerIfRunning()

// cap at maximum current
targetCurrent = math.Min(targetCurrent, maxCurrent)
Expand Down
14 changes: 7 additions & 7 deletions core/loadpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,25 +262,25 @@ func TestPVHysteresis(t *testing.T) {
{-500, 1, 0},
{-499, dt - 1, 0}, // should reset timer
{-500, dt + 1, 0}, // new begin of timer
{-500, 2*dt - 2, 0},
{-500, 2*dt - 1, minA},
{-500, 2 * dt, 0},
{-500, 2*dt + 1, minA},
}},
// reset enable timer when threshold not met while timer active and threshold not configured
{false, 0, 0, []se{
{-6*100*10 - 1, dt + 1, 0},
{-6 * 100 * 10, dt + 1, 0},
{-6 * 100 * 10, dt + 2, 0},
{-6 * 100 * 10, 2 * dt, 0},
{-6 * 100 * 10, 2*dt + 2, minA},
{-6 * 100 * 10, 2*dt + 1, minA},
}},
// reset disable timer when threshold not met while timer active
{true, 0, 500, []se{
{500, 0, minA},
{500, 1, minA},
{499, dt - 1, minA}, // reset timer
{500, dt + 1, minA}, // within reset timer duration
{500, 2*dt - 2, minA}, // still within reset timer duration
{500, 2*dt - 1, 0}, // reset timer elapsed
{499, dt - 1, minA}, // reset timer
{500, dt + 1, minA}, // within reset timer duration
{500, 2 * dt, minA}, // still within reset timer duration
{500, 2*dt + 1, 0}, // reset timer elapsed
}},
}

Expand Down

0 comments on commit bac4e07

Please sign in to comment.