|
@@ -0,0 +1,107 @@
|
|
|
+package com.sooka.sponest.monitor.uav;
|
|
|
+
|
|
|
+
|
|
|
+import com.alibaba.nacos.api.config.annotation.NacosValue;
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
+import org.apache.http.HttpResponse;
|
|
|
+import org.apache.http.client.HttpClient;
|
|
|
+import org.apache.http.client.config.RequestConfig;
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
+import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.apache.http.ssl.SSLContextBuilder;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+
|
|
|
+import javax.net.ssl.SSLContext;
|
|
|
+import java.io.IOException;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+
|
|
|
+@Api(tags = "无人机控制器")
|
|
|
+@RestController
|
|
|
+@RequestMapping("uavController")
|
|
|
+public class uavController {
|
|
|
+
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(uavController.class);
|
|
|
+
|
|
|
+ @Value("${sooka.uav_server.username}")
|
|
|
+ private String username;
|
|
|
+
|
|
|
+ @Value("${sooka.uav_server.password}")
|
|
|
+ private String password;
|
|
|
+
|
|
|
+ private static final String REQUEST_URL = "https://47.93.50.30/etkqq/login";
|
|
|
+
|
|
|
+ @GetMapping("/getToken")
|
|
|
+ public String getToken() {
|
|
|
+
|
|
|
+ try (CloseableHttpClient httpClient = createCustomHttpClient()) {
|
|
|
+ HttpPost httpPost = new HttpPost(REQUEST_URL);
|
|
|
+ // 使用 JSON 处理库构建请求体
|
|
|
+ String requestBody = "{\"username\": \"" + username + "\", \"password\": \"" + password + "\"}";
|
|
|
+ StringEntity entity = new StringEntity(requestBody, StandardCharsets.UTF_8);
|
|
|
+ httpPost.setEntity(entity);
|
|
|
+ httpPost.setHeader("Content-Type", "application/json");
|
|
|
+
|
|
|
+ // 发送请求
|
|
|
+ HttpResponse response = httpClient.execute(httpPost);
|
|
|
+ int statusCode = response.getStatusLine().getStatusCode();
|
|
|
+ if (statusCode == 200) {
|
|
|
+ HttpEntity responseEntity = response.getEntity();
|
|
|
+ if (responseEntity != null) {
|
|
|
+ return EntityUtils.toString(responseEntity, StandardCharsets.UTF_8);
|
|
|
+ } else {
|
|
|
+ log.error("响应实体为空");
|
|
|
+ return "响应实体为空";
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ log.error("请求失败,状态码: {}", statusCode);
|
|
|
+ return "请求失败,状态码: " + statusCode;
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("请求发生异常: {}", e.getMessage(), e);
|
|
|
+ return "请求发生异常: " + e.getMessage();
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 工厂方法:创建自定义配置的 HttpClient
|
|
|
+ *
|
|
|
+ * @return 配置好的 CloseableHttpClient 实例
|
|
|
+ * @throws Exception 如果配置过程中发生错误
|
|
|
+ */
|
|
|
+ private CloseableHttpClient createCustomHttpClient() throws Exception {
|
|
|
+ // 设置请求超时配置
|
|
|
+ RequestConfig requestConfig = RequestConfig.custom()
|
|
|
+ .setConnectTimeout(5000) // 连接超时:5秒
|
|
|
+ .setSocketTimeout(10000) // 读取超时:10秒
|
|
|
+ .setConnectionRequestTimeout(3000) // 请求连接超时:3秒
|
|
|
+ .build();
|
|
|
+
|
|
|
+ // 配置 SSL 上下文(可根据需求启用或禁用 SSL 验证)
|
|
|
+ SSLContext sslContext = SSLContextBuilder.create()
|
|
|
+ .loadTrustMaterial((chain, authType) -> true) // 忽略证书验证(仅用于测试环境)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ return HttpClients.custom()
|
|
|
+ .setDefaultRequestConfig(requestConfig)
|
|
|
+ .setSSLContext(sslContext)
|
|
|
+ .setSSLHostnameVerifier(new NoopHostnameVerifier()) // 忽略主机名验证(仅用于测试环境)
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+}
|