Skip to main content

一个通过 WebSocket 控制郊狼 DG-Lab 的 Python 库

Project description

PyDGLab-WS

一个用于创建郊狼 3.0 DG-Lab App Socket 控制终端和服务端的 Python 库

📖 完整文档

CodeFactor codecov Test Result Documentation Status Last Commit BSD 3-Clause Version

💡 特性

  • 通过该库可开发 Python 程序,接入 DG-Lab App
  • 完全使用 asyncio 异步,并发执行各项操作
  • 可部署第三方终端与 Socket 服务一体的服务端,降低部署复杂度和延迟
  • 使用异步生成器、上下文管理器等,结合语言特性
  • 通过 Pydantic, 枚举 管理消息结构和常量,便于开发

🔧 DG-Lab App 的 Socket 被控功能支持的操作

  • 获取 A, B 通道强度 以及 通道强度上限 的数据更新
  • 对 A, B 通道强度进行操作,支持增加、减少、设定到指定值
  • 向 App 发送持续一段时间的波形操作数据
  • 清空 App 波形操作队列
  • 获取 App 按下反馈按钮的通知

🚀 快速开始

📖 更多用法和完整 API 请查看文档:https://pydglab-ws.readthedocs.io

[!Note] 注意,您可能需要先大致了解一下第三方终端通过 WebSocket 连接控制 DG-Lab App 的基本流程和原理
官方文档:https://github.com/DG-LAB-OPENSOURCE/DG-LAB-OPENSOURCE/blob/main/socket/README.md

🔨 安装

pip3 install pydglab-ws

📡 搭建服务端

import asyncio
from pydglab_ws.server import DGLabWSServer


async def main():
    async with DGLabWSServer("0.0.0.0", 5678, 60) as server:
        while True:
            print(f"已连接的 WebSocket 客户端(终端/App):{list(server.uuid_to_ws.keys())}")
            print(f"已连接的本地终端:{list(server.local_client_ids)}")
            print(f"关系绑定:{server.client_id_to_target_id}")
            await asyncio.sleep(5)


if __name__ == "__main__":
    asyncio.run(main())

更多演示请查看 examples/server.py

🕹️ 搭建客户端 / 第三方终端

当进入 DGLabWSServer 的异步生成器时,从 WebSocket 服务端获取 clientId 的操作会 自动完成

import asyncio
from websockets import ConnectionClosedOK
from pydglab_ws import DGLabWSConnect


def print_qrcode(_: str):
    """输出二维码到终端界面"""
    ...


async def main():
    try:
        async with DGLabWSConnect("ws://192.168.1.161:5678") as client:
            # 获取二维码
            url = client.get_qrcode()
            print("请用 DG-Lab App 扫描二维码以连接")
            print_qrcode(url)

            # 等待绑定
            await client.bind()
            print(f"已与 App {client.target_id} 成功绑定")

            # 从 App 接收数据更新,并进行远控操作
            async for data in client.data_generator():
                print(f"收取到数据:{data}")
    except ConnectionClosedOK:
        print("Socket 服务端断开连接")


if __name__ == "__main__":
    asyncio.run(main())

更多演示请查看 examples/ws_client.py

🕹️ 搭建与第三方终端一体的 WebSocket 服务端

这段代码不仅提供 DG-Lab WebSocket 服务端服务,还生成了一个本地终端可供 App 连接。

[!Tip] 不管是本地终端 DGLabLocalClient 还是 WebSocket 终端 DGLabWSClient包含的主要方法都相同
因此在该段代码中,终端相关的逻辑与上面的独立的 WebSocket 终端的实现基本相同。
这种方式,省去了终端连接 WebSocket 服务端的环节,终端与 WebSocket 服务端一体,网络延迟更低,部署更方便

import asyncio
from pydglab_ws import DGLabWSServer


def print_qrcode(_: str):
    """输出二维码到终端界面"""
    ...


async def main():
    async with DGLabWSServer("0.0.0.0", 5678, 60) as server:
        client = server.new_local_client()

        url = client.get_qrcode("ws://192.168.1.161:5678")
        print("请用 DG-Lab App 扫描二维码以连接")
        print_qrcode(url)

        # 等待绑定
        await client.bind()
        print(f"已与 App {client.target_id} 成功绑定")

        # 从 App 接收数据更新,并进行远控操作
        async for data in client.data_generator():
            print(f"收取到数据:{data}")


if __name__ == "__main__":
    asyncio.run(main())

更多演示请查看 examples/local_client_with_server.py

📌 更多

🔗 链接

📐 代码覆盖率

codecov.io

许可证

PyDGLab-WS 使用 BSD 3-Clause 许可证.

Copyright © 2024 by Ljzd-PRO.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydglab_ws-1.1.0.tar.gz (18.2 kB view details)

Uploaded Source

Built Distribution

pydglab_ws-1.1.0-py3-none-any.whl (21.3 kB view details)

Uploaded Python 3

File details

Details for the file pydglab_ws-1.1.0.tar.gz.

File metadata

  • Download URL: pydglab_ws-1.1.0.tar.gz
  • Upload date:
  • Size: 18.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/6.5.0-1021-azure

File hashes

Hashes for pydglab_ws-1.1.0.tar.gz
Algorithm Hash digest
SHA256 8af349331e1b495c427e56fb1c55ee9c1fa6f5824992d62c0414f02108ee17f5
MD5 e9e58ff9dc4e6593129fdcdeecaa2d0a
BLAKE2b-256 0b84aff0da3e59cfaf15607576d11523097af7fe417eb36bbe6571670fd69e00

See more details on using hashes here.

File details

Details for the file pydglab_ws-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: pydglab_ws-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 21.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/6.5.0-1021-azure

File hashes

Hashes for pydglab_ws-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 eaf5ca66aeec998cd53775df943eb4ff43232de2154edf82b9434952188ca7fd
MD5 a0ee4c4149304266604966ac4aa9af5e
BLAKE2b-256 4bc24a53f7ead244e1302c60bd90ca4c69d110a7d756256d2e293de571a5cc6a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page