jinpengyong
2022-03-04 ffe9d36074938e837f46afcd30b99788a7fbb4a0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.moral.api.util;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
 
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
 
import com.alibaba.fastjson.JSON;
import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.Expression;
import com.moral.api.entity.DeviceAdjustValue;
import com.moral.constant.Constants;
import com.moral.util.DateUtils;
 
@Slf4j
@Component
public class AdjustDataUtils {
    /**
     * @param deviceData    设备数据
     * @param adjustFormula 校准公式
     * @param aqiMap        与设备绑定的国控站点aqi数据
     * @return Map<String, Object> 校准后数据
     */
    public Map<String, Object> adjust(Map<String, Object> deviceData, Map<String, Object> adjustFormula, Map<String, Object> aqiMap) {
        try {
            Date time = DateUtils.getDate((String) deviceData.remove("DataTime"), DateUtils.yyyyMMddHHmmss_EN);
            long finalTime = DateUtils.dataToTimeStampTime(time, DateUtils.HH_mm_ss_EN).getTime();
 
            //先校准烟气流速,再用流速校准烟气流量
            if (deviceData.get(Constants.SENSOR_CODE_CURRENT_SPEED) != null) {
                deviceData = adjustFlueSpeedAndFlow(deviceData, adjustFormula);
            }
 
            for (String key : deviceData.keySet()) {
                if (!key.contains("Flag")) {
                    //测量值
                    Object measuredValue = deviceData.get(key);
                    //单个因子校准公式
                    List<DeviceAdjustValue> sensorFormulas = (List<DeviceAdjustValue>) adjustFormula.get(key);
                    if (ObjectUtils.isEmpty(sensorFormulas)) {
                        deviceData.put(key, measuredValue);
                        continue;
                    }
                    //根据时间段筛选校准公式
                    DeviceAdjustValue deviceAdjustValue = new DeviceAdjustValue();
                    Optional<DeviceAdjustValue> optional = sensorFormulas.stream()
                            .filter(o -> o.getStartTime().getTime() <= finalTime && o.getEndTime().getTime() > finalTime)
                            .findFirst();
                    if (optional.isPresent()) {
                        deviceAdjustValue = optional.get();
                    }
                    String formula = deviceAdjustValue.getValue();
                    if (StringUtils.isEmpty(formula)) {
                        deviceData.put(key, measuredValue);
                        continue;
                    }
 
                    Expression expression = AviatorEvaluator.compile(formula);
                    Map<String, Object> env = new HashMap<>();
                    if (formula.contains("aqi")) {
                        Object aqiValue = null;
                        if (aqiMap != null) {
                            aqiValue = aqiMap.get(key);
                        }
                        env.put("aqi", ObjectUtils.isEmpty(aqiValue) ? 0F : Float.parseFloat((String) aqiValue));
                    }
                    if (formula.contains("vocs")) {
                        Object vocsValue = ObjectUtils.isEmpty(deviceData.get(Constants.SENSOR_CODE_VOCS)) ? 0F : deviceData.get(Constants.SENSOR_CODE_VOCS);
                        env.put("vocs", vocsValue);
                    }
                    if (formula.contains("cel")) {
                        env.put("cel", Float.parseFloat((String) measuredValue));
                    }
                    //校准
                    measuredValue = expression.execute(env);
                    //温度处理
                    if (!Constants.SENSOR_CODE_TEMP.equals(measuredValue) && Float.parseFloat(measuredValue.toString()) < 0) {
                        measuredValue = 0F;
                    }
                    deviceData.put(key, Double.parseDouble(String.format("%.4f", measuredValue)));
                }
            }
        } catch (Exception e) {
            log.error("param[0] deviceData:" + JSON.toJSONString(deviceData));
        }
        return deviceData;
    }
 
    /**
     * 烟道流速,烟道流量校准
     * @param deviceData 数据,其中包含各传感器值,和数据时间(用于不同时间段不同校准公式校准)
     * @param adjustFormula 该数据所属设备所有因子的校准公式,key为因子code
     */
    public Map<String, Object> adjustFlueSpeedAndFlow(Map<String, Object> deviceData, Map<String, Object> adjustFormula) {
        Date time = DateUtils.getDate((String) deviceData.remove("DataTime"), DateUtils.yyyyMMddHHmmss_EN);
        long finalTime = DateUtils.dataToTimeStampTime(time, DateUtils.HH_mm_ss_EN).getTime();
        //流速测量值
        Object measuredValue = deviceData.get(Constants.SENSOR_CODE_CURRENT_SPEED);
 
 
        //流速校准
        List<DeviceAdjustValue> speedFormulas = (List<DeviceAdjustValue>) adjustFormula.get(Constants.SENSOR_CODE_CURRENT_SPEED);
        if (!ObjectUtils.isEmpty(speedFormulas)) {
            //根据时间段筛选校准公式
            DeviceAdjustValue deviceAdjustValue = new DeviceAdjustValue();
            Optional<DeviceAdjustValue> optional = speedFormulas.stream()
                    .filter(o -> o.getStartTime().getTime() <= finalTime && o.getEndTime().getTime() > finalTime)
                    .findFirst();
            if (optional.isPresent()) {
                deviceAdjustValue = optional.get();
            }
 
            String formula = deviceAdjustValue.getValue();
            if (!StringUtils.isEmpty(formula)) {
 
                Expression expression = AviatorEvaluator.compile(formula);
                Map<String, Object> env = new HashMap<>();
                env.put("cel", Float.parseFloat((String) measuredValue));
                //校准
                measuredValue = expression.execute(env);
                deviceData.put(Constants.SENSOR_CODE_CURRENT_SPEED, Double.parseDouble(String.format("%.4f", measuredValue)));
            }
 
 
            //流量校准
            List<DeviceAdjustValue> flowFormulas = (List<DeviceAdjustValue>) adjustFormula.get(Constants.SENSOR_CODE_CURRENT_FLOW);
            //根据时间段筛选校准公式
            optional = flowFormulas.stream()
                    .filter(o -> o.getStartTime().getTime() <= finalTime && o.getEndTime().getTime() > finalTime)
                    .findFirst();
            if (optional.isPresent()) {
                deviceAdjustValue = optional.get();
            }
 
            formula = deviceAdjustValue.getValue();
            if (!StringUtils.isEmpty(formula)) {
                Expression expression = AviatorEvaluator.compile(formula);
                Map<String, Object> env = new HashMap<>();
                String currentSpeed = deviceData.get(Constants.SENSOR_CODE_CURRENT_SPEED).toString();
                env.put("currentSpeed", Float.parseFloat(currentSpeed));
                deviceData.put(Constants.SENSOR_CODE_CURRENT_FLOW, Double.parseDouble(String.format("%.4f", expression.execute(env))));
            }
        }
        return deviceData;
    }
}