Skip to content

Commit

Permalink
Improve on getting delays/departure for local stops
Browse files Browse the repository at this point in the history
  • Loading branch information
vingerha committed Apr 15, 2024
1 parent 631f392 commit bbac0f0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
11 changes: 8 additions & 3 deletions custom_components/gtfs2/gtfs_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -871,10 +871,15 @@ def get_local_stops_next_departures(self):
self._get_next_service = {}
_LOGGER.debug("Find rt for local stop route: %s - direction: %s - stop: %s", self._route , self._direction, self._stop_id)
next_service = get_rt_route_trip_statuses(self)
_LOGGER.debug("Next Service: %s", next_service)

if next_service:
departure_rt = next_service.get(self._route, {}).get(self._direction, {}).get(self._stop_id, []).get("departures", [])[0]
delay_rt = next_service.get(self._route, {}).get(self._direction, {}).get(self._stop_id, []).get("delays", [])[0]
if next_service:
delays = next_service.get(self._route, {}).get(self._direction, {}).get(self._stop_id, []).get("delays", [])
departures = next_service.get(self._route, {}).get(self._direction, {}).get(self._stop_id, []).get("departures", [])
_LOGGER.debug("ns: %s", delays)
delay_rt = delays[0] if delays else "-"
departure_rt = departures[0] if departures else "-"

_LOGGER.debug("Local stop next rt service1: %s", next_service)
timetable.append({"departure": row["departure_time"], "departure_realtime": departure_rt, "delay_realtime": delay_rt, "date": now_date, "stop_name": row['stop_name'], "route": row["route_short_name"], "route_long": row["route_long_name"], "headsign": row["trip_headsign"], "trip_id": row["trip_id"], "direction_id": row["direction_id"], "icon": self._icon})

Expand Down
10 changes: 6 additions & 4 deletions custom_components/gtfs2/gtfs_rt_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def get_rt_route_trip_statuses(self):
url=self._trip_update_url, headers=self._headers, label="trip data"
)
self._feed_entities = feed_entities
_LOGGER.debug("Search departure times for route: %s, type: %s, direction: %s", self._route_id, self._rt_group, self._direction)
_LOGGER.debug("Search departure times for route: %s, trip: %s, type: %s, direction: %s", self._route_id, self._trip_id, self._rt_group, self._direction)
for entity in feed_entities:

if entity.get('trip_update', False):
Expand All @@ -190,6 +190,7 @@ def get_rt_route_trip_statuses(self):
direction_id = self._direction

trip_id = entity["trip_update"]["trip"]["trip_id"]


if ((self._rt_group == "route" and (route_id == self._route_id and direction_id == self._direction) or (trip_id == self._trip_id and direction_id == "nn") ) or
(self._rt_group == "trip" and trip_id == self._trip_id )):
Expand All @@ -202,17 +203,17 @@ def get_rt_route_trip_statuses(self):
_LOGGER.debug("Stop found: %s", stop)
if route_id not in departure_times:
departure_times[route_id] = {}

if direction_id == "nn": # i this case the trip_id serves as a basis so one can safely set direction to the requesting entity direction
direction_id = self._direction

if direction_id not in departure_times[route_id]:
departure_times[route_id][direction_id] = {}

if not departure_times[route_id][direction_id].get(
stop_id
):
departure_times[route_id][direction_id][stop_id] = {}
departure_times[route_id][direction_id][stop_id] = {}

if not departure_times[route_id][direction_id][stop_id].get(
"departures"
Expand Down Expand Up @@ -242,6 +243,7 @@ def get_rt_route_trip_statuses(self):
_LOGGER.debug("Not using realtime stop data for old due-in-minutes: %s", due_in_minutes(datetime.fromtimestamp(stop_time)))

departure_times[route_id][direction_id][stop_id]["delays"].append(delay)


# Sort by time
for route in departure_times:
Expand Down

0 comments on commit bbac0f0

Please sign in to comment.