From f5a46f7e82042da996759674c7dded0096d02898 Mon Sep 17 00:00:00 2001 From: jinpengyong <jpy123456> Date: Mon, 23 Nov 2020 10:16:59 +0800 Subject: [PATCH] PM2.5,PM10预测数据插入forecast_weather表 --- src/main/java/com/moral/service/impl/WeatherServiceImpl.java | 381 ++++++++++++++++++++++++++++------------------------- 1 files changed, 201 insertions(+), 180 deletions(-) diff --git a/src/main/java/com/moral/service/impl/WeatherServiceImpl.java b/src/main/java/com/moral/service/impl/WeatherServiceImpl.java index 6ebfd4b..36e1d0d 100644 --- a/src/main/java/com/moral/service/impl/WeatherServiceImpl.java +++ b/src/main/java/com/moral/service/impl/WeatherServiceImpl.java @@ -18,13 +18,10 @@ import com.alibaba.fastjson.JSONObject; import com.moral.mapper.AQIMapper; -import com.moral.mapper.AreaMapper; -import com.moral.mapper.CityMapper; import com.moral.mapper.DeviceMapper; import com.moral.mapper.WeatherMapper; import com.moral.mapper.HistoryHourlyMapper; import com.moral.mapper.MonitorPointMapper; -import com.moral.mapper.ProvinceMapper; import com.moral.service.WeatherService; @Service @@ -45,6 +42,7 @@ @Resource private HistoryHourlyMapper historyHourlyMapper; + //O3,PM2.5,PM10������������������forecast_weather��� @Override public int insertForecastWeatherNew() throws ParseException { Calendar c = Calendar.getInstance(); @@ -70,6 +68,7 @@ cityList.add(ks); int count = 0; for (Map<String, Object> map : cityList) { + String cityCode = map.get("cityCode").toString(); String id = map.get("cityId").toString(); Map<String, Object> dataMap = restTemplate.getForObject("https://api.heweather.net/v7/weather/72h?key=da05c6c4852d4f7aa3364a9236ee9e26&gzip=n&location={1}", Map.class, id); if (dataMap == null) { @@ -82,51 +81,18 @@ List<Map<String, Object>> hourlyList = (List<Map<String, Object>>) JSONObject.parse(string); for (Map<String, Object> hourlyMap : hourlyList) { //��������������������� - String fxTime = hourlyMap.get("fxTime").toString().split("\\+")[0].replace("T", " ") + ":00"; - hourlyMap.remove("fxTime"); + String fxTime = hourlyMap.remove("fxTime").toString().split("\\+")[0].replace("T", " ") + ":00"; //icon������������������ hourlyMap.remove("icon"); hourlyMap.put("time", fxTime); //������ String text = hourlyMap.get("text").toString(); //������km/h->m/s - Double windSpeed = Double.valueOf(hourlyMap.get("windSpeed").toString()); + double windSpeed = Double.parseDouble(hourlyMap.get("windSpeed").toString()); windSpeed = windSpeed * 1000 / 3600; hourlyMap.put("windSpeed", df.format(windSpeed)); - String condition = ""; - if ("���".equals(text)) { - condition = "100"; - } else if ("���".equals(text)) { - condition = "90"; - } else if ("������".equals(text)) { - condition = "80"; - } else if ("���".equals(text)) { - condition = "70"; - } else if ("���".equals(text) || "������".equals(text) || "������".equals(text) || "".equals(text)) { - condition = "60"; - } else if ("������".equals(text)) { - condition = "45"; - } else if ("���������".equals(text)) { - condition = "40"; - } else if ("������������������".equals(text) || "���������������".equals(text)) { - condition = "30"; - } else if ("������".equals(text)) { - condition = "20"; - } else if ("���".equals(text)) { - condition = "10"; - } else if ("������������������".equals(text)) { - condition = "5"; - } else if ("������������������".equals(text)) { - condition = "4"; - } else if ("������".equals(text) || "������".equals(text) || "������".equals(text) - || "������".equals(text) || "������".equals(text) || "������".equals(text) - || "���������".equals(text) || "���������������������������".equals(text) || "������".equals(text) - ) { - condition = "0"; - } else { - condition = "50"; - } - hourlyMap.put("condition", condition); + hourlyMap.put("windScale", String.valueOf(getWindScale(windSpeed))); + hourlyMap.put("condition", getWeatherCondition(text)); String hour = fxTime.split(" ")[0]; String hour1 = fxTime.split(":")[0]; if (hour.equals(nextDay)) { @@ -138,39 +104,38 @@ } nextDayList.remove(0); map.put("data", nextDayList); - } + //������ + c.setTime(sdf.parse(today)); + c.add(Calendar.YEAR, -3); + Date start = c.getTime(); + Date end = sdf.parse(today); + int timeUnits1 = c.get(Calendar.YEAR); + int timeUnits2 = timeUnits1 + 1; + int timeUnits3 = timeUnits1 + 2; + int timeUnits4 = timeUnits1 + 3; + //���������15��� 70%,���������45���15%,������ 15% + c.setTime(end); + c.add(Calendar.DAY_OF_MONTH, -15); + //���15��������������� + Date slicedTime1 = c.getTime(); + //���45��������������� + c.add(Calendar.DAY_OF_MONTH, -30); + Date slicedTime2 = c.getTime(); - //today:������ - c.setTime(sdf.parse(today)); - c.add(Calendar.YEAR, -3); - Date start = c.getTime(); - Date end = sdf.parse(today); - int timeUnits1 = c.get(Calendar.YEAR); - int timeUnits2 = timeUnits1 + 1; - int timeUnits3 = timeUnits1 + 2; - int timeUnits4 = timeUnits1 + 3; - //���������15��� 70%,���������45���15%,������ 15% - c.setTime(end); - c.add(Calendar.DAY_OF_MONTH, -15); - //���15��������������� - Date slicedTime1 = c.getTime(); - //���45��������������� - c.add(Calendar.DAY_OF_MONTH, -30); - Date slicedTime2 = c.getTime(); - for (Map<String, Object> map : cityList) { - String cityCode = map.get("cityCode").toString(); - ArrayList<Map<String, Object>> dataList = (ArrayList<Map<String, Object>>) map.get("data"); - for (Map<String, Object> dataMap : dataList) { + //������������������������list + List<Map<String, Object>> resultList = new ArrayList<>(); + for (Map<String, Object> nextDayMap : nextDayList) { + //O3������ Map<String, Object> hashMap = new HashMap<>(); List<String> hours = new ArrayList<>(); hashMap.put("cityCode", cityCode); hashMap.put("start", start); hashMap.put("end", end); hashMap.put("typeFormat", "%Y-%m-%d %H:%i:%s"); - Integer condition = Integer.valueOf(dataMap.get("condition").toString()); - Double temp = Double.valueOf(dataMap.get("temp").toString()); - Integer pressure = Integer.valueOf(dataMap.get("pressure").toString()); - String time = dataMap.get("time").toString(); + int condition = Integer.parseInt(nextDayMap.get("condition").toString()); + double temp = Double.parseDouble(nextDayMap.get("temp").toString()); + int pressure = Integer.parseInt(nextDayMap.get("pressure").toString()); + String time = nextDayMap.remove("time").toString(); //������������ hours.add(time.substring(11, 13)); c.setTime(sdf1.parse(time)); @@ -195,15 +160,15 @@ } List<Integer> years = new ArrayList<>(); Collections.addAll(years, timeUnits1, timeUnits2, timeUnits3, timeUnits4); - List<String> samples = getSample(hashMap, years); - if (samples.size() > 100) { + List<String> O3Samples = getSample(hashMap, years); + if (O3Samples.size() > 100) { hashMap.put("startTemp", temp - 1.0); hashMap.put("endTemp", temp + 1.0); - samples = getSample(hashMap, years); - if (samples.size() > 100) { + O3Samples = getSample(hashMap, years); + if (O3Samples.size() > 100) { hashMap.put("startPressure", pressure - 5); hashMap.put("endPressure", pressure + 5); - samples = getSample(hashMap, years); + O3Samples = getSample(hashMap, years); } } hashMap.put("slicedTime1", slicedTime1); @@ -211,34 +176,78 @@ hashMap.put("nowTemp", temp); hashMap.put("sHour", sHour); hashMap.put("eHour", eHour); - Map<String, List<String>> sectionTimesMap = getSectionTimes(hashMap, samples, years); + Map<String, List<String>> sectionTimesMap = getSectionTimes(hashMap, O3Samples, years); Map<String, Object> params = new HashMap<>(); params.put("cityCode", cityCode); params.put("typeFormat", "%Y-%m-%d %H:%i:%s"); + params.put("sensorKey", "O3"); + params.put("sensorKey1", "O3C"); List<String> times1 = sectionTimesMap.get("times1"); List<String> times2 = sectionTimesMap.get("times2"); List<String> times3 = sectionTimesMap.get("times3"); - Double avg1 = getAvgO3(params, times1); - Double avg2 = getAvgO3(params, times2); - Double avg3 = getAvgO3(params, times3); - Double resultO3 = avg1 * 0.7 + avg2 * 0.15 + avg3 * 0.15; - dataMap.put("O3C", String.valueOf(Math.round(resultO3))); - } + Double O3Avg1 = getAvg(params, times1); + Double O3Avg2 = getAvg(params, times2); + Double O3Avg3 = getAvg(params, times3); + Double resultO3 = O3Avg1 * 0.7 + O3Avg2 * 0.15 + O3Avg3 * 0.15; + nextDayMap.put("O3C", String.valueOf(Math.round(resultO3))); + //pm2.5,pm10��������������� + //������,pm2.5,pm10������������������ + String month = time.substring(5, 7); + //��������������������� + String windDir = nextDayMap.get("windDir").toString(); + //������ + String windScale = nextDayMap.get("windScale").toString(); + Map<String, Object> hashMap1 = new HashMap<>(); + hashMap1.put("cityCode", cityCode); + hashMap1.put("start", start); + hashMap1.put("end", end); + hashMap1.put("typeFormat", "%Y-%m-%d %H:%i:%s"); + hashMap1.put("month", month); + List<String> pmSamples = getSample(hashMap1, years); + if (pmSamples.size() > 100) { + hashMap1.put("windDir", windDir); + hashMap1.put("windScale", windScale); + pmSamples = getSample(hashMap1, years); + if (pmSamples.size() > 100) { + hashMap1.put("startPressure", pressure - 5); + hashMap1.put("endPressure", pressure + 5); + pmSamples = getSample(hashMap1, years); + } + } + hashMap1.put("slicedTime1", slicedTime1); + hashMap1.put("slicedTime2", slicedTime2); + Map<String, List<String>> sectionTimesMap1 = getSectionTimes(hashMap1, pmSamples, years); + List<String> times4 = sectionTimesMap1.get("times1"); + List<String> times5 = sectionTimesMap1.get("times2"); + List<String> times6 = sectionTimesMap1.get("times3"); - List<Map<String, Object>> resultList = new ArrayList<>(); - for (Map<String, Object> dataMap : dataList) { - Map<String, Object> resultMap = new HashMap<>(); - String time = dataMap.remove("time").toString(); - Map<String, Object> params = new HashMap<>(); - params.put("cityCode", cityCode); - params.put("time", sdf1.parse(time)); + Map<String, Object> params1 = new HashMap<>(); + params1.put("cityCode", cityCode); + params1.put("typeFormat", "%Y-%m-%d %H:%i:%s"); + params1.put("sensorKey", "PM2_5"); + params1.put("sensorKey1", "PM25C"); + Double PM25Avg1 = getAvg(params1, times4); + Double PM25Avg2 = getAvg(params1, times5); + Double PM25Avg3 = getAvg(params1, times6); + params1.put("sensorKey", "PM10"); + params1.put("sensorKey1", "PM10C"); + Double PM10Avg1 = getAvg(params1, times4); + Double PM10Avg2 = getAvg(params1, times5); + Double PM10Avg3 = getAvg(params1, times6); + Double resultPM25 = PM25Avg1 * 0.7 + PM25Avg2 * 0.15 + PM25Avg3 * 0.15; + nextDayMap.put("PM25C", String.valueOf(Math.round(resultPM25))); + Double resultPM10 = PM10Avg1 * 0.7 + PM10Avg2 * 0.15 + PM10Avg3 * 0.15; + nextDayMap.put("PM10C", String.valueOf(Math.round(resultPM10))); + params.put("time", time); String beam = weatherMapper.getBeam(params); if (beam != null) { - dataMap.put("beam", beam.replace("\"", "")); + nextDayMap.put("beam", beam.replace("\"", "")); } + //������������������map + Map<String, Object> resultMap = new HashMap<>(); resultMap.put("cityCode", cityCode); resultMap.put("time", time); - resultMap.put("json", JSONObject.toJSONString(dataMap)); + resultMap.put("json", JSONObject.toJSONString(nextDayMap)); resultList.add(resultMap); } Map<String, Object> parameters = new HashMap<>(); @@ -265,23 +274,27 @@ return samples; } - private Double getAvgO3(Map<String, Object> params, List<String> times) { + private Double getAvg(Map<String, Object> params, List<String> times) { List<String> values = new ArrayList<>(); + if (times.size() == 0) { + return 0.0; + } + for (String time : times) { String s = time.substring(0, 4); params.put("time", time); - String O3 = aqiMapper.getO3ByTime(params); - if (O3 == null) { + String value = aqiMapper.getAqiFromHangzhou(params); + if (value == null) { params.put("timeUnits", s); - O3 = aqiMapper.getO3FromHistory(params); + value = aqiMapper.getAqiFromHistory(params); } - if (O3 != null) { - values.add(O3.replace("\"", "")); + if (value != null) { + values.add(value.replace("\"", "")); } } Double sum = 0.0; - for (String value : values) { - sum += Double.valueOf(value); + for (String v : values) { + sum += Double.valueOf(v); } return sum / values.size(); } @@ -297,8 +310,6 @@ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date slicedTime1 = (Date) (params.get("slicedTime1")); Date slicedTime2 = (Date) (params.get("slicedTime2")); - Double nowTemp = Double.valueOf(params.get("nowTemp").toString()); - ArrayList<String> hours = (ArrayList<String>) params.get("hours"); for (String sample : samples) { Date samTime = sdf.parse(sample); if (samTime.getTime() >= slicedTime1.getTime()) { @@ -309,32 +320,38 @@ times3.add(sample); } } + hashMap.put("times1", times1); hashMap.put("times2", times2); hashMap.put("times3", times3); - for (String key : hashMap.keySet()) { - List<String> values = hashMap.get(key); - if (values.size() == 0) { - params.put("startTemp", nowTemp - 2.0); - params.put("endTemp", nowTemp + 2.0); - if ("times1".equals(key) || "times2".equals(key)) { - years = new ArrayList<>(); - values = getSample(params, years); - } else { - values = getSample(params, years); - } + + if (params.get("nowTemp") != null && params.get("hours") != null) { + Double nowTemp = Double.valueOf(params.get("nowTemp").toString()); + ArrayList<String> hours = (ArrayList<String>) params.get("hours"); + for (String key : hashMap.keySet()) { + List<String> values = hashMap.get(key); if (values.size() == 0) { - hours.add(params.get("sHour").toString()); - hours.add(params.get("eHour").toString()); - params.put("hours", hours); + params.put("startTemp", nowTemp - 2.0); + params.put("endTemp", nowTemp + 2.0); if ("times1".equals(key) || "times2".equals(key)) { years = new ArrayList<>(); values = getSample(params, years); } else { values = getSample(params, years); } + if (values.size() == 0) { + hours.add(params.get("sHour").toString()); + hours.add(params.get("eHour").toString()); + params.put("hours", hours); + if ("times1".equals(key) || "times2".equals(key)) { + years = new ArrayList<>(); + values = getSample(params, years); + } else { + values = getSample(params, years); + } + } + hashMap.put(key, values); } - hashMap.put(key, values); } } return hashMap; @@ -387,41 +404,9 @@ Double windSpeed = Double.valueOf(nextDayMap.get("windSpeed").toString()); windSpeed = windSpeed * 1000 / 3600; nextDayMap.put("windSpeed", df.format(windSpeed)); + nextDayMap.put("windScale", String.valueOf(getWindScale(windSpeed))); String text = nextDayMap.get("text").toString(); - String condition = ""; - if ("���".equals(text)) { - condition = "100"; - } else if ("���".equals(text)) { - condition = "90"; - } else if ("������".equals(text)) { - condition = "80"; - } else if ("���".equals(text)) { - condition = "70"; - } else if ("���".equals(text) || "������".equals(text) || "������".equals(text) || "".equals(text)) { - condition = "60"; - } else if ("������".equals(text)) { - condition = "45"; - } else if ("���������".equals(text)) { - condition = "40"; - } else if ("������������������".equals(text) || "���������������".equals(text)) { - condition = "30"; - } else if ("������".equals(text)) { - condition = "20"; - } else if ("���".equals(text)) { - condition = "10"; - } else if ("������������������".equals(text)) { - condition = "5"; - } else if ("������������������".equals(text)) { - condition = "4"; - } else if ("������".equals(text) || "������".equals(text) || "������".equals(text) - || "������".equals(text) || "������".equals(text) || "������".equals(text) - || "���������".equals(text) || "���������������������������".equals(text) || "������".equals(text) - ) { - condition = "0"; - } else { - condition = "50"; - } - nextDayMap.put("condition", condition); + nextDayMap.put("condition", getWeatherCondition(text)); } map.put("data", nextDayList); } @@ -649,44 +634,80 @@ jsonMap.remove("obsTime"); jsonMap.remove("icon"); String text = jsonMap.get("text").toString(); - String condition; - if ("���".equals(text)) { - condition = "100"; - } else if ("���".equals(text)) { - condition = "90"; - } else if ("������".equals(text)) { - condition = "80"; - } else if ("���".equals(text)) { - condition = "70"; - } else if ("���".equals(text) || "������".equals(text) || "������".equals(text) || "".equals(text)) { - condition = "60"; - } else if ("������".equals(text)) { - condition = "45"; - } else if ("���������".equals(text)) { - condition = "40"; - } else if ("������������������".equals(text) || "���������������".equals(text)) { - condition = "30"; - } else if ("������".equals(text)) { - condition = "20"; - } else if ("���".equals(text)) { - condition = "10"; - } else if ("������������������".equals(text)) { - condition = "5"; - } else if ("������������������".equals(text)) { - condition = "4"; - } else if ("������".equals(text) || "������".equals(text) || "������".equals(text) - || "������".equals(text) || "������".equals(text) || "������".equals(text) - || "���������".equals(text) || "���������������������������".equals(text) || "������".equals(text) - ) { - condition = "0"; - } else { - condition = "50"; - } - jsonMap.put("condition", condition); + jsonMap.put("condition", getWeatherCondition(text)); resultMap.put("json", JSONObject.toJSONString(jsonMap)); resultList.add(resultMap); } weatherMapper.deleteRealWeather(sdf1.format(now)); return weatherMapper.insertRealWeather(resultList); } + + private int getWindScale(Double windSpeed) { + int windScale = 0; + if (windSpeed <= 1.5) { + windScale = 1; + } else if (windSpeed <= 3.3) { + windScale = 2; + } else if (windSpeed <= 5.4) { + windScale = 3; + } else if (windSpeed <= 7.9) { + windScale = 4; + } else if (windSpeed <= 10.7) { + windScale = 5; + } else if (windSpeed <= 13.8) { + windScale = 6; + } else if (windSpeed <= 17.1) { + windScale = 7; + } else if (windSpeed <= 20.7) { + windScale = 8; + } else if (windSpeed <= 24.4) { + windScale = 9; + } else if (windSpeed <= 28.4) { + windScale = 10; + } else if (windSpeed <= 32.6) { + windScale = 11; + } else if (windSpeed <= 36.9) { + windScale = 12; + } + return windScale; + } + + private String getWeatherCondition(String text) { + String condition; + if ("���".equals(text)) { + condition = "100"; + } else if ("���".equals(text)) { + condition = "90"; + } else if ("������".equals(text)) { + condition = "80"; + } else if ("���".equals(text)) { + condition = "70"; + } else if ("���".equals(text) || "������".equals(text) || "������".equals(text) || "".equals(text)) { + condition = "60"; + } else if ("������".equals(text)) { + condition = "45"; + } else if ("���������".equals(text)) { + condition = "40"; + } else if ("������������������".equals(text) || "���������������".equals(text)) { + condition = "30"; + } else if ("������".equals(text)) { + condition = "20"; + } else if ("���".equals(text)) { + condition = "10"; + } else if ("������������������".equals(text)) { + condition = "5"; + } else if ("������������������".equals(text)) { + condition = "4"; + } else if ("������".equals(text) || "������".equals(text) || "������".equals(text) + || "������".equals(text) || "������".equals(text) || "������".equals(text) + || "���������".equals(text) || "���������������������������".equals(text) || "������".equals(text) + ) { + condition = "0"; + } else { + condition = "50"; + } + return condition; + } } + + -- Gitblit v1.8.0