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 |   56 +++++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 43 insertions(+), 13 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 99dae26..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
@@ -6,10 +6,13 @@
 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;
@@ -26,9 +29,20 @@
 @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";//鐩�
+    /*uav:
+      host: http://112.98.126.2:6100/
+      username: kaifabailu
+      password: ardkj@2014
+      salt: 0123456789012345
+      */
+    private String username;//鐢ㄦ埛鍚�
+
+    private String password;//瀵嗙爜
+
+    private String salt; //鐩�
+
+    private String host;//uav鏈嶅姟鍣�11
+
 
     private Map uavUser;//鐧诲綍鐨勭敤鎴蜂俊鎭�
     private ObjectMapper om = new ObjectMapper();
@@ -39,10 +53,24 @@
 
     @PostConstruct
     public void created() {
+
+        getUavConfig();
         this.login();
     }
 
+    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
@@ -54,11 +82,12 @@
         //鎵цmethod
         this.getToken();//鑾峰彇token楠岃瘉,楠岃瘉token鏈夋晥鎬�
         try {
-            res = (String) requestMethod.invoke(this.uavClient, url, this.getToken(), data);
+            String token = this.getToken();
+            res = (String) requestMethod.invoke(this.uavClient, this.host + url, token, data);
         } catch (IllegalAccessException e) {
-            System.out.println("doUavRequest 璁块棶澶辫触" + e.getMessage());
+            log.error("doUavRequest 璁块棶澶辫触" + e.getMessage());
         } catch (InvocationTargetException e) {
-            System.out.println("doUavRequest 鎵ц澶辫触" + e.getMessage());
+            log.error("doUavRequest 鎵ц澶辫触" + e.getMessage());
             e.printStackTrace();
         }
 
@@ -75,7 +104,8 @@
         String token = (String) uavUser.get("access_token");
         //楠岃瘉token鏈夋晥鎬�
         try {
-            String res = uavClient.GET("manage/api/v1/devices", 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();
@@ -89,12 +119,15 @@
     }
 
     public void login() {
+
+        getUavConfig();
         log.debug("鐧诲綍鏃犱汉鏈哄閮ㄦ帴鍙�");
-        String codedPassword = this.Encrypt(PASSWORD, SALT);
-        String body = "{\"username\":\"" + USERNAME + "\",\"password\":\"" + codedPassword + "\"}";
+        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("login", null, body);
+            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"));
@@ -102,11 +135,8 @@
         } catch (ForestRuntimeException e) {
             log.error("鏃犱汉鏈鸿繛鎺ヨ秴鏃�" + e.getMessage());
         } catch (JsonMappingException e) {
-            e.printStackTrace();
         } catch (JsonProcessingException e) {
-            e.printStackTrace();
         } catch (IOException e) {
-            e.printStackTrace();
         }
     }
 

--
Gitblit v1.9.3