liusuyi
2026-05-30 f4f4fc53260eb67483dce406a963628273786a61
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.ard.work.device.camera.service;
 
import com.ard.common.core.constant.CacheConstants;
import com.ard.common.redis.service.RedisService;
import com.ard.work.api.domian.ArdCamera;
import com.ard.work.device.camera.domain.PtzParamDTO;
import com.ard.work.websocket.utils.PTZWebSocketUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
 
@Slf4j
@Component
public class PtzTask {
 
    @Resource
    private PtzDataCollector ptzDataCollector;
 
    @Resource
    private PtzCacheManager ptzCacheManager;
 
    @Resource
    private RedisService redisService;
 
    private final Set<String> runningCamera = ConcurrentHashMap.newKeySet();
 
    /** 缓存数据有效期: 超过此时间不再推送 */
    private static final long DATA_VALID_MS = 5_000L;
    /** 缓存数据淘汰阈值: 超过此时间从内存清掉 */
    private static final long DATA_EVICT_MS = 60_000L;
    /** 相机列表本地缓存 TTL */
    private static final long CAMERA_LIST_TTL_MS = 30_000L;
 
    private volatile List<ArdCamera> cachedCameraList = Collections.emptyList();
    private volatile long cameraListExpireAt = 0L;
 
    /**
     * 采集任务
     */
    @Scheduled(initialDelay = 5000, fixedDelay = 1000)
    public void startCollectTask() {
        List<ArdCamera> cameraList = getCameraList();
        if (cameraList.isEmpty()) return;
 
        for (ArdCamera camera : cameraList) {
            String camId = camera.getId();
            if (!runningCamera.add(camId)) continue;
            ptzDataCollector.collectCamera(camera, runningCamera);
        }
    }
 
    /**
     * 推送任务
     */
    @Scheduled(fixedDelay = 1000)
    public void pushPTZ() {
        try {
            List<PtzParamDTO> list = ptzCacheManager.getAllValidData(DATA_VALID_MS);
            if (!list.isEmpty()) {
                PTZWebSocketUtils.sendMessageAll(list);
            }
        } catch (Exception e) {
            log.error("推送PTZ异常", e);
        }
    }
 
    /**
     * 清理过期缓存,防止已禁用/删除的相机数据残留
     */
    @Scheduled(fixedDelay = 60_000)
    public void evictStaleCache() {
        int n = ptzCacheManager.evictStale(DATA_EVICT_MS);
        if (n > 0) {
            log.debug("清理过期PTZ缓存 {} 条", n);
        }
    }
 
    /**
     * 本地缓存相机列表,降低 Redis QPS
     */
    private List<ArdCamera> getCameraList() {
        long now = System.currentTimeMillis();
        if (now < cameraListExpireAt) {
            return cachedCameraList;
        }
        synchronized (this) {
            if (now < cameraListExpireAt) {
                return cachedCameraList;
            }
            List<Object> raw = redisService.getCacheMapValues(CacheConstants.CAMERA_LIST);
            List<ArdCamera> list;
            if (raw == null || raw.isEmpty()) {
                list = Collections.emptyList();
            } else {
                list = new ArrayList<>(raw.size());
                for (Object o : raw) {
                    list.add((ArdCamera) o);
                }
            }
            cachedCameraList = list;
            cameraListExpireAt = now + CAMERA_LIST_TTL_MS;
            return list;
        }
    }
}