Browse Source

refactor(alert): 重构EXA历史数据获取逻辑

- 使用Hutool工具类简化HTTP请求处理
- 替换原有的Apache HttpClient实现
- 优化参数拼接与URL构建方式
- 改进JSON反序列化处理逻辑
- 增强异常处理机制
- 清理无用的导入包并统一使用通配符导入
pull/30/head
chenjiale 1 month ago
parent
commit
b29694a1f0
  1. 82
      yudao-module-alert/yudao-module-alert-biz/src/main/java/cn/iocoder/yudao/module/alert/utils/EXAUtils.java

82
yudao-module-alert/yudao-module-alert-biz/src/main/java/cn/iocoder/yudao/module/alert/utils/EXAUtils.java

@ -1,5 +1,9 @@
package cn.iocoder.yudao.module.alert.utils;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.module.alert.controller.admin.exa.vo.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
@ -24,9 +28,7 @@ import java.io.IOException;
import java.lang.reflect.Type;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.*;
public class EXAUtils {
@ -177,57 +179,45 @@ public class EXAUtils {
* @param exaHistoryReqVo 传入对象点号开始时间结束时间
* @return exa列表
*/
public List<List<Double>> getHistory(String EXA_IP, EXAHistoryReqVO exaHistoryReqVo) {
List<List<Double>> result = new ArrayList<>();
try {
// 目标 RPC 服务的 URL
// String url = "http://"+EXA_IP+":9000/exawebapi/exatime/GetRawValueArrayFloat";
String url = "http://" + EXA_IP + ":9000/exawebapi/exatime/GetSamplingValueArrayFloat";
public List<List<Double>> getHistory(String exaIp, EXAHistoryReqVO req) {
List<List<Double>> result = Collections.emptyList();
try {
String url = String.format(
"http://%s:9000/exawebapi/EXATime/GetSamplingValueArrayFloat",
exaIp
);
// 拼接参数
HashMap<String, Object> paramMap = new HashMap<>();
paramMap.put("ItemName", req.getItemName());
paramMap.put("StartingTime", req.getStartTime());
paramMap.put("TerminalTime", req.getEndTime());
paramMap.put("SamplingPeriod", 1000 * req.getInterval());
// 发送 GET 请求
String body = HttpUtil.createGet(url)
.form(paramMap)
.header("Content-Type", "application/json")
.timeout(5000)
.execute()
.body();
//创建HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
/*
* 由于GET请求的参数都是拼装在URL地址后方所以我们要构建一个URL带参数
*/
URIBuilder uriBuilder = new URIBuilder(url);
/** 添加参数 */
uriBuilder.addParameter("ItemName", exaHistoryReqVo.getItemName());
uriBuilder.addParameter("StartingTime", exaHistoryReqVo.getStartTime());
uriBuilder.addParameter("TerminalTime", exaHistoryReqVo.getEndTime());
uriBuilder.addParameter("SamplingPeriod", String.valueOf(1000 * exaHistoryReqVo.getInterval()));
System.out.println("服务端返回的数据为:" + body);
// 处理转义字符 \\
body = StrUtil.replace(body, "\\", "");
//创建请求对象
HttpGet httpGet = new HttpGet(uriBuilder.build());
// 传输的类型
httpGet.addHeader("Content-Type", "application/json");
//发送请求,请求响应结果
CloseableHttpResponse response = httpClient.execute(httpGet);
//获取服务器返回的状态码
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("服务端返回成功的状态码为:" + statusCode);
HttpEntity entity = response.getEntity();
String body = EntityUtils.toString(entity).replaceAll("\\\\", "");
System.out.println("服务端返回的数据为:" + body);
// 去除首尾双引号
body = StrUtil.unWrap(body, '\"');
if (body.startsWith("\"") && body.endsWith("\"")) {
// 步骤3:去掉双引号
body = body.substring(1, body.length() - 1); // 去掉首尾的双引号
}
Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting().create();
Type listType = new TypeToken<List<List<Double>>>() {
}.getType();
result = gson.fromJson(body, listType);
// 反序列化
result = JSONUtil.toBean(body, new TypeReference<List<List<Double>>>() {}, true);
//关闭资源
response.close();
httpClient.close();
} catch (Exception e) {
System.err.println("请求历史数据失败:" + e.getMessage());
}
return result;
}

Loading…
Cancel
Save