zhangjianrock@163.com
2024-01-29 0017dec8baea29062ccac94a15248a61f7bdf77d
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
@@ -52,13 +80,14 @@
            e.printStackTrace();
        }
        //执行method
        this.getToken();//获取token验证,验证token有效性
       // 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,8 +104,9 @@
        String token = (String) uavUser.get("access_token");
        //验证token有效性
        try {
            String res = uavClient.GET("manage/api/v1/devices", token, "{}");
            System.out.println(res);
            String res = uavClient.GET(this.host+"manage/api/v1/devices", token, "{}");
            res+="";
            //System.out.println(res);
        } catch (ForestNetworkException fe) {
            if (fe.getStatusCode() == 401) {//token失效,重新登录
                this.login();
@@ -90,24 +120,24 @@
    }
    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) {
            if (resMap != null&& resMap.get("data") instanceof Map) {
                redisCache.setCacheMap("uav:uavUser", (Map) resMap.get("data"));
            }
        } catch (ForestRuntimeException e) {
            log.error("无人机连接超时" + e.getMessage());
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }