Skip to content

Commit

Permalink
1p3p: reduce duplicated code and publish clean values on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Aug 23, 2022
1 parent a262be8 commit a7f32ef
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions core/loadpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,10 +526,12 @@ func (lp *LoadPoint) Prepare(uiChan chan<- util.Param, pushChan chan<- push.Even
lp.publish("title", lp.Title)
lp.publish("minCurrent", lp.MinCurrent)
lp.publish("maxCurrent", lp.MaxCurrent)
lp.publish(phasesEnabled, lp.phases)
lp.publish(phasesActive, lp.activePhases())

lp.setConfiguredPhases(lp.ConfiguredPhases)
lp.publish(phasesEnabled, lp.phases)
lp.publish(phasesActive, lp.activePhases())
lp.publishTimer(phaseTimer, 0, timerInactive)
lp.publishTimer(pvTimer, 0, timerInactive)

lp.Lock()
lp.publish("mode", lp.Mode)
Expand Down Expand Up @@ -1039,6 +1041,12 @@ func (lp *LoadPoint) resetPVTimerIfRunning(typ ...string) {
lp.publishTimer(pvTimer, 0, timerInactive)
}

// resetPhaseTimer resets the phase switch timer to disabled state
func (lp *LoadPoint) resetPhaseTimer() {
lp.phaseTimer = time.Time{}
lp.publishTimer(phaseTimer, 0, timerInactive)
}

// scalePhasesRequired validates if fixed phase configuration matches enabled phases
func (lp *LoadPoint) scalePhasesRequired() bool {
_, ok := lp.charger.(api.PhaseSwitcher)
Expand Down Expand Up @@ -1078,12 +1086,12 @@ func (lp *LoadPoint) setPhases(phases int) {
if lp.GetPhases() != phases {
lp.Lock()
lp.phases = phases
lp.phaseTimer = time.Time{}
lp.Unlock()

lp.publish(phasesEnabled, lp.phases)
lp.publishTimer(phaseTimer, 0, timerInactive)
// reset timer to disabled state
lp.resetPhaseTimer()

// measure phases after switching
lp.resetMeasuredPhases()
}
}
Expand All @@ -1107,7 +1115,7 @@ func (lp *LoadPoint) scalePhases(phases int) error {
return fmt.Errorf("switch phases: %w", err)
}

// update setting
// update setting and reset timer
lp.setPhases(phases)

// allow pv mode to re-enable charger right away
Expand Down Expand Up @@ -1187,10 +1195,7 @@ func (lp *LoadPoint) pvScalePhases(availablePower, minCurrent, maxCurrent float6

// reset timer to disabled state
if !waiting && !lp.phaseTimer.IsZero() {
lp.log.DEBUG.Printf("phase timer reset")
lp.phaseTimer = time.Time{}

lp.publishTimer(phaseTimer, 0, timerInactive)
lp.resetPhaseTimer()
}

return false
Expand Down

0 comments on commit a7f32ef

Please sign in to comment.