Skip to content

Commit

Permalink
EEBus: Fix Enabled handling, improve Status and connected handling, f…
Browse files Browse the repository at this point in the history
…ix typo (#2956)
  • Loading branch information
MarkusGH authored Mar 21, 2022
1 parent c5470e0 commit bd91963
Showing 1 changed file with 31 additions and 29 deletions.
60 changes: 31 additions & 29 deletions charger/eebus.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ type EEBus struct {
socSupportAvailable bool
selfConsumptionSupportAvailable bool

maxCurrent float64
connected bool
maxCurrent float64
connected bool
expectedEnableState bool

evConnectedTime time.Time
}
Expand Down Expand Up @@ -73,14 +74,14 @@ func NewEEBus(ski string, forcePVLimits bool) (*EEBus, error) {
}

func (c *EEBus) onConnect(ski string, conn ship.Conn) error {
c.log.TRACE.Println("!! onCconnect invoked on ski ", ski)
c.log.TRACE.Println("!! onConnect invoked on ski ", ski)

eebusDevice := app.HEMS(server.EEBusInstance.DeviceInfo())
c.cc = communication.NewConnectionController(c.log.TRACE, conn, eebusDevice)
c.cc.SetDataUpdateHandler(c.dataUpdateHandler)

c.connected = true
c.setDefaultValues()
c.setConnected(true)

err := c.cc.Boot()

Expand All @@ -90,16 +91,24 @@ func (c *EEBus) onConnect(ski string, conn ship.Conn) error {
func (c *EEBus) onDisconnect(ski string) {
c.log.TRACE.Println("!! onDisconnect invoked on ski ", ski)

c.connected = false
c.setConnected(false)
c.setDefaultValues()
}

func (c *EEBus) setDefaultValues() {
c.expectedEnableState = false
c.communicationStandard = communication.EVCommunicationStandardEnumTypeUnknown
c.socSupportAvailable = false
c.selfConsumptionSupportAvailable = false
}

func (c *EEBus) setConnected(connected bool) {
if connected && !c.connected {
c.evConnectedTime = time.Now()
}
c.connected = connected
}

func (c *EEBus) setLoadpointMinMaxLimits(data *communication.EVSEClientDataType) {
if c.lp == nil {
return
Expand Down Expand Up @@ -152,7 +161,7 @@ func (c *EEBus) showCurrentChargingSetup() {

func (c *EEBus) dataUpdateHandler(dataType communication.EVDataElementUpdateType, data *communication.EVSEClientDataType) {
// we receive data, so it is connected
c.connected = true
c.setConnected(true)

c.showCurrentChargingSetup()

Expand All @@ -165,6 +174,9 @@ func (c *EEBus) dataUpdateHandler(dataType communication.EVDataElementUpdateType
}
// case communication.EVDataElementUpdateUseCaseSoC:
case communication.EVDataElementUpdateEVConnectionState:
if data.EVData.ChargeState == communication.EVChargeStateEnumTypeUnplugged {
c.expectedEnableState = false
}
c.setLoadpointMinMaxLimits(data)
case communication.EVDataElementUpdateCommunicationStandard:
c.communicationStandard = data.EVData.CommunicationStandard
Expand All @@ -189,8 +201,7 @@ func isCharging(d communication.EVDataType) bool {
d.Measurements.PowerL3 > d.LimitsL3.Min*idleFactor
}

// Status implements the api.Charger interface
func (c *EEBus) Status() (api.ChargeStatus, error) {
func (c *EEBus) updateState() (api.ChargeStatus, error) {
data, err := c.cc.GetData()
if err != nil {
c.log.TRACE.Printf("!! status: no eebus data available yet")
Expand All @@ -206,12 +217,14 @@ func (c *EEBus) Status() (api.ChargeStatus, error) {

switch currentState {
case communication.EVChargeStateEnumTypeUnknown, communication.EVChargeStateEnumTypeUnplugged: // Unplugged
c.evConnectedTime = time.Now()
c.expectedEnableState = false
return api.StatusA, nil
case communication.EVChargeStateEnumTypeFinished, communication.EVChargeStateEnumTypePaused: // Finished, Paused
return api.StatusB, nil
case communication.EVChargeStateEnumTypeActive: // Active
if isCharging(data.EVData) {
// we might already be enabled and charging due to connection issues
c.expectedEnableState = true
return api.StatusC, nil
}
return api.StatusB, nil
Expand All @@ -222,29 +235,16 @@ func (c *EEBus) Status() (api.ChargeStatus, error) {
return api.StatusNone, fmt.Errorf("properties unknown result: %s", currentState)
}

// Status implements the api.Charger interface
func (c *EEBus) Status() (api.ChargeStatus, error) {
return c.updateState()
}

// Enabled implements the api.Charger interface
// should return true if the charger allows the EV to draw power
func (c *EEBus) Enabled() (bool, error) {
var res bool

// we might already be enabled and charging due to connection issues
data, err := c.cc.GetData()
if err == nil {
// handle ev being disconnected
if data.EVData.ChargeState != communication.EVChargeStateEnumTypeUnplugged &&
data.EVData.ChargeState != communication.EVChargeStateEnumTypeUnknown {

var status api.ChargeStatus
if status, err = c.Status(); err == nil {
if status == api.StatusB || status == api.StatusC {
res = isCharging(data.EVData)
}
}
}
}

// return the save enable state as we assume enabling/disabling always works
return res, err
_, err := c.updateState()
return c.expectedEnableState, err
}

// Enable implements the api.Charger interface
Expand Down Expand Up @@ -275,6 +275,8 @@ func (c *EEBus) Enable(enable bool) error {
return api.ErrMustRetry
}

c.expectedEnableState = enable

if !enable {
// Important notes on enabling/disabling!!
// ISO15118 mode:
Expand Down

0 comments on commit bd91963

Please sign in to comment.