|
|
@@ -1,142 +1,279 @@
|
|
|
package com.jyxt.getdatabyrestful;
|
|
|
|
|
|
-import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
-import com.jyxt.getdatabyrestful.service.ApiClientService;
|
|
|
-import com.jyxt.getdatabyrestful.view.repository.TemperatureEntityRepository;
|
|
|
+import com.jyxt.getdatabyrestful.service.IBPSService;
|
|
|
+import com.jyxt.getdatabyrestful.service.PlatformService;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Objects;
|
|
|
+import com.fasterxml.jackson.databind.node.ArrayNode;
|
|
|
+import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
+
|
|
|
+import java.time.LocalTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.*;
|
|
|
|
|
|
@Component
|
|
|
public class HandleData {
|
|
|
private static final Logger log = LoggerFactory.getLogger(GetDataByRESTfulApplication.class);
|
|
|
|
|
|
- private final ObjectMapper objectMapper = new ObjectMapper(); // 修复点:声明并初始化
|
|
|
+ private static final ObjectMapper mapper = new ObjectMapper();
|
|
|
|
|
|
@Autowired
|
|
|
- private ApiClientService apiClientService;
|
|
|
+ PlatformService platformService;
|
|
|
|
|
|
@Autowired
|
|
|
- private TemperatureEntityRepository temperatureEntityRepository;
|
|
|
-
|
|
|
- @Value("${UrlAddr}")
|
|
|
- private String UrlAddr;
|
|
|
-
|
|
|
- @Value("${loginKey}")
|
|
|
- private String loginKey;
|
|
|
-
|
|
|
- public void startHandleData(String ID,String Bianzhishijian,String EntityName) {
|
|
|
- log.info("ID: {}, Bianzhishijian: {}, EntityName: {}", ID, Bianzhishijian, EntityName);
|
|
|
-// String retVal = apiClientService.sendGet("https://petstore-demo.apifox.com/pet/1");
|
|
|
-// System.out.println(retVal);
|
|
|
- System.out.println("---------------------------------------------------------------");
|
|
|
- Map<String, Object> params = new HashMap<>();
|
|
|
- params.put("loginName", "h241021fkj"); // 替换为实际值
|
|
|
- params.put("password", "h241021fkj"); // 替换为实际值
|
|
|
- String retVal2 = apiClientService.sendPost(UrlAddr+"/api/getToken",params);
|
|
|
- System.out.println(retVal2);
|
|
|
-// apiClientService.callThirdPartyApi(ID, Bianzhishijian, EntityName)
|
|
|
-// .doOnNext(response -> {
|
|
|
-// if(response.isSuccess()) {
|
|
|
-// log.info("处理成功数据: {}", response.getData());
|
|
|
-// // 业务处理逻辑
|
|
|
-// } else {
|
|
|
-// log.warn("业务异常: {}", response.getMessage());
|
|
|
-// }
|
|
|
-// })
|
|
|
-// .doOnError(e -> log.error("处理失败", e))
|
|
|
-// .subscribe(
|
|
|
-// response -> {}, // 成功处理(通常留空,因已用 doOnNext)
|
|
|
-// error -> log.error("全局捕获:", error) // 兜底异常处理
|
|
|
-// );
|
|
|
+ IBPSService ibpsService;
|
|
|
|
|
|
+ public void startHandleData() {
|
|
|
+ log.info("---------------------------------------------------------------");
|
|
|
+ List<Map<String, String>> platformList = platformService.getAllData();
|
|
|
+ List<Map<String,Object>> configList = ibpsService.getConfList();
|
|
|
+ List<Map<String,Object>> undoList = ibpsService.getUndoList();
|
|
|
+ for(Map<String,Object> undoMap : undoList){
|
|
|
+ String areaName = undoMap.get("qu_yu_ming_cheng_").toString();
|
|
|
+ String RoomNo = undoMap.get("fang_jian_hao_").toString();
|
|
|
+ Map<String,Object> configMap = getConfMap(configList,areaName,RoomNo);
|
|
|
+ if (configMap.size() != 0){
|
|
|
+ String collectID = configMap.get("cai_ji_qi_id_").toString();
|
|
|
+ List<Map<String, String>> curPlatformList = getCurPlatformList(collectID,platformList);
|
|
|
+ if (curPlatformList.size() != 0){
|
|
|
+ String id = undoMap.get("id_").toString();
|
|
|
+ String JsonStr = undoMap.get("lie_biao_shu_ju_").toString();
|
|
|
+ String timeSlot = getTimePeriod("1");
|
|
|
+ String retJsonStr = handJsonStr(JsonStr,curPlatformList,timeSlot);
|
|
|
+ String JsonFlag = checkJson(retJsonStr);
|
|
|
+ String timeStr = getTimePeriod("0");
|
|
|
+ String retStr = ibpsService.UpdateData("t_snwsdjkjlb",id,retJsonStr,JsonFlag, timeSlot, timeStr);
|
|
|
+ if (retStr.equals("1")){
|
|
|
+ log.info("update success");
|
|
|
+ } else {
|
|
|
+ log.info("update fail:"+retStr);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- public void getAllData(){
|
|
|
-// System.out.println("---------------------------------------------------------------");
|
|
|
- String urlStr = UrlAddr+"api/getToken?"+loginKey;
|
|
|
- log.info("1:url: {}", urlStr);
|
|
|
- String retVal = apiClientService.sendGet(urlStr);
|
|
|
- log.info("1:retVal: {}", retVal);
|
|
|
- String token = "";
|
|
|
- try {
|
|
|
- // 解析JSON字符串
|
|
|
- ObjectMapper mapper = new ObjectMapper();
|
|
|
- Map<String, Object> response = mapper.readValue(retVal, new TypeReference<Map<String, Object>>() {});
|
|
|
+ public Map<String,Object> getConfMap(List<Map<String, Object>> configList, String areaName, String RoomNo){
|
|
|
+ Map<String,Object> configMap = new HashMap<>();
|
|
|
+ for(Map<String, Object> config : configList){
|
|
|
+ String curAreaName = config.get("qu_yu_").toString();
|
|
|
+ String curRoomNo = config.get("fang_jian_").toString();
|
|
|
+ if(areaName.equals(curAreaName) && RoomNo.equals(curRoomNo)){
|
|
|
+ configMap = config;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return configMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<Map<String, String>> getCurPlatformList(String collectID, List<Map<String, String>> platformList){
|
|
|
+ List<Map<String, String>> retList = new ArrayList<>();
|
|
|
+ for(Map<String, String> platformMap : platformList){
|
|
|
+ String curCollectID = platformMap.get("deviceAddr").toString();
|
|
|
+ if(curCollectID.equals(collectID)){
|
|
|
+ retList.add(platformMap);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return retList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @ClassName HandleData
|
|
|
+ * @Author huangws
|
|
|
+ * @Description 模式0:返回当前时间(HH:mm格式)
|
|
|
+ * 模式1:返回时间段
|
|
|
+ * @Date 2025/8/11 16:46
|
|
|
+ **/
|
|
|
+ public static String getTimePeriod(String timeType) {
|
|
|
+ LocalTime now = LocalTime.now();
|
|
|
+
|
|
|
+ // 模式0:返回当前时间(HH:mm格式)
|
|
|
+ if ("0".equals(timeType)) {
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
|
|
|
+ return now.format(formatter);
|
|
|
+ }
|
|
|
+ // 模式1:返回时间段
|
|
|
+ else if ("1".equals(timeType)) {
|
|
|
+ LocalTime morningStart = LocalTime.of(6, 0); // 6:00
|
|
|
+ LocalTime afternoonStart = LocalTime.of(12, 0); // 12:00
|
|
|
+ LocalTime eveningStart = LocalTime.of(18, 0); // 18:00
|
|
|
|
|
|
- // 提取data字段
|
|
|
- Map<String, Object> data = (Map<String, Object>) response.get("data");
|
|
|
- if (data != null) {
|
|
|
- token = (String) data.get("token"); // 提取token值
|
|
|
- log.info("Extracted token: {}", token);
|
|
|
+ if (now.isAfter(morningStart) && now.isBefore(afternoonStart)) {
|
|
|
+ return "上午";
|
|
|
+ } else if (now.equals(afternoonStart) ||
|
|
|
+ (now.isAfter(afternoonStart) && now.isBefore(eveningStart))) {
|
|
|
+ return "下午";
|
|
|
} else {
|
|
|
- log.error("'data' field is missing in response");
|
|
|
+ return "其他时段";
|
|
|
}
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("JSON parsing failed", e);
|
|
|
}
|
|
|
- if (!token.isEmpty()) {
|
|
|
- log.info("2: Requesting group list with token in header");
|
|
|
- // 设置请求头
|
|
|
- Map<String, String> headers = new HashMap<>();
|
|
|
- headers.put("authorization", token); // 关键修改:使用header传递token
|
|
|
-
|
|
|
- // 发送带header的GET请求
|
|
|
- retVal = apiClientService.sendGetWithHeaders(
|
|
|
- UrlAddr + "api/data/getRealTimeData",
|
|
|
- headers
|
|
|
- );
|
|
|
- log.info("2:retVal: {}", retVal);
|
|
|
- try {
|
|
|
- // 解析JSON响应
|
|
|
- JsonNode rootNode = objectMapper.readTree(retVal);
|
|
|
-
|
|
|
- // 检查状态码或message
|
|
|
- if (rootNode.has("message") && "获取成功".equals(rootNode.get("message").asText())) {
|
|
|
- JsonNode dataNode = rootNode.get("data");
|
|
|
-
|
|
|
- if (dataNode.isArray()) {
|
|
|
- for (JsonNode groupNode : dataNode) {
|
|
|
- String deviceName = groupNode.has("deviceName")?groupNode.get("deviceName").asText() : "未知设备";
|
|
|
- String deviceAddr = groupNode.has("deviceAddr") ?String.valueOf(groupNode.get("deviceAddr").asInt()) : "未知地址";
|
|
|
- String deviceStatus = groupNode.has("deviceStatus")?groupNode.get("deviceStatus").asText() : "未知状态";
|
|
|
- JsonNode dataItem = groupNode.get("dataItem");
|
|
|
-
|
|
|
- if(!Objects.equals(dataItem.toString(), "null") && dataItem.isArray() && Objects.equals(deviceStatus, "normal")) {
|
|
|
- for (JsonNode registerItem : dataItem) {
|
|
|
- if (registerItem.has("registerItem") && registerItem.get("registerItem").isArray()) {
|
|
|
- for (JsonNode item : registerItem.get("registerItem")) {
|
|
|
- String registerName = item.has("registerName")?item.get("registerName").asText() : "未命名寄存器";
|
|
|
- String data = item.has("data")?item.get("data").asText() : "无数据";
|
|
|
- log.info("deviceName: {}, deviceAddr: {}, registerName: {}, data: {}",deviceName, deviceAddr, registerName, data);
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
+ // 处理无效参数
|
|
|
+ else {
|
|
|
+ throw new IllegalArgumentException("无效参数: 只支持\"0\"或\"1\"");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 20250619 huangws
|
|
|
+ * 处理json串,将获得的实际结果填入json串再返回
|
|
|
+ * @param JsonStr
|
|
|
+ * @param curPlatformList
|
|
|
+ * @param timeSlot
|
|
|
+ * @return throws JsonProcessingException
|
|
|
+ * @throws JsonProcessingException
|
|
|
+ */
|
|
|
+ public String handJsonStr(String JsonStr, List<Map<String, String>> curPlatformList, String timeSlot) {
|
|
|
+ String retVal = JsonStr;
|
|
|
+ log.info("org json string:" + JsonStr);
|
|
|
+ log.info("curPlatformList size: " + (curPlatformList != null ? curPlatformList.size() : 0));
|
|
|
+
|
|
|
+ if (curPlatformList == null || curPlatformList.isEmpty()) {
|
|
|
+ log.warn("curPlatformList is empty, return original JSON");
|
|
|
+ return retVal;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ JsonNode rootNode = mapper.readTree(JsonStr);
|
|
|
+
|
|
|
+ if (rootNode.isArray()) {
|
|
|
+ ArrayNode arrayNode = (ArrayNode) rootNode;
|
|
|
+
|
|
|
+ // 遍历所有平台数据
|
|
|
+ for (Map<String, String> platformMap : curPlatformList) {
|
|
|
+ String registerName = platformMap.get("registerName");
|
|
|
+ String dataValue = platformMap.get("data");
|
|
|
+
|
|
|
+ if (registerName == null || dataValue == null) {
|
|
|
+ log.warn("Missing registerName or data in platformMap: " + platformMap);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ String targetLabel = timeSlot + registerName; // 如"上午温度"
|
|
|
+ boolean found = false;
|
|
|
+
|
|
|
+ // 在JSON数组中查找匹配项
|
|
|
+ for (int i = 0; i < arrayNode.size(); i++) {
|
|
|
+ JsonNode item = arrayNode.get(i);
|
|
|
+ if (!item.isObject()) continue;
|
|
|
+
|
|
|
+ ObjectNode objNode = (ObjectNode) item;
|
|
|
+ String label = objNode.path("label").asText("");
|
|
|
+
|
|
|
+ if (targetLabel.equals(label)) {
|
|
|
+ found = true;
|
|
|
+
|
|
|
+ // 更新value字段
|
|
|
+ objNode.put("value", dataValue);
|
|
|
+
|
|
|
+ // 处理fixValue和result
|
|
|
+ String fixValueStr = objNode.path("fixValue").asText("");
|
|
|
+ double dataVal = parseDoubleSafe(dataValue, 0.0);
|
|
|
+ double resultVal = dataVal;
|
|
|
+
|
|
|
+ if (!fixValueStr.isEmpty()) {
|
|
|
+ double fixVal = parseDoubleSafe(fixValueStr, 0.0);
|
|
|
+ resultVal = dataVal + fixVal;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新result字段
|
|
|
+ objNode.put("result", String.valueOf(resultVal));
|
|
|
+
|
|
|
+ // 处理范围检查
|
|
|
+ JsonNode rangeNode = objNode.path("range");
|
|
|
+ if (rangeNode.isArray() && rangeNode.size() >= 2) {
|
|
|
+ double min = parseDoubleSafe(rangeNode.get(0).asText(), Double.MIN_VALUE);
|
|
|
+ double max = parseDoubleSafe(rangeNode.get(1).asText(), Double.MAX_VALUE);
|
|
|
+
|
|
|
+ if (min != Double.MIN_VALUE && max != Double.MAX_VALUE) {
|
|
|
+ String status = (resultVal >= min && resultVal <= max) ? "正常" : "失控";
|
|
|
+ objNode.put("status", status);
|
|
|
+ } else {
|
|
|
+ objNode.put("status", "范围数据错误");
|
|
|
+ log.warn("Invalid range data for label: " + label);
|
|
|
}
|
|
|
+ } else {
|
|
|
+ objNode.put("status", "范围数据缺失");
|
|
|
+ log.warn("Missing range data for label: " + label);
|
|
|
}
|
|
|
+
|
|
|
+ break; // 找到匹配项后跳出内层循环
|
|
|
}
|
|
|
- } else {
|
|
|
- log.warn("Data field is not an array!");
|
|
|
}
|
|
|
- } else {
|
|
|
- String errMsg = rootNode.has("message") ?
|
|
|
- rootNode.get("message").asText() : "Unknown error";
|
|
|
- log.error("API request failed: {}", errMsg);
|
|
|
+
|
|
|
+ if (!found) {
|
|
|
+ log.warn("No matching label found for: " + targetLabel);
|
|
|
+ }
|
|
|
}
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("JSON parsing failed", e);
|
|
|
+
|
|
|
+ retVal = mapper.writeValueAsString(rootNode);
|
|
|
}
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("处理JSON时发生错误: " + e.getMessage(), e);
|
|
|
}
|
|
|
+
|
|
|
+ return retVal;
|
|
|
}
|
|
|
|
|
|
+ private double parseDoubleSafe(String value, double defaultValue) {
|
|
|
+ if (value == null || value.isEmpty()) {
|
|
|
+ return defaultValue;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return Double.parseDouble(value);
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ log.warn("数值格式错误: " + value + ", 使用默认值: " + defaultValue);
|
|
|
+ return defaultValue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @ClassName HandleData
|
|
|
+ * @Author huangws
|
|
|
+ * @Description
|
|
|
+ * @Date 2025/8/11 14:57
|
|
|
+ **/
|
|
|
+ public String checkJson(String retJsonStr) {
|
|
|
+ boolean allResultsValid = true;
|
|
|
+ boolean hasOutOfControl = false;
|
|
|
+
|
|
|
+ try {
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ JsonNode rootNode = mapper.readTree(retJsonStr);
|
|
|
+
|
|
|
+ if (rootNode.isArray()) {
|
|
|
+ for (JsonNode item : rootNode) {
|
|
|
+ // 检查result字段是否有效
|
|
|
+ JsonNode resultNode = item.get("result");
|
|
|
+ if (resultNode == null || !resultNode.isTextual() || resultNode.asText().isEmpty()) {
|
|
|
+ allResultsValid = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查status字段是否为"失控"
|
|
|
+ JsonNode statusNode = item.get("status");
|
|
|
+ if (statusNode != null && statusNode.isTextual()) {
|
|
|
+ String status = statusNode.asText();
|
|
|
+ if ("失控".equals(status)) {
|
|
|
+ hasOutOfControl = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("校验JSON完成状态时发生错误: " + e.getMessage(), e);
|
|
|
+ allResultsValid = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构建返回结果
|
|
|
+ String completionStatus = allResultsValid ? "1" : "0";
|
|
|
+ String controlStatus = hasOutOfControl ? "1" : "0";
|
|
|
+ return completionStatus + "^" + controlStatus;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|