mirror of
https://github.com/meshcore-dev/meshcore-cli.git
synced 2026-04-20 22:13:48 +00:00
Merge pull request #43 from samm-git/no-bleakl
Make bleak dependency optional
This commit is contained in:
commit
188f00221c
1 changed files with 30 additions and 20 deletions
|
|
@ -9,8 +9,6 @@ import time, datetime
|
||||||
import getopt, json, shlex, re
|
import getopt, json, shlex, re
|
||||||
import logging
|
import logging
|
||||||
import requests
|
import requests
|
||||||
from bleak import BleakScanner, BleakClient
|
|
||||||
from bleak.exc import BleakError, BleakDBusError
|
|
||||||
import serial.tools.list_ports
|
import serial.tools.list_ports
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import traceback
|
import traceback
|
||||||
|
|
@ -27,6 +25,13 @@ from prompt_toolkit.document import Document
|
||||||
from Crypto.Cipher import AES
|
from Crypto.Cipher import AES
|
||||||
from Crypto.Hash import HMAC, SHA256
|
from Crypto.Hash import HMAC, SHA256
|
||||||
|
|
||||||
|
try:
|
||||||
|
from bleak import BleakScanner, BleakClient
|
||||||
|
from bleak.exc import BleakError, BleakDBusError
|
||||||
|
BLEAK_AVAILABLE = True
|
||||||
|
except ImportError:
|
||||||
|
BLEAK_AVAILABLE = False
|
||||||
|
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from meshcore import MeshCore, EventType, logger
|
from meshcore import MeshCore, EventType, logger
|
||||||
|
|
@ -4313,16 +4318,17 @@ async def main(argv):
|
||||||
case "-q": # quiet (turns logger to ERROR only)
|
case "-q": # quiet (turns logger to ERROR only)
|
||||||
quiet = True
|
quiet = True
|
||||||
case "-l" :
|
case "-l" :
|
||||||
print("BLE devices:")
|
if BLEAK_AVAILABLE:
|
||||||
try :
|
print("BLE devices:")
|
||||||
devices = await BleakScanner.discover(timeout=timeout)
|
try :
|
||||||
if len(devices) == 0:
|
devices = await BleakScanner.discover(timeout=timeout)
|
||||||
print(" No ble device found")
|
if len(devices) == 0:
|
||||||
for d in devices :
|
print(" No ble device found")
|
||||||
if not d.name is None and d.name.startswith("MeshCore-"):
|
for d in devices :
|
||||||
print(f" {d.address} {d.name}")
|
if not d.name is None and d.name.startswith("MeshCore-"):
|
||||||
except (BleakError, BleakDBusError):
|
print(f" {d.address} {d.name}")
|
||||||
print(" No BLE HW")
|
except (BleakError, BleakDBusError):
|
||||||
|
print(" No BLE HW")
|
||||||
print("\nSerial ports:")
|
print("\nSerial ports:")
|
||||||
ports = serial.tools.list_ports.comports()
|
ports = serial.tools.list_ports.comports()
|
||||||
for port, desc, hwid in sorted(ports):
|
for port, desc, hwid in sorted(ports):
|
||||||
|
|
@ -4331,13 +4337,14 @@ async def main(argv):
|
||||||
case "-S" :
|
case "-S" :
|
||||||
choices = []
|
choices = []
|
||||||
|
|
||||||
try :
|
if BLEAK_AVAILABLE:
|
||||||
devices = await BleakScanner.discover(timeout=timeout)
|
try :
|
||||||
for d in devices:
|
devices = await BleakScanner.discover(timeout=timeout)
|
||||||
if not d.name is None and d.name.startswith("MeshCore-"):
|
for d in devices:
|
||||||
choices.append(({"type":"ble","device":d}, f"{d.address:<22} {d.name}"))
|
if not d.name is None and d.name.startswith("MeshCore-"):
|
||||||
except (BleakError, BleakDBusError):
|
choices.append(({"type":"ble","device":d}, f"{d.address:<22} {d.name}"))
|
||||||
logger.info("No BLE Device")
|
except (BleakError, BleakDBusError):
|
||||||
|
logger.info("No BLE Device")
|
||||||
|
|
||||||
ports = serial.tools.list_ports.comports()
|
ports = serial.tools.list_ports.comports()
|
||||||
for port, desc, hwid in sorted(ports):
|
for port, desc, hwid in sorted(ports):
|
||||||
|
|
@ -4394,7 +4401,7 @@ async def main(argv):
|
||||||
mc = await MeshCore.create_tcp(host=hostname, port=port, debug=debug, only_error=json_output)
|
mc = await MeshCore.create_tcp(host=hostname, port=port, debug=debug, only_error=json_output)
|
||||||
elif not serial_port is None : # connect via serial port
|
elif not serial_port is None : # connect via serial port
|
||||||
mc = await MeshCore.create_serial(port=serial_port, baudrate=baudrate, debug=debug, only_error=json_output)
|
mc = await MeshCore.create_serial(port=serial_port, baudrate=baudrate, debug=debug, only_error=json_output)
|
||||||
else : #connect via ble
|
elif BLEAK_AVAILABLE : # connect via ble
|
||||||
client = None
|
client = None
|
||||||
if device or address and len(address.split(":")) == 6 :
|
if device or address and len(address.split(":")) == 6 :
|
||||||
pass
|
pass
|
||||||
|
|
@ -4478,6 +4485,9 @@ async def main(argv):
|
||||||
elif not address is None:
|
elif not address is None:
|
||||||
f.write(address)
|
f.write(address)
|
||||||
|
|
||||||
|
if mc is None:
|
||||||
|
return
|
||||||
|
|
||||||
handle_message.mc = mc # connect meshcore to handle_message
|
handle_message.mc = mc # connect meshcore to handle_message
|
||||||
handle_advert.mc = mc
|
handle_advert.mc = mc
|
||||||
handle_path_update.mc = mc
|
handle_path_update.mc = mc
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue