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);
|
}
|
}
|
}
|