潘志宝
2024-11-02 c96e44ceedf98afe9541b49172a3d80d983442a0
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
package com.iailab.module.data.job.task;
 
import com.iailab.module.data.dev.controller.admin.camera.vo.DevCameraPageReqVO;
import com.iailab.module.data.dev.dahua.DHCapturePictureClient;
import com.iailab.module.data.dev.dahua.DHClientFactory;
import com.iailab.module.data.dev.dto.DevCameraDTO;
import com.iailab.module.data.dev.enums.CaptureTypeEnum;
import com.iailab.module.data.dev.service.DevCameraService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
 
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * 大华摄像头截图定时任务
 *
 * @author Houzhongjian
 * @Description
 * @createTime 2024年10月30日
 */
@Component("devCameraDahuaTask")
public class DevCameraDahuaTask implements ITask {
    private Logger logger = LoggerFactory.getLogger(getClass());
 
    @Resource
    private DevCameraService devCameraService;
 
    @Resource
    private DHClientFactory dhClientFactory;
 
    @Override
    public void run(String params) {
        logger.info("devCameraDahuaTask定时任务正在执行,参数为:{}", params);
        try {
            DevCameraPageReqVO cameraPageReqVO = new DevCameraPageReqVO();
            List<DevCameraDTO> list = devCameraService.list(cameraPageReqVO);
            if (CollectionUtils.isEmpty(list)) {
                logger.info("Alarm List 为空");
                return;
            }
            Map<String, List<DevCameraDTO>> alarmGroup = list.stream().collect(Collectors.groupingBy(DevCameraDTO::getNvrId));
            for  (Map.Entry<String, List<DevCameraDTO>> nvr : alarmGroup.entrySet()) {
                DHCapturePictureClient client = dhClientFactory.getClient(nvr.getKey());
                try {
                    if(!client.isLogin()) {
                        dhClientFactory.reLogin(client, nvr.getKey());
                    }
                    if(!client.isLogin()) {
                        continue;
                    }
                    List<DevCameraDTO> cameraList = nvr.getValue();
                    for (int i = 0; i < cameraList.size(); i++) {
                        DevCameraDTO camera = cameraList.get(i);
                        boolean captureResult = false;
                        if (CaptureTypeEnum.LOCAL.getCode().equals(camera.getCaptureType())) {
                            captureResult = client.localCapturePicture(camera.getChannel(), camera.getCode());
                        } else {
                            captureResult = client.remoteCapturePicture(camera.getChannel(), camera.getCode());
                        }
                        logger.info(camera.getCode() + ":captureResult = " + captureResult);
                        Thread.sleep(100);
                    }
                } catch (Exception ex) {
                    logger.info("item:" + ex.getMessage());
                }
            }
            logger.info("devCameraDahuaTask定时任务执行完成:" + LocalDateTime.now());
 
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.info("devCameraDahuaTask定时任务失败时间:" + LocalDateTime.now());
        }
    }
}