Skip to content

Commit

Permalink
EEBus: improve configure and charger handling (evcc-io#5670)
Browse files Browse the repository at this point in the history
  • Loading branch information
DerAndereAndi authored Jan 6, 2023
1 parent 813b9f7 commit 2e90958
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
43 changes: 40 additions & 3 deletions charger/eebus.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package charger
import (
"errors"
"fmt"
"os"
"sync"
"time"

"github.com/enbility/cemd/emobility"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/core/loadpoint"
"github.com/evcc-io/evcc/server"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
)

const (
Expand All @@ -35,6 +38,8 @@ type EEBus struct {
lastIsChargingResult bool

evConnectedTime time.Time

mux sync.Mutex
}

func init() {
Expand Down Expand Up @@ -84,7 +89,29 @@ func NewEEBus(ski, ip string, hasMeter, hasChargedEnergy bool) (api.Charger, err
return decorateEEBus(c, c.currentPower, c.currents, nil), nil
}

return c, nil
// wait for first update
err := c.waitForInitialUpdate()

return c, err
}

// returns an error on failure
func (c *EEBus) waitForInitialUpdate() error {
// wait for a connection
timeout := time.After(request.Timeout)
tick := time.Tick(1 * time.Second)

// keep trying until we're timed out or got a positive result
for {
select {
case <-timeout:
return os.ErrDeadlineExceeded
case <-tick:
if c.isConnected() {
return nil
}
}
}
}

func (c *EEBus) onConnect(ski string) {
Expand All @@ -110,12 +137,22 @@ func (c *EEBus) setDefaultValues() {
}

func (c *EEBus) setConnected(connected bool) {
c.mux.Lock()
defer c.mux.Unlock()

if connected && !c.connected {
c.evConnectedTime = time.Now()
}
c.connected = connected
}

func (c *EEBus) isConnected() bool {
c.mux.Lock()
defer c.mux.Unlock()

return c.connected
}

func (c *EEBus) setLoadpointMinMaxLimits() {
if c.lp == nil {
return
Expand Down Expand Up @@ -186,7 +223,7 @@ func (c *EEBus) updateState() (api.ChargeStatus, error) {
return api.StatusNone, err
}

if !c.connected {
if !c.isConnected() {
return api.StatusNone, fmt.Errorf("%s charger reported as disconnected", c.ski)
}

Expand Down Expand Up @@ -410,7 +447,7 @@ var _ api.Identifier = (*EEBus)(nil)

// Identify implements the api.Identifier interface
func (c *EEBus) Identify() (string, error) {
if !c.connected {
if !c.isConnected() {
return "", nil
}

Expand Down
1 change: 1 addition & 0 deletions cmd/configure/devicetest.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func (d *DeviceTest) testCharger(v interface{}) (DeviceTestResult, error) {
if !ok {
return DeviceTestResultInvalid, errors.New("selected device is not a wallbox")
}

if _, err := c.Status(); err != nil {
return DeviceTestResultInvalid, err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/configure/localization/de.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Requirements_Sponsorship_Feature_Title = "Dies Verwendung dieser Funktionalität
Requirements_Sponsorship_Token = "Bist du ein Sponsor?"
Requirements_Sponsorship_Token_Input = "Bitte gib das Sponsortoken ein"
Requirements_EEBUS_Cert_Error = "Fehler: Das EEBUS Zertifikat konnte nicht erstellt werden"
Requirements_EEBUS_Pairing = "Du hast eine Wallbox ausgewählt, welche über das EEBUS Protokoll angesprochen wird.\nDazu muss die Wallbox nun mit evcc verbunden werden. Dies geschieht üblicherweise auf der Webseite der Wallbox.\nDrücke die Enter-Taste, wenn dies abgeschlossen ist."
Requirements_EEBUS_Pairing = "Du hast eine Wallbox ausgewählt, welche über das EEBUS Protokoll angesprochen wird.\nDazu muss die Wallbox nun mit evcc verbunden werden. Dies geschieht üblicherweise auf der Webseite der Wallbox.\nDrücke die Enter-Taste, wenn der Prozess gestartet ist."
Config_Title = "Führe folgende Einstellungen durch:"
Config_ModbusInterface = "Wähle die ModBus Schnittstelle aus"
Config_AddAnotherValue = "Möchtest du einen weiteren Wert hinzufügen?"
Expand Down
2 changes: 1 addition & 1 deletion cmd/configure/localization/en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Requirements_Sponsorship_Feature_Title = "To use this feature a evcc sponsorship
Requirements_Sponsorship_Token = "Are you already a sponsor?"
Requirements_Sponsorship_Token_Input = "Please enter the sponsortoken"
Requirements_EEBUS_Cert_Error = "Error: The EEBUS certificate couldn't be created"
Requirements_EEBUS_Pairing = "You selected a wallbox, which will be accessed via the EEBUS protocol.\nFor that the wallbox needs to be connected to evcc. This can usually be done in the web interface of the wallbox.\nPlease press the enter key once this is done."
Requirements_EEBUS_Pairing = "You selected a wallbox, which will be accessed via the EEBUS protocol.\nFor that the wallbox needs to be connected to evcc. This can usually be done in the web interface of the wallbox.\nPlease press the enter key once you started the process."
Config_Title = "Please provide the following settings:"
Config_ModbusInterface = "Choose the ModBus interface"
Config_AddAnotherValue = "Do you want to add another value?"
Expand Down

0 comments on commit 2e90958

Please sign in to comment.