Skip to content

Commit

Permalink
Fix RegioGuide stations
Browse files Browse the repository at this point in the history
  • Loading branch information
felix-zenk committed Jun 15, 2024
1 parent b33bd84 commit 87b372c
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions onboardapis/train/de/db/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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')
Expand All @@ -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'),
Expand All @@ -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


Expand Down

0 comments on commit 87b372c

Please sign in to comment.