From 96ff86c82a9c7892a88cdee1a00b710a0006258a Mon Sep 17 00:00:00 2001
From: zhangjianrock@163.com <zhangjianrock@163.com>
Date: 星期日, 14 一月 2024 16:21:13 +0800
Subject: [PATCH] 无人机读取配置文件

---
 ard-work/src/main/java/com/ruoyi/device/uav/service/UavService.java |  123 ++++++++++++++++++++++++++++------------
 1 files changed, 86 insertions(+), 37 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 1950bfd..5f3b443 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,12 +1,18 @@
 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.common.utils.ConfigUtils;
+import com.ruoyi.common.utils.StringUtils;
 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.beans.factory.annotation.Value;
 import org.springframework.http.HttpMethod;
 
 import org.springframework.beans.factory.annotation.Autowired;
@@ -15,29 +21,56 @@
 import javax.annotation.PostConstruct;
 import javax.crypto.Cipher;
 import javax.crypto.spec.SecretKeySpec;
+import java.io.IOException;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.util.Map;
 
 @Service
-public class UavService implements IUavService {
-    public static final String USERNAME = "ardbailu";//鐢ㄦ埛鍚�
-    public static final String PASSWORD = "ardkj12345";//瀵嗙爜
-    public static final String SALT = "0123456789012345";//鐩�
+@Slf4j(topic = "uav")
+public class UavService {
+    /*uav:
+      host: http://112.98.126.2:6100/
+      username: kaifabailu
+      password: ardkj@2014
+      salt: 0123456789012345
+      */
+    private String username;//鐢ㄦ埛鍚�
 
-    //token 鐧婚檰鍚庢瘡娆¤姹�,鍦╤eader涓惡甯�
-    private String token;//鐧诲綍鎴愬姛杩斿洖鐨則oken
+    private String password;//瀵嗙爜
+
+    private String salt; //鐩�
+
+    private String host;//uav鏈嶅姟鍣�11
+
+
+    private Map uavUser;//鐧诲綍鐨勭敤鎴蜂俊鎭�
     private ObjectMapper om = new ObjectMapper();
     @Autowired
     private UavClient uavClient;
+    @Autowired
+    private RedisCache redisCache;
 
     @PostConstruct
     public void created() {
+
+        getUavConfig();
         this.login();
     }
 
-    @Override
+    private void getUavConfig() {
+        this.host = ConfigUtils.getConfigValue("uav_host");
+        this.username = ConfigUtils.getConfigValue("uav_username");
+        this.password = ConfigUtils.getConfigValue("uav_password");
+        this.salt = ConfigUtils.getConfigValue("uav_salt");
+        if (StringUtils.isEmpty(this.host) || StringUtils.isEmpty(this.username) || StringUtils.isEmpty(this.password) || StringUtils.isEmpty(this.salt)) {
+            throw new RuntimeException("鏃犱汉鏈哄弬鏁伴厤缃己澶�:");
+        }
+    }
+
     public String doUavRequest(HttpMethod method, String url, String data) {
+
+        getUavConfig();
         String res = null;
         Method requestMethod = null;
         //鑾峰彇method
@@ -47,56 +80,73 @@
             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) {
-                throw new RuntimeException(ex);
-            } catch (InvocationTargetException ex) {
-                throw new RuntimeException(ex);
-            }
+            String token = this.getToken();
+            res = (String) requestMethod.invoke(this.uavClient, this.host + url, token, data);
         } catch (IllegalAccessException e) {
-            throw new RuntimeException(e);
+            log.error("doUavRequest 璁块棶澶辫触" + e.getMessage());
+        } catch (InvocationTargetException e) {
+            log.error("doUavRequest 鎵ц澶辫触" + e.getMessage());
+            e.printStackTrace();
         }
 
         return res;
     }
 
-    @Override
-    public void login() {
-        String codedPassword = this.Encrypt(PASSWORD, SALT);
-        String body = "{\"username\":\"" + USERNAME + "\",\"password\":\"" + codedPassword + "\"}";
+    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.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");
+            String res = uavClient.GET(this.host+"manage/api/v1/devices", token, "{}");
+            //System.out.println(res);
+        } catch (ForestNetworkException fe) {
+            if (fe.getStatusCode() == 401) {//token澶辨晥,閲嶆柊鐧诲綍
+                this.login();
+                //鍐嶆鑾峰彇token
+                uavUser = redisCache.getCacheMap("uav:uavUser");
+                token = (String) uavUser.get("access_token");
+                return token;
             }
-        } catch (ForestNetworkException e) {
-            e.printStackTrace();
+        }
+        return token;
+    }
+
+    public void login() {
+
+        getUavConfig();
+        log.debug("鐧诲綍鏃犱汉鏈哄閮ㄦ帴鍙�");
+        String codedPassword = this.Encrypt(password, salt);
+        String body = "{\"username\":\"" + username + "\",\"password\":\"" + codedPassword + "\"}";
+        log.debug("body:" + body);
+        try {
+            //uavClient.GET("logout", null, "{}");
+            String res = uavClient.POST(this.host+"/login", null, body);
+            Map resMap = om.readValue(res, Map.class);
+            if (resMap != null) {
+                redisCache.setCacheMap("uav:uavUser", (Map) resMap.get("data"));
+            }
+        } catch (ForestRuntimeException e) {
+            log.error("鏃犱汉鏈鸿繛鎺ヨ秴鏃�" + e.getMessage());
         } catch (JsonMappingException e) {
-            throw new RuntimeException(e);
         } catch (JsonProcessingException e) {
-            throw new RuntimeException(e);
+        } catch (IOException e) {
         }
     }
 
 
     public String Encrypt(String sSrc, String sKey) {
         if (sKey == null) {
-            System.out.print("Key涓虹┖null");
             return null;
         }
         // 鍒ゆ柇Key鏄惁涓�16浣�
         if (sKey.length() != 16) {
-            System.out.print("Key闀垮害涓嶆槸16浣�");
             return null;
         }
         byte[] encrypted = null;
@@ -108,7 +158,6 @@
             encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
         } catch (Exception e) {
             e.printStackTrace();
-            ;
         }
         return new Base64().encodeToString(encrypted);//姝ゅ浣跨敤BASE64鍋氳浆鐮佸姛鑳斤紝鍚屾椂鑳借捣鍒�2娆″姞瀵嗙殑浣滅敤銆�
     }

--
Gitblit v1.9.3