| | |
| | | package com.dji.sample.component; |
| | | |
| | | import com.dji.sample.component.mqtt.service.IMqttTopicService; |
| | | import com.dji.sample.component.redis.RedisConst; |
| | | import com.dji.sample.component.redis.RedisOpsUtils; |
| | | import com.dji.sample.manage.model.dto.DeviceDTO; |
| | | import com.dji.sample.manage.model.enums.DeviceDomainEnum; |
| | | import com.dji.sample.manage.service.IDeviceService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | |
| | | import org.springframework.scheduling.annotation.Scheduled; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.time.LocalDateTime; |
| | | import java.util.Map; |
| | | import java.util.Arrays; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | import static com.dji.sample.manage.model.DeviceStatusManager.DEFAULT_ALIVE_SECOND; |
| | | import static com.dji.sample.manage.model.DeviceStatusManager.STATUS_MANAGER; |
| | | |
| | | /** |
| | | * @author sean.zhou |
| | |
| | | @Autowired |
| | | private IDeviceService deviceService; |
| | | |
| | | @Autowired |
| | | private RedisOpsUtils redisOps; |
| | | |
| | | @Autowired |
| | | private IMqttTopicService topicService; |
| | | |
| | | /** |
| | | * Check the status of the devices every 30 seconds. It is recommended to use cache. |
| | | */ |
| | | @Scheduled(fixedRate = 30, timeUnit = TimeUnit.SECONDS) |
| | | @Scheduled(initialDelay = 30, fixedRate = 30, timeUnit = TimeUnit.SECONDS) |
| | | private void deviceStatusListen() { |
| | | for (Map.Entry<String, LocalDateTime> entry : STATUS_MANAGER.entrySet()) { |
| | | if (entry.getValue().isAfter( |
| | | LocalDateTime.now().minusSeconds(DEFAULT_ALIVE_SECOND))) { |
| | | continue; |
| | | int start = RedisConst.DEVICE_ONLINE_PREFIX.length(); |
| | | |
| | | redisOps.getAllKeys(RedisConst.DEVICE_ONLINE_PREFIX + "*").forEach(key -> { |
| | | long expire = redisOps.getExpire(key); |
| | | if (expire <= 30) { |
| | | DeviceDTO device = (DeviceDTO) redisOps.get(key); |
| | | if (device.getDomain().equals(DeviceDomainEnum.SUB_DEVICE.getDesc())) { |
| | | deviceService.subDeviceOffline(key.substring(start)); |
| | | } else { |
| | | deviceService.unsubscribeTopicOffline(key.substring(start)); |
| | | deviceService.pushDeviceOfflineTopo(device.getWorkspaceId(), device.getDeviceSn()); |
| | | } |
| | | redisOps.del(key); |
| | | } |
| | | }); |
| | | |
| | | String device = entry.getKey(); |
| | | int index = device.indexOf("/"); |
| | | |
| | | STATUS_MANAGER.remove(device); |
| | | |
| | | int type = Integer.parseInt(device.substring(0, index)); |
| | | String sn = device.substring(index + 1); |
| | | // Determine whether it is a gateway device. |
| | | if (DeviceDomainEnum.GATEWAY.getVal() == type) { |
| | | deviceService.deviceOffline(sn); |
| | | deviceService.unsubscribeTopicOffline(sn); |
| | | continue; |
| | | } |
| | | |
| | | deviceService.subDeviceOffline(sn); |
| | | } |
| | | log.info("Subscriptions: {}", Arrays.toString(topicService.getSubscribedTopic())); |
| | | } |
| | | |
| | | } |