|  |  |  | 
|---|
|  |  |  | package com.moral.api.utils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | import com.alibaba.fastjson.JSONObject; | 
|---|
|  |  |  | import com.moral.constant.SensorContrasts; | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | import org.springframework.stereotype.Component; | 
|---|
|  |  |  | import org.springframework.util.ObjectUtils; | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.util.Date; | 
|---|
|  |  |  | import java.util.HashMap; | 
|---|
|  |  |  | import java.util.Iterator; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.Map; | 
|---|
|  |  |  | import java.util.Optional; | 
|---|
|  |  |  | import java.util.Set; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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数据 | 
|---|
|  |  |  | * @param deviceData    设备数据 | 
|---|
|  |  |  | * @param adjustFormula 校准公式,包含所有因子校准公式,key为因子code,value为校准公式(各个时间段的公式List) | 
|---|
|  |  |  | * @param aqiMap        与设备绑定的国控站点aqi数据 | 
|---|
|  |  |  | * @return Map<String, Object> 校准后数据 | 
|---|
|  |  |  | * */ | 
|---|
|  |  |  | public Map<String, Object> adjust(Map<String, Object> deviceData, Map<String, Object> adjustFormula, Map<String, Object> aqiMap) { | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static Map<String, Object> adjust(Map<String, Object> deviceData, Map<String, Object> adjustFormula, Map<String, Object> aqiMap) { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | Date time = DateUtils.getDate((String) deviceData.get("DataTime"), DateUtils.yyyyMMddHHmmss_EN); | 
|---|
|  |  |  | 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); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //硬件没有的传感器,但是需要显示的 | 
|---|
|  |  |  | Set<String> dateKey = deviceData.keySet(); | 
|---|
|  |  |  | for (String adjustKey : adjustFormula.keySet()) { | 
|---|
|  |  |  | if (!dateKey.contains(adjustKey)) { | 
|---|
|  |  |  | deviceData.put(adjustKey, "0"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | for (String key : deviceData.keySet()) { | 
|---|
|  |  |  | if (!key.equals("mac") && !key.equals("time") && !key.equals("DataTime") && !key.equals("ver") && !key.contains("Flag")) { | 
|---|
|  |  |  | if (!key.contains("Flag")) { | 
|---|
|  |  |  | //测量值 | 
|---|
|  |  |  | Object measuredValue = deviceData.get(key); | 
|---|
|  |  |  | //单个因子校准公式 | 
|---|
|  |  |  | 
|---|
|  |  |  | deviceData.put(key, measuredValue); | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //根据时间段筛选校准公式 | 
|---|
|  |  |  | DeviceAdjustValue deviceAdjustValue = sensorFormulas.stream() | 
|---|
|  |  |  | DeviceAdjustValue deviceAdjustValue = new DeviceAdjustValue(); | 
|---|
|  |  |  | Optional<DeviceAdjustValue> optional = sensorFormulas.stream() | 
|---|
|  |  |  | .filter(o -> o.getStartTime().getTime() <= finalTime && o.getEndTime().getTime() > finalTime) | 
|---|
|  |  |  | .findFirst().get(); | 
|---|
|  |  |  | .findFirst(); | 
|---|
|  |  |  | if (optional.isPresent()) { | 
|---|
|  |  |  | deviceAdjustValue = optional.get(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | String formula = deviceAdjustValue.getValue(); | 
|---|
|  |  |  | if (StringUtils.isEmpty(formula)) { | 
|---|
|  |  |  | deviceData.put(key, measuredValue); | 
|---|
|  |  |  | 
|---|
|  |  |  | if (formula.contains("aqi")) { | 
|---|
|  |  |  | Object aqiValue = null; | 
|---|
|  |  |  | if (aqiMap != null) { | 
|---|
|  |  |  | //String jsonStr = JsonUtil.readJsonFile("classpath:SensorContrast.json"); | 
|---|
|  |  |  | //                           String jsonStr = JsonUtil.testReadFile("SensorContrast.json"); | 
|---|
|  |  |  | //                            JSONObject result = JSONObject.parseObject(jsonStr); | 
|---|
|  |  |  | //                            aqiValue = aqiMap.get(result.get(key)); | 
|---|
|  |  |  | //                            aqiValue = aqiMap.get(SensorContrasts.sensorContrastMap.get(key)); | 
|---|
|  |  |  | aqiValue = aqiMap.get(key); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | env.put("aqi", ObjectUtils.isEmpty(aqiValue) ? 0F : Float.parseFloat((String) aqiValue)); | 
|---|
|  |  |  | env.put("aqi", ObjectUtils.isEmpty(aqiValue) ? 0F : Float.parseFloat(String.valueOf(aqiValue))); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (formula.contains("vocs")) { | 
|---|
|  |  |  | Object vocsValue = ObjectUtils.isEmpty(deviceData.get(Constants.SENSOR_CODE_VOCS)) ? 0F : deviceData.get(Constants.SENSOR_CODE_VOCS); | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | log.error("param[0] deviceData:" + JSON.toJSONString(deviceData)); | 
|---|
|  |  |  | log.error(e.getMessage()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return deviceData; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 烟道流速,烟道流量校准 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param deviceData    数据,其中包含各传感器值,和数据时间(用于不同时间段不同校准公式校准) | 
|---|
|  |  |  | * @param adjustFormula 该数据所属设备所有因子的校准公式,key为因子code | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static 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; | 
|---|
|  |  |  | } | 
|---|