Example #1
0
 def add(self, report):
     if not report.get_my_loc() or report.loc_ts_delta() > config.stop_discovery_location_timeout_seconds:
         if report.get_my_loc():
             logger.debug('Report %s skipped because of large loc_ts_delta of %d. This is ok if running from a test, as date may have ben changed.' % (str(report), report.loc_ts_delta()))
             return
         else:
             logger.debug('Report %s skipped because it has not location data' % str(report))
             return
     loc = report.get_my_loc()
     wifis = [x for x in report.get_wifi_set_all() if x.SSID == 'S-ISRAEL-RAILWAYS']
     if len(wifis) > 0:
         coords = [loc.lat, loc.lon]
         stop_id_list = stops.all_stops.query_stops(coords, meter_distance_to_coord_distance(config.station_radius_in_meters))
     
     for wifi in wifis:
         if len(stop_id_list) == 1:                
             p = get_redis_pipeline()
             stop_id = stops.all_stops[stop_id_list[0]].id
             p.zincrby("bssid:%s:counters" % (wifi.key), stop_id, 1)
             p.incr("bssid:%s:total" % (wifi.key))
             p.execute()                
         else:
             if len(stop_id_list) == 0:
                 self.wifis_near_no_station.append(wifi)
             else:
                 self.wifis_near_two_or_more_stations.append(wifi)
Example #2
0
def add_report_to_tracker(tracker_id, report):
    logger.info('Adding report to tracker_id "{}": {}'.format(tracker_id, report))
    day = set_tracker_day(tracker_id, report)
    
    # update train position
    if report.get_my_loc():
        update_coords(report, tracker_id)
    
    is_updated_stop_time = stop_detector.add_report(tracker_id, report)
    
    if is_updated_stop_time:
        logger.info('stop_time updated')
        stop_times = stop_detector.get_detected_stop_times(tracker_id)
        trip_delays_ids_list_of_lists = update_trips(tracker_id, day, stop_times)
        trip_ids = get_trusted_trips(trip_delays_ids_list_of_lists)
        for trip_id in trip_ids:
            trip_delays_ids_list = [x for x in trip_delays_ids_list_of_lists if x[0][1] == trip_id][0]
            if (len(stop_times)-1) in trip_delays_ids_list[0][2]:
                if len(trip_delays_ids_list[0][2]) == 2:  # Need to add the first station
                    if (len(stop_times)-2) in trip_delays_ids_list[0][2]:
                        stop_time = stop_times[trip_delays_ids_list[0][2][-2]]
                        logger.debug(stop_time)
                        save_stop_times_to_db(tracker_id, stop_time, trip_id) 
                    else:
                        logger.error('Two stops were detected for trip, last detected stop for tracker belongs to trip, but one before last does not, while it most probably should.')
                stop_time = stop_times[trip_delays_ids_list[0][2][-1]]
                logger.debug(stop_time)
                save_stop_times_to_db(tracker_id, stop_time, trip_id)
Example #3
0
def print_bssids_by_stop(bssids):
    data = {}
    for x in bssids:
        print 'bassid', x
        data[x] = {}
        wifi_reports = analysis.models.SingleWifiReport.objects.filter(key = x)
        reports = list(set(x.report for x in wifi_reports))        
        for report in reports:
            if not report.get_my_loc() or report.loc_ts_delta() > config.stop_discovery_location_timeout_seconds:
                if report.get_my_loc():
                    logger.debug('Report %s skipped because of large loc_ts_delta of %d' % (str(report), report.loc_ts_delta()))
                continue
            loc = report.get_my_loc()
            coords = [loc.lat, loc.lon]
            stop_id_list = stops.all_stops.query_stops(coords, meter_distance_to_coord_distance(config.station_radius_in_meters))
            if stop_id_list:
                if len(stop_id_list) > 1:
                    print 'problem'
                else:
                    if not data[x].has_key(stop_id_list[0]):
                        data[x][stop_id_list[0]] = []
                    data[x][stop_id_list[0]].append((report.timestamp, report.device_id))
        for stop_id in data[x]:
            print stop_id
            for dict_tuple in sorted(data[x][stop_id]):
                print str(dict_tuple[0]), dict_tuple[1]
Example #4
0
def track_device(device_id,
                 do_print=False,
                 do_preload_reports=True,
                 set_reports_to_same_weekday_last_week=False,
                 report_limit=10000000):
    #device_coords, device_timestamps, device_accuracies_in_meters, device_accuracies_in_coords = get_location_info_from_device_id(device_id)
    now = ot_utils.get_localtime_now()
    reports_queryset = stop_detector_test.get_device_id_reports(device_id)
    assert reports_queryset.count() > 0, 'No device reports in db'
    tracker_id = device_id

    fps_period_start = time.clock()
    fps_period_length = 100
    if do_preload_reports:
        reports_queryset = list(reports_queryset)
    count = len(reports_queryset) if isinstance(
        reports_queryset, list) else reports_queryset.count()
    for i in xrange(count):
        if i > report_limit:
            break
        if i % fps_period_length == 0:
            elapsed = (time.clock() - fps_period_start)
            if elapsed > 0:
                logger.debug('%d\t%.1f qps' % (i, fps_period_length / elapsed))
            else:
                logger.debug(
                    'Elapsed time should be positive but is %d' % (elapsed))
            fps_period_start = time.clock()

        report = reports_queryset[i]

        if set_reports_to_same_weekday_last_week:
            # fix finding same weekday last week by http://stackoverflow.com/questions/6172782/find-the-friday-of-previous-last-week-in-python
            day_fix = (now.weekday() - report.timestamp.weekday()) % 7
            day = now + datetime.timedelta(days=-day_fix)
            # move day and correct for DST (daylight savings time)
            dst_before = report.get_timestamp_israel_time().dst()
            report.timestamp = report.timestamp.replace(
                year=day.year, month=day.month, day=day.day)
            dst_after = report.get_timestamp_israel_time().dst()
            report.timestamp -= dst_after - dst_before

        add_report(report)

    #tracker.print_tracked_stop_times()
    #tracker.print_possible_trips()
    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)
    return tracker_id, trips
Example #5
0
    def _stop_detector_on_real_trip(self, device_id='1cb87f1e', do_preload_reports=True, set_reports_to_same_weekday_last_week=True, do_show_fig=False):
        remove_from_redis([device_id])
        now = ot_utils.get_localtime_now()
        reports_queryset = get_device_id_reports(device_id)
        tracker_id = device_id
        if do_show_fig:
            display_utils.draw_map()

        fps_period_start = time.clock()
        fps_period_length = 100
        if do_preload_reports:
            reports_queryset = list(reports_queryset)
        count = len(reports_queryset) if isinstance(
            reports_queryset, list) else reports_queryset.count()
        for i in xrange(count):
            if i % fps_period_length == 0:
                elapsed = (time.clock() - fps_period_start)
                if elapsed > 0:
                    logger.debug('%d\t%.1f qps' %
                                 (i, fps_period_length / elapsed))
                else:
                    logger.debug(
                        'Elapsed time should be positive but is %d' % (elapsed))
                fps_period_start = time.clock()

            report = reports_queryset[i]

            if set_reports_to_same_weekday_last_week:
                # fix finding same weekday last week by
                # http://stackoverflow.com/questions/6172782/find-the-friday-of-previous-last-week-in-python
                day_fix = (now.weekday() - report.timestamp.weekday()) % 7
                day = now + datetime.timedelta(days=-day_fix)
                # move day and correct for DST (daylight savings time)
                dst_before = report.get_timestamp_israel_time().dst()
                report.timestamp = report.timestamp.replace(
                    year=day.year, month=day.month, day=day.day)
                dst_after = report.get_timestamp_israel_time().dst()
                report.timestamp -= dst_after - dst_before
            if do_show_fig:
                plt.scatter(report.my_loc.lat, report.my_loc.lon)
                plt.show()
            #print i, ot_utils.get_localtime(report.timestamp)
            is_updated_stop_time = add_report(tracker_id, report)
            if is_updated_stop_time:
                logger.debug('stop_time updated')

        stop_detector.print_tracked_stop_times(device_id)
        detected_stop_times = stop_detector.get_detected_stop_times(device_id)
        ground_truth_stops = stop_detector_ground_truth.data[device_id]
        for x, y in zip(detected_stop_times, ground_truth_stops):
            self.assertEquals(x.__str__(), y.__str__())

        remove_from_redis([device_id])
        print 'done'
        return tracker_id