diff --git a/software/management/dashboard/quick_download_trigger.py b/software/management/dashboard/quick_download_trigger.py new file mode 100644 index 00000000..8ddea7f1 --- /dev/null +++ b/software/management/dashboard/quick_download_trigger.py @@ -0,0 +1,85 @@ +from tottag import * +from datetime import timezone +import asyncio +import functools +import struct +import sys +from bleak import BleakClient, BleakScanner +from datetime import datetime +import os +import signal +import traceback + +CONNECTION_TIMEOUT = 20 + +discovered_devices = dict() +# Helper function to handle Ctrl+C +def handle_interrupt(sig, frame): + print("\nExiting gracefully...") + tasks = asyncio.all_tasks(loop) + for task in tasks: + task.cancel() + loop.stop() + + +MODE_SWITCH_UUID = "d68c3164-a23f-ee90-0c45-5231395e5d2e" +MAINTENANCE_COMMAND_SERVICE_UUID = "d68c3162-a23f-ee90-0c45-5231395e5d2e" +MAINTENANCE_DATA_SERVICE_UUID = "d68c3163-a23f-ee90-0c45-5231395e5d2e" + +MAINTENANCE_DOWNLOAD_LOG = 0x03 +MAINTENANCE_SET_LOG_DOWNLOAD_DATES = 0x04 +MAINTENANCE_DOWNLOAD_COMPLETE = 0xFF + + +async def quick_download_trigger(tag_hex_address=None): + """ + Connect to the device and trigger segger offloading + """ + # Scan for TotTag devices for 6 seconds + scanner = BleakScanner(cb={"use_bdaddr": True}) + await scanner.start() + await asyncio.sleep(5.0) + await scanner.stop() + + for device_address, device_info in scanner.discovered_devices_and_advertisement_data.items(): + #device_info[0]:BLEDevice device_infi[1]:AdvertisementData + if (device_address == tag_hex_address) or ((tag_hex_address is None) and device_info[1].local_name == 'TotTag'): + print(f"Device {device_address} is found!") + discovered_devices[device_address] = device_info[0] + async with BleakClient(device_info[0], use_cached=False) as client: + try: + await client.connect() + for service in await client.get_services(): + for characteristic in service.characteristics: + # trigger the switch + if characteristic.uuid == MODE_SWITCH_UUID: + current_local_datetime = datetime.now() + midnight_local_datetime = current_local_datetime.replace(hour=0, minute=0, second=0, microsecond=0) + midnight_utc_datetime = midnight_local_datetime.astimezone(timezone.utc) + start = int(midnight_utc_datetime.timestamp()) + end = start + 86400 + await client.write_gatt_char(MODE_SWITCH_UUID, struct.pack("