Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EVCC SoC: detect and fix invalid cached vehicle #1673

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions packages/modules/vehicles/evcc/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
gRPCRetryTime = 5
gRPCRetryCount = 5
gRPCRetryResponse = 'must retry'
gRPCVehicleNoLongerValid = 'vehicle not available'


def write_vehicle_id_mqtt(topic: str, vehicle_id: int, config: EVCCVehicleSocConfiguration):
Expand Down Expand Up @@ -44,6 +45,19 @@ def create_vehicle(config: EVCCVehicleSocConfiguration, stub: vehicle_pb2_grpc.V
return response.vehicle_id


def create_and_save_vehicle_id(
stub: vehicle_pb2_grpc.VehicleStub,
config: EVCCVehicleSocConfiguration,
vehicle: int) -> int:
vehicle_to_fetch = create_vehicle(config, stub)
log.debug("Vehicle client received: " + str(vehicle_to_fetch))

# saving vehicle id in config
topic = "openWB/set/vehicle/" + str(vehicle) + "/soc_module/config"
write_vehicle_id_mqtt(topic, vehicle_to_fetch, config)
return vehicle_to_fetch


def fetch_soc(
evcc_config: EVCCVehicleSocConfiguration,
vehicle_update_data: VehicleUpdateData,
Expand All @@ -54,12 +68,13 @@ def fetch_soc(
stub = vehicle_pb2_grpc.VehicleStub(channel)

if not evcc_config.vehicle_id: # create and fetch vehicle id if not included in config
vehicle_to_fetch = create_vehicle(evcc_config, stub)
log.debug("Vehicle client received: " + str(vehicle_to_fetch))
create_and_save_vehicle_id(stub, evcc_config, vehicle)
# vehicle_to_fetch = create_vehicle(evcc_config, stub)
# log.debug("Vehicle client received: " + str(vehicle_to_fetch))

# saving vehicle id in config
topic = "openWB/set/vehicle/" + str(vehicle) + "/soc_module/config"
write_vehicle_id_mqtt(topic, vehicle_to_fetch, evcc_config)
# topic = "openWB/set/vehicle/" + str(vehicle) + "/soc_module/config"
# write_vehicle_id_mqtt(topic, vehicle_to_fetch, evcc_config)
else:
log.debug("Vehicle id found in config: " + str(evcc_config.vehicle_id))
vehicle_to_fetch = evcc_config.vehicle_id
Expand All @@ -82,6 +97,10 @@ def fetch_soc(
time.sleep(gRPCRetryTime)
log.debug("retrying now...")
RetryCounter += 1
elif rpc_error.details() == gRPCVehicleNoLongerValid: # vehicle no longer valid
log.debug(f'cached vehicle {vehicle_to_fetch} no longer valid, creating new vehicle')
vehicle_to_fetch = create_and_save_vehicle_id(stub, evcc_config, vehicle)
RetryCounter += 1
else: # some other error, raise exception and exit
raise grpc.RpcError(rpc_error)

Expand Down