diff --git a/packages/modules/vehicles/vwid/libeuda.py b/packages/modules/vehicles/vwid/libeuda.py index 0b1c33f02e..e0f21a25d0 100755 --- a/packages/modules/vehicles/vwid/libeuda.py +++ b/packages/modules/vehicles/vwid/libeuda.py @@ -152,6 +152,7 @@ def get_oidc_state(brand: str = DEFAULT_BRAND) -> str: DATA_PATH = Path(__file__).resolve().parents[4] / "data" / "modules" / "vwid" JSON_PATH = Path(str(RAMDISK_PATH) + '/vweuda') storeFileName = '/data_' +MODULE_TYPE = 'vwid' # VIN-Brand map VIN_BRAND_MAP = { @@ -792,9 +793,25 @@ def update_result_from_payload(self, payload: dict, source: str) -> dict: """Parse an EUDA payload and publish it in the in-memory result cache.""" vin = payload['vin'] result = parse_vehicle_data(payload) - euda.result[vin] = result + _valid = True + _LOGGER.info(f"latest result=\n{json.dumps(result, indent=4)}") + if vin in euda.result: + _LOGGER.info(f"cache result=\n{json.dumps(euda.result[vin], indent=4)}") + if result['soc_timestamp'] < euda.result[vin]['soc_timestamp']: + _LOGGER.info("thread result skipped, soc_timestamp too old") + _valid = False + if result['soc'] is None: + _LOGGER.info("thread result skipped, no soc found") + _valid = False + if _valid: + if vin in euda.result and result['odometer'] < euda.result[vin]['odometer']: + _LOGGER.info("odometer less than earlier - keep earlier value") + result['odometer'] = euda.result[vin]['odometer'] + euda.result[vin] = result + _LOGGER.info("thread result is valid") + _ano_j = {ano_vin(vin): euda.result[vin]} - _LOGGER.info(f"thread result:\n{json.dumps(_ano_j, indent=4)}") + _LOGGER.info(f"\n{json.dumps(_ano_j, indent=4)}") return result @@ -814,6 +831,13 @@ def load_latest_json_result(self, vin: str) -> bool: _LOGGER.exception(f"failed to load latest EUDA JSON {latest}: {err}") return False + async def get_module_type(self, vehicle: int) -> str: + topic = f"openWB/vehicle/{vehicle}/soc_module/config" + conf = os.popen(f"mosquitto_sub -C 1 -t {topic}").read() + _LOGGER.debug(f"thread loop: conf={conf}") + _type = json.loads(conf)['type'] + return _type + # eudaThread async def async_eudaThread(self, username: str, password: str, vin: str, vehicle: int): if vin[0:3] not in VIN_BRAND_MAP: @@ -821,7 +845,8 @@ async def async_eudaThread(self, username: str, password: str, vin: str, vehicle brand = VIN_BRAND_MAP.get(vin[0:3], DEFAULT_BRAND) _LOGGER.info(f"async Thread started, brand={brand}") try: - async with aiohttp.ClientSession(headers={'Connection': 'keep-alive'}) as session: + async with aiohttp.ClientSession(headers={'Connection': 'keep-alive'}, + connector_owner=False) as session: client_id = f"{vehicle}" _k = str(euda.client.keys()) _LOGGER.info(f"libeuda.Thread client at entry: euda.client.keys={_k}") @@ -851,15 +876,14 @@ async def async_eudaThread(self, username: str, password: str, vin: str, vehicle identifier = meta.get("Identifier") # thread main loop - while True: - topic = f"openWB/vehicle/{vehicle}/soc_module/config" - conf = os.popen(f"mosquitto_sub -C 1 -t {topic}").read() - _LOGGER.debug(f"thread loop: conf={conf}") - _type = json.loads(conf)['type'] - _LOGGER.info(f"thread loop: _type={_type}") - if _type != 'vwid': + _active = True + while _active: + _type = await self.get_module_type(vehicle) + _LOGGER.info(f"thread loop: ev{vehicle} module type={_type}") + if _type != MODULE_TYPE: _LOGGER.info(f"vehicle {vehicle} is not using module vwid: terminate now") - return + _active = False + continue try: _data = None while _data is None: @@ -969,11 +993,11 @@ async def get_status(self, wait_until = time.time() + INITIAL_RESULT_WAIT while self.vin not in euda.result and time.time() < wait_until: - _LOGGER.info(f"wait for first EUDA result for VIN {self.vin}") + _LOGGER.info(f"wait for first EUDA result for VIN {ano_vin(self.vin)}") time.sleep(1) if self.vin in euda.result: - _LOGGER.debug(f"vehicle match: {self.vin}") + _LOGGER.debug(f"vehicle match: {ano_vin(self.vin)}") _ano_j = {} for vin in euda.result: _ano_j[ano_vin(vin)] = euda.result[vin]