|
34 | 34 | BATTERY_LEVEL = {0b00: "Full", 0b01: "Medium", 0b10: "Low", 0b11: "Very Low"} |
35 | 35 |
|
36 | 36 |
|
| 37 | +def _print_scanning_results(seen_devices: dict[str, list[OfflineFindingDevice]]) -> None: |
| 38 | + """Print summary of each device seen.""" |
| 39 | + # ruff: noqa: T201 |
| 40 | + print("================ RESULTS =========================") |
| 41 | + for mac, devices in seen_devices.items(): |
| 42 | + avg_rssi = sum(d.rssi for d in devices if d.rssi is not None) / len( |
| 43 | + [d for d in devices if d.rssi is not None] |
| 44 | + ) |
| 45 | + print(f"Device {mac} seen {len(devices)} times, average RSSI: {avg_rssi:.1f}") |
| 46 | + of_type = "nearby" if isinstance(devices[0], NearbyOfflineFindingDevice) else "separated" |
| 47 | + print( |
| 48 | + f" {devices[0].device_type} with {devices[0].battery_level} battery" |
| 49 | + f" ({of_type} state)" |
| 50 | + ) |
| 51 | + |
| 52 | + if isinstance(devices[0], SeparatedOfflineFindingDevice): |
| 53 | + print(f" Public key: {devices[0].adv_key_b64}") |
| 54 | + print(f" Lookup key: {devices[0].hashed_adv_key_b64}") |
| 55 | + print("===============================================") |
| 56 | + |
| 57 | + device_type_counts: dict[str, int] = {} |
| 58 | + for devs in seen_devices.values(): |
| 59 | + dev_type = devs[0].device_type |
| 60 | + device_type_counts[dev_type] = device_type_counts.get(dev_type, 0) + 1 |
| 61 | + |
| 62 | + for dev_type, count in device_type_counts.items(): |
| 63 | + print(f"Total {dev_type}: {count}") |
| 64 | + |
| 65 | + |
37 | 66 | class OfflineFindingDevice(ABC): |
38 | 67 | """Device discoverable through Apple's bluetooth-based Offline Finding protocol.""" |
39 | 68 |
|
@@ -417,36 +446,6 @@ def __init__(self, loop: asyncio.AbstractEventLoop) -> None: |
417 | 446 |
|
418 | 447 | self._scanner_count: int = 0 |
419 | 448 |
|
420 | | - def print_scanning_results(self, seen_devices: dict[str, list[OfflineFindingDevice]]) -> None: |
421 | | - """Print summary of each device seen.""" |
422 | | - # ruff: noqa: T201 |
423 | | - print("================ RESULTS =========================") |
424 | | - for mac, devices in seen_devices.items(): |
425 | | - avg_rssi = sum(d.rssi for d in devices if d.rssi is not None) / len( |
426 | | - [d for d in devices if d.rssi is not None] |
427 | | - ) |
428 | | - print(f"Device {mac} seen {len(devices)} times, average RSSI: {avg_rssi:.1f}") |
429 | | - of_type = ( |
430 | | - "nearby" if isinstance(devices[0], NearbyOfflineFindingDevice) else "separated" |
431 | | - ) |
432 | | - print( |
433 | | - f" {devices[0].device_type} with {devices[0].battery_level} battery" |
434 | | - f" ({of_type} state)" |
435 | | - ) |
436 | | - |
437 | | - if isinstance(devices[0], SeparatedOfflineFindingDevice): |
438 | | - print(f" Public key: {devices[0].adv_key_b64}") |
439 | | - print(f" Lookup key: {devices[0].hashed_adv_key_b64}") |
440 | | - print("===============================================") |
441 | | - |
442 | | - device_type_counts: dict[str, int] = {} |
443 | | - for devs in seen_devices.values(): |
444 | | - dev_type = devs[0].device_type |
445 | | - device_type_counts[dev_type] = device_type_counts.get(dev_type, 0) + 1 |
446 | | - |
447 | | - for dev_type, count in device_type_counts.items(): |
448 | | - print(f"Total {dev_type}: {count}") |
449 | | - |
450 | 449 | @classmethod |
451 | 450 | async def create(cls) -> OfflineFindingScanner: |
452 | 451 | """Create an instance of the scanner.""" |
@@ -547,7 +546,7 @@ async def scan_for( |
547 | 546 | except asyncio.TimeoutError: # timeout reached |
548 | 547 | self._device_fut = self._loop.create_future() |
549 | 548 | if print_summary: |
550 | | - self.print_scanning_results(devices_seen) |
| 549 | + _print_scanning_results(devices_seen) |
551 | 550 | return |
552 | 551 | finally: |
553 | 552 | await self._stop_scan() |
0 commit comments