New file |
| | |
| | | package com.moral.task; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.moral.service.HistoryFiveMinutelyService; |
| | | import com.moral.service.SensorService; |
| | | import com.moral.util.DateUtil; |
| | | import com.xxl.job.core.biz.model.ReturnT; |
| | | import com.xxl.job.core.handler.annotation.XxlJob; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.ObjectUtils; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | |
| | | @Component |
| | | public class HistoryFiveMinutelyTask { |
| | | @Resource |
| | | HistoryFiveMinutelyService historyFiveMinutelyService; |
| | | |
| | | @Resource |
| | | SensorService sensorService; |
| | | |
| | | @XxlJob("createHistoryFiveMinutelyTb") |
| | | public ReturnT createHistoryMinutelyTb(String param) { |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | Date date = new Date(); |
| | | date = DateUtil.rollMon(date, 1); |
| | | String year = DateUtil.getYear(date); |
| | | String month = DateUtil.getMonth(date).length() == 1 ? "0".concat(DateUtil.getMonth(date)) : DateUtil.getMonth(date); |
| | | try { |
| | | historyFiveMinutelyService.createHistoryFiveMinutelyTable(year + month); |
| | | } catch (Exception e) { |
| | | return new ReturnT(500, "建立五分钟表失败"); |
| | | } |
| | | return new ReturnT(200, "建立五分钟表成功"); |
| | | } |
| | | |
| | | |
| | | /*分钟表插入时间为每分钟的第二十秒 |
| | | * 分钟数据读取时间,每五分钟的第三十秒开始读取 |
| | | * EG: 10:15:30 插入10:10--10:15的数据 |
| | | * 10:20:30 插入10:15--10:20的数据*/ |
| | | @XxlJob("insertHistoryFiveMinutelyTb") |
| | | public ReturnT insertHistoryFiveMinutely(String param) { |
| | | /*根据时间从分钟表数据钟查询五分钟数据的平均值*/ |
| | | Map<String, Object> params = getStartAndEndTime(); |
| | | List<String> sensorKeys = sensorService.getSensorKeys(); |
| | | params.put("sensorKeys", sensorKeys); |
| | | List<Map<String, Object>> fiveMinutesSensorDatas; |
| | | try { |
| | | fiveMinutesSensorDatas = historyFiveMinutelyService.getFiveMinutesSensorData(params); |
| | | } catch (Exception e) { |
| | | return new ReturnT(500, "查询分钟表数据失败"); |
| | | } |
| | | |
| | | /*将得到的数据进行转换*/ |
| | | List<Map<String, Object>> insertDatas; |
| | | try { |
| | | insertDatas = new ArrayList<>(); |
| | | for (Map<String, Object> data : fiveMinutesSensorDatas) { |
| | | String mac = (String) data.get("mac"); |
| | | data.remove("mac"); |
| | | Map<String, Object> keyAndValueMap = new LinkedHashMap<>(); |
| | | Map<String, Object> insertDataMap = new LinkedHashMap<>(); |
| | | data.forEach((key, value) -> { |
| | | key = key.substring(3); |
| | | List<Object> list = null; |
| | | if (ObjectUtils.isEmpty(keyAndValueMap.get(key))) { |
| | | list = new ArrayList<>(); |
| | | } else { |
| | | list = (List<Object>) keyAndValueMap.get(key); |
| | | } |
| | | if (value instanceof Double) { |
| | | value = String.valueOf(value); |
| | | value = value.equals("0.0") ? 0 : Double.valueOf((String) value); |
| | | } |
| | | list.add(value); |
| | | keyAndValueMap.put(key, list); |
| | | }); |
| | | String keyAndValueJson = JSON.toJSONString(keyAndValueMap); |
| | | insertDataMap.put("mac", mac); |
| | | insertDataMap.put("time", params.get("start")); |
| | | insertDataMap.put("json", keyAndValueJson); |
| | | insertDatas.add(insertDataMap); |
| | | } |
| | | } catch (Exception e) { |
| | | return new ReturnT(500, "数据转换异常"); |
| | | } |
| | | |
| | | /*将数据插入数据库*/ |
| | | historyFiveMinutelyService.insertHistoryFiveMinutely(insertDatas, (String) params.get("yearAndMonth")); |
| | | return new ReturnT(200, "插入数据成功"); |
| | | } |
| | | |
| | | /** |
| | | * @Description: 根据当前时间获取到查询的开始时间和结束时间以及年月字符串 |
| | | * @Param: |
| | | * @return: |
| | | * @Author: 下雨听风 |
| | | * @Date: 2020/10/15 |
| | | */ |
| | | private Map<String, Object> getStartAndEndTime() { |
| | | Map<String, Object> map = new HashMap<>(); |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | Date date = new Date(); |
| | | String minute = DateUtil.getMinute(date); |
| | | String year = ""; |
| | | String month = ""; |
| | | String yearAndMonth = ""; |
| | | String startTime = ""; |
| | | String endTime = ""; |
| | | Integer endMinute = Integer.parseInt(String.valueOf(minute.charAt(minute.length() - 1))); |
| | | if (endMinute >= 5) { |
| | | StringBuilder time = new StringBuilder(sdf.format(date)); |
| | | startTime = time.replace(15, 19, "0:00").toString(); |
| | | endTime = time.replace(15, 19, "4:00").toString(); |
| | | } else { |
| | | date = DateUtil.rollMinute(date, -5); |
| | | StringBuilder time = new StringBuilder(sdf.format(date)); |
| | | startTime = time.replace(15, 19, "5:00").toString(); |
| | | endTime = time.replace(15, 19, "9:00").toString(); |
| | | } |
| | | |
| | | year = DateUtil.getYear(date); |
| | | month = DateUtil.getMonth(date); |
| | | yearAndMonth = year + month; |
| | | map.put("start", startTime); |
| | | map.put("end", endTime); |
| | | map.put("yearAndMonth", yearAndMonth); |
| | | return map; |
| | | } |
| | | |
| | | } |