mirror of
https://github.com/meshcore-dev/meshcore-cli.git
synced 2026-04-20 22:13:48 +00:00
rework connection setup
This commit is contained in:
parent
6b1c4035a9
commit
75c39f2f96
1 changed files with 19 additions and 13 deletions
|
|
@ -1783,28 +1783,34 @@ async def main(argv):
|
||||||
elif (json_output) :
|
elif (json_output) :
|
||||||
logger.setLevel(logging.ERROR)
|
logger.setLevel(logging.ERROR)
|
||||||
|
|
||||||
con = None
|
mc = None
|
||||||
if not hostname is None : # connect via tcp
|
if not hostname is None : # connect via tcp
|
||||||
con = TCPConnection(hostname, port)
|
mc = await MeshCore.create_tcp(host=hostname, port=port, debug=debug)
|
||||||
await con.connect()
|
|
||||||
elif not serial_port is None : # connect via serial port
|
elif not serial_port is None : # connect via serial port
|
||||||
con = SerialConnection(serial_port, baudrate)
|
mc = await MeshCore.create_serial(port=serial_port, baudrate=baudrate, debug=debug)
|
||||||
await con.connect()
|
|
||||||
await asyncio.sleep(0.2)
|
|
||||||
else : #connect via ble
|
else : #connect via ble
|
||||||
con = BLEConnection(address)
|
if address is None or address == "" or len(address.split(":")) != 6 :
|
||||||
address = await con.connect()
|
logger.info(f"Scanning BLE for device matching {address}")
|
||||||
if address is None or address == "" : # no device, no action
|
devices = await BleakScanner.discover(timeout=timeout)
|
||||||
logger.error("No device found, exiting ...")
|
found = False
|
||||||
return
|
for d in devices:
|
||||||
|
if not d.name is None and d.name.startswith("MeshCore-") and\
|
||||||
|
(address is None or address in d.name) :
|
||||||
|
address=d.address
|
||||||
|
logger.info(f"Found device {d.name} {d.address}")
|
||||||
|
found = True
|
||||||
|
break
|
||||||
|
if not found :
|
||||||
|
logger.info(f"Couldn't find device {address}")
|
||||||
|
return
|
||||||
|
|
||||||
|
mc = await MeshCore.create_ble(address=address, debug=debug)
|
||||||
|
|
||||||
# Store device address in configuration
|
# Store device address in configuration
|
||||||
if os.path.isdir(MCCLI_CONFIG_DIR) :
|
if os.path.isdir(MCCLI_CONFIG_DIR) :
|
||||||
with open(MCCLI_ADDRESS, "w", encoding="utf-8") as f :
|
with open(MCCLI_ADDRESS, "w", encoding="utf-8") as f :
|
||||||
f.write(address)
|
f.write(address)
|
||||||
|
|
||||||
mc = MeshCore(con, debug=debug)
|
|
||||||
await mc.connect()
|
|
||||||
handle_message.mc = mc # connect meshcore to handle_message
|
handle_message.mc = mc # connect meshcore to handle_message
|
||||||
|
|
||||||
res = await mc.commands.send_device_query()
|
res = await mc.commands.send_device_query()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue