我已经开始使用 Raspberry Pi 4 和 Python。我正在从事的项目让我在 GitHub 上找到了一个已发布的 Python 库 (keshavdv/victron-ble)。
安装该库后,我可以从 CLI 成功运行(发现、读取、转储)。然而,尝试通过 Python 脚本实现同样的目标却让我头疼不已。
使用的 CLI 命令:
victron-ble discover
victron-ble dump "b5:55:aa:4d:99:33"
victron-ble read "b5:55:aa:4d:99:33"@"eb8c557386614231dbd741db97e457c5"
这是我尝试使用的代码:
from victron_ble.cli import discover
from victron_ble.cli import dump
from victron_ble.cli import read
my_id = b'b5:55:aa:4d:99:33'
#where my_id is the MAC address of the device I wish to observe
my_key = 'eb8c557386614231dbd741db97e457c5'
#where my_key is the encypytion key of the device I wish to observe
dump_result = dump(my_key)
print(dump_result)
discover_result = discover()
print(discover_result)
希望尝试以下格式的 my_id 变量:
b'b5:55:aa:4d:99:33'
'b5:55:aa:4d:99:33'
'b555aa4d9933'
b'b5-55-aa-4d-99-33'
[b5:55:aa:4d:99:33]
一个问题是,我如何使用脚本复制 CL 指令,特别是 MAC 地址和密钥所需的格式。谢谢🙏
如果我使用您发布的代码/脚本运行,我会收到以下错误:
$ python3 with_cli_funcs.py
Usage: with_cli_funcs.py [OPTIONS] ID
Try 'with_cli_funcs.py --help' for help.
Error: Got unexpected extra arguments (b 8 c 5 5 7 3 8 6 6 1 4 2 3 1 d b d 7 4 1 d b 9 7 e 4 5 7 c 5)
Process finished with exit code 2
在我看来,
click
库希望使用装饰器将值注入到dump
函数中。 https://github.com/keshavdv/victron-ble/blob/e28c5f8cc5f9f3062a2f36c2115d38214c07e741/victron_ble/cli.py#L45-L55
@cli.command(help="Dump all advertisements matching the given device ID")
@click.argument("id", type=str)
def dump(id: str):
loop = asyncio.get_event_loop()
async def scan():
scanner = DebugScanner(id)
await scanner.start()
asyncio.ensure_future(scan())
loop.run_forever()
我没有任何合适的设备,所以我无法测试这个,但看起来以下应该可行:
import asyncio
import logging
from victron_ble.scanner import DebugScanner
def my_scan(id: str):
loop = asyncio.get_event_loop()
async def scan():
scanner = DebugScanner(id)
await scanner.start()
asyncio.ensure_future(scan())
loop.run_forever()
if __name__ == '__main__':
my_id = 'b5:55:aa:4d:99:33'
logger = logging.getLogger("victron_ble")
logging.basicConfig()
logger.setLevel(logging.DEBUG)
my_scan(my_id)