Skip to content

Commit 75c39f2

Browse files
committed
rework connection setup
1 parent 6b1c403 commit 75c39f2

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

src/meshcore_cli/meshcore_cli.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,28 +1783,34 @@ async def main(argv):
17831783
elif (json_output) :
17841784
logger.setLevel(logging.ERROR)
17851785

1786-
con = None
1786+
mc = None
17871787
if not hostname is None : # connect via tcp
1788-
con = TCPConnection(hostname, port)
1789-
await con.connect()
1788+
mc = await MeshCore.create_tcp(host=hostname, port=port, debug=debug)
17901789
elif not serial_port is None : # connect via serial port
1791-
con = SerialConnection(serial_port, baudrate)
1792-
await con.connect()
1793-
await asyncio.sleep(0.2)
1790+
mc = await MeshCore.create_serial(port=serial_port, baudrate=baudrate, debug=debug)
17941791
else : #connect via ble
1795-
con = BLEConnection(address)
1796-
address = await con.connect()
1797-
if address is None or address == "" : # no device, no action
1798-
logger.error("No device found, exiting ...")
1799-
return
1792+
if address is None or address == "" or len(address.split(":")) != 6 :
1793+
logger.info(f"Scanning BLE for device matching {address}")
1794+
devices = await BleakScanner.discover(timeout=timeout)
1795+
found = False
1796+
for d in devices:
1797+
if not d.name is None and d.name.startswith("MeshCore-") and\
1798+
(address is None or address in d.name) :
1799+
address=d.address
1800+
logger.info(f"Found device {d.name} {d.address}")
1801+
found = True
1802+
break
1803+
if not found :
1804+
logger.info(f"Couldn't find device {address}")
1805+
return
1806+
1807+
mc = await MeshCore.create_ble(address=address, debug=debug)
18001808

18011809
# Store device address in configuration
18021810
if os.path.isdir(MCCLI_CONFIG_DIR) :
18031811
with open(MCCLI_ADDRESS, "w", encoding="utf-8") as f :
18041812
f.write(address)
18051813

1806-
mc = MeshCore(con, debug=debug)
1807-
await mc.connect()
18081814
handle_message.mc = mc # connect meshcore to handle_message
18091815

18101816
res = await mc.commands.send_device_query()

0 commit comments

Comments
 (0)