print msgs to stderr

This commit is contained in:
Florent 2025-02-14 13:19:01 +01:00
parent 3bcc253b83
commit 3541f3e057

View file

@ -29,6 +29,9 @@ MCCLI_ADDRESS = MCCLI_CONFIG_DIR + "default_address"
# if None or "" then a scan is performed # if None or "" then a scan is performed
ADDRESS = "" ADDRESS = ""
def printerr (str) :
sys.stderr.write(str)
class MeshCore: class MeshCore:
""" """
Interface to a BLE MeshCore device Interface to a BLE MeshCore device
@ -61,11 +64,11 @@ class MeshCore:
if self.address is None or self.address == "" : if self.address is None or self.address == "" :
scanner = BleakScanner() scanner = BleakScanner()
print("Scanning for devices") printerr("Scanning for devices")
device = await scanner.find_device_by_filter(match_meshcore_device) device = await scanner.find_device_by_filter(match_meshcore_device)
if device is None : if device is None :
return None return None
print(f"Found device : {device}") printerr(f"Found device : {device}")
self.client = BleakClient(device) self.client = BleakClient(device)
self.address = self.client.address self.address = self.client.address
else: else:
@ -85,7 +88,7 @@ class MeshCore:
await self.send_appstart() await self.send_appstart()
print("Connexion started") printerr("Connexion started")
return self.address return self.address
def handle_rx(self, _: BleakGATTCharacteristic, data: bytearray): def handle_rx(self, _: BleakGATTCharacteristic, data: bytearray):
@ -158,22 +161,22 @@ class MeshCore:
self.result.set_result(False) self.result.set_result(False)
# push notifications # push notifications
case 0x80: case 0x80:
print ("Advertisment received") printerr ("Advertisment received")
case 0x81: case 0x81:
print("Code path update") printerr ("Code path update")
case 0x82: case 0x82:
self.ack_ev.set() self.ack_ev.set()
print("Received ACK") printerr ("Received ACK")
case 0x83: case 0x83:
self.rx_sem.release() self.rx_sem.release()
print("Msgs are waiting") printerr ("Msgs are waiting")
# unhandled # unhandled
case _: case _:
print(f"Unhandled data received {data}") printerr (f"Unhandled data received {data}")
def handle_disconnect(self, _: BleakClient): def handle_disconnect(self, _: BleakClient):
""" Callback to handle disconnection """ """ Callback to handle disconnection """
print("Device was disconnected, goodbye.") printerr ("Device was disconnected, goodbye.")
# cancelling all tasks effectively ends the program # cancelling all tasks effectively ends the program
for task in asyncio.all_tasks(): for task in asyncio.all_tasks():
task.cancel() task.cancel()
@ -186,7 +189,7 @@ class MeshCore:
res = await asyncio.wait_for(self.result, timeout) res = await asyncio.wait_for(self.result, timeout)
return res return res
except TimeoutError : except TimeoutError :
print ("Timeout ...") printerr ("Timeout ...")
return False return False
async def send_appstart(self): async def send_appstart(self):
@ -285,7 +288,7 @@ async def next_cmd(mc, cmds):
argnum = 1 argnum = 1
await asyncio.sleep(int(cmds[1])) await asyncio.sleep(int(cmds[1]))
print (f"cmd {cmds[0:argnum+1]} processed ...") printerr (f"cmd {cmds[0:argnum+1]} processed ...")
return cmds[argnum+1:] return cmds[argnum+1:]
def usage () : def usage () :
@ -339,7 +342,7 @@ async def main(argv):
mc = MeshCore(address) mc = MeshCore(address)
address = await mc.connect() address = await mc.connect()
if address is None or address == "" : # no device, no action if address is None or address == "" : # no device, no action
print("No device found, exiting ...") printerr ("No device found, exiting ...")
return return
# Store device address in configuration # Store device address in configuration