def update_scheduled_location(self): now = datetime.now(tz=tz.tzlocal()) if self.scheduled_active(): for station_a, station_b in window(self.stops): station_a_dept_time = self.scheduled_departures[station_a] if now >= station_a_dept_time: station_b_dept_time = self.scheduled_departures[station_b] station_b_arrv_time = station_b_dept_time - DWELL_TIME if now >= station_b_arrv_time and now < station_b_dept_time: self.scheduled_location = station_b self.scheduled_location_progress = utils.time_range_progress( station_b_arrv_time, station_b_dept_time, now ) self.scheduled_location_remaining = station_b_dept_time - now logging.info("Train %s is waiting at %s" % (repr(self), repr(station_b))) break elif now >= station_a_dept_time and now < station_b_arrv_time: self.scheduled_location = station_a.segment_to_station(station_b) self.scheduled_location_progress = utils.time_range_progress( station_a_dept_time, station_b_arrv_time, now ) self.scheduled_location_remaining = station_b_arrv_time - now logging.info( "%s is %s from %s" % (repr(self), str(self.scheduled_location_remaining), repr(station_b)) ) break elif self.scheduled_not_yet_started(): self.scheduled_location = self.origin self.scheduled_location_progress = 0.0 self.scheduled_location_remaining = self.scheduled_departure_for_station(self.origin) - now elif self.scheduled_ended(): logging.info("%s has reached the end of the line." % repr(self)) self.scheduled_location = self.destination self.scheduled_location_progress = 1.0 self.scheduled_location_remaining = timedelta(seconds=0)
def update_real_location(self): now = datetime.now(tz=tz.tzlocal()) departures = self.system.real_departures next_station = self.next_stop(self.scheduled_location) stations_to_check = [] if next_station: # might be end of line stations_to_check.append(next_station) if self.scheduled_location.__class__ == Station: stations_to_check.append(self.scheduled_location) if len(stations_to_check) < 1: # end of line self.real_location = self.destination self.real_location_progress = 1.0 self.real_location_remaining = timedelta(seconds=0) likely_matches = [] for station_to_check in stations_to_check: # Search backwards for likely match (direction, destination, time, known length) if station_to_check in departures and self.destination in departures[station_to_check]: possible_trains = departures[station_to_check][self.destination] else: possible_trains = [] for train in possible_trains: if not train["direction"] == self.direction(self.scheduled_location): continue if self.num_cars > 0 and train["length"] != self.num_cars: continue # this may screw things up if a wrong train gets matched at some point likely_matches.append(train) scheduled_departure = self.scheduled_departure_for_station(station_to_check) # when match is decided, set values for match in likely_matches: # filter out if more than a minute early if match["departure_time"] < scheduled_departure - DWELL_TIME - timedelta(minutes=1): continue else: arrival_time = match["departure_time"] - DWELL_TIME self.num_cars = match["length"] self.delay_amount = match["departure_time"] - scheduled_departure if now > arrival_time and now <= match["departure_time"]: self.real_location = station_to_check self.real_location_progress = utils.time_range_progress( arrival_time, match["departure_time"], now ) self.real_location_remaining = match["departure_time"] - now - DWELL_TIME elif now < arrival_time: # must be in previous segment prev_station = self.previous_stop(station_to_check) self.real_location = prev_station.segment_to_station(station_to_check) segment_total_time = self.real_location.time departure_time = arrival_time - segment_total_time self.real_location_progress = utils.time_range_progress(departure_time, arrival_time, now) self.real_location_remaining = arrival_time - now break