From 6ae790cc95cf87e40f0f1fa49ed04fb722530211 Mon Sep 17 00:00:00 2001 From: jinpengyong <jpy123456> Date: Fri, 06 Aug 2021 17:22:15 +0800 Subject: [PATCH] 均值计算加入数据有效性限制条件 --- screen-common/src/main/java/com/moral/util/AmendUtils.java | 158 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 139 insertions(+), 19 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 9e15cb5..960de0b 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,10 +43,14 @@ } /** - * @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++) { @@ -51,6 +58,26 @@ 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()); + + //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) upper) { + continue; + } + } + int hour = DateUtils.getHour((Date) dataMap.get("time")); if (hour == 0) { hour = 24; @@ -68,39 +95,81 @@ 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; } /** - * @param list ���������[value={"������"���12������������������200},.....] + * @param params ������ * @return ��������������������������� */ - public static Object getWindDirAvg(List<Map<String, Object>> 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; double sumSin = 0d; double sumCos = 0d; int size = 0; - for (Map<String, Object> map : list) { + 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 (!Constants.MARKER_BIT_TRUE.equals(flagDir) || !Constants.MARKER_BIT_TRUE.equals(flagSpeed)) { continue; } - size++; + + if (ObjectUtils.isEmpty(wind) || ObjectUtils.isEmpty(speed)) { + continue; + } double windDir = Double.parseDouble(wind.toString()); double windSpeed = Double.parseDouble(speed.toString()); + + //��������������������������������������������������� + 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) { @@ -110,7 +179,17 @@ } else { avgDir = Math.atan(avgSin / avgCos) * 180 / Math.PI + 360; } - return sciCal(avgDir, 4); + 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; } /** @@ -132,38 +211,79 @@ } //������������������������������ - public static Object getCOAvgOfWeekOrMonth(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) { + Object flag = dataValue.get(Constants.SENSOR_CODE_CO + Constants.MARKER_BIT_KEY); + if (!Constants.MARKER_BIT_TRUE.equals(flag)) { + continue; + } + 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) upper) { + 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 getO3AvgOfWeekOrMonth(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) { + Object flag = dataValue.get(Constants.SENSOR_CODE_O3 + Constants.MARKER_BIT_KEY); + if (!Constants.MARKER_BIT_TRUE.equals(flag)) { + continue; + } + 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) upper) { + continue; + } + } data.add(o3); } if (data.size() == 0) { - return null; + return result; } - return percentile(data, 90); + result.put(Constants.SENSOR_CODE_O3, percentile(data, 90)); + return result; } } -- Gitblit v1.8.0