package com.mh.user.job; import com.mh.user.constants.Constant; import com.mh.user.serialport.SerialPortSendReceive; import com.mh.user.serialport.SerialPortThread; import com.mh.user.service.DeviceCodeParamService; import com.mh.user.service.DealDataService; import com.mh.user.utils.AnalysisReceiveOrder485; import com.mh.user.utils.ComThreadPoolService; import com.mh.user.utils.GetReadOrder485; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ThreadPoolExecutor; /** * @author ljf * @title :定时处理采集回来的历史数据 * @description : * @updateTime 2020-07-28 * @throws : */ @Slf4j @Component public class DealDataJob { private final DealDataService dealDataService; @Autowired private DeviceCodeParamService deviceCodeParamService; private static int taskTimes = 1; public DealDataJob(DealDataService dealDataService) { this.dealDataService = dealDataService; } ThreadPoolExecutor comThreadPool = ComThreadPoolService.getInstance(); /** * 定时处理汇总数据:每15分钟处理一次,十分钟(0 0/10 * * * ?) */ @Scheduled(cron = "0 0/15 * * * ?") public void ProEnergy() { try { SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:00:00"); Date date = new Date(); String curDate = sdf1.format(date); String name = dealDataService.customName(); if (name != null && name.contains("华夏学院")) { dealDataService.proEnergy2(curDate); } else { dealDataService.proEnergy(curDate); //yyyy-MM-dd HH:00:00 } dealDataService.proGatewayState(); //判断网关在线状态:在线或离线 log.info("进入定时调试数据库过程汇总数据!yyyy-MM-dd HH:00:00"); } catch (Exception e) { log.error("定时处理数据汇总异常==>", e); } } /** * 采集 */ @Scheduled(cron = "35 0/2 * * * ?") // @Scheduled(cron = "0/10 * * * * ?") 0 0/5 * * * ? // @Scheduled(cron = "0 0/5 * * * ?") //5分钟 public void collect() { try { log.info("------定时采集开始>>>>Constant.FLAG==" + Constant.FLAG + "------"); if (!Constant.FLAG) { if (!Constant.WEB_FLAG) { Constant.FLAG = true; log.info("------Constant.WEB_FLAG==" + false + "------"); for (int i = 1; i <= 4; i++) { if (!Constant.WEB_FLAG) { break; } String threadName; if (i == 1 || i == 3) { threadName = "1"; log.info("------采集水位、水温!" + i + "------"); } else if (i == 2) { threadName = "2"; log.info("------采集水、电、运行状态!" + i + "------"); } else { threadName = "3"; log.info("------采集设定温度、设定水位、故障状态!" + i + "------"); } CountDownLatch countDownLatch = new CountDownLatch(10); for (int j = 1; j < 11; j++) { if (!Constant.WEB_FLAG) { break; } SerialPortThread myThread = new SerialPortThread(); myThread.setName(threadName, String.valueOf(j), countDownLatch); Thread thread = new Thread(myThread); comThreadPool.execute(thread); } // 等待执行完成 countDownLatch.await(); // 释放资源 countDownLatch = null; } } } } catch (Exception e) { log.error("定时采集异常==>", e); } finally { Constant.FLAG = false; log.info("------定时采集结束>>>>Constant.FLAG==" + Constant.FLAG + "------"); } } /** * 定时处理数据:每十五分钟处理一次 */ @Scheduled(cron = "0 0/15 * * * ?") public void dealData() { try { SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd"); Date date = new Date(); String curDate = sdf1.format(date); String name = dealDataService.customName(); if (name != null && name.length() > 0 && name.contains("华夏学院")) { dealDataService.proEnergySum2(curDate); } else { dealDataService.proEnergySum(curDate); } dealDataService.proMaintainSum(curDate); //汇总维修数 dealDataService.proAlarmManage(curDate); //报警信息 dealDataService.proAlarmInfoSum(curDate); //汇总相关警报数 dealDataService.proAnalysisMonth(curDate); dealDataService.proAnalysisYear(curDate); dealDataService.proDeviceState(curDate); //汇总设备状态 dealDataService.proTotalPumpMinutes(curDate); //统计周\月热泵运行时长 log.info("进入定时调试数据库过程汇总数据!yyyy-MM-dd"); } catch (Exception e) { log.error("定时处理数据异常==>", e); } } /** * 定时删除历史流水记录(删除前三个月的记录) */ // @Scheduled(cron = "0 0 0 1 1/1 ? ") // public void deleteDataHistory() { // try { // dealDataService.deleteChillersDataHistory(); // } catch (Exception e) { // e.printStackTrace(); // } // } }