From d5e714fecf9cba48c1a225e6eb53078cdad647c7 Mon Sep 17 00:00:00 2001 From: yaowanxin <3588231647@qq.com> Date: 星期四, 31 七月 2025 13:55:19 +0800 Subject: [PATCH] 获取redis数据,接收前端数据存reids --- cnas-device/src/main/java/com/ruoyi/device/utils/DataAcquisition.java | 172 +++++++++++++++++++++++++++++++++++++++++++++++--------- 1 files changed, 143 insertions(+), 29 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 98c04ec..c717dd4 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,6 +7,8 @@ import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; import com.baomidou.mybatisplus.core.toolkit.StringUtils; +import com.ruoyi.common.utils.RedisUtil; +import com.ruoyi.device.constant.DCResistanceMqttConstants; import com.ruoyi.device.dto.DeviceCollectionDto; import com.ruoyi.device.pojo.DataConfig; import com.ruoyi.device.pojo.Device; @@ -43,7 +45,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")) { @@ -76,7 +78,10 @@ "&sampleCode=" + sampleCode + "&mdbEntrustCode=" + device.getEntrustCode() + "&mdbSampleCode=" + device.getSampleCode() + - "&dbFileName=" + device.getDbFileName(); + "&dbFileName=" + device.getDbFileName()+ + "&dbUserName=" + dbUserName + + "&dbPassword=" + dbPassword + + "&dbTable=" + dbTable; System.out.println("璇锋眰鐨� URL: " + http); String result = null; try { @@ -134,10 +139,18 @@ } 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; @@ -151,6 +164,71 @@ return map; } } + /** + * 浠� Redis 涓幏鍙栧疄闄呯數闃诲�硷紝骞朵互閿�煎褰㈠紡杩斿洖 + * @param dataConfig 鏁版嵁閰嶇疆淇℃伅 + * @param device 璁惧淇℃伅 + * @return 鍖呭惈瀹為檯鐢甸樆鍊煎鐞嗙粨鏋滅殑閿�煎锛涜嫢鏈幏鍙栧埌鏈夋晥鏁版嵁鍒欒繑鍥炵┖ Map + */ + public static Map<String, Object> getActualResistanceValueAsMap(Map<String, List<DataConfig>> dataConfig, Device device) { + Map<String, Object> map = new HashMap<>(); + if (ObjectUtils.isEmpty(dataConfig)) { + return map; + } + 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()); + // 浠� Redis 涓幏鍙栧�� + Object value = RedisUtil.get(refery); + if (ObjectUtils.isNotEmpty(value)) { + if (value instanceof String && ((String) value).startsWith("[") && ((String) value).endsWith("]")) { + try { + // 瑙f瀽 JSON 鏁扮粍瀛楃涓蹭负 List<BigDecimal> + List<BigDecimal> values = JSONArray.parseArray((String) value, BigDecimal.class); + list.addAll(values); + numberOfDataEntries.addAndGet(values.size()); + } catch (Exception e) { + // 瑙f瀽澶辫触锛岃褰曢敊璇棩蹇楋紝浣跨敤鍘熷瓧绗︿覆鍊� + System.err.println("瑙f瀽 Redis 杩斿洖鐨� JSON 鏁扮粍澶辫触: " + value); + list.add(value); + numberOfDataEntries.addAndGet(1); + } + } else { + numberOfDataEntries.addAndGet(1); + list.add(value); + + } + } + } + // 鎷兼帴鏁伴噰閰嶇疆 + 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; + } + if (index < list.size()) { + 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; + } + public static Map<String, Object> createFrequency(String entrustCode, String sampleCode, Map<String, Object> map) { Set<String> set = new LinkedHashSet<>(); @@ -187,6 +265,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 +323,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()); @@ -445,7 +576,7 @@ listResult.addAll(list); } // 涓轰簡缁欏墠绔仛鏁版嵁鍖哄垎 - if (listResult.size() > 1) { + if (listResult.size() >= 1) { hashMap.put("result", listResult); } else { hashMap.put("result", listResult.get(0).toString()); @@ -546,24 +677,7 @@ }); return map; } - public static Map<String, Object> analysisList1(String data, Map<String, List<DataConfig>> dataConfig, - Device device, String entrustCode, String sampleCode) { - Map<String, Object> map = new HashMap<>(); - dataConfig.forEach((k, v) -> { - List<Object> list = new ArrayList<>(); - // 濮旀墭缂栧彿涓庢牱鍝佺紪鍙蜂笉瀛樺湪锛屽畾锛�1銆乊瀹氳寖鍥达紝X瀹氭í鍧愭爣锛�2銆佸彧瀛樺湪Y锛�3銆佸彧瀛樺湪X - if (ObjectUtils.isEmpty(device.getEntrustCode()) && ObjectUtils.isEmpty(device.getSampleCode())) { - list = analyzeData(data, v, k, splitIdentifier); - // 濮旀墭缂栧彿涓庢牱鍝佺紪鍙峰瓨鍦� - } else if (ObjectUtils.isNotEmpty(device.getEntrustCode()) && ObjectUtils.isNotEmpty(device.getSampleCode())) { - list = analyzeDataEntrustCodAndSampleCode(data, v, k, splitIdentifier, device, entrustCode, sampleCode); - } - // 杩涜鍏紡璁$畻 - Object resultValue = calculationFormula(list, v.get(0), k, device); - map.put(k, resultValue); - }); - return map; - } + private static List<Object> analyzeDataEntrustCodAndSampleCode(String data, List<DataConfig> v, String k, String splitIdentifier, Device device, String entrustCodeValue, String sampleCodeValue) { entrustCodeValue = entrustCodeValue.replaceAll(" ", ""); @@ -783,7 +897,7 @@ continue; } // 鏈�缁堢粨鏋� -// List<Object> result = new ArrayList<>(); + List<Object> result = new ArrayList<>(); // 閫氳繃\n灏嗗瓧绗︿覆鍒嗗壊涓鸿 String[] aColumnY = data.replaceAll(" ", "").split("\n"); Integer end = null; @@ -807,7 +921,7 @@ } try { // System.out.println("----------"); - list.add(split1[j]); + result.add(split1[j]); } catch (Exception e) { throw new ErrorException(k + "锛歑杞村畾浣嶈秴鍑猴紒"); @@ -831,7 +945,7 @@ if (aLineX[j].contains(referx)) { try { // System.out.println("77777777777777"); - list.add(aLineX[j + x]); + result.add(aLineX[j + x]); } catch (Exception e) { throw new ErrorException(k + "锛歑杞村畾浣嶈秴鍑猴紒"); } @@ -848,7 +962,7 @@ if (aLineX[j].contains(referx)) { try { // System.out.println("4444444444444444"); - list.add(aLineX[j + x]); + result.add(aLineX[j + x]); } catch (Exception e) { throw new ErrorException(k + "锛歑杞村畾浣嶈秴鍑猴紒"); } @@ -856,11 +970,11 @@ } } } - // 闃叉璁$畻鍏紡鐨勬椂鍊欏嚭鐜帮細[null] 杩欑鏁版嵁 -// if (ObjectUtils.isNotEmpty(result)) { +// 闃叉璁$畻鍏紡鐨勬椂鍊欏嚭鐜帮細[null] 杩欑鏁版嵁 + if (ObjectUtils.isNotEmpty(result)) { // String formatProcessing = getFormatProcessing(result); -// list.addAll(result); -// } + list.addAll(result); + } } return list; } -- Gitblit v1.9.3