dongyukun
2024-11-05 e8ad669f7c97d45cd23630dc101180a130d6c17e
提交 | 用户 | 时间
ce910c 1 package com.iailab.module.data.dev.dahua;
H 2
3 import com.iailab.module.data.dev.dto.DevLogDTO;
4 import com.iailab.module.data.dev.dto.DevNvrDTO;
5 import com.iailab.module.data.dev.entity.DevNvrEntity;
6 import com.iailab.module.data.dev.enums.CameraStatusEnum;
7 import com.iailab.module.data.dev.service.DevLogService;
8 import com.iailab.module.data.dev.service.DevNvrService;
9 import org.springframework.beans.factory.annotation.Autowired;
10 import org.springframework.stereotype.Component;
11
12 import java.util.Date;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.concurrent.ConcurrentHashMap;
16
17 /**
18  * @author PanZhibao
19  * @Description
20  * @createTime 2024年04月08日
21  */
22 @Component
23 public class DHClientFactory {
24
25     @Autowired
26     private DevNvrService devNvrService;
27
28     @Autowired
29     private DevLogService devLogService;
30
31     private Map<String, DHCapturePictureClient> clientMap = new ConcurrentHashMap<>();
32
33     public synchronized DHCapturePictureClient getClient(String nvrId) {
34         if (clientMap.containsKey(nvrId)) {
35             return clientMap.get(nvrId);
36         }
37
38         DHCapturePictureClient client = new DHCapturePictureClient();
39         if (!DHCapturePictureClient.isInit()) {
40             boolean bInit = client.initSDK();
41             DevLogDTO dto = new DevLogDTO();
42             dto.setCreateDate(new Date());
43             dto.setEvent("初始化SDK");
44             dto.setDevId("SDK");
45             dto.setContent(bInit ? "初始化SDK成功" : "初始化SDK失败");
46             devLogService.save(dto);
47         }
48         DevNvrDTO devNvrDTO = devNvrService.get(nvrId);
49         if (!client.isLogin()) {
50             boolean bLogin = client.login(devNvrDTO.getIp(), devNvrDTO.getPort(), devNvrDTO.getUsername(), devNvrDTO.getPassword());
51
52             DevLogDTO dto = new DevLogDTO();
53             dto.setCreateDate(new Date());
54             dto.setEvent("登录设备");
55             dto.setDevId(devNvrDTO.getIp());
56             dto.setContent(bLogin ? "登录成功" : "登录失败");
57             if (bLogin) {
58                 devNvrService.setStatus(devNvrDTO.getIp(), CameraStatusEnum.ONLIEN.getCode());
59             } else {
60                 devNvrService.setStatus(devNvrDTO.getIp(), CameraStatusEnum.OFFLINE.getCode());
61             }
62             devLogService.save(dto);
63         }
64         clientMap.put(nvrId, client);
65         return client;
66     }
67
68     public void reLogin(DHCapturePictureClient client, String nvrId) {
69         DevNvrDTO devNvrDTO = devNvrService.get(nvrId);
70         boolean bLogin = client.login(devNvrDTO.getIp(), devNvrDTO.getPort(), devNvrDTO.getUsername(), devNvrDTO.getPassword());
71         DevLogDTO dto = new DevLogDTO();
72         dto.setCreateDate(new Date());
73         dto.setEvent("登录设备");
74         dto.setDevId(devNvrDTO.getIp());
75         dto.setContent(bLogin ? "登录成功" : "登录失败");
76         if (bLogin) {
77             devNvrService.setStatus(devNvrDTO.getIp(), CameraStatusEnum.ONLIEN.getCode());
78         } else {
79             devNvrService.setStatus(devNvrDTO.getIp(), CameraStatusEnum.OFFLINE.getCode());
80         }
81         devLogService.save(dto);
82     }
83
84 //    public void getNvrOnlineStatus(List<DevNvrEntity> nvrs) {
85 //        nvrs.stream().forEach(nvr -> {
86 //            DHCapturePictureClient client = getClient(nvr.getId().toString());
87 //            if(!client.isLogin()) {
88 //                reLogin(client, nvr.getId().toString());
89 //            }
90 //            boolean bLogin = client.login(nvr.getIp(), nvr.getPort(), nvr.getUsername(), nvr.getPassword());
91 //            DevLogDTO dto = new DevLogDTO();
92 //            dto.setCreateDate(new Date());
93 //            dto.setEvent("登录设备");
94 //            dto.setDevId(nvr.getIp());
95 //            dto.setContent(bLogin ? "登录成功" : "登录失败");
96 //            if (bLogin) {
97 //                nvr.setStatus(CameraStatusEnum.ONLIEN.getCode());
98 //                devNvrService.setStatus(nvr.getIp(), CameraStatusEnum.ONLIEN.getCode());
99 //            } else {
100 //                nvr.setStatus(CameraStatusEnum.OFFLINE.getCode());
101 //                devNvrService.setStatus(nvr.getIp(), CameraStatusEnum.OFFLINE.getCode());
102 //            }
103 //            devLogService.save(dto);
104 //        });
105 //    }
106 }