package com.iailab.module.data.video.dahua; 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 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 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); // }); // } }