rain
2024-08-03 fa7479cd93d2b87c3a62a7c954c1833df34f3f0c
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
package com.dji.sample.droneairport.service.impl;
 
import com.dji.sample.droneairport.model.dto.DroneStateDto;
import com.dji.sample.droneairport.model.param.AddDeviceParam;
import com.dji.sample.droneairport.model.param.RegistParam;
import com.dji.sample.droneairport.service.RegistService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
 
import java.util.List;
 
@Service
public class RegistServiceImpl implements RegistService {
    private final RestTemplate restTemplate = new RestTemplate();
    private final ObjectMapper objectMapper = new ObjectMapper();
 
    public String registPort(RegistParam param) {
        try {
            // 构建请求体
            String jsonBody = buildRequestBody(param);
            // 设置请求头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            headers.setAccept(List.of(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN, MediaType.ALL));
            headers.set("Accept-Language", "zh-CN,zh;q=0.9");
            headers.setConnection("keep-alive");
            headers.set("x-auth-token", "");
            headers.set("x-lc-token","");
            // 构建请求实体
            HttpEntity<String> requestEntity = new HttpEntity<>(jsonBody, headers);
            // 发送请求
            ResponseEntity<String> response = restTemplate.exchange(
                    "https://wrj.shuixiongit.com/drone-api/droneAirport/test",
                    HttpMethod.POST,
                    requestEntity,
                    String.class);
            return response.getBody();
 
        } catch (Exception e) {
            // 异常处理
            throw new IllegalArgumentException("Request failed: " + e.getMessage(), e);
        }
    }
 
    private String buildRequestBody(RegistParam param) {
        try {
            return objectMapper.writeValueAsString(param);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("数据有误", e);
        }
    }
 
    @Override
    public String addDrone(AddDeviceParam param) {
        try {
            // 构建请求体
            String jsonBody = buildRequestBody(param);
            // 设置请求头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            headers.setAccept(List.of(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN, MediaType.ALL));
            headers.set("Accept-Language", "zh-CN,zh;q=0.9");
            headers.setConnection("keep-alive");
            headers.set("x-auth-token", "");
            headers.set("x-lc-token","");
            // 构建请求实体
            HttpEntity<String> requestEntity = new HttpEntity<>(jsonBody, headers);
            // 发送请求
            ResponseEntity<String> response = restTemplate.exchange(
                    "https://wrj.shuixiongit.com/drone-api/droneAirport/test",
                    HttpMethod.POST,
                    requestEntity,
                    String.class);
            return response.getBody();
 
        } catch (Exception e) {
            // 异常处理
            throw new IllegalArgumentException("Request failed: " + e.getMessage(), e);
        }
    }
 
    @Override
    public DroneStateDto getDroneState(String deviceId) {
        return null;
    }
 
    private String buildRequestBody(AddDeviceParam param) {
        try {
            return objectMapper.writeValueAsString(param);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("数据有误", e);
        }
    }
}