From 91f6e60919b92b7f99a41f2ddeb42df17cd0c487 Mon Sep 17 00:00:00 2001
From: liusuyi <1951119284@qq.com>
Date: Fri, 25 Oct 2024 10:48:26 +0800
Subject: [PATCH] 优化

---
 ard-work/src/main/java/com/ard/tianyu/service/impl/TyServiceImpl.java |  170 ++++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 132 insertions(+), 38 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 e9d3dbd..b9f8059 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
@@ -2,19 +2,22 @@
 
 import com.ard.tianyu.config.TyConfiguration;
 import com.ard.tianyu.http.TyClient;
+import com.ard.tianyu.model.cache.UavSignalInfoCache;
+import com.ard.tianyu.model.cache.UavStateCache;
+import com.ard.tianyu.model.cache.UavTelemetryDataCache;
 import com.ard.tianyu.model.dto.BoxHistoryDTO;
 import com.ard.tianyu.model.dto.BoxPhotosDTO;
 import com.ard.tianyu.model.dto.BoxSettingsDTO;
+import com.ard.tianyu.model.dto.converter.UavSignalInfoConverter;
+import com.ard.tianyu.model.dto.converter.UavStateConverter;
+import com.ard.tianyu.model.dto.converter.UavTelemetryDataConverter;
+import com.ard.tianyu.model.dto.signal.SignalInfo;
+import com.ard.tianyu.model.dto.state.UavState;
+import com.ard.tianyu.model.dto.telemetry.TelemetryData;
 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 com.ard.vtdu.util.ArdTool;
-import io.netty.buffer.ByteBuf;
-import io.netty.buffer.Unpooled;
 import io.netty.channel.Channel;
 import io.netty.channel.ChannelFuture;
 import lombok.extern.slf4j.Slf4j;
@@ -23,6 +26,7 @@
 
 import javax.annotation.Resource;
 import java.net.InetSocketAddress;
+import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -44,7 +48,7 @@
     /**
      * 获取天宇token信息
      *
-     * @return
+     * @return AccessTokenReceiver
      */
     @Override
     public AccessTokenReceiver getAccessToken() {
@@ -54,31 +58,36 @@
     /**
      * 获取云盒列表
      *
-     * @return
+     * @return List<BoxInfoReceiver>
      */
     @Override
-    public List<BoxReceiver> getBoxList() {
+    public List<BoxInfoReceiver> getBoxList() {
         //请求云盒列表
-        TyHttpCommonReceiver<List<BoxReceiver>> 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<>();
+        }
     }
 
     /**
      * 获取直播地址
      *
-     * @return
+     * @return BoxStreamUrlReceiver
      */
     @Override
-    public PullStreamUrlReceiver getBoxLiveUrl(String boxSn) {
+    public BoxStreamUrlReceiver getBoxLiveUrl(String boxSn) {
 
-        TyHttpCommonReceiver<PullStreamUrlReceiver> boxLiveUrl = tyClient.getBoxLiveUrl(boxSn, tyConfiguration.getAccessToken().convertFromDTO());
+        TyHttpCommonReceiver<BoxStreamUrlReceiver> boxLiveUrl = tyClient.getBoxLiveUrl(boxSn, tyConfiguration.getAccessToken().convertFromDTO());
         return boxLiveUrl.getContent();
     }
 
     /**
      * 获取飞行历史
      *
-     * @return
+     * @return List<BoxHistoryReceiver>
      */
     @Override
     public List<BoxHistoryReceiver> getBoxHistory(BoxHistoryDTO boxHistoryDTO) {
@@ -89,7 +98,7 @@
     /**
      * 获取盒子图片
      *
-     * @return
+     * @return List<BoxPhotosReceiver>
      */
     @Override
     public List<BoxPhotosReceiver> getBoxPhotos(BoxPhotosDTO boxPhotosDTO) {
@@ -102,7 +111,7 @@
      */
     @Override
     public Boolean updateBoxSettings(BoxSettingsDTO boxSettingsDTO) {
-        TyHttpCommonReceiver<String> result = tyClient.updateBoxSettings(boxSettingsDTO, tyConfiguration.getAccessToken().convertFromDTO());
+        TyHttpCommonReceiver<?> result = tyClient.updateBoxSettings(boxSettingsDTO, tyConfiguration.getAccessToken().convertFromDTO());
         return result.getState() == 1;
     }
 
@@ -115,47 +124,132 @@
         RegisterCommand registerMessage = new RegisterCommand(tyToken.getCompanyId(), tyToken.getAccessToken().getBytes());
         this.sendMessage(tyConfiguration.getIpPort(), registerMessage);
     }
+
+
     /**
      * 发送消息
      */
-    public boolean sendMessage(String key, BaseCommand msg) {
-        //将从用户接收的信息发送到天宇 commandCache
-        boolean cmdResult = false;
+    public void sendMessage(String key, BaseCommand msg) {
         try {
-            ChannelFuture future = TyNettyTcpClient.channels.get(key).getFuture();
+            ChannelFuture future = TyNettyTcpClient.channels.get(key).getChannelFuture();
             Channel channel = future.channel();
-            if (future != null && channel.isActive()) {
+            if (channel.isActive()) {
                 InetSocketAddress ipSocket = (InetSocketAddress) channel.remoteAddress();
                 String host = ipSocket.getHostString();
                 int port = ipSocket.getPort();
-                // 创建 ByteBuf 并编码消息
-                ByteBuf buffer = Unpooled.buffer();
-                msg.encode(buffer); // 确保对消息进行编码
-
-                channel.writeAndFlush(buffer); // 发送编码后的数据
-                cmdResult = true;
-                log.info("向天宇服务器" + host + ":" + port + "发送数据" + ",命令码:" + String.format("0x%02X", msg.getCmdCode() & 0xFF));
+                channel.writeAndFlush(msg); // 发送编码后的数据
+                log.debug("向天宇服务器【{}:{}】发送数据,命令码:{}", host, port, String.format("0x%02X", msg.getCmdCode() & 0xFF));
             } else {
                 log.error("与天宇服务器链接断开,无法发送数据!");
             }
         } catch (Exception e) {
-            log.info("Failed to publish the message. {}", "转发信息");
-            e.printStackTrace();
-        } finally {
-            return cmdResult;
+            log.error("发送消息异常:{}", e.getMessage());
         }
     }
 
+    /**
+     * 云台控制基于角度
+     */
     @Override
-    public void controlAngle(String boxSn) {
-        ControlAngleCommand controlAngleCommand = new ControlAngleCommand(boxSn.getBytes(),(byte)0x00,(byte)0x00,(byte)0x05);
+    public void angleControl(String boxSn, Integer actionId, Integer rotationAngle) {
+        ControlAngleCommand controlAngleCommand = new ControlAngleCommand(boxSn, actionId.byteValue(), rotationAngle.byteValue());
         this.sendMessage(tyConfiguration.getIpPort(), controlAngleCommand);
     }
 
-    @Scheduled(fixedRate = 5000)
+    @Override
+    public void absAngleControl(String boxSn, Integer actionId, float pitch, float yaw, float roll) {
+
+    }
+
+    /**
+     * 获取无人机状态
+     */
+    @Override
+    public UavState getUavState(String boxSn) {
+        return UavStateCache.getUavState(boxSn);
+    }
+
+    /**
+     * 获取无人机遥测数据
+     */
+    @Override
+    public TelemetryData getUavTelemetryData(String boxSn) {
+        return UavTelemetryDataCache.getUavTelemetryData(boxSn);
+    }
+
+    /**
+     * 获取无人机遥测数据
+     */
+    @Override
+    public SignalInfo getSignalInfo(String boxSn) {
+        return UavSignalInfoCache.getUavSignalInfo(boxSn);
+    }
+
+    @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.debug("收到心跳消息-->处理心跳消息{}", heartbeatReplyCommand.getTimestamp());
+                break;
+            case 0x03:
+                // 处理订阅消息
+                break;
+            case 0x04:
+                // 处理取消订阅消息
+                break;
+            case (byte) 0xA8:
+                // 处理无人机状态消息
+                log.debug("收到无人机状态消息-->无人机状态消息");
+                UavStateCommand uavStateCommand = (UavStateCommand) tyCommand;
+                UavState uavState = UavStateConverter.convertProtoToEntity(uavStateCommand.getUavState());
+                UavStateCache.putUavState(uavState);
+                break;
+            case (byte) 0xA9:
+                // 处理无人机遥测消息
+                log.debug("收到无人机遥测消息-->无人机遥测消息");
+                UavTelemetryDataCommand uavTelemetryDataCommand = (UavTelemetryDataCommand) tyCommand;
+                TelemetryData telemetryData = UavTelemetryDataConverter.convertProtoToEntity(uavTelemetryDataCommand.getTelemetryData());
+                UavTelemetryDataCache.putUavTelemetryData(telemetryData);
+                break;
+            case (byte) 0x0C:
+                // 处理无人机信道质量数据
+                log.debug("收到无人机信道质量数据消息-->无人机信道质量数据消息");
+                SignalInfoCommand signalInfoCommand = (SignalInfoCommand) tyCommand;
+                SignalInfo signalInfo = UavSignalInfoConverter.convertProtoToEntity(signalInfoCommand.getSignalInfo());
+                UavSignalInfoCache.putUavSignalInfoCache(signalInfo);
+                break;
+            case (byte) 0xD1:
+                // 处理无人机
+                UavReadyNotificationCommand droneReadyNotificationCommand = (UavReadyNotificationCommand) tyCommand;
+                if (droneReadyNotificationCommand.getActionCode() == (byte) 0x42) {
+                    UavInitInfoCommand uavInitCommand = new UavInitInfoCommand(droneReadyNotificationCommand.getBoxSn());
+                    this.sendMessage(tyConfiguration.getIpPort(), uavInitCommand);
+                    log.info("收到无人机初始化详情-回复防止再次发送");
+                }
+                break;
+            default:
+                // 处理其他消息
+                log.debug("收到其他消息-->其他消息");
+        }
+    }
+
+    @Scheduled(fixedRate = 3000)
     public void sendHeartBeat() {
         long timestamp = System.currentTimeMillis();
-        log.info("定时向天宇发送⼼跳,时间戳{}", timestamp);
+        log.debug("定时向天宇发送⼼跳,时间戳{}", timestamp);
         HeartbeatCommand heartbeatMessage = new HeartbeatCommand(timestamp);
         this.sendMessage(tyConfiguration.getIpPort(), heartbeatMessage);
     }

--
Gitblit v1.9.3