zouyu
6 天以前 dc7300e21fe53f74e08eb2fa494a83430e2e54ca
cnas-device/src/main/java/com/ruoyi/device/utils/DataAcquisition.java
@@ -2,11 +2,13 @@
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import com.ruoyi.common.core.domain.Result;
import com.ruoyi.common.utils.RedisUtil;
import com.ruoyi.device.constant.DCResistanceMqttConstants;
@@ -128,17 +130,19 @@
                case ".docx":
                    map = analysisString(data, userMap, device, entrustCode, sampleCode);
                    break;
                case ".xls":
                case ".xlsx":
                case ".csv":
                    map = analysisList(data, userMap, device, entrustCode, sampleCode);
                    break;
                case ".xls":
                    map = analysisTxt(data, userMap, device, entrustCode, sampleCode);
                case ".serialPort":
                    map = analysisSerialPortList(data, userMap, device, entrustCode, sampleCode);
                    break;
                case ".pngInExcel":
                    map = analysisPngInExcel(data, userMap, device, entrustCode, sampleCode);
                    break;
                case ".txt":
                    map = analysisTxt(data, userMap, device, entrustCode, sampleCode);
                    break;
                case ".csv":
                    map = analysisList(data, userMap, device, entrustCode, sampleCode);
                    break;
                case ".mdb":
                    // 判断是否是拉力机JCZX-ZB-FF01014
@@ -149,10 +153,8 @@
                    }
                    break;
                case ".db":
//                    map = analysisDb1(data, userMap, device);
                    map = analysisDb(data, userMap, device);
                    break;
                case ".mysql":
                case ".sqlserver":
//                    map = analysisDb1(data, userMap, device);
                    map = analysisDb(data, userMap, device);
                    break;
@@ -345,23 +347,25 @@
                }
            }
            // 拼接数采配置
            List<Object> result = new ArrayList<>();
            for (int i = 0; i < numberOfDataEntries.get(); i++) {
                String aggregate = "";
                for (int j = 0; j < v.size(); j++) {
                    int index;
                    if (j == 0) {
                        index = i;
                    } else {
                        index = numberOfDataEntries.get() + i;
                    }
                    aggregate += list.get(index).toString() + ",";
                }
                int lastIndex = aggregate.lastIndexOf(",");
                String substring = aggregate.substring(0, lastIndex);
                result.add(substring);
            }
            List<Object> result = new ArrayList<>(list);
//            String join = String.join(",", list.stream().map(String::valueOf).collect(Collectors.toList()));
//            result.add(join);
//            for (int i = 0; i < numberOfDataEntries.get(); i++) {
//                String aggregate = "";
//                for (int j = 0; j < v.size(); j++) {
//                    int index;
//                    if (j == 0) {
//                        index = i;
//                    } else {
//                        index = numberOfDataEntries.get() + i;
//                    }
//                    aggregate += list.get(index).toString() + ",";
//                }
//                int lastIndex = aggregate.lastIndexOf(",");
//                String substring = aggregate.substring(0, lastIndex);
//                result.add(substring);
//
//            }
            // 进行公式计算
            Object resultValue = calculationFormula(result, v.get(0), k, device);
            map.put(k, resultValue);
@@ -496,7 +500,7 @@
     * @return
     */
    public static Object calculationFormula(List<Object> list, DataConfig dataConfig, String insProductItem, Device device) {
        if (list.size() == 0) {
        if (list.isEmpty()) {
            Map<String, Object> hashMap = new HashMap<>();
            hashMap.put("equipName", device.getDeviceName());
            hashMap.put("equipValue", device.getManagementNumber());
@@ -571,6 +575,45 @@
    }
    /**
     * 解析excel中识别到的图片内容
     *
     * @param data       采集到的文件字符串
     * @param dataConfig 用户配置好的x,y轴定位数据与参照物
     * @return
     */
    private static Map<String, Object> analysisPngInExcel(String data, Map<String, List<DataConfig>> dataConfig,
                                                   Device device, String entrustCode, String sampleCode) {
        Map<String, Object> map = new HashMap<>();
        //处理data
        Map<String,List<Object>> dataMap = new HashMap<>();
        String[] rows = data.split("\n");
        String[] headRow = rows[0].split(" ");//获取表头列表
        for (int i = 0; i < headRow.length; i++) {
            //数据最多五条
            List<Object> vals = new ArrayList<>();
            for (int j = 1; j <= 5; j++) {
                String[] cols = rows[j].split(" ");
                vals.add(cols[i]);
            }
            dataMap.put(headRow[i],vals);
        }
        dataConfig.forEach((k, v) -> {
            List<Object> list = new ArrayList<>();
            v.stream().sorted(Comparator.comparing(DataConfig::getX)).forEach(config->{
                for (Map.Entry<String, List<Object>> entry : dataMap.entrySet()) {
                    if(entry.getKey().contains(config.getReferx())){
                        list.addAll(entry.getValue());
                    }
                }
            });
            // 进行公式计算
            Object resultValue = calculationFormula(list, v.get(0), k, device);
            map.put(k, resultValue);
        });
        return map;
    }
    /**
     * @param data       采集到的文件字符串
     * @param dataConfig 用户配置好的x,y轴定位数据与参照物
     * @return
@@ -617,6 +660,25 @@
            } 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;
    }
    /**
     * 处理串口数据
     *
     * @param data       采集到的文件字符串
     * @param dataConfig 用户配置好的x,y轴定位数据与参照物
     * @return
     */
    public static Map<String, Object> analysisSerialPortList(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 = JSONObject.parseObject(data,new TypeReference<List<Object>>(){}.getType());
            // 进行公式计算
            Object resultValue = calculationFormula(list, v.get(0), k, device);
            map.put(k, resultValue);
@@ -709,117 +771,7 @@
    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: " + k);
                continue;
            }
            // 最终结果
            List<Object> result = new ArrayList<>();
            // 通过\n将字符串分割为行
            String[] aColumnY = processedData.replaceAll(" ", "").split("\n");
            Integer end = null;
            // 采集数据:Y轴
            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 {
                        // 取Y坐标值
                        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 + ":Y轴定位超出数据范围,当前数据行数: " + aColumnY.length + ",尝试访问行: " + (i + y));
                                    continue;
                                }
                                String[] split1 = aColumnY[i + y].split(split);
                                if (j >= split1.length) {
                                    System.err.println(k + ":X轴定位超出数据范围,当前行元素个数: " + split1.length + ",尝试访问位置: " + j);
                                    continue;
                                }
                                result.add(split1[j]);
                            }
                        }
                    } catch (Exception e) {
                        System.err.println(k + ":在处理仅Y参照逻辑时出现异常: " + e.getMessage());
                    }
                }
                // 如果Y参照不为空与X参照不为空则执行,此处Y定区域
                else if (ObjectUtils.isNotEmpty(refery) && ObjectUtils.isNotEmpty(referx)) {
                    try {
                        // 取x的值,防止报错
                        int x = getXOrY(v.get(configIndex).getX(), k, "X");
                        // 取Y坐标值
                        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 + ":X轴定位超出数据范围,当前行元素个数: " + aLineX.length + ",尝试访问位置: " + (j + x));
                                        continue;
                                    }
                                    result.add(aLineX[j + x]);
                                    break;
                                }
                            }
                        }
                    } catch (Exception e) {
                        System.err.println(k + ":在处理XY参照逻辑时出现异常: " + e.getMessage());
                    }
                }
                // 如果X参照不为空同时该行包含X参照,则执行下面的代码
                else if (line.contains(referx) && ObjectUtils.isEmpty(refery)) {
                    try {
                        String[] aLineX = line.split(split);
                        // 取x的值,防止报错
                        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 + ":X轴定位超出数据范围,当前行元素个数: " + aLineX.length + ",尝试访问位置: " + (j + x));
                                    continue;
                                }
                                result.add(aLineX[j + x]);
                            }
                        }
                    } catch (Exception e) {
                        System.err.println(k + ":在处理仅X参照逻辑时出现异常: " + e.getMessage());
                    }
                }
            }
            // 防止计算公式的时候出现:[null] 这种数据
            if (ObjectUtils.isNotEmpty(result)) {
                list.addAll(result);
            }
        }
        return list;
    }
    /**
     * 委托编号与样品编号都为空执行
     *