zouyu
2025-10-17 c65ab218b14e87489f1594b2d932f7bd54b3ba11
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("所在电脑未安装或未启动:LIMS文件采集器!");
@@ -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 -> {
@@ -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());
@@ -546,24 +621,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、Y定范围,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(" ", "");
@@ -646,7 +704,7 @@
    }
    // 防止参照物为空报错,进行判断如果为空赋值空字符
    private static String getRefer(String refer) {
    public static String getRefer(String refer) {
        return ObjectUtils.isNotEmpty(refer) ? refer.replaceAll(" ", "") : "";
    }
    /**
@@ -783,7 +841,7 @@
                continue;
            }
            // 最终结果
//            List<Object> result = new ArrayList<>();
            List<Object> result = new ArrayList<>();
            // 通过\n将字符串分割为行
            String[] aColumnY = data.replaceAll(" ", "").split("\n");
            Integer end = null;
@@ -807,7 +865,7 @@
                            }
                            try {
//                                System.out.println("----------");
                                list.add(split1[j]);
                                result.add(split1[j]);
                            } catch (Exception e) {
                                throw new ErrorException(k + ":X轴定位超出!");
@@ -831,7 +889,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 + ":X轴定位超出!");
                                }
@@ -848,7 +906,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 + ":X轴定位超出!");
                            }
@@ -856,11 +914,11 @@
                    }
                }
            }
            // 防止计算公式的时候出现:[null] 这种数据
//            if (ObjectUtils.isNotEmpty(result)) {
//             防止计算公式的时候出现:[null] 这种数据
            if (ObjectUtils.isNotEmpty(result)) {
//                String formatProcessing = getFormatProcessing(result);
//                list.addAll(result);
//            }
                list.addAll(result);
            }
        }
        return list;
    }