| | |
| | | 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 { |
| | | Object dataTime = deviceData.get("DataTime"); |
| | | //清除毫秒,四舍五入 |
| | | long time = Math.round(new Double((String) dataTime) / 1000) * 1000L; |
| | | long finalTime = DateUtils.dataToTimeStampTime(new Date(time), DateUtils.HH_mm_ss_EN).getTime(); |
| | | 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.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); |
| | |
| | | env.put("aqi", ObjectUtils.isEmpty(aqiValue) ? 0F : Float.parseFloat((String) aqiValue)); |
| | | } |
| | | if (formula.contains("vocs")) { |
| | | Object vocsValue = ObjectUtils.isEmpty(deviceData.get("a99054")) ? 0F : deviceData.get("a99054"); |
| | | 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")) { |
| | |
| | | //校准 |
| | | measuredValue = expression.execute(env); |
| | | //温度处理 |
| | | if (Float.parseFloat(measuredValue.toString()) < 0 && !"a01001".equals(measuredValue)) { |
| | | if (!Constants.SENSOR_CODE_TEMP.equals(measuredValue) && Float.parseFloat(measuredValue.toString()) < 0) { |
| | | measuredValue = 0F; |
| | | } |
| | | deviceData.put(key, Double.parseDouble(String.format("%.3f", measuredValue))); |
| | | deviceData.put(key, Double.parseDouble(String.format("%.4f", measuredValue))); |
| | | } |
| | | } |
| | | } catch (Exception e) { |