jinpengyong
2021-06-23 69d00365e9f099b26c4fc7a298cabeb131956d8a
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
package com.moral.api.util;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
 
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.api.service.DeviceService;
import com.moral.constant.RedisConstants;
import com.moral.util.DateUtils;
 
@Slf4j
@Component
public class AdjustDataUtils {
 
    @Autowired
    private DeviceService deviceService;
 
    @Autowired
    private RedisTemplate redisTemplate;
 
    public Map<String, Object> adjust(Map<String, Object> deviceData) {
        try {
            Object dataTime = deviceData.get("DataTime");
            String mac = deviceData.get("mac").toString();
            //清除毫秒,四舍五入
            long time = Math.round(new Double((String) dataTime) / 1000) * 1000L;
            long finalTime = DateUtils.dataToTimeStampTime(new Date(time), DateUtils.HH_mm_ss_EN).getTime();
            //设备信息
            Map<String, Object> deviceInfo = deviceService.getDeviceByMac(mac);
            Map<String, Object> monitorPoint = (Map<String, Object>) deviceInfo.get("monitorPoint");
            Object areaCode = monitorPoint.get("areaCode");
            Object cityCode = monitorPoint.get("cityCode");
            for (String key : deviceData.keySet()) {
                if (!key.equals("mac") && !key.equals("time") && !key.equals("DataTime") && !key.equals("ver") && !key.contains("Flag")) {
                    //测量值
                    Object measuredValue = deviceData.get(key);
                    List<DeviceAdjustValue> adjustValues = (List<DeviceAdjustValue>) redisTemplate.opsForHash().get(RedisConstants.ADJUST + "_" + mac, key);
                    if (ObjectUtils.isEmpty(adjustValues)) {
                        deviceData.put(key, measuredValue);
                        continue;
                    }
 
                    //根据时间段筛选校准公式
                    DeviceAdjustValue deviceAdjustValue = adjustValues.stream()
                            .filter(o -> o.getStartTime().getTime() <= finalTime && o.getEndTime().getTime() > finalTime)
                            .findFirst().get();
                    String adjustValue = deviceAdjustValue.getValue();
                    if (ObjectUtils.isEmpty(adjustValue)) {
                        deviceData.put(key, measuredValue);
                        continue;
                    }
 
                    Expression expression = AviatorEvaluator.compile(adjustValue);
                    Map<String, Object> env = new HashMap<>();
                    if (adjustValue.contains("aqi")) {
                        Object aqiValue = redisTemplate.opsForHash().get("aqi_" + areaCode, key);
                        if (ObjectUtils.isEmpty(aqiValue)) {
                            aqiValue = redisTemplate.opsForHash().get("aqi_" + cityCode, key);
                        }
                        env.put("aqi", ObjectUtils.isEmpty(aqiValue) ? 0F : Float.parseFloat((String) aqiValue));
                    }
                    if (adjustValue.contains("vocs")) {
                        Object vocsValue = ObjectUtils.isEmpty(deviceData.get("a99054")) ? 0F : deviceData.get("a99054");
                        env.put("vocs", vocsValue);
                    }
                    if (adjustValue.contains("cel")) {
                        env.put("cel", Float.parseFloat((String) measuredValue));
                    }
                    //校准
                    measuredValue = expression.execute(env);
                    //温度处理
                    if (Float.parseFloat(measuredValue.toString()) < 0 && !"a01001".equals(measuredValue)) {
                        measuredValue = 0F;
                    }
                    deviceData.put(key, Double.parseDouble(String.format("%.3f", measuredValue)));
 
                }
            }
        } catch (Exception e) {
            log.error("param[0] deviceData:" + JSON.toJSONString(deviceData));
            log.error(e.getMessage());
        }
        return deviceData;
    }
 
}