jinpengyong
2021-08-17 6f4e852b84c577454a4876f83c7085bd360fe4fb
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
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 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();
            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 = sensorFormulas.stream()
                            .filter(o -> o.getStartTime().getTime() <= finalTime && o.getEndTime().getTime() > finalTime)
                            .findFirst().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));
            log.error(e.getMessage());
        }
        return deviceData;
    }
}