From 87b372c5d147f73f8ae410e5c975e2b687a64be5 Mon Sep 17 00:00:00 2001 From: felix-zenk Date: Sat, 15 Jun 2024 10:53:23 +0200 Subject: [PATCH] Fix RegioGuide stations --- onboardapis/train/de/db/apis.py | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/onboardapis/train/de/db/apis.py b/onboardapis/train/de/db/apis.py index 1ed7abe..31e9b30 100644 --- a/onboardapis/train/de/db/apis.py +++ b/onboardapis/train/de/db/apis.py @@ -14,7 +14,14 @@ from ....units import meters_per_second from ... import Train, TrainStation from .mappings import id_name_map -from .interfaces import ICEPortalAPI, RegioGuideAPI, ICEPortalInternetInterface, ICEPortalInternetAccessAPI +from .interfaces import ( + ICEPortalAPI, + ICEPortalInternetInterface, + ICEPortalInternetAccessAPI, + RegioGuideAPI, + RegioGuideInternetAccessInterface, + RegioGuideInternetAccessAPI, +) logger = logging.getLogger(__name__) @@ -238,15 +245,21 @@ def internet_status_change(self) -> timedelta | None: return None if default(remaining_seconds) is None else timedelta(seconds=int(remaining_seconds)) -class RegioGuide(Train, StationsMixin[TrainStation]): +class RegioGuide(Train, StationsMixin[TrainStation], InternetAccessMixin): """Wrapper for interacting with the DB Regio-Guide API, formerly Zug Portal.""" _api: RegioGuideAPI + _internet_access: RegioGuideInternetAccessInterface def __init__(self): self._api = RegioGuideAPI() + self._internet_access = RegioGuideInternetAccessInterface(RegioGuideInternetAccessAPI()) Train.__init__(self) + @property + def now(self) -> datetime: + return datetime.fromisoformat(self._api['journey'].get('date')) + @property def id(self) -> ID: return self._api['journey'].get('no') @@ -270,12 +283,12 @@ def stations_dict(self) -> dict[ID, TrainStation]: actual=stop.get('track', {}).get('prediction') ), arrival=None if stop.get('arrivalTime') is None else ScheduledEvent( - scheduled=datetime.fromtimestamp(stop.get('arrivalTime', {}).get('targetTimeInMs', 0)), - actual=datetime.fromtimestamp(stop.get('arrivalTime', {}).get('predictedTimeInMs', 0)) + scheduled=datetime.fromisoformat(stop.get('arrivalTime', {})['target']), + actual=datetime.fromisoformat(stop.get('arrivalTime', {})['predicted']) ), departure=None if stop.get('departureTime') is None else ScheduledEvent( - scheduled=datetime.fromtimestamp(stop.get('departureTime', {}).get('targetTimeInMs', 0)), - actual=datetime.fromtimestamp(stop.get('departureTime', {}).get('predictedTimeInMs', 0)) + scheduled=datetime.fromisoformat(stop.get('departureTime', {})['target']), + actual=datetime.fromisoformat(stop.get('departureTime', {})['predicted']) ), position=Position( latitude=stop.get('station', {}).get('position', {}).get('latitude'), @@ -289,10 +302,9 @@ def stations_dict(self) -> dict[ID, TrainStation]: @property def current_station(self) -> TrainStation: - station, *_ = (*filter( - lambda s: self.now < default(s.arrival.actual, s.departure.actual), - self.stations - ), None) + station, *_ = *filter( + lambda s: self.now < s.arrival.actual, filter(lambda s: s.arrival is not None, self.stations) + ), None return self.destination if station is None else station