Skip to main content

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+版本兼容性问题

Release files for aioeway 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aioeway 1.0.3
File Size Uploaded
aioeway-1.0.3.tar.gz 28.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aioeway 1.0.3
File Interpreter ABI Platform
aioeway-1.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 44.3 kB

Release files / aioeway-1.0.3.tar.gz

Download URL aioeway-1.0.3.tar.gz
Size 28.9 kB
Tags Source
SHA-256 checksum
How to use checksums
bae63588ce14e7c218c84fc185a3aa9310456d40923b0c60284142ca0e344e89
BLAKE2b-256 checksum
How to use checksums
6961003a418aa0df43fd49b6f6c5c6ae78e47c2145d379c73aaebd1cf54ee810
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.11

Release files / aioeway-1.0.3-py3-none-any.whl

Download URL aioeway-1.0.3-py3-none-any.whl
Size 15.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a2768f80aca507fa4fc242a690b0dc98b59068c1b33fd04385a9d707a65e527c
BLAKE2b-256 checksum
How to use checksums
06f50c2ac3242b0486faa24a585a262fe45118b40fc985b27ed52393130f85fe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.11

Release history Release notifications | RSS feed

This release

1.0.3 This release

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page