houzhongjian
2024-11-14 08b6a55a3a111aaee9842999b85afa60b6d61dc3
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
package com.iailab.module.data.video.dahua;
 
import com.iailab.framework.tenant.core.context.TenantContextHolder;
import com.iailab.module.data.video.dto.LogDTO;
import com.iailab.module.data.video.dto.NvrDTO;
import com.iailab.module.data.video.enums.CameraStatusEnum;
import com.iailab.module.data.video.service.LogService;
import com.iailab.module.data.video.service.NvrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * @author PanZhibao
 * @Description
 * @createTime 2024年04月08日
 */
@Component
public class DHClientFactory {
 
    @Autowired
    private NvrService nvrService;
 
    @Autowired
    private LogService logService;
 
    private Map<String, DHCapturePictureClient> clientMap = new ConcurrentHashMap<>();
 
    public synchronized DHCapturePictureClient getClient(String nvrId) {
        if (clientMap.containsKey(nvrId)) {
            return clientMap.get(nvrId);
        }
 
        DHCapturePictureClient client = new DHCapturePictureClient();
        if (!DHCapturePictureClient.isInit()) {
            boolean bInit = client.initSDK();
            LogDTO dto = new LogDTO();
            dto.setCreateDate(new Date());
            dto.setEvent("初始化SDK");
            dto.setDevId("SDK");
            dto.setContent(bInit ? "初始化SDK成功" : "初始化SDK失败");
            logService.save(dto);
        }
        NvrDTO nvrDTO = nvrService.get(nvrId);
        if (!client.isLogin()) {
            boolean bLogin = client.login(nvrDTO.getIp(), nvrDTO.getPort(), nvrDTO.getUsername(), nvrDTO.getPassword());
 
            LogDTO dto = new LogDTO();
            dto.setCreateDate(new Date());
            dto.setEvent("登录设备");
            dto.setDevId(nvrDTO.getIp());
            dto.setContent(bLogin ? "登录成功" : "登录失败");
            if (bLogin) {
                nvrService.setStatus(nvrDTO.getIp(), CameraStatusEnum.ONLIEN.getCode());
            } else {
                nvrService.setStatus(nvrDTO.getIp(), CameraStatusEnum.OFFLINE.getCode());
            }
            logService.save(dto);
        }
        clientMap.put(nvrId, client);
        return client;
    }
 
    public void reLogin(DHCapturePictureClient client, String nvrId) {
        NvrDTO nvrDTO = nvrService.get(nvrId);
        boolean bLogin = client.login(nvrDTO.getIp(), nvrDTO.getPort(), nvrDTO.getUsername(), nvrDTO.getPassword());
        LogDTO dto = new LogDTO();
        dto.setCreateDate(new Date());
        dto.setEvent("登录设备");
        dto.setDevId(nvrDTO.getIp());
        dto.setContent(bLogin ? "登录成功" : "登录失败");
        if (bLogin) {
            nvrService.setStatus(nvrDTO.getIp(), CameraStatusEnum.ONLIEN.getCode());
        } else {
            nvrService.setStatus(nvrDTO.getIp(), CameraStatusEnum.OFFLINE.getCode());
        }
        logService.save(dto);
    }
 
//    public void getNvrOnlineStatus(List<DevNvrEntity> nvrs) {
//        nvrs.stream().forEach(nvr -> {
//            DHCapturePictureClient client = getClient(nvr.getId().toString());
//            if(!client.isLogin()) {
//                reLogin(client, nvr.getId().toString());
//            }
//            boolean bLogin = client.login(nvr.getIp(), nvr.getPort(), nvr.getUsername(), nvr.getPassword());
//            DevLogDTO dto = new DevLogDTO();
//            dto.setCreateDate(new Date());
//            dto.setEvent("登录设备");
//            dto.setDevId(nvr.getIp());
//            dto.setContent(bLogin ? "登录成功" : "登录失败");
//            if (bLogin) {
//                nvr.setStatus(CameraStatusEnum.ONLIEN.getCode());
//                devNvrService.setStatus(nvr.getIp(), CameraStatusEnum.ONLIEN.getCode());
//            } else {
//                nvr.setStatus(CameraStatusEnum.OFFLINE.getCode());
//                devNvrService.setStatus(nvr.getIp(), CameraStatusEnum.OFFLINE.getCode());
//            }
//            devLogService.save(dto);
//        });
//    }
}