修复代码, 现在可以正常接收平台心跳包并返回设备状态
This commit is contained in:
@@ -92,36 +92,24 @@ class RelayService:
|
||||
# 创建WebSocket客户端
|
||||
self.websocket_client = WebSocketClient(self)
|
||||
|
||||
# 连接到平台
|
||||
if await self.websocket_client.connect():
|
||||
# 启动心跳任务
|
||||
heartbeat_task = asyncio.create_task(self._heartbeat_routine())
|
||||
|
||||
# 监听平台消息
|
||||
await self.websocket_client.listen()
|
||||
|
||||
# 取消心跳任务
|
||||
heartbeat_task.cancel()
|
||||
else:
|
||||
logger.error("无法连接到平台")
|
||||
|
||||
async def _heartbeat_routine(self):
|
||||
"""心跳协程"""
|
||||
logger.info("心跳协程启动")
|
||||
|
||||
try:
|
||||
while self.running:
|
||||
if self.websocket_client and self.websocket_client.is_connected():
|
||||
await self.websocket_client.send_heartbeat()
|
||||
# 持续尝试连接到平台
|
||||
while self.running:
|
||||
# 连接到平台
|
||||
if await self.websocket_client.connect():
|
||||
# 监听平台消息
|
||||
await self.websocket_client.listen()
|
||||
|
||||
# 每30秒发送一次心跳
|
||||
await asyncio.sleep(30)
|
||||
except asyncio.CancelledError:
|
||||
logger.info("心跳协程被取消")
|
||||
except Exception as e:
|
||||
logger.error(f"心跳协程发生错误: {e}")
|
||||
# 如果连接断开但服务仍在运行,则尝试重新连接
|
||||
if self.running and not self.websocket_client.is_connected():
|
||||
logger.info("连接已断开,尝试重新连接...")
|
||||
await self.websocket_client.reconnect()
|
||||
else:
|
||||
# 连接失败,等待后重试
|
||||
logger.info("连接失败,5秒后重试...")
|
||||
await asyncio.sleep(5)
|
||||
|
||||
logger.info("心跳协程停止")
|
||||
# 服务停止时断开连接
|
||||
await self.websocket_client.disconnect()
|
||||
|
||||
def _main_loop(self):
|
||||
"""主循环"""
|
||||
@@ -174,11 +162,13 @@ class RelayService:
|
||||
self._handle_query_device_status(command_data)
|
||||
elif command_name == 'query_all_device_status':
|
||||
self._handle_query_all_device_status(command_data)
|
||||
elif command_name == 'heartbeat':
|
||||
self._handle_heartbeat_command(command_data)
|
||||
else:
|
||||
logger.warning(f"未知指令: {command_name}")
|
||||
self._send_error_response(command_data, f"未知指令: {command_name}")
|
||||
elif command_type == 'heartbeat':
|
||||
self._handle_heartbeat(command_data)
|
||||
self._handle_heartbeat_message(command_data)
|
||||
elif command_type == 'system':
|
||||
# 处理系统消息
|
||||
logger.info(f"收到系统消息: {command_name}")
|
||||
@@ -224,21 +214,59 @@ class RelayService:
|
||||
# 发送响应到平台
|
||||
self._send_command_response(command_data, result)
|
||||
|
||||
def _handle_heartbeat(self, command_data):
|
||||
"""处理心跳消息"""
|
||||
logger.info("收到平台心跳消息")
|
||||
def _handle_heartbeat_command(self, command_data):
|
||||
"""处理心跳指令"""
|
||||
logger.info("收到平台心跳指令")
|
||||
|
||||
# 获取所有设备状态
|
||||
devices_status = []
|
||||
|
||||
# 添加中继器自身状态
|
||||
devices_status.append({
|
||||
"device_id": self.device_id,
|
||||
"device_type": "relay",
|
||||
"status": "running"
|
||||
})
|
||||
|
||||
# 如果启用了模拟模式,获取模拟设备状态
|
||||
if self.lora_handler and config.simulation_enabled and self.lora_handler.device_manager:
|
||||
# 获取所有区域主控设备状态
|
||||
controllers = self.lora_handler.device_manager.get_all_controllers()
|
||||
for controller in controllers:
|
||||
devices_status.append({
|
||||
"device_id": controller.device_id,
|
||||
"device_type": "area_controller",
|
||||
"status": controller.status
|
||||
})
|
||||
|
||||
# 获取该控制器下的所有普通设备状态
|
||||
devices = controller.get_all_devices()
|
||||
for device in devices:
|
||||
devices_status.append({
|
||||
"device_id": device.device_id,
|
||||
"device_type": device.device_type,
|
||||
"status": device.status
|
||||
})
|
||||
|
||||
# 发送心跳响应
|
||||
response = {
|
||||
"type": "response",
|
||||
"command": "heartbeat",
|
||||
"status": "success",
|
||||
"message": "心跳响应",
|
||||
"timestamp": datetime.utcnow().isoformat() + 'Z'
|
||||
"data": {
|
||||
"devices": devices_status
|
||||
},
|
||||
"timestamp": command_data.get("timestamp") # 原封不动地返回平台发送的时间戳
|
||||
}
|
||||
|
||||
self._send_response(response)
|
||||
|
||||
def _handle_heartbeat_message(self, command_data):
|
||||
"""处理心跳消息"""
|
||||
logger.info("收到平台心跳消息")
|
||||
|
||||
# 根据RELAY_API.md,平台发送的心跳消息不需要特殊处理
|
||||
# 设备的心跳响应已经在_handle_heartbeat_command中处理
|
||||
pass
|
||||
|
||||
def _send_command_response(self, command_data, result_data):
|
||||
"""发送指令响应"""
|
||||
response = {
|
||||
|
||||
Reference in New Issue
Block a user