# uk_gateway_client **Repository Path**: ukiot/uk_gateway_client ## Basic Information - **Project Name**: uk_gateway_client - **Description**: MQTT 网关客户端 - **Primary Language**: Python - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-04-27 - **Last Updated**: 2025-04-27 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # UKIOT网关设备通信协议Python实现 本项目实现了UKIOT网关设备通信协议,提供了基于MQTT的设备与服务器通信框架。 # 创建客户 gateway/ ├── __init__.py ├── client.py # 客户端主类 ├── mqtt/ # MQTT通信模块 │ ├── __init__.py │ ├── mqtt_client.py # MQTT客户端封装 │ └── topic_handler.py # 主题处理器 ├── message/ # 消息模块 │ ├── __init__.py │ ├── message.py # 基础消息类 │ ├── builder.py # 消息构建器 │ └── parser.py # 消息解析器 ├── session/ # 会话管理模块 │ ├── __init__.py │ ├── login.py # 登录/登出处理 │ └── heartbeat.py # 心跳处理 ├── handlers/ # 业务处理模块 │ ├── __init__.py │ ├── data_report.py # 数据上报 │ ├── event_report.py # 事件上报 │ ├── control.py # 控制命令处理 │ ├── config.py # 配置管理 │ └── firmware.py # 固件更新 ├── security/ # 安全模块 │ ├── __init__.py │ ├── auth.py # 认证 │ ├── encryption.py # 加密 │ └── signature.py # 签名 └── utils/ # 工具类 ├── __init__.py ├── time_sync.py # 时间同步 ├── compression.py # 数据压缩 └── logger.py # 日志 ## 特性 - 完整实现UKIOT网关设备通信协议 - 模块化设计,便于扩展 - 支持设备会话管理(登录、心跳、登出) - 支持数据与事件上报 - 支持设备控制命令处理 - 支持配置管理 - 支持固件更新 - 内置安全通信机制 ## 安装 ```bash pip install ukiot-gateway ``` 或者从源码安装: ```bash git clone https://gitee.com/ukiot/uk_gateway_client.git cd gateway-client pip install -e . ``` ## 快速开始 以下是一个简单的示例,展示如何使用该模块: ```python import time from ukiot_gateway import UKIoTClient # 创建客户端实例 client = UKIoTClient( device_id="device001", broker_host="mqtt.example.com", broker_port=1883, use_tls=True, ca_certs="ca.crt" ) # 注册控制命令处理函数 def handle_switch_command(params): status = params.get("status") print(f"收到开关命令: {status}") # 返回(代码, 消息, 数据)元组 return 0, "执行成功", {"status": status} client.register_control_handler("switch", handle_switch_command) # 启动客户端 client.start() # 登录 client.login(auth_method="token", auth_token="your-auth-token") # 等待连接成功 time.sleep(2) # 上报数据 client.report_data("sensor", { "temperature": 25.6, "humidity": 60.2 }) # 上报事件 client.report_event( event_type="alert", level="warning", code="high_temp", description="温度过高", data={"temperature": 30.5, "threshold": 30.0} ) # 保持运行 try: while True: time.sleep(1) except KeyboardInterrupt: # 停止客户端 client.stop() ``` ## 文档 详细的API文档可以在[这里](https://ukiot.github.io/gateway-client)找到。 ## 示例 查看[examples](./examples)目录获取更多使用示例。 ## 开发 ### 环境设置 创建虚拟环境: ```bash python -m venv venv source venv/bin/activate # 在Windows上使用: venv\Scripts\activate pip install -e ".[dev]" ``` ```bash python start.py --config .env_example ``` ## 贡献 欢迎贡献代码!请查看[CONTRIBUTING.md](CONTRIBUTING.md)了解详情。 ## 许可证 该项目采用MIT许可证 - 查看[LICENSE](LICENSE)文件了解详情。