Adds support for ble logging characteristic

This commit is contained in:
Ben Meadors
2024-06-29 09:41:06 -05:00
parent f5febc566f
commit 897adfb8c2
3 changed files with 31 additions and 1 deletions

View File

@@ -6,6 +6,7 @@ import struct
import asyncio
from threading import Thread, Event
from typing import Optional
from print_color import print
from bleak import BleakScanner, BleakClient
@@ -16,6 +17,8 @@ SERVICE_UUID = "6ba1b218-15a8-461f-9fa8-5dcae273eafd"
TORADIO_UUID = "f75c76d2-129e-4dad-a1dd-7866124401e7"
FROMRADIO_UUID = "2c55e69e-4993-11ed-b878-0242ac120002"
FROMNUM_UUID = "ed9da18c-a800-4f66-a670-aa7547e34453"
LOGRADIO_UUID = "6c6fd238-78fa-436b-aacf-15c5be1ef2e2"
class BLEInterface(MeshInterface):
@@ -70,6 +73,7 @@ class BLEInterface(MeshInterface):
logging.debug("Register FROMNUM notify callback")
self.client.start_notify(FROMNUM_UUID, self.from_num_handler)
self.client.start_notify(LOGRADIO_UUID, self.log_radio_handler)
async def from_num_handler(self, _, b): # pylint: disable=C0116
@@ -77,6 +81,20 @@ class BLEInterface(MeshInterface):
logging.debug(f"FROMNUM notify: {from_num}")
self.should_read = True
async def log_radio_handler(self, _, b): # pylint: disable=C0116
log_radio = b.decode('utf-8').replace('\n', '')
if log_radio.startswith("DEBUG"):
print(log_radio, color="cyan", end=None)
elif log_radio.startswith("INFO"):
print(log_radio, color="white", end=None)
elif log_radio.startswith("WARN"):
print(log_radio, color="yellow", end=None)
elif log_radio.startswith("ERROR"):
print(log_radio, color="red", end=None)
else:
print(log_radio, end=None)
self.should_read = False
def scan(self):
"Scan for available BLE devices"

13
poetry.lock generated
View File

@@ -762,6 +762,17 @@ files = [
dev = ["pre-commit", "tox"]
testing = ["pytest", "pytest-benchmark"]
[[package]]
name = "print-color"
version = "0.4.6"
description = "A simple package to print in color to the terminal"
optional = false
python-versions = ">=3.7,<4.0"
files = [
{file = "print_color-0.4.6-py3-none-any.whl", hash = "sha256:494bd1cdb84daf481f0e63bd22b3c32f7d52827d8f5d9138a96bb01ca8ba9299"},
{file = "print_color-0.4.6.tar.gz", hash = "sha256:d3aafc1666c8d31a85fffa6ee8e4f269f5d5e338d685b4e6179915c71867c585"},
]
[[package]]
name = "protobuf"
version = "5.27.1"
@@ -1552,4 +1563,4 @@ tunnel = []
[metadata]
lock-version = "2.0"
python-versions = "^3.8,<3.13"
content-hash = "8548a8b432a3f62db158f5b35254b05b2599aafe75ef12100471937fd4603e3c"
content-hash = "8e82c70af84ffd1525ece9c446bf06c9a1a1235cdf3bb6c563413daf389de353"

View File

@@ -21,6 +21,7 @@ pyyaml = "^6.0.1"
pypubsub = "^4.0.3"
bleak = "^0.21.1"
packaging = "^24.0"
print-color = "^0.4.6"
[tool.poetry.group.dev.dependencies]
hypothesis = "^6.103.2"