From 7c2578e6a5b451a4c1c129b678b23cdb36c1620f Mon Sep 17 00:00:00 2001
From: zhangjian <zhangjianrock@163.com>
Date: 星期二, 11 七月 2023 11:52:26 +0800
Subject: [PATCH] 无人机转发接口

---
 /dev/null                                                                    |    9 ---
 ard-work/src/main/java/com/ruoyi/device/uav/controller/ArdUavController.java |   92 ++----------------------------
 ard-work/src/main/java/com/ruoyi/utils/forest/UavAuthLifeCircle.java         |   24 --------
 ard-work/src/main/java/com/ruoyi/device/uav/service/UavService.java          |   15 +---
 4 files changed, 11 insertions(+), 129 deletions(-)

diff --git a/ard-work/src/main/java/com/ruoyi/device/uav/controller/ArdUavController.java b/ard-work/src/main/java/com/ruoyi/device/uav/controller/ArdUavController.java
index feb37e3..07d1185 100644
--- a/ard-work/src/main/java/com/ruoyi/device/uav/controller/ArdUavController.java
+++ b/ard-work/src/main/java/com/ruoyi/device/uav/controller/ArdUavController.java
@@ -6,6 +6,7 @@
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.github.pagehelper.util.StringUtil;
 import com.ruoyi.common.core.controller.BaseController;
+import com.ruoyi.device.uav.service.UavService;
 import com.ruoyi.utils.forest.UavClient;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
@@ -38,115 +39,34 @@
 @RequestMapping("/device/uav")
 @Api(tags = "鏃犱汉鏈哄閮ㄦ帴鍙�")
 public class ArdUavController extends BaseController {
-    public static final String USERNAME = "ardbailu";//鐢ㄦ埛鍚�
-    public static final String PASSWORD = "ardkj12345";//瀵嗙爜
-    public static final String SALT = "0123456789012345";//鐩�
-
-    //token 鐧婚檰鍚庢瘡娆¤姹�,鍦╤eader涓惡甯�
-    private String token;//鐧诲綍鎴愬姛杩斿洖鐨則oken
-    private ObjectMapper om = new ObjectMapper();
 
     @Autowired
-    private UavClient uavClient;
+    private UavService uavService;
 
     @PostConstruct
     public void created() {
-        this.uavLogin();
+        this.uavService.login();
     }
 
-    public void uavLogin() {
-        String codedPassword = this.Encrypt(PASSWORD, SALT);
-        String body = "{\"username\":\"" + USERNAME + "\",\"password\":\"" + codedPassword + "\"}";
-        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");
-            }
-        } catch (ForestNetworkException e) {
-            e.printStackTrace();
-        } catch (JsonMappingException e) {
-            throw new RuntimeException(e);
-        } catch (JsonProcessingException e) {
-            throw new RuntimeException(e);
-        }
-    }
 
     @GetMapping("/")
     @ApiOperation("鏃犱汉鏈篻et鎺ュ彛")
     public Object get(@RequestParam String url, @RequestParam String data) {
-        return doUavRequest(GET, url, data);
+        return this.uavService.doUavRequest(GET, url, data);
 
     }
 
     @PostMapping("/")
     @ApiOperation("鏃犱汉鏈簆ost鎺ュ彛")
     public Object post(@RequestParam String url, @RequestParam String data) {
-        return doUavRequest(POST, url, data);
+        return this.uavService.doUavRequest(POST, url, data);
     }
 
     @DeleteMapping("/")
     @ApiOperation("鏃犱汉鏈篸elete鎺ュ彛")
     public Object delete(@RequestParam String url, @RequestParam String data) {
-        return doUavRequest(DELETE, url, data);
+        return this.uavService.doUavRequest(DELETE, url, data);
     }
 
-    @Nullable
-    private String doUavRequest(HttpMethod method, String url, String data) {
-        String res = null;
-        Method requestMethod = null;
-        //鑾峰彇method
-        try {
-            requestMethod = UavClient.class.getMethod(method.name(), String.class, String.class, String.class);
-        } catch (NoSuchMethodException e) {
-            e.printStackTrace();
-        }
-        //鎵цmethod
-        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.uavLogin();
-            }
-            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);
-            }
-        } catch (IllegalAccessException e) {
-            throw new RuntimeException(e);
-        }
-
-        return res;
-    }
-
-
-    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;
-        try {
-            byte[] raw = sKey.getBytes("utf-8");
-            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
-            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"绠楁硶/妯″紡/琛ョ爜鏂瑰紡"
-            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
-            encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
-        } catch (Exception e) {
-            e.printStackTrace();
-            ;
-        }
-        return new Base64().encodeToString(encrypted);//姝ゅ浣跨敤BASE64鍋氳浆鐮佸姛鑳斤紝鍚屾椂鑳借捣鍒�2娆″姞瀵嗙殑浣滅敤銆�
-    }
 
 }
diff --git a/ard-work/src/main/java/com/ruoyi/device/uav/service/IUavService.java b/ard-work/src/main/java/com/ruoyi/device/uav/service/IUavService.java
deleted file mode 100644
index a9e8893..0000000
--- a/ard-work/src/main/java/com/ruoyi/device/uav/service/IUavService.java
+++ /dev/null
@@ -1,9 +0,0 @@
-package com.ruoyi.device.uav.service;
-
-import org.springframework.http.HttpMethod;
-
-public interface IUavService {
-    String doUavRequest(HttpMethod method, String url, String data);
-
-    void login();
-}
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..026c3bd 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
@@ -20,7 +20,7 @@
 import java.util.Map;
 
 @Service
-public class UavService implements IUavService {
+public class UavService {
     public static final String USERNAME = "ardbailu";//鐢ㄦ埛鍚�
     public static final String PASSWORD = "ardkj12345";//瀵嗙爜
     public static final String SALT = "0123456789012345";//鐩�
@@ -36,7 +36,6 @@
         this.login();
     }
 
-    @Override
     public String doUavRequest(HttpMethod method, String url, String data) {
         String res = null;
         Method requestMethod = null;
@@ -57,9 +56,9 @@
             try {//鍐嶆璋冪敤鎺ュ彛
                 res = (String) requestMethod.invoke(this.uavClient, url, this.token, data);
             } catch (IllegalAccessException ex) {
-                throw new RuntimeException(ex);
+                ex.printStackTrace();
             } catch (InvocationTargetException ex) {
-                throw new RuntimeException(ex);
+                ex.printStackTrace();
             }
         } catch (IllegalAccessException e) {
             throw new RuntimeException(e);
@@ -68,7 +67,6 @@
         return res;
     }
 
-    @Override
     public void login() {
         String codedPassword = this.Encrypt(PASSWORD, SALT);
         String body = "{\"username\":\"" + USERNAME + "\",\"password\":\"" + codedPassword + "\"}";
@@ -82,21 +80,19 @@
         } catch (ForestNetworkException e) {
             e.printStackTrace();
         } catch (JsonMappingException e) {
-            throw new RuntimeException(e);
+            e.printStackTrace();
         } catch (JsonProcessingException e) {
-            throw new RuntimeException(e);
+            e.printStackTrace();
         }
     }
 
 
     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 +104,6 @@
             encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
         } catch (Exception e) {
             e.printStackTrace();
-            ;
         }
         return new Base64().encodeToString(encrypted);//姝ゅ浣跨敤BASE64鍋氳浆鐮佸姛鑳斤紝鍚屾椂鑳借捣鍒�2娆″姞瀵嗙殑浣滅敤銆�
     }
diff --git a/ard-work/src/main/java/com/ruoyi/utils/forest/UavAuthLifeCircle.java b/ard-work/src/main/java/com/ruoyi/utils/forest/UavAuthLifeCircle.java
index 83aea24..f010584 100644
--- a/ard-work/src/main/java/com/ruoyi/utils/forest/UavAuthLifeCircle.java
+++ b/ard-work/src/main/java/com/ruoyi/utils/forest/UavAuthLifeCircle.java
@@ -7,25 +7,6 @@
 public class UavAuthLifeCircle implements MethodAnnotationLifeCycle<UavAuth, Object> {
 
 
-    /**
-     * 褰撴柟娉曡皟鐢ㄦ椂璋冪敤姝ゆ柟娉曪紝姝ゆ椂杩樻病鏈夋墽琛岃姹傚彂閫�
-     * 娆℃柟娉曞彲浠ヨ幏寰楄姹傚搴旂殑鏂规硶璋冪敤淇℃伅锛屼互鍙婂姩鎬佷紶鍏ョ殑鏂规硶璋冪敤鍙傛暟鍒楄〃
-     */
-    @Override
-    public void onInvokeMethod(ForestRequest request, ForestMethod method, Object[] args) {
-
-        System.out.println("!!!!!onInvokeMethod!!!!!! '" + method.getMethodName() + "' Arguments>>>>>>>>>: ");
-    /*    for (Object arg :
-                args) {
-            System.out.println(arg);
-        }
-        String token = (String) args[1];
-        if (token != null) {
-            request.addHeader("x-auth-token", token);
-        }*/
-
-        System.out.println("=================");
-    }
 
     /**
      * 鍙戦�佽姹傚墠鎵ц姝ゆ柟娉�
@@ -33,10 +14,6 @@
     @Override
     public boolean beforeExecute(ForestRequest request) {
         String token = (String) getAttribute(request, "token");
-        System.out.println("!!!!beforeExecute!!!!");
-        System.out.println("request.getMethod().getMethodName():" + request.getMethod().getMethodName());
-        System.out.println("  token:" + token);
-        System.out.println("=================");
         request.addHeader("x-auth-token", token);
         return true;
     }
@@ -47,6 +24,5 @@
 
     @Override
     public void onMethodInitialized(ForestMethod forestMethod, UavAuth uavAuth) {
-        System.out.println("Method '" + forestMethod.getMethodName());
     }
 }

--
Gitblit v1.9.3