a
    ki                     @   s6   d dl Z d dlZd dlmZ g dZG dd dZdS )    N)gtfs_realtime_pb2)ZEMPTYZMANY_SEATS_AVAILABLEZFEW_SEATS_AVAILABLEZSTANDING_ROOM_ONLYZCRUSHED_STANDING_ROOM_ONLYZFULLZNOT_ACCEPTING_PASSENGERSc                   @   sh   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd ZdS )RealtimeDataFetcherz
    This class fetches TripUpdates.pb and VehicleUpdates.pb files, and converts them into dictionaries. Data is cached
    and refreshed when needed.
    c                 C   s4   || _ || _t | _d | _t | _t | _d | _d S N)_tripupdates_url_positions_urldict_delays_delays_last_updated
_positions_occupancies_positions_last_updated)selfZtripupdates_urlZvehicle_positions_url r   $/var/www/html/RealtimeDataFetcher.py__init__   s    zRealtimeDataFetcher.__init__c                 C   s   |   r|   | jS )z
        Get the data. Cached data if it was fetched recently,
        or a fresh copy if the stored data is expired.
        :return: a dict mapping (trip id, stop sequence) tuples to the delay for that departure.
        )_are_delays_outdated_refresh_delaysr   r   r   r   r   
get_delays   s    zRealtimeDataFetcher.get_delaysc                 C   s   |   r|   | jS )z
        Get the data. Cached data if it was fetched recently,
        or a fresh copy if the stored data is expired.
        :return: a dict mapping trip_ids to vehicle positions.
        )_are_positions_outdated_refresh_vehicle_position_datar
   r   r   r   r   get_positions(   s    z!RealtimeDataFetcher.get_positionsc                 C   s   |   r|   | jS )z
        Get the data. Cached data if it was fetched recently,
        or a fresh copy if the stored data is expired.
        :return: a dict mapping trip_ids to occupancies positions.
        )r   r   r   r   r   r   r   get_occupancies2   s    z#RealtimeDataFetcher.get_occupanciesc                 C   s$   t t }| jd u p"|| j dkS )N<   )inttimer	   r   nowr   r   r   r   =   s    z(RealtimeDataFetcher._are_delays_outdatedc                 C   s$   t t }| jd u p"|| j dkS )N   )r   r   r   r   r   r   r   r   B   s    z+RealtimeDataFetcher._are_positions_outdatedc           	      C   s   t  }t }tj| jddj}|| |jD ]L}|	dr0|j
jj}|j
jD ]*}|j}|	dsfqP|jj}||||f< qPq0|| _tt | _d S )NFverifytrip_update	departure)r   r   FeedMessagerequestsgetr   contentParseFromStringentityHasFieldr!   triptrip_idZstop_time_updatestop_sequencer"   delayr   r   r   r	   )	r   Zdelaysfeedresponser(   r+   updater,   r-   r   r   r   r   G   s    




z#RealtimeDataFetcher._refresh_delaysc                 C   s   t  }t  }t }tj| jddj}|| |jD ]J}|	dr6|j
j}|j
jj}|j|j|j|jd d||< |j
j||< q6|| _|| _tt | _d S )NFr   vehicleg@)latitude	longitudebearingspeed)r   r   r#   r$   r%   r   r&   r'   r(   r)   r1   positionr*   r+   r2   r3   r4   r5   Zoccupancy_statusr   r
   r   r   r   )r   Z	positionsZoccupanciesr.   r/   r(   r6   r+   r   r   r   r   [   s$    




z2RealtimeDataFetcher._refresh_vehicle_position_datac                 C   s,   |   }t|}||f|vr dS |||f S )Nr   )r   r   )r   r+   r,   datar   r   r   get_delay_for_trip_stopr   s
    z+RealtimeDataFetcher.get_delay_for_trip_stopc                 C   s   |   }||vri S || S r   )r   r   r+   r7   r   r   r   get_position_for_tripz   s    z)RealtimeDataFetcher.get_position_for_tripc                 C   s    |   }||vrdS t||  S )NUNKNOWN)r   OCCUPANCY_STATUS_MAPr9   r   r   r   get_occupancy_for_trip   s    z*RealtimeDataFetcher.get_occupancy_for_tripN)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r8   r:   r=   r   r   r   r   r      s   	

r   )r   r$   Zgoogle.transitr   r<   r   r   r   r   r   <module>   s   	