diff --git a/custom_components/gtfs2/gtfs_helper.py b/custom_components/gtfs2/gtfs_helper.py index d7ccd10..8315b4d 100644 --- a/custom_components/gtfs2/gtfs_helper.py +++ b/custom_components/gtfs2/gtfs_helper.py @@ -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}) diff --git a/custom_components/gtfs2/gtfs_rt_helper.py b/custom_components/gtfs2/gtfs_rt_helper.py index acb7b53..6d8d8f9 100644 --- a/custom_components/gtfs2/gtfs_rt_helper.py +++ b/custom_components/gtfs2/gtfs_rt_helper.py @@ -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): @@ -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 )): @@ -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" @@ -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: