예제 #1
0
파일: csv.py 프로젝트: zebrajack/kapture
def records_wifi_from_file(
        filepath: str,
        wifi_ids: Optional[Set[str]] = None
) -> kapture.RecordsWifi:
    """
    Reads RecordsWifi from CSV file.

    :param filepath: input file path
    :param wifi_ids: input set of valid device ids. Any record of other than the given ones will be ignored.
                            If omitted, then it loads all devices.
    :return: Wifi records
    """
    records_wifi = kapture.RecordsWifi()
    with open(filepath) as file:
        table = table_from_file(file)
        records_wifi_current = {}
        cur_timestamp = -1
        # timestamp (ScanEndTime), device_id, BSSID, RSSI, FREQ, SCANTIME, VISIBLENAME
        for timestamp, device_id, bssid, *wifi_params in table:
            if wifi_ids is not None and device_id not in wifi_ids:
                # just ignore
                continue
            if timestamp != cur_timestamp:
                if records_wifi_current:
                    records_wifi[(int(cur_timestamp), str(device_id))] = records_wifi_current
                records_wifi_current = {}
                cur_timestamp = timestamp
            records_wifi_current[bssid] = kapture.RecordWifi(*wifi_params)
        # Don't forget last line
        if records_wifi_current:
            records_wifi[(int(cur_timestamp), str(device_id))] = records_wifi_current

    return records_wifi
예제 #2
0
 def test_init_wifi(self):
     records_wifi = kapture.RecordsWifi()
     timestamp0, timestamp1 = 0, 1
     device_id0, device_id1 = 'AC01324954_WIFI', 'AC01324955_WIFI'
     bssid = '68:72:51:80:52:df'
     rssi = -33
     freq = 2417
     scan_time = 1555398770280
     visible_name = 'M1X_PicoM2'
     # assign
     wifi_data = {}
     wifi_data[bssid] = kapture.RecordWifi(rssi, freq, scan_time,
                                           visible_name)
     records_wifi[timestamp0, device_id0] = wifi_data
     kapture_data = kapture.Kapture(records_wifi=records_wifi)
     self.assertEqual(1, len(kapture_data.records_wifi.keys()))
     self.assertEqual(1, len(kapture_data.records_wifi.key_pairs()))
     self.assertIn(timestamp0, kapture_data.records_wifi)
     self.assertIn(device_id0, kapture_data.records_wifi[timestamp0])
     self.assertIn((timestamp0, device_id0), kapture_data.records_wifi)
     self.assertEqual(wifi_data, kapture_data.records_wifi[timestamp0,
                                                           device_id0])
     kapture_data.records_wifi[timestamp1, device_id1] = wifi_data
     self.assertEqual(2, len(kapture_data.records_wifi.keys()))
     self.assertEqual(2, len(kapture_data.records_wifi.key_pairs()))
     kapture_data.records_wifi[timestamp0][device_id1] = wifi_data
     self.assertEqual(2, len(kapture_data.records_wifi.keys()))
     self.assertEqual(3, len(kapture_data.records_wifi.key_pairs()))
예제 #3
0
    def test_init_wifi(self):
        records_wifi = kapture.RecordsWifi()
        timestamp0, timestamp1 = 0, 1
        device_id0, device_id1 = 'AC01324954_WIFI', 'AC01324955_WIFI'
        bssid, ssid = '68:72:51:80:52:df', 'M1X_PicoM2'
        rssi = -33
        freq = 2417
        scan_time_start, scan_time_end = 1555398770280, 1555398770290
        # assign

        wifi_data = kapture.RecordWifi({bssid: kapture.RecordWifiSignal(ssid=ssid, rssi=rssi, frequency=freq,
                                                                        scan_time_start=scan_time_start,
                                                                        scan_time_end=scan_time_end)})
        records_wifi[timestamp0, device_id0] = wifi_data
        kapture_data = kapture.Kapture(records_wifi=records_wifi)
        self.assertEqual(1, len(kapture_data.records_wifi.keys()))
        self.assertEqual(1, len(kapture_data.records_wifi.key_pairs()))
        self.assertIn(timestamp0, kapture_data.records_wifi)
        self.assertIn(device_id0, kapture_data.records_wifi[timestamp0])
        self.assertIn((timestamp0, device_id0), kapture_data.records_wifi)
        self.assertEqual(wifi_data, kapture_data.records_wifi[timestamp0, device_id0])
        kapture_data.records_wifi[timestamp1, device_id1] = wifi_data
        self.assertEqual(2, len(kapture_data.records_wifi.keys()))
        self.assertEqual(2, len(kapture_data.records_wifi.key_pairs()))
        kapture_data.records_wifi[timestamp0][device_id1] = wifi_data
        self.assertEqual(2, len(kapture_data.records_wifi.keys()))
        self.assertEqual(3, len(kapture_data.records_wifi.key_pairs()))
예제 #4
0
def merge_records_gnss(
    records_gnss_list: List[Optional[kapture.RecordsGnss]]
) -> kapture.RecordsGnss:
    """
    Merge several gnss records lists. For gnss record with the same timestamp and sensor identifier,
     keep only the first one.

    :param records_gnss_list: list of gnss records
    :return: merged gnss records
    """
    assert len(records_gnss_list) > 0

    merged_records_gnss = kapture.RecordsWifi()
    for records_gnss in records_gnss_list:
        if records_gnss is None:
            continue
        for timestamp, sensor_id, record_gnss in kapture.flatten(records_gnss):
            if (timestamp, sensor_id) in merged_records_gnss:
                continue
            merged_records_gnss[(timestamp, sensor_id)] = record_gnss
    return merged_records_gnss
예제 #5
0
def merge_records_wifi(
    records_wifi_list: List[Optional[kapture.RecordsWifi]]
) -> kapture.RecordsWifi:
    """
    Merge several wifi records lists. For wifi record with the same timestamp and sensor identifier,
     keep only the first one.

    :param records_wifi_list: list of wifi records
    :return: merged wifi records
    """
    assert len(records_wifi_list) > 0

    merged_records_wifi = kapture.RecordsWifi()
    for records_wifi in records_wifi_list:
        if records_wifi is None:
            continue
        for timestamp, sensor_id, record_wifi in kapture.flatten(records_wifi):
            if (timestamp, sensor_id) in merged_records_wifi:
                continue
            merged_records_wifi[(timestamp, sensor_id)] = record_wifi
    return merged_records_wifi
예제 #6
0
def merge_records_wifi(
        records_wifi_list: List[Optional[kapture.RecordsWifi]],
        sensor_mappings: List[Dict[str, str]]) -> kapture.RecordsWifi:
    """
    Merge several wifi records list into one list with new identifiers for the sensors.

    :param records_wifi_list: list of wifi records to merge
    :param sensor_mappings: mapping of the sensor identifiers to their new identifiers
    :return: merged wifi records
    """
    assert len(records_wifi_list) > 0
    assert len(records_wifi_list) == len(sensor_mappings)

    merged_wifi_records = kapture.RecordsWifi()
    for wifi_records, sensor_mapping in zip(records_wifi_list,
                                            sensor_mappings):
        if wifi_records is None:
            continue
        for timestamp, sensor_id, record_wifi in kapture.flatten(wifi_records):
            new_sensor_id = sensor_mapping[sensor_id]
            merged_wifi_records[(timestamp, new_sensor_id)] = record_wifi
    return merged_wifi_records
예제 #7
0
    def test_equal_records_wifi(self):
        kapture_data_a = copy.deepcopy(self._kapture_data)
        kapture_data_b = copy.deepcopy(self._kapture_data)
        # check for deep copy
        self.assertTrue(
            equal_records_wifi(kapture_data_a.records_wifi,
                               kapture_data_b.records_wifi))

        record_wifi_1 = kapture.RecordWifi({
            '68:72:51:80:52:df':
            kapture.RecordWifiSignal(frequency=2417,
                                     rssi=-33.0,
                                     ssid='M1X_PicoM2'),
            '68:9c:e2:e1:b0:60':
            kapture.RecordWifiSignal(frequency=5765,
                                     rssi=-49.0,
                                     ssid='@HYUNDAI-WiFi'),
        })
        record_wifi_2 = kapture.RecordWifi({
            '68:72:51:80:52:df':
            kapture.RecordWifiSignal(frequency=2417,
                                     rssi=-35.0,
                                     ssid='M1X_PicoM2'),
            '68:9c:e2:e1:b0:60':
            kapture.RecordWifiSignal(frequency=5765,
                                     rssi=-47.0,
                                     ssid='@HYUNDAI-WiFi'),
        })
        record_wifi_2_str = kapture.RecordWifi({
            '68:72:51:80:52:df':
            kapture.RecordWifiSignal(frequency='2417',
                                     rssi='-35.0',
                                     ssid='M1X_PicoM2'),
            '68:9c:e2:e1:b0:60':
            kapture.RecordWifiSignal(frequency='5765',
                                     rssi='-47.0',
                                     ssid='@HYUNDAI-WiFi'),
        })
        # check deep copy just did nothing: recreate the B
        kapture_data_b.records_wifi = kapture.RecordsWifi()
        kapture_data_b.records_wifi[1555398770307,
                                    'AC01324954_wifi'] = record_wifi_1
        kapture_data_b.records_wifi[1555398771307,
                                    'AC01324954_wifi'] = record_wifi_2
        self.assertTrue(
            equal_records_wifi(kapture_data_a.records_wifi,
                               kapture_data_b.records_wifi))
        # check str arguments are casted to the proper types
        kapture_data_b.records_wifi[1555398771307,
                                    'AC01324954_wifi'] = record_wifi_2_str
        self.assertTrue(
            equal_records_wifi(kapture_data_a.records_wifi,
                               kapture_data_b.records_wifi))

        # check if some are missing, its not equal
        kapture_data_b.records_wifi = kapture.RecordsWifi()
        kapture_data_b.records_wifi[1555398770307,
                                    'AC01324954_wifi'] = record_wifi_1
        self.assertFalse(
            equal_records_wifi(kapture_data_a.records_wifi,
                               kapture_data_b.records_wifi))

        # just modify a single frequency
        kapture_data_b = copy.deepcopy(self._kapture_data)
        kapture_data_b.records_wifi[
            1555398770307,
            'AC01324954_wifi']['68:72:51:80:52:df'].frequency += 1
        self.assertFalse(
            equal_records_wifi(kapture_data_a.records_wifi,
                               kapture_data_b.records_wifi))

        # just modify a single rssi
        kapture_data_b = copy.deepcopy(self._kapture_data)
        kapture_data_b.records_wifi[
            1555398770307, 'AC01324954_wifi']['68:72:51:80:52:df'].rssi += 2.0
        self.assertFalse(
            equal_records_wifi(kapture_data_a.records_wifi,
                               kapture_data_b.records_wifi))

        # just modify a single bssid
        kapture_data_b = copy.deepcopy(self._kapture_data)
        dropped = kapture_data_b.records_wifi[
            1555398770307, 'AC01324954_wifi'].pop('68:72:51:80:52:df')
        self.assertFalse(
            equal_records_wifi(kapture_data_a.records_wifi,
                               kapture_data_b.records_wifi))
        kapture_data_b.records_wifi[
            1555398770307, 'AC01324954_wifi']['XX:XX:XX:XX:XX:XX'] = dropped
        self.assertFalse(
            equal_records_wifi(kapture_data_a.records_wifi,
                               kapture_data_b.records_wifi))