From 2a18523931228c78ae359997986d004a39545d51 Mon Sep 17 00:00:00 2001 From: zhangjian <zhangjianrock@163.com> Date: 星期六, 23 九月 2023 10:34:36 +0800 Subject: [PATCH] 去掉异常打印 --- ard-work/src/main/java/com/ruoyi/device/uav/service/UavService.java | 69 +++++++++++++++++++++++----------- 1 files changed, 46 insertions(+), 23 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 026c3bd..65e8448 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; @@ -15,21 +18,24 @@ 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 +@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 鐧婚檰鍚庢瘡娆¤姹�,鍦╤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() { @@ -46,43 +52,60 @@ 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(); - } + String token = this.getToken(); + res = (String) requestMethod.invoke(this.uavClient, url, token, 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, "{}"); + 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; + } + } + 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) { - e.printStackTrace(); + } catch (IOException e) { } } -- Gitblit v1.9.3