‘liusuyi’
2023-07-07 5f5cf5b1b4683a56fd0c85a0d89d14a4c0268cde
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
package com.ruoyi.device.uav.controller;
 
import com.dtflys.forest.exceptions.ForestNetworkException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.util.StringUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.utils.forest.UavClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.*;
 
import org.apache.commons.codec.binary.Base64;
 
/**
 * 相机设备Controller
 *
 * @author ruoyi
 * @date 2023-01-31
 */
@Slf4j(topic = "uav")
@RestController
@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 登陆后每次请求,在header中携带
    private String token = null;
    private ObjectMapper om = new ObjectMapper();
 
    @Autowired
    private UavClient uavClient;
 
    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");
            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);
        }
    }
 
    public String getToken() {
        if (StringUtil.isEmpty(this.token)) {
            this.uavLogin();
        }
        return this.token;
    }
 
    @GetMapping("/")
    @ApiOperation("无人机get接口")
    public Object get(@RequestParam String url, @RequestParam String data) {
        String res = null;
        String token = getToken();
        try {
            res = uavClient.get(url, token, data);
        } catch (ForestNetworkException e) {
            if (e.getStatusCode() == 401) {
                this.uavLogin();
                token = getToken();
                res = uavClient.get(url, token, data);
            }
        }
 
        return res;
 
    }
 
    @PostMapping("/")
    @ApiOperation("无人机post接口")
    public Object post(@RequestParam String url, @RequestParam String data) {
        String res = null;
        String token = getToken();
        try {
            res = uavClient.post(url, token, data);
        } catch (ForestNetworkException e) {
            if (e.getStatusCode() == 401) {
                this.uavLogin();
                token = getToken();
                res = uavClient.post(url, token, data);
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
 
        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次加密的作用。
    }
 
}