Skip to content

Commit

Permalink
Improve realtime
Browse files Browse the repository at this point in the history
- reduced number of internet calls for local stops by using locally stored file
- removed procedure get_rt_route_statuses and get_rt_trip_statuses as no added value
Still requires testing and further cleaning plus remove debug logging
  • Loading branch information
vingerha committed Mar 22, 2024
1 parent a4f5327 commit 4e18b20
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 235 deletions.
19 changes: 9 additions & 10 deletions custom_components/gtfs2/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
ATTR_RT_UPDATED_AT
)
from .gtfs_helper import get_gtfs, get_next_departure, check_datasource_index, create_trip_geojson, check_extracting, get_local_stops_next_departures
from .gtfs_rt_helper import get_rt_route_statuses, get_rt_trip_statuses, get_next_services, get_rt_alerts
from .gtfs_rt_helper import get_next_services, get_rt_alerts

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -145,15 +145,14 @@ async def _async_update_data(self) -> dict[str, str]:
self._trip_id = self._data.get('next_departure', {}).get('trip_id', None)
self._direction = data["direction"]
self._relative = False
try:
self._get_rt_trip_statuses = await self.hass.async_add_executor_job(get_rt_trip_statuses, self)
self._get_rt_alerts = await self.hass.async_add_executor_job(get_rt_alerts, self)
self._get_next_service = await self.hass.async_add_executor_job(get_next_services, self)
self._data["next_departure_realtime_attr"] = self._get_next_service
self._data["next_departure_realtime_attr"]["gtfs_rt_updated_at"] = dt_util.utcnow()
self._data["alert"] = self._get_rt_alerts
except Exception as ex: # pylint: disable=broad-except
_LOGGER.error("Error getting gtfs realtime data, for origin: %s with error: %s", data["origin"], ex)
#try:
self._get_rt_alerts = await self.hass.async_add_executor_job(get_rt_alerts, self)
self._get_next_service = await self.hass.async_add_executor_job(get_next_services, self)
self._data["next_departure_realtime_attr"] = self._get_next_service
self._data["next_departure_realtime_attr"]["gtfs_rt_updated_at"] = dt_util.utcnow()
self._data["alert"] = self._get_rt_alerts
#except Exception as ex: # pylint: disable=broad-except
# _LOGGER.error("Error getting gtfs realtime data, for origin: %s with error: %s", data["origin"], ex)
else:
_LOGGER.debug("GTFS RT: RealTime = false, selected in entity options")
else:
Expand Down
21 changes: 19 additions & 2 deletions custom_components/gtfs2/gtfs_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
DEFAULT_PATH_GEOJSON,
DEFAULT_LOCAL_STOP_TIMERANGE,
DEFAULT_LOCAL_STOP_RADIUS,
DEFAULT_PATH_RT,
ICON,
ICONS,
DOMAIN,
TIME_STR_FORMAT
)
from .gtfs_rt_helper import get_rt_route_statuses, get_rt_trip_statuses, get_next_services, get_rt_alerts, get_rt_route_trip_statuses
from .gtfs_rt_helper import get_rt_route_trip_statuses, get_gtfs_rt

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -800,7 +801,22 @@ def get_local_stops_next_departures(self):
prev_stop_id = ""
prev_entry = entry = {}
# params for getting rt data
self._rt_group = "trip"
if self._realtime:
self._rt_group = "trip"
self._rt_data = {
"url": self._trip_update_url,
"headers": self._headers,
"file": self._data["name"] + "_localstop",
}
check = get_gtfs_rt(self.hass,DEFAULT_PATH_RT,self._rt_data)
# check if local file created
if not check:
_LOGGER.error("Could not download RT data from: %s", self._trip_update_url)
return False
else:
# use local file created as new url
self._trip_update_url = "file://" + DEFAULT_PATH_RT + "/" + self._data["name"] + "_localstop.rt"


for row_cursor in result:
row = row_cursor._asdict()
Expand Down Expand Up @@ -857,3 +873,4 @@ async def update_gtfs_local_stops(hass, data):
_LOGGER.debug("Reloading local stops for config_entry_id: %s", cf_entry)
reload = await hass.config_entries.async_reload(cf_entry)
return

Loading

0 comments on commit 4e18b20

Please sign in to comment.