liusuyi
2026-05-14 9958bc1501d0daee954d9bba383475e55e24ebab
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
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.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();
 
    /**
     * 采集任务(不会堆积)
     */
    @Scheduled(initialDelay = 5000, fixedDelay = 1000)
    public void startCollectTask() {
 
        List<Object> cameraList = redisService.getCacheMapValues(CacheConstants.CAMERA_LIST);
        if (cameraList == null || cameraList.isEmpty()) return;
 
        for (Object obj : cameraList) {
            ArdCamera camera = (ArdCamera) obj;
            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(5000);
            if (!list.isEmpty()) {
                PTZWebSocketUtils.sendMessageAll(list);
            }
        } catch (Exception e) {
            log.error("推送PTZ异常", e);
        }
    }
}