|
@@ -1,7 +1,12 @@
|
|
|
package com.jwkj.qsydw.controller;
|
|
|
|
|
|
+import com.alibaba.fastjson2.JSON;
|
|
|
+import com.jwkj.qsydw.services.HttpClientService;
|
|
|
import com.jwkj.qsydw.vo.Params;
|
|
|
import com.jwkj.qsydw.vo.PointPosition;
|
|
|
+import com.jwkj.qsydw.vo.ReadResults;
|
|
|
+import com.jwkj.qsydw.vo.wrapper.Wrapper;
|
|
|
+import jakarta.annotation.Resource;
|
|
|
import org.apache.http.HttpEntity;
|
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
@@ -9,12 +14,14 @@ import org.apache.http.entity.StringEntity;
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
import org.apache.http.impl.client.HttpClientBuilder;
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
+import org.nlsd.common.BaseResponse;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* @author Xu·LinPeng
|
|
@@ -26,6 +33,8 @@ public class ApiController {
|
|
|
Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
|
|
|
PointPosition pointPosition = new PointPosition();
|
|
|
+ @Resource
|
|
|
+ HttpClientService httpClientService;
|
|
|
|
|
|
@GetMapping("/data")
|
|
|
public String data(String params) {
|
|
@@ -76,81 +85,32 @@ public class ApiController {
|
|
|
}
|
|
|
|
|
|
@PostMapping("/data")
|
|
|
- public String dataV2(Params params) {
|
|
|
+ public BaseResponse<List<ReadResults>> dataV2(@RequestBody Params params) {
|
|
|
|
|
|
- String var1 = params.getPointPosition();
|
|
|
- String var2 = params.getPointPositions();
|
|
|
- if (var1 !=null && var2==null){
|
|
|
- return httpService(var1);
|
|
|
- }
|
|
|
- if (var1 == null && var2 != null){
|
|
|
- return httpService(var2);
|
|
|
- }
|
|
|
- if (var1 != null){
|
|
|
- var1 = var1.substring(0, var1.length() - 1);
|
|
|
- var2 = var2.substring(0, var2.length() - 1);
|
|
|
- var2 = var2.substring(1);
|
|
|
- var1+= ","+var2+"]";
|
|
|
- logger.info(var1);
|
|
|
- return httpService(var1);
|
|
|
- }
|
|
|
+ String var = params.getPointPositions();
|
|
|
|
|
|
- return "查出的信息为null";
|
|
|
+ if (var == null || var.isEmpty()){
|
|
|
+ return new BaseResponse<>(400,"Bad Request",null,"参数不正确");
|
|
|
+ }
|
|
|
+ String res = httpClientService.httpService(var);
|
|
|
+ Wrapper wrapper = JSON.parseObject(res, Wrapper.class);
|
|
|
+ List<ReadResults> readResultsList = wrapper.getReadResults();
|
|
|
+ return new BaseResponse<>(200,"OK",readResultsList);
|
|
|
}
|
|
|
|
|
|
- public String httpService(String params) {
|
|
|
- CloseableHttpClient httpClient = HttpClientBuilder.create().build();
|
|
|
-
|
|
|
- // 创建Post请求
|
|
|
- HttpPost httpPost = new HttpPost("http://10.71.141.78:39320/iotgateway/read");
|
|
|
-
|
|
|
-
|
|
|
- HttpEntity httpEntity = new StringEntity(params, StandardCharsets.UTF_8);
|
|
|
+ @PostMapping("dataV2")
|
|
|
+ public BaseResponse<List<ReadResults>> test(@RequestBody Params params) {
|
|
|
|
|
|
- httpPost.setEntity(httpEntity);
|
|
|
-
|
|
|
- // 设置ContentType(注:如果只是传普通参数的话,ContentType不一定非要用application/json)
|
|
|
- httpPost.setHeader("Content-Type", "application/json;charset=utf8");
|
|
|
-
|
|
|
- // 响应模型
|
|
|
- CloseableHttpResponse response = null;
|
|
|
- try {
|
|
|
- response = httpClient.execute(httpPost);
|
|
|
- HttpEntity responseEntity = response.getEntity();
|
|
|
+ String var = params.getPointPosition();
|
|
|
|
|
|
- System.out.println("响应状态为:" + response.getStatusLine());
|
|
|
- if (responseEntity != null) {
|
|
|
- return EntityUtils.toString(responseEntity);
|
|
|
- }
|
|
|
- } catch (IOException e) {
|
|
|
- logger.error(e.getMessage());
|
|
|
- } finally {
|
|
|
- try {
|
|
|
- // 释放资源
|
|
|
- if (httpClient != null) {
|
|
|
- httpClient.close();
|
|
|
- }
|
|
|
- if (response != null) {
|
|
|
- response.close();
|
|
|
- }
|
|
|
- } catch (IOException e) {
|
|
|
- logger.error(e.getMessage());
|
|
|
- }
|
|
|
+ if (var == null || var.isEmpty()){
|
|
|
+ return new BaseResponse<>(400,"Bad Request",null,"没有匹配项");
|
|
|
}
|
|
|
- return "HttpService ERROR!!!";
|
|
|
- }
|
|
|
-
|
|
|
- @PostMapping("test")
|
|
|
- public void test(@RequestBody Params params) {
|
|
|
-
|
|
|
- dataV2(params);
|
|
|
+ String res = httpClientService.httpService(var);
|
|
|
+ Wrapper wrapper = JSON.parseObject(res, Wrapper.class);
|
|
|
+ List<ReadResults> readResultsList = wrapper.getReadResults();
|
|
|
+ return new BaseResponse<>(200,"OK",readResultsList);
|
|
|
|
|
|
-// String pointPosition1 = pointPosition.getPointPosition(params);
|
|
|
-// System.out.println(pointPosition1);
|
|
|
-// if (pointPosition1 == null || pointPosition1.isEmpty()){
|
|
|
-// return "暂无参数的匹配项目!!! :(";
|
|
|
-// }
|
|
|
-// return pointPosition.getPointPosition(params);
|
|
|
}
|
|
|
|
|
|
|