From 3940ebf3e39da287974c4f49474e668d33fed2df Mon Sep 17 00:00:00 2001
From: zhangjian <zhangjianrock@163.com>
Date: 星期六, 19 八月 2023 17:28:27 +0800
Subject: [PATCH] 处理连接失败异常
---
ard-work/src/main/java/com/ruoyi/utils/forest/UavClient.java | 2
ard-work/src/main/java/com/ruoyi/device/uav/controller/ArdUavController.java | 13 +++++-
ard-work/src/main/java/com/ruoyi/device/uav/service/UavService.java | 53 +++++++++++++++-----------
3 files changed, 43 insertions(+), 25 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 a8f40e1..ea8ad38 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
@@ -44,7 +44,7 @@
@PostMapping("/")
@ApiOperation("鏃犱汉鏈簆ost鎺ュ彛")
- public Object post(@RequestBody Map<String,String> param) {
+ public Object post(@RequestBody Map<String, String> param) {
String url = param.get("url");
String data = param.get("data");
return this.uavService.doUavRequest(POST, url, data);
@@ -52,9 +52,18 @@
@DeleteMapping("/")
@ApiOperation("鏃犱汉鏈篸elete鎺ュ彛")
- public Object delete(@RequestParam String url, @RequestParam String data) {
+ public Object delete(@RequestBody Map<String, String> param) {
+ String url = param.get("url");
+ String data = param.get("data");
return this.uavService.doUavRequest(DELETE, url, data);
}
+ @GetMapping("/getToken")
+ @ApiOperation("鏃犱汉鏈篸elete鎺ュ彛")
+ public Object getToken() {
+
+ return this.uavService.getToken();
+ }
+
}
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 14acb4d..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
@@ -5,6 +5,7 @@
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;
@@ -29,12 +30,12 @@
public static final String PASSWORD = "ardkj12345";//瀵嗙爜
public static final String SALT = "0123456789012345";//鐩�
- //token 鐧婚檰鍚庢瘡娆¤姹�,鍦╤eader涓惡甯�
- private String token;//鐧诲綍鎴愬姛杩斿洖鐨則oken
private Map uavUser;//鐧诲綍鐨勭敤鎴蜂俊鎭�
private ObjectMapper om = new ObjectMapper();
@Autowired
private UavClient uavClient;
+ @Autowired
+ private RedisCache redisCache;
@PostConstruct
public void created() {
@@ -51,32 +52,40 @@
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() {
- if (this.uavUser == null) {
- this.login();
+ 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 this.uavUser;
+ return token;
}
public void login() {
@@ -84,11 +93,11 @@
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);
- this.uavUser = (Map) resMap.get("data");
- if (this.uavUser != null) {
- this.token = (String) this.uavUser.get("access_token");
+ if (resMap != null) {
+ redisCache.setCacheMap("uav:uavUser", (Map) resMap.get("data"));
}
} catch (ForestRuntimeException e) {
log.error("鏃犱汉鏈鸿繛鎺ヨ秴鏃�" + e.getMessage());
diff --git a/ard-work/src/main/java/com/ruoyi/utils/forest/UavClient.java b/ard-work/src/main/java/com/ruoyi/utils/forest/UavClient.java
index 8fcd0ad..370e866 100644
--- a/ard-work/src/main/java/com/ruoyi/utils/forest/UavClient.java
+++ b/ard-work/src/main/java/com/ruoyi/utils/forest/UavClient.java
@@ -17,7 +17,7 @@
@Post(IP + "${url}")
String POST(@DataVariable("url") String url, @Header("x-auth-token") String token, @JSONBody String body);
- @Post(IP + "${url}")
+ @Delete(IP + "${url}")
String DELETE(@DataVariable("url") String url, @Header("x-auth-token") String token, @JSONBody String body);
--
Gitblit v1.9.3