| | |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.LocalDateTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.Collection; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.Iterator; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Optional; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | import javax.annotation.Resource; |
| | |
| | | Map<String, Object> parameters = getParametersStartingWith(request, null); |
| | | weatherService.updateForecastWeather(parameters); |
| | | } |
| | | |
| | | @GetMapping("dataByTimeSlot") |
| | | @ApiOperation(value = "根据时间类型获取因子的值", notes = "根据时间类型获取因子的值") |
| | | @ApiImplicitParams(value = { |
| | | @ApiImplicitParam(name = "mac", value = "mac地址", required = true, paramType = "query", dataType = "String"), |
| | | @ApiImplicitParam(name = "startTime", value = "起始时间(格式:2020-08-03/2020-08-03-13)", required = true, paramType = "query", dataType = "String"), |
| | | @ApiImplicitParam(name = "endTime", value = "结束时间(格式:2020-08-04/2020-08-04-14)", required = true, paramType = "query", dataType = "String")}) |
| | | public ResultBean<List<Map<String, Object>>> getDataByTimeSlot(HttpServletRequest request) throws Exception { |
| | | Map<String, Object> parameters = getParametersStartingWith(request, null); |
| | | String mac = parameters.get("mac").toString(); |
| | | String startTime = parameters.get("startTime").toString(); |
| | | String endTime = parameters.get("endTime").toString(); |
| | | //对时间进行操作的接口 |
| | | Calendar calendar = Calendar.getInstance(); |
| | | DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); |
| | | List<Map<String, Object>> list = new ArrayList(); |
| | | Map device = deviceService.getAllFieldByMac(parameters); |
| | | Map monitorPointMap = monitorPointService.selectAllById(device.get("monitor_point_id").toString()); |
| | | Map<String, String> sensotMap = sensorService.getSensorsMap(parameters); |
| | | if (startTime.length()==10&&endTime.length()==10){ |
| | | //获取当日时间 |
| | | Date dd = new Date(); |
| | | //格式化 |
| | | SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd"); |
| | | //获取系统当前时间 |
| | | String nowTime = sim.format(dd); |
| | | int days = endTime.compareTo(nowTime); |
| | | System.out.println(days); |
| | | /*if (days<0){ |
| | | Date date=null; |
| | | try { |
| | | date = new SimpleDateFormat("yy-MM-dd").parse(endTime); |
| | | } catch (ParseException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | calendar.setTime(date); |
| | | int day=calendar.get(Calendar.DATE); |
| | | calendar.set(Calendar.DATE,day+1); |
| | | |
| | | endTime=new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()); |
| | | }*/ |
| | | List<Map<String, Object>> sameDayHourlyList = null; |
| | | if (days>=0){ |
| | | SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | //获取系统当前时间 |
| | | String currentTime = df.format(dd); |
| | | sameDayHourlyList = historyHourlyService.getDataByTimeSlot(mac,nowTime,currentTime); |
| | | } |
| | | List<Map<String, Object>> dataList = historyDailyService.getDataByTimeSlot(mac,startTime,endTime); |
| | | Set<String> keys = sensotMap.keySet(); |
| | | for (String key:keys) { |
| | | Map<String,Object> oneSensorMap = new LinkedHashMap<>(); |
| | | oneSensorMap.put("monitorPointName",monitorPointMap.get("name")); |
| | | oneSensorMap.put("deviceName",device.get("name")); |
| | | oneSensorMap.put("sersorKey",sensotMap.get(key)); |
| | | for (Map<String, Object> dataMap: dataList) { |
| | | JSONObject dataJson = JSONObject.parseObject(dataMap.get("json").toString()); |
| | | oneSensorMap.put(dataMap.get("time").toString().substring(0,10),dataJson.getJSONArray(key).get(2)); |
| | | } |
| | | Double avg = 0.0; |
| | | if (sameDayHourlyList != null){ |
| | | for (Map sameDayHourlyMap:sameDayHourlyList){ |
| | | List<Double> arr = new ArrayList<>(); |
| | | JSONObject hourlyJson = JSONObject.parseObject(sameDayHourlyMap.get("json").toString()); |
| | | arr.add(Double.parseDouble(hourlyJson.getJSONArray(key).get(2).toString())); |
| | | Double sum = 0.0; |
| | | for (int i=0;i<arr.size();i++){ |
| | | sum += arr.get(i); |
| | | } |
| | | avg = sum / arr.size(); |
| | | } |
| | | oneSensorMap.put(nowTime,avg); |
| | | } |
| | | list.add(oneSensorMap); |
| | | } |
| | | }else if (startTime.length()==13&&endTime.length()==13){ |
| | | StringBuilder startTime_sb = new StringBuilder(startTime); |
| | | startTime_sb.replace(10,11," "); |
| | | startTime = startTime_sb.toString()+":00:00"; |
| | | StringBuilder endTime_sb = new StringBuilder(endTime); |
| | | endTime_sb.replace(10,11," "); |
| | | endTime = endTime_sb.toString()+":00:00"; |
| | | |
| | | List<Map<String, Object>> hourlyList = historyHourlyService.getDataByTimeSlot(mac,startTime,endTime); |
| | | Set<String> keys = sensotMap.keySet(); |
| | | for (String key:keys) { |
| | | Map<String,Object> oneSensorMap = new LinkedHashMap<>(); |
| | | oneSensorMap.put("monitorPointName",monitorPointMap.get("name")); |
| | | oneSensorMap.put("deviceName",device.get("name")); |
| | | oneSensorMap.put("sersorKey",sensotMap.get(key)); |
| | | for (Map<String, Object> hourlyMap: hourlyList) { |
| | | JSONObject hourlyJson = JSONObject.parseObject(hourlyMap.get("json").toString()); |
| | | oneSensorMap.put(hourlyMap.get("time").toString().substring(0,19),hourlyJson.getJSONArray(key).get(2)); |
| | | |
| | | } |
| | | list.add(oneSensorMap); |
| | | } |
| | | }else { |
| | | return null; |
| | | } |
| | | return new ResultBean<List<Map<String, Object>>>(list); |
| | | } |
| | | } |