|
|
|
@ -2,7 +2,10 @@ package cn.iocoder.yudao.module.alert.utils; |
|
|
|
|
|
|
|
import cn.hutool.core.lang.TypeReference; |
|
|
|
import cn.hutool.core.util.StrUtil; |
|
|
|
import cn.hutool.http.HttpResponse; |
|
|
|
import cn.hutool.http.HttpStatus; |
|
|
|
import cn.hutool.http.HttpUtil; |
|
|
|
import cn.hutool.json.JSONObject; |
|
|
|
import cn.hutool.json.JSONUtil; |
|
|
|
import cn.iocoder.yudao.module.alert.controller.admin.exa.vo.*; |
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
@ -29,6 +32,10 @@ import java.lang.reflect.Type; |
|
|
|
import java.net.URISyntaxException; |
|
|
|
import java.nio.charset.Charset; |
|
|
|
import java.util.*; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
|
|
|
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.EXA_HISTORY_FAILED; |
|
|
|
|
|
|
|
public class EXAUtils { |
|
|
|
|
|
|
|
@ -187,7 +194,7 @@ public class EXAUtils { |
|
|
|
* @return exa列表 |
|
|
|
* [[1698369232000,0.0],[1698369332000,0.0],[1698369432000,0.0],[1698369532000,0.0],[1698369632000,0.0],[1698369732000,0.0],[1698369832000,0.0],[1698369932000,0.0],[1698370032000,0.0],[1698370132000,0.0],[1698370232000,0.0],[1698370332000,0.0],[1698370432000,0.0],[1698370532000,0.0],[1698370632000,0.0],[1698370732000,0.0],[1698370832000,0.0],[1698370932000,0.0]] |
|
|
|
*/ |
|
|
|
public List<List<Double>> getHistory(String exaIp, EXAHistoryReqVO req) { |
|
|
|
public List<List<Double>> getHistory_old(String exaIp, EXAHistoryReqVO req) { |
|
|
|
List<List<Double>> result = Collections.emptyList(); |
|
|
|
|
|
|
|
try { |
|
|
|
@ -220,14 +227,92 @@ public class EXAUtils { |
|
|
|
body = StrUtil.unWrap(body, '\"'); |
|
|
|
|
|
|
|
// 反序列化
|
|
|
|
result = JSONUtil.toBean(body, new TypeReference<List<List<Double>>>() {}, true); |
|
|
|
result = JSONUtil.toBean(body, new TypeReference<List<List<Double>>>() {},true); |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
System.err.println("请求历史数据失败:" + e.getMessage()); |
|
|
|
throw exception(EXA_HISTORY_FAILED,"请求历史数据失败:" + e.getMessage()); |
|
|
|
} |
|
|
|
|
|
|
|
return result; |
|
|
|
} |
|
|
|
/** |
|
|
|
* 根据点号、开始时间、结束时间查询历史数据 |
|
|
|
* |
|
|
|
* @param exaHistoryReqVo 传入对象,点号、开始时间、结束时间 |
|
|
|
* itemName 单点号 |
|
|
|
* @return exa列表 |
|
|
|
* [[1698369232000,0.0],[1698369332000,0.0],[1698369432000,0.0],[1698369532000,0.0],[1698369632000,0.0],[1698369732000,0.0],[1698369832000,0.0],[1698369932000,0.0],[1698370032000,0.0],[1698370132000,0.0],[1698370232000,0.0],[1698370332000,0.0],[1698370432000,0.0],[1698370532000,0.0],[1698370632000,0.0],[1698370732000,0.0],[1698370832000,0.0],[1698370932000,0.0]] |
|
|
|
*/ |
|
|
|
public List<List<Double>> getHistory(String exaIp, EXAHistoryReqVO req) { |
|
|
|
|
|
|
|
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()); |
|
|
|
|
|
|
|
HttpResponse response = HttpUtil.createGet(url) |
|
|
|
.form(paramMap) |
|
|
|
.header("Content-Type", "application/json") |
|
|
|
.timeout(5000) |
|
|
|
.execute(); |
|
|
|
|
|
|
|
String body = response.body(); |
|
|
|
System.out.println("服务端返回的数据为:" + body); |
|
|
|
|
|
|
|
// 1️⃣ HTTP 状态码校验
|
|
|
|
if (response.getStatus() != HttpStatus.HTTP_OK) { |
|
|
|
throw exception( |
|
|
|
EXA_HISTORY_FAILED, |
|
|
|
"EXA 接口请求失败,HTTP 状态码:" + response.getStatus() + ",响应:" + body |
|
|
|
); |
|
|
|
} |
|
|
|
// 1️⃣ 如果被包成字符串,先解包
|
|
|
|
if (body.startsWith("\"")) { |
|
|
|
body = StrUtil.replace(body, "\\", ""); |
|
|
|
body = StrUtil.unWrap(body, '\"'); |
|
|
|
} |
|
|
|
// 2️⃣ 如果是 JSON,先判断是否为错误响应
|
|
|
|
if (JSONUtil.isTypeJSONObject(body)) { |
|
|
|
JSONObject json = JSONUtil.parseObj(body); |
|
|
|
|
|
|
|
// RFC 9110 风格错误
|
|
|
|
if (json.containsKey("status") && json.containsKey("errors")) { |
|
|
|
String title = json.getStr("title"); |
|
|
|
Integer status = json.getInt("status"); |
|
|
|
JSONObject errors = json.getJSONObject("errors"); |
|
|
|
|
|
|
|
String errorMsg = errors.entrySet().stream() |
|
|
|
.map(e -> e.getKey() + ": " + e.getValue()) |
|
|
|
.collect(Collectors.joining("; ")); |
|
|
|
|
|
|
|
throw exception( |
|
|
|
EXA_HISTORY_FAILED, |
|
|
|
String.format("EXA 接口校验失败(status=%d):%s,%s", |
|
|
|
status, title, errorMsg) |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 4️⃣ 反序列化业务数据
|
|
|
|
try { |
|
|
|
return JSONUtil.toBean( |
|
|
|
body, |
|
|
|
new TypeReference<List<List<Double>>>() {}, |
|
|
|
true |
|
|
|
); |
|
|
|
} catch (Exception e) { |
|
|
|
throw exception( |
|
|
|
EXA_HISTORY_FAILED, |
|
|
|
"EXA 返回数据格式异常,无法解析为历史数据,body=" + body |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 批量查询历史数据
|
|
|
|
/** |
|
|
|
|