package com.iailab.module.data.dev.dahua; import com.iailab.module.data.dev.dto.DevLogDTO; import com.iailab.module.data.dev.dto.DevNvrDTO; import com.iailab.module.data.dev.entity.DevNvrEntity; import com.iailab.module.data.dev.enums.CameraStatusEnum; import com.iailab.module.data.dev.service.DevLogService; import com.iailab.module.data.dev.service.DevNvrService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.Date; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /** * @author PanZhibao * @Description * @createTime 2024年04月08日 */ @Component public class DHClientFactory { @Autowired private DevNvrService devNvrService; @Autowired private DevLogService devLogService; 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(); DevLogDTO dto = new DevLogDTO(); dto.setCreateDate(new Date()); dto.setEvent("初始化SDK"); dto.setDevId("SDK"); dto.setContent(bInit ? "初始化SDK成功" : "初始化SDK失败"); devLogService.save(dto); } DevNvrDTO devNvrDTO = devNvrService.get(nvrId); if (!client.isLogin()) { boolean bLogin = client.login(devNvrDTO.getIp(), devNvrDTO.getPort(), devNvrDTO.getUsername(), devNvrDTO.getPassword()); DevLogDTO dto = new DevLogDTO(); dto.setCreateDate(new Date()); dto.setEvent("登录设备"); dto.setDevId(devNvrDTO.getIp()); dto.setContent(bLogin ? "登录成功" : "登录失败"); if (bLogin) { devNvrService.setStatus(devNvrDTO.getIp(), CameraStatusEnum.ONLIEN.getCode()); } else { devNvrService.setStatus(devNvrDTO.getIp(), CameraStatusEnum.OFFLINE.getCode()); } devLogService.save(dto); } clientMap.put(nvrId, client); return client; } public void reLogin(DHCapturePictureClient client, String nvrId) { DevNvrDTO devNvrDTO = devNvrService.get(nvrId); boolean bLogin = client.login(devNvrDTO.getIp(), devNvrDTO.getPort(), devNvrDTO.getUsername(), devNvrDTO.getPassword()); DevLogDTO dto = new DevLogDTO(); dto.setCreateDate(new Date()); dto.setEvent("登录设备"); dto.setDevId(devNvrDTO.getIp()); dto.setContent(bLogin ? "登录成功" : "登录失败"); if (bLogin) { devNvrService.setStatus(devNvrDTO.getIp(), CameraStatusEnum.ONLIEN.getCode()); } else { devNvrService.setStatus(devNvrDTO.getIp(), CameraStatusEnum.OFFLINE.getCode()); } devLogService.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); // }); // } }