diff --git a/backend/backend/idfm_interface/__init__.py b/backend/backend/idfm_interface/__init__.py index 070099a..21afc48 100644 --- a/backend/backend/idfm_interface/__init__.py +++ b/backend/backend/idfm_interface/__init__.py @@ -2,6 +2,7 @@ from .idfm_interface import IdfmInterface from .idfm_types import ( Coordinate, + Destinations, FramedVehicleJourney, IdfmLineState, IdfmOperator, @@ -35,6 +36,7 @@ from .idfm_types import ( __all__ = [ "Coordinate", + "Destinations", "FramedVehicleJourney", "IdfmInterface", "IdfmLineState", diff --git a/backend/backend/idfm_interface/idfm_interface.py b/backend/backend/idfm_interface/idfm_interface.py index a717b07..c6cf33e 100644 --- a/backend/backend/idfm_interface/idfm_interface.py +++ b/backend/backend/idfm_interface/idfm_interface.py @@ -22,6 +22,7 @@ from ..db import Database from ..models import ConnectionArea, Line, LinePicto, Stop, StopArea, StopShape from .idfm_types import ( ConnectionArea as IdfmConnectionArea, + Destinations as IdfmDestinations, IdfmLineState, IdfmResponse, Line as IdfmLine, @@ -521,20 +522,40 @@ class IdfmInterface: return ret - async def get_destinations(self, stop_point_id: str) -> Iterable[str]: - # TODO: Store in database the destination for the given stop and line id. + async def get_destinations(self, stop_id: int) -> IdfmDestinations | None: begin_ts = time() - destinations: dict[str, str] = {} - if (res := await self.get_next_passages(stop_point_id)) is not None: + + destinations: IdfmDestinations = defaultdict(set) + + if (stop := await Stop.get_by_id(stop_id)) is not None: + expected_stop_ids = {stop.id} + + elif (stop_area := await StopArea.get_by_id(stop_id)) is not None: + expected_stop_ids = {stop.id for stop in stop_area.stops} + + else: + return None + + if (res := await self.get_next_passages(stop_id)) is not None: + for delivery in res.Siri.ServiceDelivery.StopMonitoringDelivery: if delivery.Status == IdfmState.true: for stop_visit in delivery.MonitoredStopVisit: + + monitoring_ref = stop_visit.MonitoringRef.value + try: + monitored_stop_id = int(monitoring_ref.split(":")[-2]) + except (IndexError, ValueError): + print(f"Unable to get stop id from {monitoring_ref}") + continue + journey = stop_visit.MonitoredVehicleJourney - if (destination_name := journey.DestinationName) and ( - line_ref := journey.LineRef - ): - line_id = line_ref.value.replace("STIF:Line::", "")[:-1] - print(f"{line_id = }") - destinations[line_id] = destination_name[0].value + if ( + dst_names := journey.DestinationName + ) and monitored_stop_id in expected_stop_ids: + + line_id = journey.LineRef.value.split(":")[-2] + destinations[line_id].add(dst_names[0].value) + print(f"get_next_passages: {time() - begin_ts}") return destinations diff --git a/backend/backend/idfm_interface/idfm_types.py b/backend/backend/idfm_interface/idfm_types.py index efb2c71..e742171 100644 --- a/backend/backend/idfm_interface/idfm_types.py +++ b/backend/backend/idfm_interface/idfm_types.py @@ -197,6 +197,8 @@ class Line(Struct): Lines = dict[str, Line] +Destinations = dict[str, set[str]] + # TODO: Set structs frozen class StopLineAssoFields(Struct):