Exemplo n.º 1
0
def get_relevant_service_ids(tracker_id):
    result = load_by_key(get_train_tracker_relevant_services_key(tracker_id))
    if not result:
        start_date = load_by_key(get_train_tracker_day(tracker_id))
        relevant_services = gtfs.models.Service.objects.filter(start_date \
                                                               = start_date)
        result = [x[0] for x in relevant_services.all().values_list(\
            'service_id')]
        save_by_key(get_train_tracker_relevant_services_key(tracker_id),\
                    result)    
    return result
Exemplo n.º 2
0
def update_coords(report, tracker_id):
    loc = report.get_my_loc()
    coords = [loc.lat, loc.lon]
    res_shape_sampled_point_ids, _ = shapes.all_shapes.query_sampled_points(coords, loc.accuracy_in_coords)
     
    added_count = cl.sadd(get_train_tracker_visited_shape_sampled_point_ids_key(tracker_id), res_shape_sampled_point_ids)

    trips, time_deviation_in_seconds = get_trips(tracker_id)
    trip = get_trusted_trip_or_none(trips, time_deviation_in_seconds)

    if trip:
        cl.setex(get_current_trip_id_report_timestamp_key(trip), TRACKER_TTL, ot_utils.dt_time_to_unix_time(report.timestamp))
        if added_count > 0:
            save_by_key(get_current_trip_id_coords_key(trip),\
                        coords,\
                        timeout=TRACKER_TTL, cl=p)
            save_by_key(get_current_trip_id_coords_timestamp_key(trip),\
                        ot_utils.dt_time_to_unix_time(report.timestamp),\
                        timeout=TRACKER_TTL, cl=p)
        else: # extend expiration:
            p.expire(get_current_trip_id_coords_key(trip), TRACKER_TTL)
            p.expire(get_current_trip_id_coords_timestamp_key(trip), TRACKER_TTL)
        p.execute()          
Exemplo n.º 3
0
def update_coords(report, tracker_id):
    loc = report.get_my_loc()
    coords = [loc.lat, loc.lon]
    logger.info('Updating coords for tracker_id={} by report={} to coords={}'.format(tracker_id, report, coords))    
    res_shape_sampled_point_ids, _ = shapes.all_shapes.query_sampled_points(coords, loc.accuracy_in_coords)
     
    added_count = cl.sadd(get_train_tracker_visited_shape_sampled_point_ids_key(tracker_id), res_shape_sampled_point_ids)

    trip_delays_ids_list_of_lists = load_by_key(get_train_tracker_trip_delays_ids_list_of_lists_key(tracker_id))
    trips = get_trusted_trips(trip_delays_ids_list_of_lists)

    for trip in trips:
        cl.setex(get_current_trip_id_report_timestamp_key(trip), TRACKER_TTL, ot_utils.dt_time_to_unix_time(report.timestamp))
        if added_count > 0:
            save_by_key(get_current_trip_id_coords_key(trip),\
                        coords,\
                        timeout=TRACKER_TTL, cl=p)
            save_by_key(get_current_trip_id_coords_timestamp_key(trip),\
                        ot_utils.dt_time_to_unix_time(report.timestamp),\
                        timeout=TRACKER_TTL, cl=p)
        else: # extend expiration:
            p.expire(get_current_trip_id_coords_key(trip), TRACKER_TTL)
            p.expire(get_current_trip_id_coords_timestamp_key(trip), TRACKER_TTL)
        p.execute()          
Exemplo n.º 4
0
def try_update_coords(report, tracker_id):
    loc = report.get_my_loc()
    coords = [loc.lat, loc.lon]
    res_shape_sampled_point_ids, _ = shapes.all_shapes.query_sampled_points(coords, loc.accuracy_in_coords)
     
    added_count = cl.sadd(get_train_tracker_visited_shape_sampled_point_ids_key(tracker_id), res_shape_sampled_point_ids)

    trips = load_by_key(get_train_tracker_trip_ids_key(tracker_id))
    trip = trips[0] if trips else None
    if added_count > 0:
        
        save_by_key(get_train_tracker_coords_key(tracker_id), coords, cl=p)
        
        if trip is not None and len(trips) <= TRACKER_REPORT_FOR_TRIP_COUNT_LOWER_THAN:
            save_by_key(get_current_trip_id_coords_key(trip), coords, timeout=TRACKER_TTL, cl=p)
            save_by_key(get_current_trip_id_coords_timestamp_key(trip), ot_utils.dt_time_to_unix_time(report.timestamp), timeout=TRACKER_TTL, cl=p)
        p.execute()              
    
    if trip is not None:    
        cl.setex(get_current_trip_id_report_timestamp_key(trip), TRACKER_TTL, ot_utils.dt_time_to_unix_time(report.timestamp))
Exemplo n.º 5
0
 def set_current(self, state, stop_id, timestamp):
     key = get_train_tracker_data_key(
         self.tracker_id)
     value = (state, str(stop_id), timestamp.isoformat())
     logger.info('key={}, value={}'.format(key, value))
     save_by_key(key, value, cl=p)
Exemplo n.º 6
0
def update_trips(tracker_id):
    trips, time_deviation_in_seconds = get_possible_trips(tracker_id)
    #if len(trips) <= 100:
    save_by_key(get_train_tracker_trip_ids_key(tracker_id), trips)
    save_by_key(get_train_tracker_trip_ids_deviation_seconds_key(tracker_id), time_deviation_in_seconds)
    save_trips_to_db(tracker_id, trips, time_deviation_in_seconds)
Exemplo n.º 7
0
def add_report_to_tracker(tracker_id, report):
    if not load_by_key(get_train_tracker_relevant_services_key(tracker_id)):
        start_date = report.timestamp.strftime("%Y-%m-%d")
        relevant_services = gtfs.models.Service.objects.filter(start_date = start_date)
        relevant_service_ids = [x[0] for x in relevant_services.all().values_list('service_id')]
        save_by_key(get_train_tracker_relevant_services_key(tracker_id), relevant_service_ids)
         
    # update train position
    if report.get_my_loc():
        try_update_coords(report, tracker_id)
        
        ##commented out below is code that filters trips based on shape
        #coords_updated = False
        #p = get_redis_pipeline()
        #p.zincrby("train_tracker:%s:counters" % (tracker_id), res_shape_ids[i], inc_by)
        #p.incr("train_tracker:%s:total" % (tracker_id), inc_by)
        #p.execute()        

        #for i in xrange(len(res_shape_point_ids)):
            #cl = get_redis_client()
            #if cl.sadd("train_tracker:%s:visited_shape_point_ids" % (tracker_id), res_shape_point_ids[i]) == 0:
                #if not coords_updated: #update if report adds new points on tracks
                    #self.coords = coords
                    #coords_updated = True
                #p = get_redis_pipeline()
                #p.zincrby("train_tracker:%s:counters" % (tracker_id), res_shape_ids[i], 1)
                #p.incr("train_tracker:%s:total" % (tracker_id))
                #p.execute()
                ##self.shape_counts[res_shape_ids[i]] = self.shape_counts[res_shape_ids[i]] + 1
        
        # 1) add stop or non-stop to prev_stops and prev_stops_timestamps     
        # 2) set calc_hmm to true if according to wifis and/or location, our state changed from stop to non-stop or vice versa
    prev_current_stop_id_by_hmm = cl.get(get_train_tracker_current_stop_id_key(tracker_id))
      
    if not prev_current_stop_id_by_hmm:
        prev_state = tracker_states.INITIAL
    elif prev_current_stop_id_by_hmm == stops.NOSTOP:
        prev_state = tracker_states.NOSTOP
    else:
        prev_state = tracker_states.STOP

    stop_id = try_get_stop_id(report)
    if not stop_id:
        current_state = tracker_states.UNKNOWN
    elif stop_id == nostop_id:
        current_state = tracker_states.NOSTOP
    else:
        current_state = tracker_states.STOP

    if current_state != tracker_states.UNKNOWN:
        timestamp = report.get_timestamp_israel_time()
        prev_stop_id = add_prev_stop(tracker_id, stop_id, timestamp)
        
    # calculate hmm to get state_sequence, update stop_times and current_stop if needed
    if  current_state != tracker_states.UNKNOWN and prev_state != current_state:

        prev_stops_and_timestamps = cl.zrange(get_train_tracker_timestamp_sorted_stop_ids_key(tracker_id), 0, -1, withscores=True)
        prev_stop_ids_order = [int(x[0].split("_")[0]) for x in prev_stops_and_timestamps]
        prev_stops_and_timestamps = [x for (y,x) in sorted(zip(prev_stop_ids_order,prev_stops_and_timestamps))]
        
        prev_stop_ids = [x[0].split("_")[1] for x in prev_stops_and_timestamps]
        
        prev_stop_int_ids = np.array([stops.all_stops.id_list.index(x) for x in prev_stop_ids])
        #assert np.array_equal(prev_stop_int_ids, np.array(self.prev_stops))
        prev_stop_hmm_logprob, prev_stop_int_ids_by_hmm = hmm.decode(prev_stop_int_ids)
        prev_stop_int_ids_by_hmm_for_debug = prev_stop_int_ids_by_hmm
        
        # update current_stop_id_by_hmm and current_state by hmm:        
        current_stop_id_by_hmm = stops.all_stops.id_list[prev_stop_int_ids_by_hmm[-1]]
        cl.set(get_train_tracker_current_stop_id_key(tracker_id), current_stop_id_by_hmm)
        if current_stop_id_by_hmm == stops.NOSTOP:
            current_state = tracker_states.NOSTOP
        else:
            current_state = tracker_states.STOP

        if prev_state != current_state: # change in state
            prev_stops_by_hmm = [stops.all_stops.id_list[x] for x in prev_stop_int_ids_by_hmm]
            prev_stops_timestamps = [ot_utils.unix_time_to_localtime((x[1])) for x in prev_stops_and_timestamps]
            index_of_oldest_current_state = max(0, find_index_of_first_consecutive_value(prev_stops_by_hmm, len(prev_stops_by_hmm)-1))
            index_of_most_recent_previous_state = index_of_oldest_current_state-1
              
            if current_state == tracker_states.NOSTOP:
                stop_id = prev_stops_by_hmm[index_of_most_recent_previous_state]
                unix_timestamp = ot_utils.dt_time_to_unix_time(prev_stops_timestamps[index_of_most_recent_previous_state])

                if prev_state == tracker_states.INITIAL:
                    pass #do nothing
                else: # previous_state == tracker_states.STOP - need to set stop_time departure
                    stop_time = cl.zrange(get_train_tracker_tracked_stops_key(tracker_id), -1, -1, withscores=True)
                    departure_unix_timestamp = unix_timestamp
                    stop_id_and_departure_time = "%s_%d" % (prev_current_stop_id_by_hmm, departure_unix_timestamp)
                    update_stop_time(tracker_id, prev_stop_id, stop_time[0][1], stop_id_and_departure_time)
                    update_trips(tracker_id)
            else: # current_state == tracker_states.STOP
                stop_id = prev_stops_by_hmm[index_of_oldest_current_state]
                unix_timestamp = ot_utils.dt_time_to_unix_time(prev_stops_timestamps[index_of_oldest_current_state])
                
                arrival_unix_timestamp = unix_timestamp
                stop_id_and_departure_time = "%s_" % (current_stop_id_by_hmm)
                update_stop_time(tracker_id, prev_stop_id, arrival_unix_timestamp, stop_id_and_departure_time)
                update_trips(tracker_id)
                    
            prev_timestamp = unix_timestamp
                
        print_tracked_stop_times(tracker_id)
Exemplo n.º 8
0
def set_tracker_day(tracker_id, report):
    day_str = report.timestamp.strftime("%Y-%m-%d")
    save_by_key(get_train_tracker_day_key(tracker_id), day_str, cl=p)
    return report.timestamp.date()
Exemplo n.º 9
0
def update_trips(tracker_id, day, detected_stop_times):
    logger.info('Updating trips for tracker_id={}'.format(tracker_id))
    trip_delays_ids_list_of_lists = trip_matcher.get_matched_trips(tracker_id, detected_stop_times, day)
    logger.info('Updated trip_delays_ids_list_of_lists for tracker_id={}: {}'.format(tracker_id, trip_delays_ids_list_of_lists))
    save_by_key(get_train_tracker_trip_delays_ids_list_of_lists_key(tracker_id), trip_delays_ids_list_of_lists)
    return trip_delays_ids_list_of_lists
Exemplo n.º 10
0
def update_trips(tracker_id, detected_stop_times):
    relevant_service_ids = get_relevant_service_ids(tracker_id)
    trips, time_deviation_in_seconds = trip_matcher.get_matched_trips(tracker_id, detected_stop_times, relevant_service_ids)
    save_by_key(get_train_tracker_trip_ids_key(tracker_id), trips)
    save_by_key(get_train_tracker_trip_ids_deviation_seconds_key(tracker_id), time_deviation_in_seconds)
    return trips, time_deviation_in_seconds
Exemplo n.º 11
0
def SetRedisData(value, redis_key, day=None):
  if day:
    redis_key += ':' + _DayToDayStr(day)
  value_json = json.dumps(value,default=json_dump_dt)
  save_by_key(redis_key, value_json)
  return value_json