DeviceStatusSchedule.java 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package com.zy.bms.schedule;
  2. import com.zy.bms.model.Device;
  3. import com.zy.bms.service.DeviceService;
  4. import com.zy.bms.service.RabbitMQApi;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.scheduling.annotation.Async;
  8. import org.springframework.scheduling.annotation.EnableAsync;
  9. import org.springframework.scheduling.annotation.EnableScheduling;
  10. import org.springframework.scheduling.annotation.Scheduled;
  11. import java.util.List;
  12. import java.util.Set;
  13. /**
  14. * 定时更新设备在线状态
  15. *
  16. * @author yang xiao kun
  17. * create on 2021/5/14
  18. */
  19. @EnableAsync
  20. @Configuration
  21. @EnableScheduling
  22. public class DeviceStatusSchedule {
  23. @Autowired
  24. private RabbitMQApi rabbitMQApi;
  25. @Autowired
  26. private DeviceService deviceService;
  27. /**
  28. * 更新频率 2分钟
  29. * 定时更新设备在线状态
  30. */
  31. @Async
  32. @Scheduled(cron = "0 0/2 * * * ?")
  33. public void updateDeviceStatus() {
  34. //全部的设备
  35. List<Device> devices = deviceService.list();
  36. Set<String> online = rabbitMQApi.getOnlineDevice();
  37. for (Device item : devices) {
  38. item.setStatus(online.contains(item.getClientId()) ? 1 : 0);
  39. }
  40. deviceService.updateDeviceStatus(devices);
  41. }
  42. }