From f28149d8183a62f87fa9c8df9ae589070d83f612 Mon Sep 17 00:00:00 2001
From: 于紫祥_1901 <email@yuzixiang_1910>
Date: Thu, 24 Dec 2020 13:47:50 +0800
Subject: [PATCH] 波动补偿
---
src/main/java/com/moral/service/impl/HistoryHourlyServiceImpl.java | 533 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 528 insertions(+), 5 deletions(-)
diff --git a/src/main/java/com/moral/service/impl/HistoryHourlyServiceImpl.java b/src/main/java/com/moral/service/impl/HistoryHourlyServiceImpl.java
index 2fabdce..c5272bf 100644
--- a/src/main/java/com/moral/service/impl/HistoryHourlyServiceImpl.java
+++ b/src/main/java/com/moral/service/impl/HistoryHourlyServiceImpl.java
@@ -1,10 +1,18 @@
package com.moral.service.impl;
+import java.lang.reflect.Array;
+import java.math.BigDecimal;
import java.text.DecimalFormat;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.*;
+import com.moral.entity.MachineActivate;
+import com.moral.mapper.HistoryMapper;
+import com.moral.util.AQICalculation;
+import com.moral.util.AQICalculation2;
+import com.moral.util.DateUtil;
+import io.swagger.models.auth.In;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Service;
@@ -16,12 +24,17 @@
import com.moral.service.DeviceService;
import com.moral.service.HistoryHourlyService;
import com.moral.service.SensorService;
+import org.springframework.util.ObjectUtils;
+
import javax.annotation.Resource;
@Service
public class HistoryHourlyServiceImpl implements HistoryHourlyService {
@Resource
private HistoryHourlyMapper historyHourlyMapper;
+
+ @Resource
+ private HistoryMapper historyMapper;
@Resource
private DeviceService deviceService;
@@ -55,13 +68,523 @@
}
@Override
+ public Map<String, Object> getPollutionSourceDataByHour(Map<String, Object> parameters) throws Exception {
+ String yearAndDay = String.valueOf(parameters.get("yearAndDay"));
+ yearAndDay = yearAndDay.replace("-","");
+ parameters.put("yearAndDay",yearAndDay);
+ List<Sensor> sensors = sensorMapper.getSensorsByMac(parameters);
+ List<String> sensorKeys = new ArrayList<>();
+ for (Sensor sensor : sensors) {
+ sensorKeys.add(sensor.getSensorKey());
+ }
+ parameters.put("sensorKeys", sensorKeys);
+ Map<String, Object> pollutionSourceData = historyHourlyMapper.getPollutionSourceDataByHour(parameters);
+ if (MapUtils.isNotEmpty(pollutionSourceData)) {
+ Map<String, String> sensorsMap = sensorService.getSensorsMap(parameters);
+ String selectSensorKey = parameters.get("sensorKey").toString();
+ for (Map.Entry<String, String> s : sensorsMap.entrySet()) {
+ if (selectSensorKey.equals(s.getKey())) {
+ selectSensorKey = s.getValue();
+ }
+ }
+ pollutionSourceData.put("selectSensorKey", selectSensorKey);
+ }
+ return pollutionSourceData;
+ }
+
+ @Override
+ public Map<String, Object> getPollutionSourceDataAll(Map<String, Object> parameters) throws Exception {
+ String yearAndDay = String.valueOf(parameters.get("yearAndDay"));
+ yearAndDay = yearAndDay.replace("-","");
+ parameters.put("yearAndDay",yearAndDay);
+ List<Sensor> sensors = sensorMapper.getSensorsByMac(parameters);
+ List<String> sensorKeys = new ArrayList<>();
+ for (Sensor sensor : sensors) {
+ sensorKeys.add(sensor.getSensorKey());
+ }
+ parameters.put("sensorKeys", sensorKeys);
+ Map<String, Object> pollutionSourceData = historyHourlyMapper.getPollutionSourceDataAll(parameters);
+ if (MapUtils.isNotEmpty(pollutionSourceData)) {
+ Map<String, String> sensorsMap = sensorService.getSensorsMap(parameters);
+ String selectSensorKey = parameters.get("sensorKey").toString();
+ for (Map.Entry<String, String> s : sensorsMap.entrySet()) {
+ if (selectSensorKey.equals(s.getKey())) {
+ selectSensorKey = s.getValue();
+ }
+ }
+ pollutionSourceData.put("selectSensorKey", selectSensorKey);
+ }
+ return pollutionSourceData;
+ }
+
+ @Override
+ public String getTVOCByMac(String mac,String time,String sensor) {
+ return historyHourlyMapper.getTVOCByMac(mac,time,sensor);
+ }
+
+ @Override
+ public String getPressureByMac(String mac, String time) {
+ return historyHourlyMapper.getPressureByMac(mac,time);
+ }
+
+ @Override
+ public Map<String,Object> getDataByMac(String mac, String time) {
+ return historyHourlyMapper.getDataByMac(mac,time);
+ }
+
+ @Override
+ public List<Map<String, Object>> getDataByTimeSlot(String mac, String startTime, String endTime) throws Exception {
+ List<Map<String, Object>> resultMap = historyHourlyMapper.getDataByTimeSlot(mac,startTime,endTime);
+ return resultMap;
+ }
+
+ @Override
+ public List<Map> getDataByMacAndTime(Map param) {
+ String mac = param.get("mac").toString();
+ String sensor = param.get("sensor").toString();
+ String time = param.get("time").toString();
+ String[] times = time.split("���");
+
+ String time1=times[0].substring(0,8);
+ for (int i = 0; i <times.length ; i++) {
+ if (i!=0){
+ times[i]=time1+""+times[i];
+ }
+ }
+ List<List> lists = new ArrayList<List>();
+ List<Map> perList = new ArrayList<Map>();
+ try {
+ for (String perTime : times) {
+ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//���������������MM
+ Date date = simpleDateFormat.parse(perTime);
+ Calendar calendar = Calendar.getInstance();
+ calendar.setTime(date);
+ calendar.add(Calendar.DATE, 1);
+ Date date1 = calendar.getTime();
+ String date2 = simpleDateFormat.format(date1);
+ List<Map> list = historyHourlyMapper.getDataByMacAndTime(mac, sensor, perTime, date2);
+ list.remove(0);
+
+ /*������1-2���������������������������������1���������������������������2���������������������1-24���
+ * ���24���������������������������������������������������������������*/
+ /*������������start----------------------------------------------------------------*/
+ if (list.size() == 23) {
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ //���������������������yyyy-MM-dd HH:mm:ss
+ perTime = perTime + " 00:00:00";
+ Date perTimeDate = sdf.parse(perTime);
+ perTimeDate = DateUtil.rollDay(perTimeDate, 1);
+ Date endTimeDate = DateUtil.rollDay(perTimeDate, 2);
+ //������������������������
+ String startTime = simpleDateFormat.format(perTimeDate);
+ String endTime = simpleDateFormat.format(endTimeDate);
+
+ //������������2020-11-16���������������������������17���0������������������������������������������2020-11-17---2020-11-18
+ List<Map> nextDayList = historyHourlyMapper.getDataByMacAndTime(mac, sensor, startTime, endTime);
+ if(!ObjectUtils.isEmpty(nextDayList)){
+ perTime=perTime.replaceAll(" 00:00:00"," 24");
+ Map<String, Object> datas = nextDayList.get(0);
+ datas.put("time",perTime);
+ list.add(datas);
+ }
+ }
+ /*������������end----------------------------------------------------------------*/
+
+ List<Map> newList = new ArrayList<Map>();
+ for (int i = 0; i <= 24; i++) {
+ Map map=new HashMap();
+ if (i < 10) {
+ map.put("time","0"+i);
+ map.put(sensor,"");
+ newList.add(map);
+ }else {
+ map.put("time",""+i);
+ map.put(sensor,"");
+ newList.add(map);
+ }
+ }
+ if (list.size() <= 24 && list.size() >= 0) {
+ for (int i = 0; i <list.size() ; i++) {
+ String timeS = list.get(i).get("time").toString().substring(11, 13);
+ for (Map map : newList) {
+ if (timeS.equals(map.get("time"))){
+ map.replace(sensor,"",list.get(i).get(sensor));
+ }
+ }
+ }
+ }
+ lists.add(newList);
+ }
+
+ for (int i = 0; i <= 24; i++) {
+ Map<String, Object> map = new HashMap<>();
+ List iList = new ArrayList();
+ for (List<Map> list : lists) {
+ String timeS = list.get(i).get("time").toString();
+ if (i < 10) {
+ if (timeS.equals("0" + i)) {
+ iList.add(list.get(i).get(sensor));
+ } else {
+ iList.add("");
+ }
+ } else {
+ if (timeS.equals(i + "")) {
+ iList.add(list.get(i).get(sensor));
+ } else {
+ iList.add("");
+ }
+ }
+ }
+ map.put("values", iList);
+ map.put("time", i);
+ perList.add(map);
+ }
+
+ } catch (ParseException e) {
+ e.printStackTrace();
+ }
+ /*for (int i = 0; i <perList.size() ; i++) {
+ perList.get(i).replace("time",perList.get(i).get("time"),(Integer) perList.get(i).get("time")+1);
+ }*/
+ int j=0;
+ for (int i = 0; i <perList.size() ; i++) {
+ if (!((List)perList.get(i).get("values")).get(0).equals("")){
+ j=1;
+ }
+ }
+ if (j==0){
+ perList.clear();
+ }
+ return perList;
+ }
+
+ @Override
+ public List<Map<String,Object>> getDataAvbByMIdAndTime(String mId, String startTime, String endTime) {
+ List<List<Map<String,String>>> listArrayList = new ArrayList<>();
+ List<Map<String, Object>> dataAvbByMIdAndTime = historyHourlyMapper.getDataAvbByMIdAndTime(mId, startTime, endTime);
+ List<Map> mapList = new ArrayList<>();
+ for (Map<String, Object> stringStringMap : dataAvbByMIdAndTime) {
+ Map<String,String> map = new HashMap();
+ String s = stringStringMap.get("O3").toString();
+ Object time = stringStringMap.get("time");
+ map.put("O3",s);
+ map.put("time",time.toString());
+ mapList.add(map);
+ }
+ if (mapList.size()>=8){
+ List<Map<String,String>> mapList1 = new ArrayList<>();
+ for (int i = 7; i <=mapList.size()-1 ; i++) {
+ if (mapList.get(i).get("O3")!=null){
+ List<Integer> numList = new ArrayList();
+ Map<String,String> O3_8 = new HashMap<>();
+ for (int j =i; j<= mapList.size(); j--) {
+ if (j<0){
+ break;
+ }
+ numList.add(j);
+ if (numList.size()==8){
+ List e15 = new ArrayList();
+ for (int k = 0; k <numList.size() ; k++) {
+ String value = mapList.get(Integer.valueOf(numList.get(k))).get("O3").toString();
+ e15.add(value);
+ }
+ double sum =0;
+ for (Object o : e15) {
+ sum=sum+Double.parseDouble(o.toString());
+ }
+ Double avg1 = sum/e15.size();
+ int timeOfHour = numList.get(0);
+ if (timeOfHour==23){
+ timeOfHour=0;
+ }else {
+ timeOfHour=timeOfHour+1;
+ }
+ O3_8.put("time", String.valueOf(timeOfHour));
+ O3_8.put("O3_8h", String.valueOf(avg1));
+ mapList1.add(O3_8);
+ }else {
+ continue;
+ }
+ }
+ }
+ listArrayList.add(mapList1);
+ }
+ }
+ Double maxO3=0.0;
+ if(listArrayList.size()>0){
+ List<Map<String, String>> mapList1 = listArrayList.get(0);
+ List<Double> O3List = new ArrayList();
+ for (Map<String, String> stringStringMap : mapList1) {
+ O3List.add(Double.parseDouble(stringStringMap.get("O3_8h")));
+ }
+ maxO3 = Collections.max(O3List);
+ for (int i = 0; i <dataAvbByMIdAndTime.size() ; i++) {
+ dataAvbByMIdAndTime.get(i).put("O3_8h","");
+ int time = Integer.parseInt(((Object)dataAvbByMIdAndTime.get(i).get("time")).toString().substring(11,13)) ;
+ for (int j = 0; j <mapList1.size() ; j++) {
+ int time1 = Integer.parseInt(mapList1.get(j).get("time").toString()) ;
+ if (time==time1){
+ Double O3_8h = Double.parseDouble(mapList1.get(j).get("O3_8h").toString());
+ dataAvbByMIdAndTime.get(i).put("O3_8h",new BigDecimal(O3_8h).setScale(0,BigDecimal.ROUND_HALF_UP).doubleValue());
+ }
+ }
+ }
+ }
+ Map<String, Object> dailyAvgData = historyMapper.getDailyAvgData(mId, startTime);
+ if (dailyAvgData!=null){
+ dailyAvgData.put("maxO3_8h",new BigDecimal(maxO3).setScale(0,BigDecimal.ROUND_HALF_UP).doubleValue());
+ }
+ List<Map<String,Object>> finalList = new ArrayList<>();
+ for (int i = 0; i <dataAvbByMIdAndTime.size() ; i++) {
+ Map<String,Object> timeAndDate = new HashMap();
+ String time = dataAvbByMIdAndTime.get(i).get("time").toString().substring(11,13);
+ dataAvbByMIdAndTime.get(i).remove("time");
+ timeAndDate.put("time",time);
+ timeAndDate.put("data",dataAvbByMIdAndTime.get(i));
+ finalList.add(timeAndDate);
+ }
+ Map<String,Object> timeAndDate = new HashMap();
+ String time = startTime.substring(0,11);
+ timeAndDate.put("time",time);
+ if (dailyAvgData!=null){
+ //List<Map<String, Object>> dailyData = historyHourlyMapper.getDataAvbByMIdAndT;
+ timeAndDate.put("data",dailyAvgData);
+ }else {
+ timeAndDate.put("data","");
+ }
+ finalList.add(timeAndDate);
+ Map<String,Object> map = new HashMap<>();
+ map.put("maxO3",maxO3);
+ finalList.add(map);
+ return finalList;
+ }
+
+ @Override
+ public List<Map<String, Object>> getDataByMonitorPoints(String[] mIds, String startTime, String endTime) {
+ List<Map<String, Object>> dataByMonitorPoints = historyHourlyMapper.getDataByMonitorPoints(mIds, startTime, endTime);
+ List<Map> mapList = new ArrayList<>();
+ for (Map<String, Object> dataByMonitorPoint : dataByMonitorPoints) {
+ Map<String,String> map = new HashMap();
+ String s = dataByMonitorPoint.get("O3").toString();
+ Object time = dataByMonitorPoint.get("time");
+ map.put("O3",s);
+ map.put("time",time.toString());
+ mapList.add(map);
+ }
+ List<List<Map<String,String>>> listArrayList = new ArrayList<>();
+ if (mapList.size()>=8){
+ List<Map<String,String>> mapList1 = new ArrayList<>();
+ for (int i = 7; i <=mapList.size()-1 ; i++) {
+ if (mapList.get(i).get("O3")!=null){
+ List<Integer> numList = new ArrayList();
+ Map<String,String> O3_8 = new HashMap<>();
+ for (int j =i; j<= mapList.size(); j--) {
+ if (j<0){
+ break;
+ }
+ numList.add(j);
+ if (numList.size()==8){
+ List e15 = new ArrayList();
+ for (int k = 0; k <numList.size() ; k++) {
+ String value = mapList.get(Integer.valueOf(numList.get(k))).get("O3").toString();
+ e15.add(value);
+ }
+ double sum =0;
+ for (Object o : e15) {
+ sum=sum+Double.parseDouble(o.toString());
+ }
+ Double avg1 = sum/e15.size();
+ int timeOfHour = numList.get(0);
+ if (timeOfHour==23){
+ timeOfHour=0;
+ }else {
+ timeOfHour=timeOfHour+1;
+ }
+ O3_8.put("time", String.valueOf(timeOfHour));
+ O3_8.put("O3_8h", String.valueOf(avg1));
+ mapList1.add(O3_8);
+ }else {
+ continue;
+ }
+ }
+ }
+ listArrayList.add(mapList1);
+ }
+ }
+ Double maxO3=0.0;
+ if(listArrayList.size()>0){
+ List<Map<String, String>> mapList1 = listArrayList.get(0);
+ List<Double> O3List = new ArrayList();
+ for (Map<String, String> stringStringMap : mapList1) {
+ O3List.add(Double.parseDouble(stringStringMap.get("O3_8h")));
+ }
+ maxO3 = Collections.max(O3List);
+ for (int i = 0; i <dataByMonitorPoints.size() ; i++) {
+ dataByMonitorPoints.get(i).put("O3_8h","");
+ int time = Integer.parseInt(((Object)dataByMonitorPoints.get(i).get("time")).toString().substring(11,13)) ;
+ for (int j = 0; j <mapList1.size() ; j++) {
+ int time1 = Integer.parseInt(mapList1.get(j).get("time").toString()) ;
+ if (time==time1){
+ Double O3_8h = Double.parseDouble(mapList1.get(j).get("O3_8h").toString());
+ dataByMonitorPoints.get(i).put("O3_8h",new BigDecimal(O3_8h).setScale(0,BigDecimal.ROUND_HALF_UP).doubleValue());
+ }
+ }
+ }
+ }
+
+ List<Map<String,Object>> finalList = new ArrayList<>();
+ for (int i = 0; i <dataByMonitorPoints.size() ; i++) {
+ Map<String,Object> timeAndDate = new HashMap();
+ String time = dataByMonitorPoints.get(i).get("time").toString().substring(11,13);
+ dataByMonitorPoints.get(i).remove("time");
+ timeAndDate.put("time",time);
+ timeAndDate.put("data",dataByMonitorPoints.get(i));
+ finalList.add(timeAndDate);
+ }
+ Map<String, Object> dailyAvgData = historyMapper.getDailyAvgDataByMIds(mIds, startTime);
+ if (dailyAvgData!=null){
+ Map<String,Object> timeAndDate = new HashMap();
+ dailyAvgData.put("maxO3_8h",new BigDecimal(maxO3).setScale(0,BigDecimal.ROUND_HALF_UP).doubleValue());
+ timeAndDate.put("data",dailyAvgData);
+ timeAndDate.put("time",startTime.substring(0,11));
+ finalList.add(timeAndDate);
+ }else {
+ Map<String, Object> avgDataByMIds = historyHourlyMapper.getAvgDataByMIds(mIds, startTime, endTime);
+ Map<String,Object> timeAndDate = new HashMap();
+ if (avgDataByMIds == null){
+ avgDataByMIds = new HashMap<>();
+ avgDataByMIds.put("PM2_5","");
+ avgDataByMIds.put("NO2","");
+ avgDataByMIds.put("primary_pollutants","-");
+ avgDataByMIds.put("SO2","");
+ avgDataByMIds.put("O3","");
+ avgDataByMIds.put("PM10","");
+ avgDataByMIds.put("O3_8h","");
+ avgDataByMIds.put("CO","");
+ avgDataByMIds.put("AQI","");
+ }else {
+ avgDataByMIds.put("maxO3_8h",maxO3);
+ }
+
+ timeAndDate.put("data",avgDataByMIds);
+ timeAndDate.put("time",startTime.substring(0,11));
+ finalList.add(timeAndDate);
+ }
+ for (int i=0;i<finalList.size();i++) {
+ if (i!=finalList.size()-1){
+ Map<String, Object> map1 = AQICalculation2.hourlyAQI((Map<String, Object>) finalList.get(i).get("data"));
+ if (Double.parseDouble(map1.get("AQI").toString())>50.0){
+ ((Map<String, Object>) finalList.get(i).get("data")).put("primary_pollutants",map1.get("maxSensor"));
+ ((Map<String, Object>) finalList.get(i).get("data")).put("AQI",map1.get("AQI"));
+ }else {
+ ((Map<String, Object>) finalList.get(i).get("data")).put("primary_pollutants","-");
+ ((Map<String, Object>) finalList.get(i).get("data")).put("AQI",map1.get("AQI"));
+ }
+ }else {
+ Map<String, Object> map1 = AQICalculation2.dayAQI((Map<String, Object>) finalList.get(i).get("data"));
+ if (map1.get("AQI").toString().equals("")){
+ break;
+ }
+ if (Double.parseDouble(map1.get("AQI").toString())>=50.0){
+ ((Map<String, Object>) finalList.get(i).get("data")).put("primary_pollutants",map1.get("maxSensor"));
+ ((Map<String, Object>) finalList.get(i).get("data")).put("AQI",map1.get("AQI"));
+ }else {
+ ((Map<String, Object>) finalList.get(i).get("data")).put("primary_pollutants","-");
+ ((Map<String, Object>) finalList.get(i).get("data")).put("AQI",map1.get("AQI"));
+ }
+ }
+ }
+ Map<String,Object> timeAndDate = new HashMap();
+ String time = startTime.substring(0,11);
+ timeAndDate.put("time",time);
+ if (dailyAvgData!=null){
+ timeAndDate.put("data",dailyAvgData);
+ }else {
+ Map map = new HashMap<>();
+ Map map1 = new HashMap();
+ map1.put("PM2_5","");
+ map1.put("NO2","");
+ map1.put("primary_pollutants","-");
+ map1.put("SO2","");
+ map1.put("O3","");
+ map1.put("PM10","");
+ map1.put("O3_8h","");
+ map1.put("CO","");
+ map1.put("AQI","");
+ map.put("data",map1);
+ timeAndDate.put("data",map);
+ }
+ List timeList = new ArrayList();
+ for (int q = 0; q < finalList.size(); q++) {
+ timeList.add(finalList.get(q).get("time"));
+ }
+ if (finalList.size()<25){
+ for (int j = 1; j < 25; j++) {
+ Map<String,Object> map = null;
+ for (int q = 0; q < finalList.size(); q++) {
+ if (finalList.get(q).get("time").toString().length() > 2){
+ continue;
+ }else {
+ String sj="";
+ if (j<10){
+ sj="0"+j;
+ }else {
+ if (j==24){
+ sj="00";
+ }else {
+ sj=""+j;
+ }
+ }
+ if (!timeList.contains(sj)){
+ map = new HashMap<>();
+ Map map1 = new HashMap();
+ map1.put("PM2_5","");
+ map1.put("NO2","");
+ map1.put("primary_pollutants","");
+ map1.put("SO2","");
+ map1.put("O3","");
+ map1.put("PM10","");
+ map1.put("O3_8h","");
+ map1.put("CO","");
+ map1.put("AQI","");
+ map.put("time",sj);
+ map.put("data",map1);
+ finalList.add(map);
+ break;
+ }else {
+ break;
+ }
+ }
+ }
+
+ }
+ }
+ for (int j = 0; j <finalList.size() ; j++) {
+ if (finalList.get(j).get("time").toString().length()>2){
+ Map map2 = finalList.get(j);
+ finalList.remove(j);
+ finalList.add(map2);
+ }
+ }
+ return finalList;
+ }
+
+ @Override
+ public Map<String, Object> getAvgDataByMId(String mId, String startTime, String endTime) {
+ return historyHourlyMapper.getAvgDataByMId(mId,startTime,endTime);
+ }
+
+ @Override
public Point getDirPoint(Map<String, Object> parameters) throws Exception {
- Map<String, Object> pollutionSourceData = getPollutionSourceData(parameters);
+ Map<String, Object> pollutionSourceData = getPollutionSourceDataByHour(parameters);
+ if (MapUtils.isEmpty(pollutionSourceData)) {
+ pollutionSourceData = getPollutionSourceDataAll(parameters);
+ }
String mac = parameters.get("mac").toString();
Device device = deviceService.getDeviceByMac(mac, false);
Point pointEnd = new Point();
if (MapUtils.isNotEmpty(pollutionSourceData)) {
- if (pollutionSourceData.get("e18") != null && pollutionSourceData.get("e23") != null && pollutionSourceData.get("e6") != null) {
+ if (pollutionSourceData.get("e18") != null && pollutionSourceData.get("e23") != null) {
double windSpeed = Double.valueOf(pollutionSourceData.get("e18").toString());
double winDir = Double.valueOf(pollutionSourceData.get("e23").toString());
double distance = windSpeed * 3600;
--
Gitblit v1.8.0