这两天在做一个物联网的项目,设备是智能断漏器,使用场景,固定时间关闭,固定时间打开。也就是固定时间开电,固定时间关电。设置了一个一张表用于存储需要执行的任务。界面如下:
根据上边提供的时间,如果时间到了,就执行调用设备对应的远程控制指令。但是,需要有一个定时器实时监测。
如下便是关于 springboot 的@Scheduled 定时器
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.List;
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.Order; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component;
import com.ruoyi.system.domain.FireLeakbreakerTimer; import com.ruoyi.system.service.IFireLeakbreakerDeviceService; import com.ruoyi.system.service.IFireLeakbreakerTimerService; /*** * 断漏器定时开关 * @author jason * */ @Component @Configuration @EnableScheduling //启用调度器 public class ScheduleJobInitListenerLeakBreaker{ private Logger logger = LoggerFactory.getLogger(getClass()); @Autowired IFireLeakbreakerTimerService iFireLeakbreakerTimerService; @Autowired IFireLeakbreakerDeviceService iFireLeakbreakerDeviceService; @Scheduled(cron="0 */5 * * * ?") //这里是没5分钟执行一次 public void run() throws Exception { logger.info("ScheduleJobInitListenerLeakBreaker start .... "); FireLeakbreakerTimer fireLeakbreakerTimer = new FireLeakbreakerTimer(); //批量打开 List<FireLeakbreakerTimer> openList = iFireLeakbreakerTimerService.selectFireLeakbreakerTimerListByOpenTime();//这里查询对应的任务信息 for (FireLeakbreakerTimer fireLeakbreakerTimer2 : openList) { if(fireLeakbreakerTimer2.getNextOpenTimer().after(new Date())){ //调用打开指令。 iFireLeakbreakerDeviceService.openDeviceCommand(fireLeakbreakerTimer2.getDeviceId()+""); //更新断漏器定时任务下次打开时间 fireLeakbreakerTimer2.setNextOpenTimer(getDate(fireLeakbreakerTimer2.getOpenTimerHour(),fireLeakbreakerTimer2.getOpenTimerMinute())); iFireLeakbreakerTimerService.updateFireLeakbreakerTimer(fireLeakbreakerTimer2); } } //批量关闭 List<FireLeakbreakerTimer> closeList = iFireLeakbreakerTimerService.selectFireLeakbreakerTimerListByCloseTime();//这里查询对应的任务信息 for (FireLeakbreakerTimer fireLeakbreakerTimer2 : closeList) { if(fireLeakbreakerTimer2.getNextOpenTimer().after(new Date())){ //调用关闭指令。 iFireLeakbreakerDeviceService.closeDeviceCommand(fireLeakbreakerTimer2.getDeviceId()+""); //更新断漏器定时任务下次关闭时间 fireLeakbreakerTimer2.setNextCloseTimer(getDate(fireLeakbreakerTimer2.getCloseTimerHour(),fireLeakbreakerTimer2.getCloseTimerMinute())); iFireLeakbreakerTimerService.updateFireLeakbreakerTimer(fireLeakbreakerTimer2); } } } /*** * 获取当天时间并且天数加1,加上传入的小时和分钟 * @param hours * @param minute * @return * @throws ParseException */ private Date getDate(String hours,String minute) throws ParseException{ Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, 1); int day = cal.get(Calendar.DATE); int month = cal.get(Calendar.MONTH) + 1; int year = cal.get(Calendar.YEAR); String date = year+"-"+month+"-"+day+" " +hours+":"+minute; SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm"); return dateFormat.parse(date); } }