‘liusuyi’
2024-01-13 43f46c3801b0aa8544a1703e4f1a27ecc8db22b8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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.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;
import org.springframework.stereotype.Service;
 
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 {
    /*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();
    @Autowired
    private UavClient uavClient;
    @Autowired
    private RedisCache redisCache;
 
    @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) {
        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
        this.getToken();//获取token验证,验证token有效性
        try {
            String token = this.getToken();
            res = (String) requestMethod.invoke(this.uavClient, this.host + url, token, data);
        } catch (IllegalAccessException e) {
            log.error("doUavRequest 访问失败" + e.getMessage());
        } catch (InvocationTargetException e) {
            log.error("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(this.host+"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() {
 
        getUavConfig();
        log.debug("登录无人机外部接口");
        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(this.host+"login", null, body);
            Map resMap = om.readValue(res, Map.class);
            if (resMap != null) {
                redisCache.setCacheMap("uav:uavUser", (Map) resMap.get("data"));
            }
        } catch (ForestRuntimeException e) {
            log.error("无人机连接超时" + e.getMessage());
        } catch (JsonMappingException e) {
        } catch (JsonProcessingException e) {
        } catch (IOException e) {
        }
    }
 
 
    public String Encrypt(String sSrc, String sKey) {
        if (sKey == null) {
            return null;
        }
        // 判断Key是否为16位
        if (sKey.length() != 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次加密的作用。
    }
}