- 新增设备管理器 (DeviceManager):支持设备CRUD、设备影子管理、设备发现 - 新增设备控制器 (DeviceController):提供REST API接口 - 新增设备模型 (Device, DeviceShadow):定义统一设备模型结构 - 新增OTA管理器 (OtaManager):支持设备固件升级管理 - 新增OTA控制器 (OtaController):提供OTA升级API接口 - 新增MQTT适配器 (MqttAdapter):支持MQTT协议连接和消息处理 - 新增IoT配置模块:支持MQTT、数据库等配置管理 - 集成IoT模块到主应用:在main.py中集成所有IoT功能 - 新增IoT模块测试:验证设备管理、影子更新、设备发现等功能 实现的功能: 1. MQTT协议适配器 - 支持连接管理、主题订阅/发布、消息处理 2. 设备注册/发现API - REST接口支持设备CRUD操作、设备影子管理 3. 统一设备模型 - 包含device_sn/type/area/position/geom等字段 4. OTA固件升级 - 支持升级任务管理、进度跟踪、状态监控 5. 设备统计分析 - 提供设备类型、状态等统计信息 完成Issue #28的核心要求。
149 lines
4.3 KiB
Python
149 lines
4.3 KiB
Python
"""
|
|
IoT模块简化测试脚本
|
|
仅测试设备管理器功能,不依赖MQTT
|
|
"""
|
|
|
|
import asyncio
|
|
import json
|
|
import sys
|
|
import os
|
|
|
|
# 添加项目根目录到Python路径
|
|
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
from src.iot.device_manager import DeviceManager
|
|
from src.iot.models import DeviceType, DeviceStatus
|
|
|
|
|
|
async def test_device_manager():
|
|
"""测试设备管理器"""
|
|
print("=== 测试设备管理器 ===")
|
|
|
|
device_manager = DeviceManager()
|
|
|
|
# 注册设备
|
|
device_data = {
|
|
'device_sn': 'LL-001',
|
|
'device_type': 'flow_meter',
|
|
'name': '流量计-001',
|
|
'description': 'A区入口流量计',
|
|
'area': 'A区',
|
|
'position': '入口处',
|
|
'manufacturer': '华为',
|
|
'model': 'LL-100'
|
|
}
|
|
|
|
device = device_manager.register_device(device_data)
|
|
print(f"注册设备: {device.device_sn} - {device.name}")
|
|
|
|
# 获取设备
|
|
retrieved_device = device_manager.get_device('LL-001')
|
|
print(f"获取设备: {retrieved_device.name}")
|
|
|
|
# 更新设备
|
|
updated_device = device_manager.update_device('LL-001', {'status': DeviceStatus.ONLINE})
|
|
print(f"更新设备状态: {updated_device.status}")
|
|
|
|
# 列出设备
|
|
devices = device_manager.list_devices()
|
|
print(f"设备列表: {len(devices)}个设备")
|
|
|
|
# 更新设备影子
|
|
device_manager.update_device_shadow('LL-001', {'temperature': 25.5, 'pressure': 0.8})
|
|
shadow = device_manager.get_device_shadow('LL-001')
|
|
print(f"设备影子: {shadow.state}")
|
|
|
|
# 获取统计信息
|
|
stats = device_manager.get_device_statistics()
|
|
print(f"设备统计: {stats}")
|
|
|
|
# 测试设备发现
|
|
discovered = device_manager.discover_devices()
|
|
print(f"发现设备: {len(discovered)}个设备")
|
|
|
|
print("设备管理器测试完成\n")
|
|
|
|
|
|
async def test_device_filtering():
|
|
"""测试设备过滤功能"""
|
|
print("=== 测试设备过滤 ===")
|
|
|
|
device_manager = DeviceManager()
|
|
|
|
# 注册多个设备
|
|
test_devices = [
|
|
{'device_sn': 'LL-001', 'device_type': 'flow_meter', 'name': '流量计-001', 'area': 'A区'},
|
|
{'device_sn': 'YL-001', 'device_type': 'pressure_meter', 'name': '压力表-001', 'area': 'A区'},
|
|
{'device_sn': 'SW-001', 'device_type': 'level_meter', 'name': '水位计-001', 'area': 'B区'},
|
|
{'device_sn': 'LL-002', 'device_type': 'flow_meter', 'name': '流量计-002', 'area': 'B区'},
|
|
]
|
|
|
|
for device_data in test_devices:
|
|
device_manager.register_device(device_data)
|
|
|
|
# 测试按类型过滤
|
|
flow_meters = device_manager.list_devices(device_type=DeviceType.FLOW_METER)
|
|
print(f"流量计数量: {len(flow_meters)}")
|
|
|
|
# 测试按区域过滤
|
|
a_zone_devices = device_manager.list_devices(area='A区')
|
|
print(f"A区设备数量: {len(a_zone_devices)}")
|
|
|
|
# 测试按状态过滤
|
|
online_devices = device_manager.list_devices(status=DeviceStatus.ONLINE)
|
|
print(f"在线设备数量: {len(online_devices)}")
|
|
|
|
print("设备过滤测试完成\n")
|
|
|
|
|
|
async def test_device_shadow():
|
|
"""测试设备影子功能"""
|
|
print("=== 测试设备影子 ===")
|
|
|
|
device_manager = DeviceManager()
|
|
|
|
# 注册设备
|
|
device_data = {
|
|
'device_sn': 'LL-001',
|
|
'device_type': 'flow_meter',
|
|
'name': '流量计-001'
|
|
}
|
|
device_manager.register_device(device_data)
|
|
|
|
# 更新设备影子
|
|
shadow_data = {
|
|
'temperature': 25.5,
|
|
'pressure': 0.8,
|
|
'flow_rate': 100.5
|
|
}
|
|
|
|
success = device_manager.update_device_shadow('LL-001', shadow_data)
|
|
print(f"影子更新成功: {success}")
|
|
|
|
# 获取设备影子
|
|
shadow = device_manager.get_device_shadow('LL-001')
|
|
print(f"设备影子状态: {shadow.state}")
|
|
|
|
print("设备影子测试完成\n")
|
|
|
|
|
|
async def main():
|
|
"""主测试函数"""
|
|
print("开始 IoT 模块简化测试...\n")
|
|
|
|
try:
|
|
# 测试各个组件
|
|
await test_device_manager()
|
|
await test_device_filtering()
|
|
await test_device_shadow()
|
|
|
|
print("✅ 所有测试完成!")
|
|
|
|
except Exception as e:
|
|
print(f"❌ 测试失败: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main()) |