Skip to content

Commit

Permalink
Synchronize enabled state with charger if state doesn't match (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed May 19, 2020
1 parent 044eb87 commit c02802f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
18 changes: 18 additions & 0 deletions core/loadpoint.go
Expand Up @@ -494,6 +494,19 @@ func (lp *LoadPoint) resetGuard() {
lp.guardUpdated = lp.clock.Now().Add(-2 * lp.GuardDuration)
}

// syncSettings synchronizes charger settings to expected state
func (lp *LoadPoint) syncSettings() {
enabled, err := lp.charger.Enabled()
if err == nil && enabled != lp.enabled {
log.ERROR.Printf("%s sync enabled", lp.Name)
err = lp.charger.Enable(lp.enabled)
}

if err != nil {
log.ERROR.Printf("%s charge controller error: %v", lp.Name, err)
}
}

// update is the main control function. It reevaluates meters and charger state
func (lp *LoadPoint) update() {
// read and publish meters first
Expand All @@ -513,6 +526,11 @@ func (lp *LoadPoint) update() {
lp.publish("connected", lp.connected())
lp.publish("charging", lp.charging)

// sync settings with charger
if lp.status != api.StatusA {
lp.syncSettings()
}

// check if car connected and ready for charging
var err error

Expand Down
9 changes: 9 additions & 0 deletions core/loadpoint_test.go
Expand Up @@ -185,6 +185,11 @@ func TestInitialUpdate(t *testing.T) {
cm.EXPECT().CurrentPower().Return(minPower, nil)
}

// syncSettings
if tc.status != api.StatusA {
wb.EXPECT().Enabled().Return(true, nil)
}

// disable if not connected
if tc.mode == api.ModeOff {
wb.EXPECT().Enable(false)
Expand Down Expand Up @@ -262,6 +267,7 @@ func TestImmediateOnOff(t *testing.T) {
wb.EXPECT().MaxCurrent(lpMaxCurrent)
}

wb.EXPECT().Enabled().Return(true, nil) // syncSettings
lp.update()

// max current if connected & mode now
Expand Down Expand Up @@ -294,6 +300,7 @@ func TestImmediateOnOff(t *testing.T) {

wb.EXPECT().MaxCurrent(2 * lpMinCurrent)

wb.EXPECT().Enabled().Return(true, nil) // syncSettings
lp.update()

// -- round 3
Expand All @@ -310,6 +317,8 @@ func TestImmediateOnOff(t *testing.T) {
wb.EXPECT().MaxCurrent(lpMinCurrent)

lp.SetMode(api.ModeOff)

wb.EXPECT().Enabled().Return(true, nil) // syncSettings
lp.update()

ctrl.Finish()
Expand Down

0 comments on commit c02802f

Please sign in to comment.