From f9f8f90ac63d6ce3274410d3721b173f40db6e41 Mon Sep 17 00:00:00 2001 From: jinpengyong <jpy123456> Date: Fri, 25 Aug 2023 14:12:48 +0800 Subject: [PATCH] chore:过滤海城小时数据 --- screen-manage/src/main/java/com/moral/api/util/AdjustDataUtils.java | 188 ++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 171 insertions(+), 17 deletions(-) diff --git a/screen-manage/src/main/java/com/moral/api/util/AdjustDataUtils.java b/screen-manage/src/main/java/com/moral/api/util/AdjustDataUtils.java index 1f1deba..8d59d01 100644 --- a/screen-manage/src/main/java/com/moral/api/util/AdjustDataUtils.java +++ b/screen-manage/src/main/java/com/moral/api/util/AdjustDataUtils.java @@ -1,33 +1,62 @@ package com.moral.api.util; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; +import java.math.BigDecimal; +import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; +import java.util.Set; import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import com.googlecode.aviator.AviatorEvaluator; import com.googlecode.aviator.Expression; import com.moral.api.entity.DeviceAdjustValue; +import com.moral.api.mapper.HistoryHourlyMapper; +import com.moral.constant.Constants; import com.moral.util.DateUtils; @Slf4j @Component public class AdjustDataUtils { - //��������������� - public Map<String, Object> adjust(Map<String, Object> deviceData, Map<String, Object> adjustFormula, Map<String, Object> aqiMap) { + + @Autowired + private HistoryHourlyMapper historyHourlyMapper; + /** + * @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,String code) { 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); + String mac = deviceData.remove("mac").toString(); + 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); //������������������������ @@ -36,11 +65,14 @@ 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); @@ -54,27 +86,149 @@ if (aqiMap != null) { aqiValue = aqiMap.get(key); } - env.put("aqi", ObjectUtils.isEmpty(aqiValue) ? 0F : Float.parseFloat((String) aqiValue)); + env.put("aqi", ObjectUtils.isEmpty(aqiValue) ? 0F : new BigDecimal(String.valueOf(aqiValue)).floatValue()); } 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")) { - env.put("cel", Float.parseFloat((String) measuredValue)); + if (formula.contains("ce1")) { + //env.put("cel", Float.parseFloat((String) measuredValue)); + env.put("ce1", new BigDecimal(String.valueOf(measuredValue)).floatValue()); + }else if(formula.contains("cel")){ + env.put("cel", new BigDecimal(String.valueOf(measuredValue)).floatValue()); } + //������ 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))); } } + if (code.equals("2")){ + HashMap<String, Object> result = new HashMap<>(); + String time1 = deviceData.get("time").toString(); + long l = Long.parseLong(time1); + + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:00:00"); + String format1 = simpleDateFormat.format(l); + Date date = DateUtils.getDate(format1, DateUtils.yyyy_MM_dd_HH_mm_ss_EN); + + String yearAndMonth = DateUtils.dateToDateString(DateUtils.addHours(date, -2), DateUtils.yyyyMM_EN); + String format = simpleDateFormat.format(DateUtils.addHours(date, -2)); + result.put("timeUnits",yearAndMonth); + result.put("mac",mac); + result.put("time",format); + Map<String, Object> map = historyHourlyMapper.selectHistoryHourly(result); + if (ObjectUtils.isEmpty(map)){ + return deviceData; + } + JSONObject jsonObject = JSONObject.parseObject(map.get("value").toString()); + if (ObjectUtils.isEmpty(aqiMap)){ + deviceData.put("a21005",Double.parseDouble(jsonObject.get("a21005").toString())); + deviceData.put("a21026",Double.parseDouble(jsonObject.get("a21026").toString())); + deviceData.put("a21004",Double.parseDouble(jsonObject.get("a21004").toString())); + deviceData.put("a34002",Double.parseDouble(jsonObject.get("a34002").toString())); + deviceData.put("a34004",Double.parseDouble(jsonObject.get("a34004").toString())); + deviceData.put("a05024",Double.parseDouble(jsonObject.get("a05024").toString())); + }else { + if (ObjectUtils.isEmpty(aqiMap.get("a21005"))){ + + String a21005 = jsonObject.get("a21005").toString(); + deviceData.put("a21005",Double.parseDouble(a21005)); + } + if (ObjectUtils.isEmpty(aqiMap.get("a21026"))){ +// JSONObject jsonObject = JSONObject.parseObject(map.get("value").toString()); + String a21026 = jsonObject.get("a21026").toString(); + deviceData.put("a21026",Double.parseDouble(a21026)); + } + if (ObjectUtils.isEmpty(aqiMap.get("a21004"))){ +// JSONObject jsonObject = JSONObject.parseObject(map.get("value").toString()); + String a21004 = jsonObject.get("a21004").toString(); + deviceData.put("a21004",Double.parseDouble(a21004)); + } + if (ObjectUtils.isEmpty(aqiMap.get("a34002"))){ +// JSONObject jsonObject = JSONObject.parseObject(map.get("value").toString()); + String a34002 = jsonObject.get("a34002").toString(); + deviceData.put("a34002",Double.parseDouble(a34002)); + } + if (ObjectUtils.isEmpty(aqiMap.get("a34004"))){ +// JSONObject jsonObject = JSONObject.parseObject(map.get("value").toString()); + String a34004 = jsonObject.get("a34004").toString(); + deviceData.put("a34004",Double.parseDouble(a34004)); + } + if (ObjectUtils.isEmpty(aqiMap.get("a05024"))){ +// JSONObject jsonObject = JSONObject.parseObject(map.get("value").toString()); + String a05024 = jsonObject.get("a05024").toString(); + deviceData.put("a05024",Double.parseDouble(a05024)); + } + } + } + } catch (Exception e) { log.error("param[0] deviceData:" + JSON.toJSONString(deviceData)); - log.error(e.getMessage()); + } + 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; } -- Gitblit v1.8.0