| | |
| | | import com.moral.service.HistoryService; |
| | | import com.moral.service.OrganizationService; |
| | | import com.moral.service.SensorService; |
| | | import com.moral.util.DateUtil; |
| | | import com.moral.util.WeatherUtil; |
| | | import com.xxl.job.core.biz.model.ReturnT; |
| | | import com.xxl.job.core.handler.annotation.XxlJob; |
| | |
| | | |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.CollectionUtils; |
| | | import org.springframework.util.ObjectUtils; |
| | |
| | | @Resource |
| | | private WeatherUtil weatherUtil; |
| | | |
| | | @Resource |
| | | RedisTemplate redisTemplate; |
| | | |
| | | @XxlJob("historyMinutely") |
| | | public ReturnT insertHistoryMinutelyTable(String params) { |
| | | LocalDateTime time = LocalDateTime.now(); |
| | | int year = time.getYear(); |
| | | int month = time.getMonthValue(); |
| | | int day = time.getDayOfMonth(); |
| | | int hour = time.getHour(); |
| | | int minute = time.getMinute(); |
| | | if (day == 1) { |
| | | if (hour == 0 && minute == 0) { |
| | | if (month == 1) { |
| | | month = 12; |
| | | year = year - 1; |
| | | } else { |
| | | month = month - 1; |
| | | } |
| | | } |
| | | } |
| | | String monthStr = month < 10 ? ("0" + month) : month + ""; |
| | | String yearAndMonth = year + monthStr; |
| | | LocalDateTime endTime = time.truncatedTo(ChronoUnit.MINUTES); |
| | | LocalDateTime startTime = endTime.minusMinutes(1); |
| | | List<String> sensorKeys = sensorService.getSensorKeys(); |
| | | //正式环境使用 |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:00"); |
| | | Date insertTimeDate = new Date(); |
| | | Date startTimeDate = DateUtil.rollMinute(insertTimeDate, -2); |
| | | Date endTimeDate = DateUtil.rollMinute(insertTimeDate, -1); |
| | | StringBuilder insertTime = new StringBuilder(sdf.format(insertTimeDate)); |
| | | StringBuilder endTime = new StringBuilder(sdf.format(endTimeDate)); |
| | | StringBuilder startTime = new StringBuilder(sdf.format(startTimeDate)); |
| | | String queryYearAndMonthDay = DateUtil.getYear(startTimeDate) + DateUtil.getMonth(startTimeDate) + DateUtil.getDay(startTimeDate); |
| | | |
| | | //测试试用 |
| | | /*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | Date endTimeDate = sdf.parse("2020-11-01 00:00:20"); |
| | | Date startTimeDate = DateUtil.rollMinute(endTimeDate, -1); |
| | | StringBuilder endTime = new StringBuilder(sdf.format(endTimeDate)); |
| | | StringBuilder startTime = new StringBuilder(sdf.format(startTimeDate)); |
| | | endTime.replace(17, 19, "00"); |
| | | startTime.replace(17, 19, "00"); |
| | | String queryYearAndMonthDay = DateUtil.getYear(startTimeDate) + DateUtil.getMonth(startTimeDate) + DateUtil.getDay(startTimeDate);*/ |
| | | |
| | | Map<String, Object> devices = new HashMap<>(); |
| | | List<String> sensorKeys = sensorService.getSensorKeys(); |
| | | devices.put("sensorKeys", sensorKeys); |
| | | devices.put("start", startTime); |
| | | devices.put("end", endTime); |
| | | devices.put("start", startTime.toString()); |
| | | devices.put("end", endTime.toString()); |
| | | devices.put("yearAndMonthDay", queryYearAndMonthDay); |
| | | |
| | | try { |
| | | List<Map<String, Object>> minutelyData = deviceService.getSensorData(devices); |
| | | XxlJobLogger.log("historyMinutelyData:" + minutelyData.size()); |
| | |
| | | for (Map<String, Object> deviceData : minutelyData) { |
| | | if (!ObjectUtils.isEmpty(deviceData)) { |
| | | Map<String, Object> minutelyDataMap = new LinkedHashMap<>(); |
| | | JSONObject jo = new JSONObject(true); |
| | | Map<String, Object> map = new HashMap<>(); |
| | | minutelyDataMap.put("mac", deviceData.get("mac")); |
| | | minutelyDataMap.put("time", startTime); |
| | | JSONArray jsonArray = new JSONArray(); |
| | | minutelyDataMap.put("time", insertTime.toString()); |
| | | for (String key : deviceData.keySet()) { |
| | | if (!key.equals("mac") && !key.startsWith("M")) { |
| | | List<Object> date = new ArrayList<>(); |
| | | date.add(deviceData.get(key)); |
| | | date.add(deviceData.get("MIN" + key)); |
| | | date.add(deviceData.get("MAX" + key)); |
| | | jo.put(key, date); |
| | | map.put(key, date); |
| | | } |
| | | } |
| | | jsonArray.add(jo); |
| | | minutelyDataMap.put("json", jsonArray.get(0).toString()); |
| | | minutelyDataMap.put("json", JSONObject.toJSONString(map)); |
| | | minutelyDataList.add(minutelyDataMap); |
| | | } |
| | | } |
| | | if (!CollectionUtils.isEmpty(minutelyDataList)) { |
| | | historyMinutelyService.insertHistoryMinutely(minutelyDataList, yearAndMonth); |
| | | String insertYearAndMonth = DateUtil.getYear(endTimeDate) + DateUtil.getMonth(endTimeDate); |
| | | historyMinutelyService.insertHistoryMinutely(minutelyDataList, insertYearAndMonth); |
| | | ReturnT returnT = new ReturnT(200, "插入分钟表成功"); |
| | | return returnT; |
| | | } |
| | |
| | | XxlJobLogger.log("historyMinutelyException:" + e.getMessage()); |
| | | logger.error(e.getMessage()); |
| | | e.printStackTrace(); |
| | | ReturnT returnT = new ReturnT(500, e.getMessage()); |
| | | return returnT; |
| | | } |
| | | List record = new ArrayList(); |
| | | record.add("repairMinutelyData_"+endTimeDate.getTime()/1000); |
| | | redisTemplate.opsForList().leftPushAll("unrepair_data", record); |
| | | ReturnT returnT = new ReturnT(500, "插入分钟表失败"); |
| | | return returnT; |
| | | } |
| | | |
| | | @XxlJob("historyHourly") |
| | | public ReturnT insertHistoryHourlyTable(String params) { |
| | | LocalDateTime time = LocalDateTime.now(); |
| | | int year = time.getYear(); |
| | | int month = time.getMonthValue(); |
| | | int day = time.getDayOfMonth(); |
| | | int hour = time.getHour(); |
| | | if (day == 1) { |
| | | if (hour == 0) { |
| | | if (month == 1) { |
| | | month = 12; |
| | | year = year - 1; |
| | | } else { |
| | | month = month - 1; |
| | | } |
| | | } |
| | | } |
| | | String monthStr = month < 10 ? ("0" + month) : month + ""; |
| | | String yearAndMonth = year + monthStr; |
| | | LocalDateTime endTime = time.truncatedTo(ChronoUnit.HOURS); |
| | | LocalDateTime startTime = endTime.minusHours(1); |
| | | //正式环境使用 |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:00:00"); |
| | | Date endTimeDate = new Date(); |
| | | Date startTimeDate = DateUtil.rollHour(endTimeDate, -1); |
| | | StringBuilder endTime = new StringBuilder(sdf.format(endTimeDate)); |
| | | StringBuilder startTime = new StringBuilder(sdf.format(startTimeDate)); |
| | | String yearAndMonth = DateUtil.getYear(startTimeDate) + DateUtil.getMonth(startTimeDate); |
| | | |
| | | //测试环境使用,用于手动指定时间段 |
| | | /* SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | Date endTimeDate = sdf.parse("2020-11-01 00:00:22"); |
| | | Date startTimeDate = DateUtil.rollHour(endTimeDate, -1); |
| | | StringBuilder endTime = new StringBuilder(sdf.format(endTimeDate)); |
| | | StringBuilder startTime = new StringBuilder(sdf.format(startTimeDate)); |
| | | endTime.replace(14, 19, "00:00"); |
| | | startTime.replace(14, 19, "00:00"); |
| | | String yearAndMonth = DateUtil.getYear(startTimeDate) + DateUtil.getMonth(startTimeDate);*/ |
| | | |
| | | List<String> sensorKeys = sensorService.getSensorKeys(); |
| | | List<String> macs = deviceService.getMacs(); |
| | | Map<String, Object> devices = new HashMap<>(); |
| | | devices.put("sensorKeys", sensorKeys); |
| | | devices.put("start", startTime); |
| | | devices.put("end", endTime); |
| | | devices.put("start", startTime.toString()); |
| | | devices.put("end", endTime.toString()); |
| | | devices.put("macs", macs); |
| | | devices.put("yearAndMonth", yearAndMonth); |
| | | |
| | | try { |
| | | List<Map<String, Object>> hourlyData = historyMinutelyService.getMinutelySensorData(devices); |
| | | XxlJobLogger.log("historyHourlyData:" + hourlyData.size()); |
| | |
| | | Map<String, Object> hourlyDataMap = new LinkedHashMap<>(); |
| | | JSONObject jo = new JSONObject(true); |
| | | hourlyDataMap.put("mac", deviceData.get("mac")); |
| | | hourlyDataMap.put("time", startTime); |
| | | hourlyDataMap.put("time", endTime.toString()); |
| | | JSONArray jsonArray = new JSONArray(); |
| | | for (String key : deviceData.keySet()) { |
| | | if (!key.equals("mac") && !key.startsWith("M")) { |
| | |
| | | e.printStackTrace(); |
| | | } |
| | | ReturnT returnT = new ReturnT(500, "插入小时表失败"); |
| | | List record = new ArrayList(); |
| | | record.add("repairHourlyData_"+endTimeDate.getTime()/1000); |
| | | redisTemplate.opsForList().leftPushAll("unrepair_data", record); |
| | | return returnT; |
| | | } |
| | | |
| | |
| | | String s1=format.format(date1); |
| | | Map<String, Object> devices = new HashMap<>(); |
| | | devices.put("macList", macList); |
| | | devices.put("startTime", s); |
| | | devices.put("endTime", s1); |
| | | devices.put("startTime", s1); |
| | | devices.put("endTime", s); |
| | | try { |
| | | int count = historyService.insertHistorySpecialTable(devices); |
| | | XxlJobLogger.log("insertHistorySpecialTable:" + count); |