Skip to content

Commit

Permalink
detect and fix invalid cached vehicle (#1673)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinRinas authored Jul 8, 2024
1 parent 1a47509 commit 46dfeb4
Showing 1 changed file with 23 additions and 4 deletions.
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

0 comments on commit 46dfeb4

Please sign in to comment.