From c65ab218b14e87489f1594b2d932f7bd54b3ba11 Mon Sep 17 00:00:00 2001
From: zouyu <2723363702@qq.com>
Date: 星期五, 17 十月 2025 13:34:19 +0800
Subject: [PATCH] 数采调整
---
cnas-device/src/main/java/com/ruoyi/device/utils/DataAcquisition.java | 223 ++++++++++++++++++++++++++++++++++++++++++++++++++++---
1 files changed, 210 insertions(+), 13 deletions(-)
diff --git a/cnas-device/src/main/java/com/ruoyi/device/utils/DataAcquisition.java b/cnas-device/src/main/java/com/ruoyi/device/utils/DataAcquisition.java
index 02ec469..751b778 100644
--- a/cnas-device/src/main/java/com/ruoyi/device/utils/DataAcquisition.java
+++ b/cnas-device/src/main/java/com/ruoyi/device/utils/DataAcquisition.java
@@ -7,7 +7,11 @@
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
+import com.ruoyi.common.core.domain.Result;
+import com.ruoyi.common.utils.RedisUtil;
+import com.ruoyi.device.constant.DCResistanceMqttConstants;
import com.ruoyi.device.dto.DeviceCollectionDto;
+import com.ruoyi.device.mapper.CollectBridgeMapper;
import com.ruoyi.device.pojo.DataConfig;
import com.ruoyi.device.pojo.Device;
import com.ruoyi.framework.exception.ErrorException;
@@ -24,6 +28,8 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;
+import static com.ruoyi.framework.datasource.DynamicDataSourceContextHolder.log;
+
public class DataAcquisition {
private static final String HTTP = "http://";
@@ -36,6 +42,8 @@
public static final String frequency = "frequency";
+ private CollectBridgeMapper collectBridgeMapper;
+
/**
* 鏁伴噰鍏ュ彛
*
@@ -43,7 +51,7 @@
* @param device
* @return
*/
- public static Map<String, Object> dataAcquisitionEntrance(List<DataConfig> dataConfig, Device device, String entrustCode, String sampleCode, String ip, String cableTag) {
+ public static Map<String, Object> dataAcquisitionEntrance(List<DataConfig> dataConfig, Device device, String entrustCode, String sampleCode, String ip, String cableTag, String dbUserName, String dbPassword,String dbTable) {
// 鍒ゆ柇鏄惁鏄奖鍍忔祴閲忎华
if (device.getManagementNumber().equals("JCZX-ZB-OP07001")) {
if (device.getFileType().equals(".xlsx")) {
@@ -69,19 +77,24 @@
* mdbEntrustCode mdb鏂囦欢闇�瑕侊細濮旀墭缂栧彿瀛楁 涓轰粈涔堟病鏈夊幓杩欎釜mdb鍓嶇紑鍛紵鍥犱负宸茬粡缁欏鎴风殑閮ㄥ垎鐢佃剳涓婂畨瑁呬簡閲囬泦鍣紝鑰岀敤鎴蜂笉鎺ュ彈閲嶆柊瀹夎閲囬泦鍣紝鎵�浠ュ氨娌℃湁鍘婚櫎
* mdbSampleCode mdb鏂囦欢闇�瑕侊細鏍峰搧缂栧彿瀛楁
*/
+ String mdbEntrustCode = StringUtils.isNotBlank(device.getEntrustCode())?device.getEntrustCode():"";
+ String mdbSampleCode = StringUtils.isNotBlank(device.getSampleCode())?device.getSampleCode():"";
+ String dbFileName = StringUtils.isNotBlank(device.getDbFileName())?device.getDbFileName():"";
String http = HTTP + ip + GETFILE +
"?filePath=" + device.getCollectUrl() +
"&fileExtension=" + device.getFileType() +
"&entrustCode=" + entrustCode +
"&sampleCode=" + sampleCode +
- "&mdbEntrustCode=" + device.getEntrustCode() +
- "&mdbSampleCode=" + device.getSampleCode() +
- "&dbFileName=" + device.getDbFileName();
+ "&mdbEntrustCode=" + mdbEntrustCode +
+ "&mdbSampleCode=" + mdbSampleCode +
+ "&dbFileName=" + dbFileName +
+ "&dbUserName=" + dbUserName +
+ "&dbPassword=" + dbPassword +
+ "&dbTable=" + dbTable;
System.out.println("璇锋眰鐨� URL: " + http);
String result = null;
try {
result = HttpUtil.get(http);
- System.out.println("璇锋眰杩斿洖缁撴灉: " + result);
} catch (IORuntimeException e) {
e.printStackTrace();
throw new ErrorException("鎵�鍦ㄧ數鑴戞湭瀹夎鎴栨湭鍚姩锛歀IMS鏂囦欢閲囬泦鍣紒");
@@ -94,7 +107,7 @@
throw new ErrorException(jsonObject.get("msg") + "");
}
} else {
- String data = jsonObject.get("data") + "";
+ String data = jsonObject.get("data") + "";
// 鑰冭檻鍒颁竴涓娴嬮」鍙兘浼氬瓨鍦ㄥ涓暟閲囬厤缃紝鎵�浠ラ渶瑕佽繘琛屽垎缁�
Map<String, List<DataConfig>> userMap = dataConfig.stream()
.peek(i -> {
@@ -117,7 +130,7 @@
map = analysisList(data, userMap, device, entrustCode, sampleCode);
break;
case ".xls":
- map = analysisString(data, userMap, device, entrustCode, sampleCode);
+ map = analysisTxt(data, userMap, device, entrustCode, sampleCode);
break;
case ".txt":
map = analysisTxt(data, userMap, device, entrustCode, sampleCode);
@@ -134,11 +147,19 @@
}
break;
case ".db":
+// map = analysisDb1(data, userMap, device);
+ map = analysisDb(data, userMap, device);
+ break;
+ case ".mysql":
+// map = analysisDb1(data, userMap, device);
map = analysisDb(data, userMap, device);
break;
case ".png":
map = readPngString(data, userMap, device);
break;
+// case ".mqtt":
+// map = getActualResistanceValueAsMap(userMap, device);
+// break;
default:
map = null;
break;
@@ -151,6 +172,7 @@
return map;
}
}
+
public static Map<String, Object> createFrequency(String entrustCode, String sampleCode, Map<String, Object> map) {
Set<String> set = new LinkedHashSet<>();
@@ -187,6 +209,56 @@
return "1";
}
+ private static Map<String, Object> analysisDb1(String data, Map<String, List<DataConfig>> dataConfig, Device device) {
+ JSONObject jsonObject = JSON.parseObject(data);
+ Map<String, Object> map = new HashMap<>();
+ if (jsonObject.isEmpty()) {
+ return map;
+ }
+ // 浠� JSON 鏁版嵁涓彁鍙� data 鏁扮粍
+ JSONArray dataList = JSONArray.parseArray(jsonObject.get("data").toString());
+ dataConfig.forEach((k, v) -> {
+ AtomicInteger numberOfDataEntries = new AtomicInteger();
+ List<Object> list = new ArrayList<>();
+ for (int config = 0; config < v.size(); config++) {
+ String refery = getRefer(v.get(config).getRefery());
+ for (int i = 0; i < dataList.size(); i++) {
+ JSONObject jsonObject1 = JSON.parseObject(dataList.get(i).toString());
+// if (ObjectUtils.isNotEmpty(jsonObject1.get(refery))) {
+// numberOfDataEntries.addAndGet(1);
+// list.add(jsonObject1.get(refery));
+// }
+ Object o = jsonObject1.get(refery);
+ if (ObjectUtils.isNotEmpty(o)) {
+ numberOfDataEntries.addAndGet(1);
+ list.add(o);
+ }
+ }
+ }
+ // 鎷兼帴鏁伴噰閰嶇疆
+ List<Object> result = new ArrayList<>();
+ for (int i = 0; i < numberOfDataEntries.get(); i++) {
+ StringBuilder aggregate = new StringBuilder();
+ for (int j = 0; j < v.size(); j++) {
+ int index;
+ if (j == 0) {
+ index = i;
+ } else {
+ index = numberOfDataEntries.get() + i;
+ }
+ aggregate.append(list.get(index).toString()).append(",");
+ }
+ if (aggregate.length() > 0) {
+ aggregate.setLength(aggregate.length() - 1);
+ }
+ result.add(aggregate.toString());
+ }
+ // 杩涜鍏紡璁$畻
+ Object resultValue = calculationFormula(result, v.get(0), k, device);
+ map.put(k, resultValue);
+ });
+ return map;
+ }
/**
* 闇�瑕侀�氳繃X,Y杞村畾浣�
@@ -195,17 +267,20 @@
* @param dataConfig
* @return
*/
+
private static Map<String, Object> analysisDb(String data, Map<String, List<DataConfig>> dataConfig, Device device) {
JSONObject jsonObject = JSON.parseObject(data);
Map<String, Object> map = new HashMap<>();
if (jsonObject.isEmpty()) {
return map;
}
+ // 浠� JSON 鏁版嵁涓彁鍙� data 鏁扮粍
JSONArray dataList = JSONArray.parseArray(jsonObject.get("data").toString());
dataConfig.forEach((k, v) -> {
AtomicInteger numberOfDataEntries = new AtomicInteger();
List<Object> list = new ArrayList<>();
for (int config = 0; config < v.size(); config++) {
+// String refery = v.get(config).getRefery();
String refery = getRefer(v.get(config).getRefery());
for (int i = 0; i < dataList.size(); i++) {
JSONObject jsonObject1 = JSON.parseObject(dataList.get(i).toString());
@@ -418,7 +493,7 @@
* @param dataConfig 瀛樺偍鍏紡鐨勫璞�
* @return
*/
- private static Object calculationFormula(List<Object> list, DataConfig dataConfig, String insProductItem, Device device) {
+ public static Object calculationFormula(List<Object> list, DataConfig dataConfig, String insProductItem, Device device) {
if (list.size() == 0) {
Map<String, Object> hashMap = new HashMap<>();
hashMap.put("equipName", device.getDeviceName());
@@ -445,10 +520,10 @@
listResult.addAll(list);
}
// 涓轰簡缁欏墠绔仛鏁版嵁鍖哄垎
- if (listResult.size() > 1) {
+ if (listResult.size() >= 1) {
hashMap.put("result", listResult);
} else {
- hashMap.put("result", listResult.get(0).toString());
+ hashMap.put("result", listResult);
}
hashMap.put("equipName", device.getDeviceName());
hashMap.put("equipValue", device.getManagementNumber());
@@ -479,6 +554,7 @@
String firstNumber = lastLine.split("\t")[0];
list.add(firstNumber);
} else {
+
list = analyzeData(data, v, k, ",");
}
// 濮旀墭缂栧彿涓庢牱鍝佺紪鍙峰瓨鍦�
@@ -628,10 +704,120 @@
}
// 闃叉鍙傜収鐗╀负绌烘姤閿欙紝杩涜鍒ゆ柇濡傛灉涓虹┖璧嬪�肩┖瀛楃
- private static String getRefer(String refer) {
+ public static String getRefer(String refer) {
return ObjectUtils.isNotEmpty(refer) ? refer.replaceAll(" ", "") : "";
}
+ /**
+ * 濮旀墭缂栧彿涓庢牱鍝佺紪鍙烽兘涓虹┖鎵ц
+ *
+ * @param data 閲囬泦鍒扮殑鏂囦欢瀛楃涓�
+ * @param v 鐢ㄦ埛閰嶇疆濂界殑x,y杞村畾浣嶆暟鎹笌鍙傜収鐗�
+ * @param k 妫�楠岄」鍚嶇О
+ * @param split 鍒嗗壊绗�
+ * @return 鎻愬彇鐨勬暟鎹垪琛�
+ */
+ public static List<Object> analyzeData1(String data, List<DataConfig> v, String k, String split) {
+ List<Object> list = new ArrayList<>();
+ // 棰勫鐞嗘暟鎹細绉婚櫎澶氫綑绌烘牸骞朵繚鐣欏叧閿垎闅旂
+ String processedData = data.replaceAll("\\s+", " ").trim();
+ for (int configIndex = 0; configIndex < v.size(); configIndex++) {
+ // 鍙栦袱涓敤鎴烽厤缃殑鍙傜収鐗�
+ String referx = getRefer(v.get(configIndex).getReferx());
+ String refery = getRefer(v.get(configIndex).getRefery());
+ if (ObjectUtils.isEmpty(refery) && ObjectUtils.isEmpty(referx)) {
+ System.out.println("鍙傜収鐗╀负绌猴紝璺宠繃褰撳墠閰嶇疆锛宬: " + k);
+ continue;
+ }
+ // 鏈�缁堢粨鏋�
+ List<Object> result = new ArrayList<>();
+ // 閫氳繃\n灏嗗瓧绗︿覆鍒嗗壊涓鸿
+ String[] aColumnY = processedData.replaceAll(" ", "").split("\n");
+ Integer end = null;
+ // 閲囬泦鏁版嵁锛歒杞�
+ for (int i = 0; i < aColumnY.length; i++) {
+ String line = aColumnY[i].trim();
+ if (line.isEmpty()) continue; // 璺宠繃绌鸿
+ // 濡傛灉Y鍙傜収涓嶄负绌轰笌X鍙傜収涓虹┖鍒欐墽琛岋紝鍚屾椂璇ヨ鍖呭惈Y鍙傜収
+ if (ObjectUtils.isNotEmpty(refery) && ObjectUtils.isEmpty(referx) && line.contains(refery)) {
+ try {
+ // 鍙朰鍧愭爣鍊�
+ int y = getXOrY(v.get(configIndex).getY(), k, "Y");
+ String[] aLineX = line.split(split);
+ for (int j = 0; j < aLineX.length; j++) {
+ if (aLineX[j].contains(refery)) {
+ if (i + y >= aColumnY.length) {
+ System.err.println(k + "锛歒杞村畾浣嶈秴鍑烘暟鎹寖鍥达紝褰撳墠鏁版嵁琛屾暟: " + aColumnY.length + "锛屽皾璇曡闂: " + (i + y));
+ continue;
+ }
+ String[] split1 = aColumnY[i + y].split(split);
+ if (j >= split1.length) {
+ System.err.println(k + "锛歑杞村畾浣嶈秴鍑烘暟鎹寖鍥达紝褰撳墠琛屽厓绱犱釜鏁�: " + split1.length + "锛屽皾璇曡闂綅缃�: " + j);
+ continue;
+ }
+ result.add(split1[j]);
+ }
+ }
+ } catch (Exception e) {
+ System.err.println(k + "锛氬湪澶勭悊浠匶鍙傜収閫昏緫鏃跺嚭鐜板紓甯�: " + e.getMessage());
+ }
+ }
+ // 濡傛灉Y鍙傜収涓嶄负绌轰笌X鍙傜収涓嶄负绌哄垯鎵ц,姝ゅY瀹氬尯鍩�
+ else if (ObjectUtils.isNotEmpty(refery) && ObjectUtils.isNotEmpty(referx)) {
+ try {
+ // 鍙杧鐨勫�硷紝闃叉鎶ラ敊
+ int x = getXOrY(v.get(configIndex).getX(), k, "X");
+ // 鍙朰鍧愭爣鍊�
+ int y = getXOrY(v.get(configIndex).getY(), k, "Y");
+ // 缂撳瓨Y鐨勭粨鏉熷��
+ if (ObjectUtils.isEmpty(end) && line.contains(refery)) {
+ end = i + y;
+ }
+ // 鍒ゆ柇鏄惁鍦ㄥ弬鐓х墿涓鸿捣鐐癸紝Y鍧愭爣鍊间负鏈�缁堣寖鍥�
+ if (ObjectUtils.isNotEmpty(end) && i <= end) {
+ String[] aLineX = line.split(split);
+ for (int j = 0; j < aLineX.length; j++) {
+ if (aLineX[j].contains(referx)) {
+ if (j + x >= aLineX.length) {
+ System.err.println(k + "锛歑杞村畾浣嶈秴鍑烘暟鎹寖鍥达紝褰撳墠琛屽厓绱犱釜鏁�: " + aLineX.length + "锛屽皾璇曡闂綅缃�: " + (j + x));
+ continue;
+ }
+ result.add(aLineX[j + x]);
+ break;
+ }
+ }
+ }
+ } catch (Exception e) {
+ System.err.println(k + "锛氬湪澶勭悊XY鍙傜収閫昏緫鏃跺嚭鐜板紓甯�: " + e.getMessage());
+ }
+ }
+ // 濡傛灉X鍙傜収涓嶄负绌哄悓鏃惰琛屽寘鍚玐鍙傜収锛屽垯鎵ц涓嬮潰鐨勪唬鐮�
+ else if (line.contains(referx) && ObjectUtils.isEmpty(refery)) {
+ try {
+ String[] aLineX = line.split(split);
+ // 鍙杧鐨勫�硷紝闃叉鎶ラ敊
+ int x = getXOrY(v.get(configIndex).getX(), k, "X");
+ for (int j = 0; j < aLineX.length; j++) {
+ if (aLineX[j].contains(referx)) {
+ if (j + x >= aLineX.length) {
+ System.err.println(k + "锛歑杞村畾浣嶈秴鍑烘暟鎹寖鍥达紝褰撳墠琛屽厓绱犱釜鏁�: " + aLineX.length + "锛屽皾璇曡闂綅缃�: " + (j + x));
+ continue;
+ }
+ result.add(aLineX[j + x]);
+ }
+ }
+ } catch (Exception e) {
+ System.err.println(k + "锛氬湪澶勭悊浠匵鍙傜収閫昏緫鏃跺嚭鐜板紓甯�: " + e.getMessage());
+ }
+ }
+ }
+ // 闃叉璁$畻鍏紡鐨勬椂鍊欏嚭鐜帮細[null] 杩欑鏁版嵁
+ if (ObjectUtils.isNotEmpty(result)) {
+ list.addAll(result);
+ }
+ }
+ return list;
+ }
/**
* 濮旀墭缂栧彿涓庢牱鍝佺紪鍙烽兘涓虹┖鎵ц
*
@@ -643,6 +829,10 @@
*/
public static List<Object> analyzeData(String data, List<DataConfig> v, String k, String split) {
List<Object> list = new ArrayList<>();
+
+ // 棰勫鐞嗘暟鎹細绉婚櫎澶氫綑绌烘牸骞朵繚鐣欏叧閿垎闅旂
+// String processedData = data.replaceAll("\\s+", " ").trim();
+
for (int config = 0; config < v.size(); config++) {
// 鍙栦袱涓敤鎴烽厤缃殑鍙傜収鐗�
String referx = getRefer(v.get(config).getReferx());
@@ -657,11 +847,14 @@
Integer end = null;
// 閲囬泦鏁版嵁锛歒杞�
for (int i = 0; i < aColumnY.length; i++) {
+// String line = aColumnY[i].trim();
+// if (line.isEmpty()) continue; // 璺宠繃绌鸿
// 濡傛灉Y鍙傜収涓嶄负绌轰笌X鍙傜収涓虹┖鍒欐墽琛岋紝鍚屾椂璇ヨ鍖呭惈Y鍙傜収
- if (ObjectUtils.isNotEmpty(refery) && ObjectUtils.isEmpty(referx) && aColumnY[i].contains(refery)) {
+ if (ObjectUtils.isNotEmpty(refery) && ObjectUtils .isEmpty(referx) && aColumnY[i].contains(refery)) {
// 鍙朰鍧愭爣鍊�
int y = getXOrY(v.get(config).getY(), k, "Y");
String[] aLineX = aColumnY[i].split(split);
+// String[] aLineX = line.split(split);
for (int j = 0; j < aLineX.length; j++) {
if (aLineX[j].contains(refery)) {
String[] split1 = new String[0];
@@ -671,7 +864,9 @@
throw new ErrorException(k + "锛歒杞村畾浣嶈秴鍑猴紒");
}
try {
+// System.out.println("----------");
result.add(split1[j]);
+
} catch (Exception e) {
throw new ErrorException(k + "锛歑杞村畾浣嶈秴鍑猴紒");
}
@@ -693,6 +888,7 @@
for (int j = 0; j < aLineX.length; j++) {
if (aLineX[j].contains(referx)) {
try {
+// System.out.println("77777777777777");
result.add(aLineX[j + x]);
} catch (Exception e) {
throw new ErrorException(k + "锛歑杞村畾浣嶈秴鍑猴紒");
@@ -709,6 +905,7 @@
for (int j = 0; j < aLineX.length; j++) {
if (aLineX[j].contains(referx)) {
try {
+// System.out.println("4444444444444444");
result.add(aLineX[j + x]);
} catch (Exception e) {
throw new ErrorException(k + "锛歑杞村畾浣嶈秴鍑猴紒");
@@ -717,7 +914,7 @@
}
}
}
- // 闃叉璁$畻鍏紡鐨勬椂鍊欏嚭鐜帮細[null] 杩欑鏁版嵁
+// 闃叉璁$畻鍏紡鐨勬椂鍊欏嚭鐜帮細[null] 杩欑鏁版嵁
if (ObjectUtils.isNotEmpty(result)) {
// String formatProcessing = getFormatProcessing(result);
list.addAll(result);
--
Gitblit v1.9.3