StaticScheduleTask.java 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package com.sooka.sponest.dataexchange.scheduleTask;
  2. import cn.hutool.json.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.ruoyi.common.core.utils.DateUtils;
  5. import com.sooka.sponest.dataexchange.monitoringEquipment.inserctpests.service.PlantDiseasesAndInsectPestsService;
  6. import com.sooka.sponest.dataexchange.monitoringEquipment.sennor.service.SennorService;
  7. import org.apache.commons.lang3.time.FastDateFormat;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. import org.springframework.beans.factory.annotation.Value;
  11. import org.springframework.context.annotation.Configuration;
  12. import org.springframework.scheduling.annotation.EnableScheduling;
  13. import org.springframework.scheduling.annotation.Scheduled;
  14. import javax.annotation.Resource;
  15. import java.text.ParseException;
  16. import java.util.Calendar;
  17. /**
  18. * @author limeng
  19. * @date 2023年11月30日 8:50
  20. */
  21. @Configuration
  22. @EnableScheduling
  23. public class StaticScheduleTask {
  24. protected static final Logger logger = LoggerFactory.getLogger(StaticScheduleTask.class);
  25. private static FastDateFormat simpleDateFormat = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
  26. @Resource
  27. private SennorService sennorService;
  28. @Resource
  29. private PlantDiseasesAndInsectPestsService plantDiseasesAndInsectPestsService;
  30. /**精讯物联网设备类型**/
  31. @Value("${monitoringEquipment.sennor.sennorDeviceTypes:}")
  32. public String sennorDeviceTypes;
  33. /**精讯物联网病虫害设备类型**/
  34. @Value("${monitoringEquipment.insect.sennorInsectDeviceType:}")
  35. public String sennorInsectDeviceType;
  36. // @Scheduled(cron = "0 0 0/2 * * ?")//添加定时任务 每2小时执行
  37. // @Scheduled(cron = "0 */5 * * * ?")//添加定时任务 每1分钟执行
  38. private void configureTasks() {
  39. logger.info("执行物联网数据采集定时任务=>{}", DateUtils.dateTimeNow());
  40. JSONArray jsonArray = sennorService.getDevideCodeMapByTypes(sennorDeviceTypes);
  41. for (Object object : jsonArray){
  42. JSONObject jsonObject = JSONObject.parseObject(object.toString());
  43. sennorService.getDeviceInfo(jsonObject.getString("device_code"));
  44. }
  45. }
  46. // @Scheduled(cron = "0 0 0/2 * * ?")//添加定时任务 每2小时执行
  47. // @Scheduled(cron = "0 */5 * * * ?")//添加定时任务 每1分钟执行
  48. private void configureTask() {
  49. logger.info("执行虫情数据采集定时任务=>{}", DateUtils.dateTimeNow());
  50. JSONArray jsonArray = sennorService.getDevideCodeMapByTypes(sennorInsectDeviceType);
  51. for (Object object : jsonArray){
  52. JSONObject jsonObject = JSONObject.parseObject(object.toString());
  53. plantDiseasesAndInsectPestsService.plantDiseasesAndInsectPestsMessageReceiver(jsonObject.getString("device_code"));
  54. }
  55. }
  56. /**
  57. * 比较两个日期大小
  58. **/
  59. private boolean daysBefore(String time) {
  60. boolean result = false;
  61. try {
  62. Calendar calendar = getCalendar(time);
  63. Calendar now = Calendar.getInstance();
  64. result = calendar.before(now);
  65. } catch (ParseException e) {
  66. logger.error(e.getMessage());
  67. }
  68. return result;
  69. }
  70. /**
  71. * 将String时间转换为Calendar
  72. **/
  73. private static Calendar getCalendar(String time) throws ParseException {
  74. Calendar calendar = Calendar.getInstance();
  75. calendar.setTime(simpleDateFormat.parse(time));
  76. return calendar;
  77. }
  78. }