Skip to main content

一个基于异步编程的MQTT设备通信库

Project description

AIOEway - 异步MQTT设备交互库

一个基于Python异步编程的MQTT设备通信库,用于与IoT设备进行高效的双向通信。

特性

  • 🚀 异步编程: 基于asyncio,支持高并发设备连接
  • 🔒 安全连接: 支持TLS/SSL加密连接
  • 📊 数据结构化: 内置设备信息和运行数据的结构化处理
  • 🔄 双向通信: 支持设备信息查询和实时数据监控
  • 📝 完整日志: 详细的连接和通信日志记录
  • 🛠️ 易于使用: 简洁的API设计,快速上手

安装

pip install aioeway

快速开始

基本使用

import asyncio
from device_mqtt_client import DeviceMQTTClient

async def on_device_info(device_info):
    print(f"设备信息: {device_info}")

async def on_device_data(device_data_list):
    for data in device_data_list:
        print(f"设备数据: 功率={data.gen_power}W, 温度={data.temperature}°C")

async def main():
    # 创建客户端
    client = DeviceMQTTClient(
        device_model="MODEL001",
        device_sn="SN123456",
        username="your_username",
        password="your_password",
        broker_host="mqtt.example.com",
        broker_port=1883,
        use_tls=True
    )
    
    try:
        # 连接到MQTT代理
        if await client.connect():
            print("连接成功!")
            
            # 开始监控设备
            await client.start_monitoring(
                device_id="productCode",
                device_sn="deviceNum",
                info_callback=on_device_info,
                data_callback=on_device_data
            )
            
            # 保持连接
            await asyncio.sleep(60)
            
    finally:
        await client.disconnect()

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

同步请求设备信息

async def get_device_info():
    client = DeviceMQTTClient(...)
    
    if await client.connect():
        # 同步获取设备信息
        device_info = await client.request_device_info_and_wait(
            device_id="productCode",
            device_sn="deviceNum",
            timeout=10.0
        )
        
        if device_info:
            print(f"设备IP: {device_info.ip}")
            print(f"WiFi: {device_info.wifi_ssid}")
            print(f"固件版本: {device_info.app_firm_ver}")
        
        await client.disconnect()

数据结构

设备信息 (DeviceInfo)

@dataclass
class DeviceInfo:
    net_firm_ver: float      # 网络模块固件版本
    app_firm_ver: float      # 主控模块固件版本
    wifi_ssid: str           # WiFi名称
    ip: str                  # IP地址
    wifi_is_normal: int      # WiFi是否可用(0:是 1:否)
    is_lock: int             # 是否锁定(0:是 1:否)
    board: List[Dict]        # 板卡信息

设备运行数据 (DeviceData)

@dataclass
class DeviceData:
    sort: int                # 序号
    input_voltage: float     # 输入电压(V)
    input_current: float     # 输入电流(A)
    grid_voltage: float      # 电网电压(V)
    grid_freq: float         # 电网频率(Hz)
    gen_power: float         # 发电功率(W)
    gen_power_today: int     # 今日发电量(Wh)
    gen_power_total: int     # 总发电量(kWh)
    temperature: float       # 温度(℃)
    err_code: int            # 错误码
    duration: int            # 工作时长(s)

MQTT主题格式

  • 设备信息推送: {device_id}/{device_sn}/info/post
  • 设备数据推送: {device_id}/{device_sn}/data/post
  • 设备信息获取: {device_id}/{device_sn}/info/get
  • 设备数据获取: {device_id}/{device_sn}/data/get
  • 信息请求: {device_id}/{device_sn}/info/request
  • 数据请求: {device_id}/{device_sn}/data/request

API 参考

DeviceMQTTClient

初始化参数

  • device_model: 设备机型码
  • device_sn: 设备序列号
  • username: MQTT用户名
  • password: MQTT密码
  • broker_host: MQTT代理服务器地址
  • broker_port: MQTT代理服务器端口(默认1883)
  • keepalive: 心跳间隔秒数(默认60)
  • use_tls: 是否使用TLS加密(默认True)

主要方法

  • connect(): 连接到MQTT代理
  • disconnect(): 断开连接
  • subscribe_device_info(): 订阅设备信息
  • subscribe_device_data(): 订阅设备数据
  • request_device_info_and_wait(): 同步请求设备信息
  • request_device_data_and_wait(): 同步请求设备数据
  • start_monitoring(): 开始监控设备
  • stop_monitoring(): 停止监控设备

要求

  • Python 3.7+
  • aiomqtt >= 2.0.0

许可证

MIT License

贡献

欢迎提交Issue和Pull Request!

更新日志

v1.0.0

  • 初始版本发布
  • 支持异步MQTT通信
  • 支持TLS加密连接
  • 完整的设备信息和数据结构
  • 同步和异步API支持

v1.0.1

  • 修改包格式错误

v1.0.2

  • 断线重连
  • 防止多个监听任务冲突

v1.0.3

  • 修复aiomqtt 2.0+版本兼容性问题

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

aioeway-1.0.3.tar.gz (28.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aioeway-1.0.3-py3-none-any.whl (15.4 kB view details)

Uploaded Python 3

File details

Details for the file aioeway-1.0.3.tar.gz.

File metadata

  • Download URL: aioeway-1.0.3.tar.gz
  • Upload date:
  • Size: 28.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for aioeway-1.0.3.tar.gz
Algorithm Hash digest
SHA256 bae63588ce14e7c218c84fc185a3aa9310456d40923b0c60284142ca0e344e89
MD5 aeb9bbe8cdfecc7b674f7ad74354a73a
BLAKE2b-256 6961003a418aa0df43fd49b6f6c5c6ae78e47c2145d379c73aaebd1cf54ee810

See more details on using hashes here.

File details

Details for the file aioeway-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: aioeway-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 15.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for aioeway-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a2768f80aca507fa4fc242a690b0dc98b59068c1b33fd04385a9d707a65e527c
MD5 bdd91eb28cd142334768f00d82c9dac4
BLAKE2b-256 06f50c2ac3242b0486faa24a585a262fe45118b40fc985b27ed52393130f85fe

See more details on using hashes here.

Supported by

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