From ba1e219d97e123b91657dc424f13a2fdeaa85d21 Mon Sep 17 00:00:00 2001
From: ‘liusuyi’ <1951119284@qq.com>
Date: Mon, 21 Aug 2023 13:37:01 +0800
Subject: [PATCH] 修改兴趣点列表井号模糊查询

---
 ard-work/src/main/java/com/ruoyi/device/uav/service/UavService.java |   63 +++++++++++++++++++++----------
 1 files changed, 42 insertions(+), 21 deletions(-)

diff --git a/ard-work/src/main/java/com/ruoyi/device/uav/service/UavService.java b/ard-work/src/main/java/com/ruoyi/device/uav/service/UavService.java
index f2d3986..99dae26 100644
--- a/ard-work/src/main/java/com/ruoyi/device/uav/service/UavService.java
+++ b/ard-work/src/main/java/com/ruoyi/device/uav/service/UavService.java
@@ -1,10 +1,13 @@
 package com.ruoyi.device.uav.service;
 
 import com.dtflys.forest.exceptions.ForestNetworkException;
+import com.dtflys.forest.exceptions.ForestRuntimeException;
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.JsonMappingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.ruoyi.common.core.redis.RedisCache;
 import com.ruoyi.utils.forest.UavClient;
+import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.codec.binary.Base64;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpMethod;
@@ -21,16 +24,18 @@
 import java.util.Map;
 
 @Service
+@Slf4j(topic = "uav")
 public class UavService {
     public static final String USERNAME = "ardbailu";//用户名
     public static final String PASSWORD = "ardkj12345";//密码
     public static final String SALT = "0123456789012345";//盐
 
-    //token 登陆后每次请求,在header中携带
-    private String token;//登录成功返回的token
+    private Map uavUser;//登录的用户信息
     private ObjectMapper om = new ObjectMapper();
     @Autowired
     private UavClient uavClient;
+    @Autowired
+    private RedisCache redisCache;
 
     @PostConstruct
     public void created() {
@@ -47,39 +52,55 @@
             e.printStackTrace();
         }
         //执行method
+        this.getToken();//获取token验证,验证token有效性
         try {
-            res = (String) requestMethod.invoke(this.uavClient, url, this.token, data);
-        } catch (InvocationTargetException e) {
-            ForestNetworkException fe = (ForestNetworkException) e.getCause();
-            if (fe.getStatusCode() == 401) {//token失效,重新登录
-                this.login();
-            }
-            try {//再次调用接口
-                res = (String) requestMethod.invoke(this.uavClient, url, this.token, data);
-            } catch (IllegalAccessException ex) {
-                ex.printStackTrace();
-            } catch (InvocationTargetException ex) {
-                ex.printStackTrace();
-            }
+            res = (String) requestMethod.invoke(this.uavClient, url, this.getToken(), data);
         } catch (IllegalAccessException e) {
-            throw new RuntimeException(e);
+            System.out.println("doUavRequest 访问失败" + e.getMessage());
+        } catch (InvocationTargetException e) {
+            System.out.println("doUavRequest 执行失败" + e.getMessage());
+            e.printStackTrace();
         }
 
         return res;
     }
 
+    public Map getUavUser() {
+        return redisCache.getCacheMap("uav:uavUser");
+    }
+
+    public String getToken() {
+        //获取uav用户信息
+        Map uavUser = redisCache.getCacheMap("uav:uavUser");
+        String token = (String) uavUser.get("access_token");
+        //验证token有效性
+        try {
+            String res = uavClient.GET("manage/api/v1/devices", token, "{}");
+        } catch (ForestNetworkException fe) {
+            if (fe.getStatusCode() == 401) {//token失效,重新登录
+                this.login();
+                //再次获取token
+                uavUser = redisCache.getCacheMap("uav:uavUser");
+                token = (String) uavUser.get("access_token");
+                return token;
+            }
+        }
+        return token;
+    }
+
     public void login() {
+        log.debug("登录无人机外部接口");
         String codedPassword = this.Encrypt(PASSWORD, SALT);
         String body = "{\"username\":\"" + USERNAME + "\",\"password\":\"" + codedPassword + "\"}";
         try {
+            //uavClient.GET("logout", null, "{}");
             String res = uavClient.POST("login", null, body);
             Map resMap = om.readValue(res, Map.class);
-            Map data = (Map) resMap.get("data");
-            if (data != null) {
-                this.token = (String) data.get("access_token");
+            if (resMap != null) {
+                redisCache.setCacheMap("uav:uavUser", (Map) resMap.get("data"));
             }
-        } catch (ForestNetworkException e) {
-            e.printStackTrace();
+        } catch (ForestRuntimeException e) {
+            log.error("无人机连接超时" + e.getMessage());
         } catch (JsonMappingException e) {
             e.printStackTrace();
         } catch (JsonProcessingException e) {

--
Gitblit v1.9.3