diff --git a/charger/keba-modbus.go b/charger/keba-modbus.go index 359767476..2d4fa5362 100644 --- a/charger/keba-modbus.go +++ b/charger/keba-modbus.go @@ -167,68 +167,55 @@ func (wb *Keba) heartbeat(timeout time.Duration) { } } -func (wb *Keba) isConnected() (bool, error) { - b, err := wb.conn.ReadHoldingRegisters(kebaRegCableState, 2) - - // 0: No cable is plugged. - // 1: Cable is connected to the charging station (not to the electric vehicle). - // 3: Cable is connected to the charging station and locked (not to the electric vehicle). - // 5: Cable is connected to the charging station and the electric vehicle (not locked). - // 7: Cable is connected to the charging station and the electric vehicle and locked (charging). - - return binary.BigEndian.Uint32(b)&5 != 0, err -} - -func (wb *Keba) getChargingState() (uint32, error) { - b, err := wb.conn.ReadHoldingRegisters(kebaRegChargingState, 2) - - // 0: Start-up of the charging station - // 1: The charging station is not ready for charging. The charging station is not connected to an electric vehicle, it is locked by the authorization function or another mechanism. - // 2: The charging station is ready for charging and waits for a reaction from the electric vehicle. - // 3: A charging process is active. - // 4: An error has occurred. - // 5: The charging process is temporarily interrupted because the temperature is too high or the wallbox is in suspended mode. - - return binary.BigEndian.Uint32(b), err -} - // Status implements the api.Charger interface func (wb *Keba) Status() (api.ChargeStatus, error) { - if connected, err := wb.isConnected(); err != nil || !connected { - return api.StatusA, err + b, err := wb.conn.ReadHoldingRegisters(kebaRegCableState, 2) + if err != nil { + return api.StatusNone, err } - s, err := wb.getChargingState() - if err != nil { - return api.StatusA, err + switch status := binary.BigEndian.Uint32(b); status { + case 0, 1, 3: + return api.StatusA, nil + + case 5: + return api.StatusB, nil + + case 7: + b, err := wb.conn.ReadHoldingRegisters(kebaRegChargingState, 2) + if err != nil { + return api.StatusNone, err + } + if binary.BigEndian.Uint32(b) == 3 { + return api.StatusC, err + } + return api.StatusB, nil + + default: + return api.StatusNone, fmt.Errorf("invalid status: %d", status) } - if s == 3 { - return api.StatusC, nil - } - return api.StatusB, nil } // statusReason implements the api.StatusReasoner interface func (wb *Keba) statusReason() (api.Reason, error) { - if connected, err := wb.isConnected(); err != nil || !connected { - return api.ReasonUnknown, err + res := api.ReasonUnknown + + b, err := wb.conn.ReadHoldingRegisters(kebaRegChargingState, 2) + if err == nil && binary.BigEndian.Uint32(b) == 1 { + res = api.ReasonWaitingForAuthorization } - if s, err := wb.getChargingState(); err != nil || s != 1 { - return api.ReasonUnknown, err - } - - return api.ReasonWaitingForAuthorization, nil + return res, err } // Enabled implements the api.Charger interface func (wb *Keba) Enabled() (bool, error) { - s, err := wb.getChargingState() + b, err := wb.conn.ReadHoldingRegisters(kebaRegChargingState, 2) if err != nil { return false, err } - - return !(s == 5 || s == 1), nil + status := binary.BigEndian.Uint32(b) + return !(status == 5 || status == 1), nil } // Enable implements the api.Charger interface