| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- package com.zksy.audio.utils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.http.HttpEntity;
- import org.springframework.http.HttpHeaders;
- import org.springframework.http.MediaType;
- import com.zksy.api.scheduling.RefreshableOfflineCheckTask;
- import org.springframework.stereotype.Component;
- import com.zksy.api.service.CollectionLastReceiptService;
- import org.springframework.web.client.RestTemplate;
- import java.util.Date;
- import java.util.Map;
- import java.util.Set;
- import java.util.concurrent.ConcurrentHashMap;
- import java.time.Clock;
- import java.util.Arrays;
- @Component
- public class DeviceOfflineCheckTask extends RefreshableOfflineCheckTask {
- private static final Logger logger = LoggerFactory.getLogger(DeviceOfflineCheckTask.class);
- public static ConcurrentHashMap<String, Date> deviceLastReceiveTimeMap = new ConcurrentHashMap<>();
- private final Set<String> offlineDeviceSet = ConcurrentHashMap.newKeySet();
- private final Set<String> onlineDeviceSet = ConcurrentHashMap.newKeySet();
- private final Object[] deviceLocks = new Object[128];
- private Clock clock = Clock.systemUTC();
- public DeviceOfflineCheckTask() {
- super(1560);
- Arrays.setAll(deviceLocks, i -> new Object());
- }
- @Autowired
- private RestTemplate restTemplate;
- @Autowired
- private CollectionLastReceiptService lastReceiptService;
- @Override
- public void checkDeviceOffline() {
- int timeoutMinutes = effectiveOfflineTimeoutMinutes();
- logger.info("离线巡检开始: collector=audio, timeoutMinutes={}, memoryDevices={}",
- timeoutMinutes, deviceLastReceiveTimeMap.size());
- if (lastReceiptService != null) {
- lastReceiptService.restore("audio", deviceLastReceiveTimeMap, offlineDeviceSet);
- }
- Date now = new Date(clock.millis());
- long timeoutMs = (long) timeoutMinutes * 60 * 1000;
- for (Map.Entry<String, Date> entry : deviceLastReceiveTimeMap.entrySet()) {
- synchronized (deviceLock(entry.getKey())) {
- // 与在线恢复串行,且重新读取时间,避免旧快照把刚收到上报的设备置离线。
- Date latest = deviceLastReceiveTimeMap.get(entry.getKey());
- if (latest == null || now.getTime() - latest.getTime() <= timeoutMs) continue;
- if (offlineDeviceSet.add(entry.getKey())) {
- if (!updateDeviceOnlineStatus(entry.getKey(), 0)) {
- offlineDeviceSet.remove(entry.getKey());
- continue;
- }
- onlineDeviceSet.remove(entry.getKey());
- logger.info("设备 {} 已离线(超过{}分钟未收到数据)", entry.getKey(), timeoutMinutes);
- }
- }
- }
- }
- public void markDeviceOnline(String deviceCode) {
- if (deviceCode == null || deviceCode.trim().isEmpty()) return;
- synchronized (deviceLock(deviceCode)) {
- // 首次上报/服务重启后也需同步,不能只依赖本实例曾经标记过离线。
- if (!onlineDeviceSet.contains(deviceCode) && updateDeviceOnlineStatus(deviceCode, 1)) {
- onlineDeviceSet.add(deviceCode);
- offlineDeviceSet.remove(deviceCode);
- logger.info("设备 {} 已在线", deviceCode);
- }
- }
- }
- private Object deviceLock(String deviceCode) {
- return deviceLocks[(deviceCode.hashCode() & Integer.MAX_VALUE) % deviceLocks.length];
- }
- private boolean updateDeviceOnlineStatus(String deviceCode, int onlineStatus) {
- try {
- Map<String, Object> params = Map.of("deviceCode", deviceCode, "onlineStatus", onlineStatus);
- HttpHeaders headers = new HttpHeaders();
- headers.setContentType(MediaType.APPLICATION_JSON);
- HttpEntity<Map<String, Object>> request = new HttpEntity<>(params, headers);
- Map<?, ?> response = restTemplate.postForObject("http://zk-api-service/equipmentStatus/updateOnlineStatus", request, Map.class);
- if (response == null || !Boolean.TRUE.equals(response.get("success"))) {
- logger.warn("在线状态同步未成功,下次重试: deviceCode={}, onlineStatus={}", deviceCode, onlineStatus);
- return false;
- }
- return true;
- } catch (Exception e) {
- logger.error("更新设备在线状态失败: deviceCode={}, onlineStatus={}", deviceCode, onlineStatus, e);
- return false;
- }
- }
- }
|