package org.sxkj.odm.service;
|
|
import com.alibaba.fastjson.JSONObject;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.http.HttpEntity;
|
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.MediaType;
|
import org.springframework.stereotype.Component;
|
import org.springframework.web.client.RestTemplate;
|
import org.sxkj.common.utils.SpringContextUtil;
|
import org.sxkj.odm.config.OdmProperties;
|
import org.sxkj.odm.constant.OdmConstant;
|
|
import javax.naming.directory.SearchResult;
|
import java.net.URLEncoder;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* request 请求发送
|
*/
|
@Slf4j
|
@Component
|
public class PyTifSendRequest {
|
|
@Autowired
|
private RestTemplate restTemplate;
|
|
@Autowired
|
private OdmProperties odmProperties;
|
|
/**
|
* 调用接口获取数据-post-json
|
*
|
* @param url 请求url
|
* @param map 数据
|
* @return
|
*/
|
public String sendPostJsonByMap(String url, Map<String, Object> map) {
|
//设置请求头
|
HttpHeaders headers = new HttpHeaders();
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
String json = JSONObject.toJSONString(map);
|
//封装请求头
|
HttpEntity<String> entity = new HttpEntity<>(json, headers);
|
try {
|
//发送请求
|
String result = restTemplate.postForObject(url, entity, String.class);
|
log.info("结果:{}", result);
|
// 返回
|
return result;
|
} catch (Exception e) {
|
log.error("请求异常:{}", e.getMessage());
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
/**
|
* 调用白膜API规划飞行路径
|
*
|
* @param url 请求url
|
* @param routeSegments 路线段列表,每个元素包含起始点和终点的经纬度及高度信息
|
* 格式: [{"start_latitude": 纬度, "start_longitude": 经度, "start_height": 高度,
|
* "end_latitude": 纬度, "end_longitude": 经度, "end_height": 高度}]
|
* @return API响应结果
|
*/
|
public String planFlightPath(String url, List<Map<String, Object>> routeSegments) {
|
//设置请求头
|
HttpHeaders headers = new HttpHeaders();
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
|
//构建请求体 - 直接使用Map而不是JSONObject
|
Map<String, Object> requestBody = new HashMap<>();
|
requestBody.put("route_segments", routeSegments);
|
|
//封装请求
|
HttpEntity<Map<String, Object>> entity = new HttpEntity<>(requestBody, headers);
|
|
try {
|
//发送POST请求
|
String result = restTemplate.postForObject(url, entity, String.class);
|
log.info("飞行路径规划结果:{}", result);
|
return result;
|
} catch (Exception e) {
|
log.error("飞行路径规划请求异常:{}", e.getMessage());
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
|
/**
|
* 调用接口获取数据-get-json
|
*
|
* @param url 请求url
|
* @return
|
*/
|
public SearchResult sendGetByMap(String url) {
|
try {
|
//发送请求
|
SearchResult searchResult = restTemplate.getForObject(url, SearchResult.class);
|
log.info("结果:{}", searchResult);
|
// 返回
|
return searchResult;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
/**
|
* 调用接口获取数据-get-json
|
*
|
* @param url 请求地址
|
* @param map 请求参数
|
* @return 响应结果
|
*/
|
public String sendGetJsonByMap(String url, Map<String, Object> map) {
|
try {
|
// 构建带参数的URL
|
StringBuilder urlBuilder = new StringBuilder(url);
|
if (map != null && !map.isEmpty()) {
|
urlBuilder.append("?");
|
for (Map.Entry<String, Object> entry : map.entrySet()) {
|
if (entry.getValue() != null) {
|
urlBuilder.append(entry.getKey())
|
.append("=")
|
.append(URLEncoder.encode(String.valueOf(entry.getValue()), "UTF-8"))
|
.append("&");
|
}
|
}
|
// 删除最后一个多余的&
|
urlBuilder.deleteCharAt(urlBuilder.length() - 1);
|
}
|
|
// 发送GET请求
|
String result = restTemplate.getForObject(urlBuilder.toString(), String.class);
|
log.info("结果:{}", result);
|
return result;
|
} catch (Exception e) {
|
log.error("发送GET请求失败,url: {}, 参数: {}", url, map, e);
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
|
/**
|
* 调用接口获取数据-post-json
|
*
|
* @param url 请求url
|
* @param json 数据
|
* @return
|
*/
|
public JSONObject sendPostJsonByJson(String url, String json) {
|
url = odmProperties.getApiBaseUrl() + url;
|
//设置请求头
|
HttpHeaders headers = new HttpHeaders();
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
//封装请求头
|
HttpEntity<String> entity = new HttpEntity<>(json, headers);
|
try {
|
//发送请求
|
String result = restTemplate.postForObject(url, entity, String.class);
|
JSONObject jsonObject = JSONObject.parseObject(result);
|
// 返回
|
return jsonObject;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
}
|