ble.py - added guessing subroutine for ble adapter

This commit is contained in:
matt committed 2026-10-18 04:47:23 -07:00
1 parent 080efd9ddc
commit 63b2f85eb3
1 file changed
+26 -3
+26 -3
View File
@@ -337,11 +337,33 @@ def test(test_count = 0, bt_usb_id = None) -> None:
test_list = mac_list_new test_list = mac_list_new
log.warning(f" Re-do list - {test_list}") log.warning(f" Re-do list - {test_list}")
def usb_id_guess():
log.warning("USB ID Not found, I'll try and guess...")
result = ''
guess_command = "lsusb | grep -i blue | awk '{print $6'}"
try:
# Run the command in a shell because of the pipe
output = subprocess.run(
guess_command,
shell=True,
capture_output=True,
text=True,
check=True,
).stdout.strip()
result = output
log.info(f"USB Best guess - {result}")
except subprocess.CalledProcessError as exc:
log.error(f"USB-ID guess failed: {exc}")
return result
# --------------------------------------------------------------------------- # # --------------------------------------------------------------------------- #
# If run as a script, execute the legacy test # If run as a script, execute the legacy test
# Since I intend to run this service for months at a time,
# I need to do some robust long term tests on the driver
# --------------------------------------------------------------------------- # # --------------------------------------------------------------------------- #
if __name__ == "__main__": if __name__ == "__main__":
# if running a test, make a log file in temp
import json import json
from datetime import datetime from datetime import datetime
logfile_path = f"/tmp/ble-test-{datetime.now().strftime('%y%m%d-%H%M%S')}.log" logfile_path = f"/tmp/ble-test-{datetime.now().strftime('%y%m%d-%H%M%S')}.log"
@@ -350,13 +372,12 @@ if __name__ == "__main__":
_file_handler.setFormatter(_stdout_handler.formatter) # reuse formatter _file_handler.setFormatter(_stdout_handler.formatter) # reuse formatter
_logger.addHandler(_file_handler) _logger.addHandler(_file_handler)
log.info(f"Logfile initialized at {logfile_path}") log.info(f"Logfile initialized at {logfile_path}")
# 1. Grab the argument (string) # Make sure the test count is passed
if len(sys.argv) < 2: if len(sys.argv) < 2:
print("Usage: python3 test.py <count>") print("Usage: python3 test.py <count>")
sys.exit(1) sys.exit(1)
try: try:
count = int(sys.argv[1]) # convert to integer count = int(sys.argv[1])
except ValueError: except ValueError:
print("The count must be an integer.") print("The count must be an integer.")
sys.exit(1) sys.exit(1)
@@ -378,6 +399,8 @@ if __name__ == "__main__":
for usb_id in present_usb_id_output: for usb_id in present_usb_id_output:
if usb_id in bt_usb_ids: if usb_id in bt_usb_ids:
current_usb_id = usb_id current_usb_id = usb_id
if len(current_usb_id) == 0:
current_usb_id = usb_id_guess()
log.info(f"BLE USB ID Detected: {current_usb_id} - Resetting") log.info(f"BLE USB ID Detected: {current_usb_id} - Resetting")
reset_adapter() reset_adapter()
test_count = 0 test_count = 0