From 27e6bc3df3e39e0d0b147b155a89ad6837ea972b Mon Sep 17 00:00:00 2001 From: cjl <909710561@qq.com> Date: Mon, 06 Jan 2025 09:19:24 +0800 Subject: [PATCH] Merge branch 'cjl' into dev --- screen-common/src/main/java/com/moral/util/AmendUtils.java | 274 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 files changed, 236 insertions(+), 38 deletions(-) diff --git a/screen-common/src/main/java/com/moral/util/AmendUtils.java b/screen-common/src/main/java/com/moral/util/AmendUtils.java index 90aa50c..ac3c4e0 100644 --- a/screen-common/src/main/java/com/moral/util/AmendUtils.java +++ b/screen-common/src/main/java/com/moral/util/AmendUtils.java @@ -1,9 +1,12 @@ package com.moral.util; +import org.springframework.util.ObjectUtils; + import java.math.BigDecimal; import java.util.ArrayList; import java.util.Collections; import java.util.Date; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -40,21 +43,45 @@ } /** - * @param list ���������[value={"O3"���12},.....] + * @param params ���������������������data���������type���������upper���������lower * @return ������������������������������ */ - public static Object getO3AvgOfDay(List<Map<String, Object>> list) { + public static Map<String, Object> getO3AvgOfDay(Map<String, Object> params) { + Map<String, Object> result = new HashMap<>(); + List<Map<String, Object>> list = (List<Map<String, Object>>) params.get("data"); + Object upper = params.get("o3Upper"); + Object lower = params.get("o3Lower"); double max; List<Double> avgs = new ArrayList<>(); - for (int i = 8; i <= 24; i++) { + for (int i = 7; i <= 23; i++) { List<Double> data = new ArrayList<>(); for (Map<String, Object> dataMap : list) { Map<String, Object> dataValue = JSONObject.parseObject((String) dataMap.get("value"), Map.class); - Double o3 = Double.parseDouble(dataValue.get(Constants.SENSOR_CODE_O3).toString()); - int hour = DateUtils.getHour((Date) dataMap.get("time")); - if (hour == 0) { - hour = 24; + if (ObjectUtils.isEmpty(dataValue.get(Constants.SENSOR_CODE_O3))){ + continue; } + Double o3 = Double.parseDouble(dataValue.get(Constants.SENSOR_CODE_O3).toString()); + + //O3��������������� + Object flag = dataValue.get(Constants.SENSOR_CODE_O3 + "-" + Constants.MARKER_BIT_KEY); + //������������������������������ + if (!Constants.MARKER_BIT_TRUE.equals(flag)) { + continue; + } + + //������������������������������ + if (!ObjectUtils.isEmpty(upper)) { + if (o3 > (Double) upper) { + continue; + } + } + if (!ObjectUtils.isEmpty(lower)) { + if (o3 < (Double) lower) { + continue; + } + } + + int hour = DateUtils.getHour((Date) dataMap.get("time")); if (hour <= i && hour >= i - 7) { data.add(o3); } @@ -68,49 +95,153 @@ max = avgs.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getMax(); if (avgs.size() < 14) { if (max < 160d) { - return null; + return result; } } - return sciCal(max, 4); + result.put(Constants.SENSOR_CODE_O3, sciCal(max, 4)); + return result; + } + + //������aqi��������� + public static Double o3OfDay(List<Map<String, Object>> value) { + List<Map<String, Object>> o3_8H = getO3_8H(value); + if (!ObjectUtils.isEmpty(o3_8H)) { + double o3 = 0d; + for (Map<String, Object> o : o3_8H) { + double temp = (double) o.get("O3"); + if (temp > o3) { + o3 = temp; + } + } + return sciCal(o3, 0); + } + return null; } /** - * @param list ���������[value={"������"���12������������������200},.....] + * @param data ������ time:Date������,������key:o3 + * @return ���������������8��������������������� + */ + public static List<Map<String, Object>> getO3_8H(List<Map<String, Object>> data) { + Date date = (Date) data.get(0).get("time"); + List<Map<String, Object>> result = new ArrayList<>(); + for (int i = 7; i <= 23; i++) { + Map<String, Object> map = new HashMap<>(); + map.put("time", DateUtils.addHours(date, i)); + List<Double> value = new ArrayList<>(); + for (Map<String, Object> dataMap : data) { + Map<String, Object> sensorValue = JSONObject.parseObject((String) dataMap.get("value"), Map.class); + Double o3 = Double.parseDouble(sensorValue.get("O3").toString()); + Date time = (Date) dataMap.get("time"); + int hour = DateUtils.getHour(time); + if (hour <= i && hour >= i - 7) { + value.add(o3); + } + } + if (value.size() < 6) { + continue; + } + double average = value.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getAverage(); + map.put("O3", sciCal(average, 0)); + result.add(map); + } + return result; + } + + /** + * @param params ������ * @return ��������������������������� */ - public static Object getWindDirAvg(List<Map<String, Object>> list) { - double avgDir; - double sumSin = 0d; - double sumCos = 0d; - int size = 0; - for (Map<String, Object> map : list) { + public static Map<String, Object> getWindDirAvg(Map<String, Object> params) { + Map<String, Object> result = new HashMap<>(); + List<Map<String, Object>> data = (List<Map<String, Object>>) params.get("data"); + String type = params.get("type").toString(); + Object windDirUpper = params.get("windDirUpper"); + Object windDirLower = params.get("windDirLower"); + Object windSpeedUpper = params.get("windSpeedUpper"); + Object windSpeedLower = params.get("windSpeedLower"); + + Double avgDir = null; + Double sumSin = 0d; + Double sumCos = 0d; + Integer size = 0; + for (Map<String, Object> map : data) { Map<String, Object> dataValue = JSONObject.parseObject((String) map.get("value"), Map.class); Object wind = dataValue.get(Constants.SENSOR_CODE_WIND_DIR); Object speed = dataValue.get(Constants.SENSOR_CODE_WIND_SPEED); - if (wind == null || speed == null) { + Object flagDir = dataValue.get(Constants.SENSOR_CODE_WIND_DIR + "-" + Constants.MARKER_BIT_KEY); + Object flagSpeed = dataValue.get(Constants.SENSOR_CODE_WIND_SPEED + "-" + Constants.MARKER_BIT_KEY); + + if (!ObjectUtils.isEmpty(flagDir) && !ObjectUtils.isEmpty(flagSpeed)) { + if (!Constants.MARKER_BIT_TRUE.equals(flagDir) || !Constants.MARKER_BIT_TRUE.equals(flagSpeed)) { + continue; + } + } + + if (ObjectUtils.isEmpty(wind) || ObjectUtils.isEmpty(speed)) { continue; } - size++; double windDir = Double.parseDouble(wind.toString()); double windSpeed = Double.parseDouble(speed.toString()); - double sin = windSpeed * Math.sin(windDir / 180d) * Math.PI; - double cos = windSpeed * Math.cos(windDir / 180d) * Math.PI; + + //��������������������������������������������������� + if (!ObjectUtils.isEmpty(windDirUpper)) { + if (windDir > (Double) windDirUpper) { + continue; + } + } + + if (!ObjectUtils.isEmpty(windDirLower)) { + if (windDir < (Double) windDirLower) { + continue; + } + } + + if (!ObjectUtils.isEmpty(windSpeedUpper)) { + if (windSpeed > (Double) windSpeedUpper) { + continue; + } + } + + if (!ObjectUtils.isEmpty(windSpeedLower)) { + if (windSpeed < (Double) windSpeedLower) { + continue; + } + } + + size++; + double sin = windSpeed * Math.sin(windDir / 180d * Math.PI); + double cos = windSpeed * Math.cos(windDir / 180d * Math.PI); sumSin += sin; sumCos += cos; } if (size == 0) { - return null; + return result; } + double avgSin = sumSin / size; double avgCos = sumCos / size; + if (avgSin > 0 && avgCos > 0) { avgDir = Math.atan(avgSin / avgCos) * 180 / Math.PI; } else if ((avgSin > 0 && avgCos < 0) || (avgSin < 0 && avgCos < 0)) { avgDir = Math.atan(avgSin / avgCos) * 180 / Math.PI + 180; - } else { + } else if (avgSin < 0 && avgCos > 0) { avgDir = Math.atan(avgSin / avgCos) * 180 / Math.PI + 360; } - return sciCal(avgDir, 4); + if (!ObjectUtils.isEmpty(avgDir)) { + double v = sciCal(avgDir, 4); + result.put(Constants.SENSOR_CODE_WIND_DIR, v); + if ("hour".equals(type)) { + //���������>=45���,������������ N,<45���H H:��������������� + if (size >= 45) { + result.put(Constants.SENSOR_CODE_WIND_DIR + "-" + Constants.MARKER_BIT_KEY, Constants.MARKER_BIT_TRUE); + } else { + result.put(Constants.SENSOR_CODE_WIND_DIR + "-" + Constants.MARKER_BIT_KEY, Constants.MARKER_BIT_FALSE); + } + } + } + return result; } /** @@ -121,48 +252,115 @@ public static double percentile(List<Double> data, int p) { int n = data.size(); Collections.sort(data); - double v = n / (100 / p); - System.out.println(n % (100 / p)); - if (n % (100 / p) == 0) { - if (v == n) { - return data.get(n - 1); - } - return (data.get((int) v - 1) + data.get((int) v)) / 2; - } + double v = MathUtils.division(MathUtils.mul(n, p),100d,1); + v = Math.ceil(v)-1; return sciCal(data.get((int) v), 4); } - public static Object getCOAvgOfWeek(List<Map<String, Object>> list) { + + + //������������������������������ + public static Map<String, Object> getCOAvgOfWeekOrMonth(Map<String, Object> params) { + Map<String, Object> result = new HashMap<>(); + List<Map<String, Object>> list = (List<Map<String, Object>>) params.get("data"); + Object upper = params.get("coUpper"); + Object lower = params.get("coLower"); + List<Double> data = new ArrayList<>(); for (Map<String, Object> dataMap : list) { Map<String, Object> dataValue = JSONObject.parseObject((String) dataMap.get("value"), Map.class); Object o = dataValue.get(Constants.SENSOR_CODE_CO); - if (o == null) { + if (ObjectUtils.isEmpty(o)) { continue; } Double co = Double.parseDouble(o.toString()); + //������������������������������ + if (!ObjectUtils.isEmpty(upper)) { + if (co > (Double) upper) { + continue; + } + } + if (!ObjectUtils.isEmpty(lower)) { + if (co < (Double) lower) { + continue; + } + } data.add(co); } if (data.size() == 0) { - return null; + return result; } - return percentile(data, 95); + result.put(Constants.SENSOR_CODE_CO, percentile(data, 95)); + return result; } - public static Object getO3AvgOfWeek(List<Map<String, Object>> list) { + //������������������������ + public static Map<String, Object> getO3AvgOfWeekOrMonth(Map<String, Object> params) { + Map<String, Object> result = new HashMap<>(); + List<Map<String, Object>> list = (List<Map<String, Object>>) params.get("data"); + Object upper = params.get("o3Upper"); + Object lower = params.get("o3Lower"); List<Double> data = new ArrayList<>(); for (Map<String, Object> dataMap : list) { Map<String, Object> dataValue = JSONObject.parseObject((String) dataMap.get("value"), Map.class); Object o = dataValue.get(Constants.SENSOR_CODE_O3); - if (o == null) { + if (ObjectUtils.isEmpty(o)) { continue; } Double o3 = Double.parseDouble(o.toString()); + //������������������������������ + if (!ObjectUtils.isEmpty(upper)) { + if (o3 > (Double) upper) { + continue; + } + } + if (!ObjectUtils.isEmpty(lower)) { + if (o3 < (Double) lower) { + continue; + } + } data.add(o3); + } + if (data.size() == 0) { + return result; + } + result.put(Constants.SENSOR_CODE_O3, percentile(data, 90)); + return result; + } + + //������������������������ + /** + * ��������������� + * PM2.5,PM10,CO,���������95��������� + * SO2,NO2,���������98��������� + * O3���������������90��������� + * + * @param list ������ + * @param sensorCode ������code + */ + public static Double getAvgOfYear(List<Map<String, Object>> list, String sensorCode) { + List<Double> data = new ArrayList<>(); + for (Map<String, Object> dataMap : list) { + Map<String, Object> dataValue = JSONObject.parseObject((String) dataMap.get("value"), Map.class); + Object o = dataValue.get(sensorCode); + if (ObjectUtils.isEmpty(o)) { + continue; + } + Double sensorValue = Double.parseDouble(o.toString()); + data.add(sensorValue); } if (data.size() == 0) { return null; } - return percentile(data, 90); + + Double v = null; + if ("PM2_5".equals(sensorCode) || "PM10".equals(sensorCode) || "CO".equals(sensorCode)) { + v = percentile(data, 95); + } else if ("SO2".equals(sensorCode) || "NO2".equals(sensorCode)) { + v = percentile(data, 98); + } else if ("O3".equals(sensorCode)) { + v = percentile(data, 90); + } + return v; } } -- Gitblit v1.8.0