From 4c66e35afa9e9ecb31d1cc1705147f8a2d35f24a Mon Sep 17 00:00:00 2001
From: liusuyi <1951119284@qq.com>
Date: Wed, 23 Oct 2024 11:16:56 +0800
Subject: [PATCH] 优化

---
 ard-work/src/main/java/com/ard/tianyu/service/impl/TyServiceImpl.java |   49 ++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 42 insertions(+), 7 deletions(-)

diff --git a/ard-work/src/main/java/com/ard/tianyu/service/impl/TyServiceImpl.java b/ard-work/src/main/java/com/ard/tianyu/service/impl/TyServiceImpl.java
index de07206..b18e6d8 100644
--- a/ard-work/src/main/java/com/ard/tianyu/service/impl/TyServiceImpl.java
+++ b/ard-work/src/main/java/com/ard/tianyu/service/impl/TyServiceImpl.java
@@ -6,10 +6,7 @@
 import com.ard.tianyu.model.dto.BoxPhotosDTO;
 import com.ard.tianyu.model.dto.BoxSettingsDTO;
 import com.ard.tianyu.model.receiver.*;
-import com.ard.tianyu.netty.command.BaseCommand;
-import com.ard.tianyu.netty.command.ControlAngleCommand;
-import com.ard.tianyu.netty.command.HeartbeatCommand;
-import com.ard.tianyu.netty.command.RegisterCommand;
+import com.ard.tianyu.netty.command.*;
 import com.ard.tianyu.netty.TyNettyTcpClient;
 import com.ard.tianyu.service.ITyService;
 import io.netty.buffer.ByteBuf;
@@ -22,6 +19,7 @@
 
 import javax.annotation.Resource;
 import java.net.InetSocketAddress;
+import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -58,8 +56,13 @@
     @Override
     public List<BoxInfoReceiver> getBoxList() {
         //请求云盒列表
-        TyHttpCommonReceiver<List<BoxInfoReceiver>> boxListReveiver = tyClient.getBoxList(tyConfiguration.getAccessToken().convertFromDTO());
-        return boxListReveiver.getContent();
+        try {
+            TyHttpCommonReceiver<List<BoxInfoReceiver>> boxListReveiver = tyClient.getBoxList(tyConfiguration.getAccessToken().convertFromDTO());
+            return boxListReveiver.getContent();
+        } catch (Exception e) {
+            log.error("获取云盒列表失败:{}", e.getMessage());
+            return new ArrayList<>();
+        }
     }
 
     /**
@@ -130,7 +133,7 @@
                 ByteBuf buffer = Unpooled.buffer();
                 msg.encode(buffer); // 确保对消息进行编码
                 channel.writeAndFlush(buffer); // 发送编码后的数据
-                log.info("向天宇服务器【{}:{}】发送数据,命令码:{}", host, port, String.format("0x%02X", msg.getCmdCode() & 0xFF));
+                log.debug("向天宇服务器【{}:{}】发送数据,命令码:{}", host, port, String.format("0x%02X", msg.getCmdCode() & 0xFF));
             } else {
                 log.error("与天宇服务器链接断开,无法发送数据!");
             }
@@ -145,6 +148,38 @@
         this.sendMessage(tyConfiguration.getIpPort(), controlAngleCommand);
     }
 
+    @Override
+    public void processMessage(String key, BaseCommand tyCommand) {
+        byte cmdCode = tyCommand.getCmdCode();
+        switch (cmdCode) {
+            case 0x01:
+                // 处理心跳消息
+                RegisterReplyCommand registerReplyCommand = (RegisterReplyCommand) tyCommand;
+                log.info("收到注册消息-->处理注册消息{}", registerReplyCommand.getSuccess());
+                if (registerReplyCommand.getSuccess() != 1) {
+                    // 注册失败,重新注册
+                    log.info("注册失败,重新注册");
+                    registerMessage();
+                }
+                break;
+            case 0x02:
+                // 处理注册消息
+                HeartbeatReplyCommand heartbeatReplyCommand = (HeartbeatReplyCommand) tyCommand;
+                log.info("收到心跳消息-->处理心跳消息{}", heartbeatReplyCommand.getTimestamp());
+                break;
+            case (byte) 0xA8:
+                // 处理无人机状态消息
+                log.info("收到无人机状态消息-->无人机状态消息");
+                break;
+            case (byte) 0xA9:
+                // 处理无人机遥测消息
+                log.info("收到无人机遥测消息-->无人机遥测消息");
+                break;
+            default:
+                // 处理其他消息
+        }
+    }
+
     @Scheduled(fixedRate = 5000)
     public void sendHeartBeat() {
         long timestamp = System.currentTimeMillis();

--
Gitblit v1.9.3