|
|
|
@ -1,8 +1,11 @@ |
|
|
|
package com.inspect.nvr.service.impl; |
|
|
|
|
|
|
|
import cn.hutool.core.util.ObjectUtil; |
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
|
|
import com.inspect.nvr.controller.CameraController; |
|
|
|
import com.inspect.nvr.domain.Infrared.Camera; |
|
|
|
import com.inspect.nvr.domain.Infrared.NvrInfo; |
|
|
|
import com.inspect.nvr.domain.Infrared.TemperatureData; |
|
|
|
import com.inspect.nvr.domain.device.*; |
|
|
|
import com.inspect.nvr.hikVision.utils.AjaxResult; |
|
|
|
import com.inspect.nvr.hikVision.utils.StringUtils; |
|
|
|
@ -23,14 +26,14 @@ import org.springframework.util.StreamUtils; |
|
|
|
|
|
|
|
import javax.annotation.Resource; |
|
|
|
import java.io.ByteArrayInputStream; |
|
|
|
import java.io.File; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.nio.file.Files; |
|
|
|
import java.nio.file.Path; |
|
|
|
import java.nio.file.Paths; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Date; |
|
|
|
import java.util.List; |
|
|
|
import java.util.*; |
|
|
|
import java.util.concurrent.CountDownLatch; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
@Slf4j |
|
|
|
@ -50,6 +53,8 @@ public class IvsCameraServiceImpl implements IvsCameraService { |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private DahuaService dahuaService; |
|
|
|
@Autowired |
|
|
|
private CameraController cameraController; |
|
|
|
|
|
|
|
@Override |
|
|
|
public IvsPresetListView ptzPresetList(String cameraCode, String domainCode) { |
|
|
|
@ -79,79 +84,114 @@ public class IvsCameraServiceImpl implements IvsCameraService { |
|
|
|
.build(); |
|
|
|
} |
|
|
|
Camera camera = new Camera(); |
|
|
|
//赋值 |
|
|
|
// camera.setIp("192.168.1.231"); |
|
|
|
// camera.setPort(8000); |
|
|
|
// camera.setUserName("admin"); |
|
|
|
// camera.setPassword("2016.h.BD"); |
|
|
|
// camera.setChannel(11); |
|
|
|
// camera.setPointNum(210); |
|
|
|
//目前写死的 nvr信息 cameraType=0海康 1大华 |
|
|
|
String ip = "192.168.1.231"; |
|
|
|
int port = 8000; |
|
|
|
int channel = 11; |
|
|
|
int pointNum = 210; |
|
|
|
String username = "admin"; |
|
|
|
String password = "sshw1234"; |
|
|
|
int cameraType = 0; |
|
|
|
String[] splitArray = param.getAddress().split(":"); |
|
|
|
if (splitArray.length >= 5) { |
|
|
|
log.info("PTZ_CONTROL CORRECT CONFIG"); |
|
|
|
ip = splitArray[0]; |
|
|
|
port = Integer.parseInt(splitArray[1]); |
|
|
|
channel = Integer.parseInt(splitArray[2]); |
|
|
|
pointNum = Integer.parseInt(splitArray[3]); |
|
|
|
cameraType = Integer.parseInt(splitArray[4]); |
|
|
|
username = splitArray[5]; |
|
|
|
password = splitArray[6]; |
|
|
|
} |
|
|
|
//给camera赋值 |
|
|
|
//cameraType=0海康 1大华 |
|
|
|
String ip = splitArray[0]; |
|
|
|
int port = Integer.parseInt(splitArray[1]); |
|
|
|
int channel = Integer.parseInt(splitArray[2]); |
|
|
|
int pointNum = Integer.parseInt(splitArray[3]); |
|
|
|
String username = splitArray[5]; |
|
|
|
String password = splitArray[6]; |
|
|
|
int cameraType = Integer.parseInt(splitArray[4]); |
|
|
|
camera.setIp(ip); |
|
|
|
camera.setPort(port); |
|
|
|
camera.setChannel(channel); |
|
|
|
camera.setPointNum(pointNum); |
|
|
|
camera.setUserName(username); |
|
|
|
camera.setPassword(password); |
|
|
|
log.info("PTZ_CONTROL ip:" + ip + " port:" + port + "username:" + username + "password" + password + " channel:" + channel + " pointNum:" + pointNum); |
|
|
|
log.info("PTZ_CONTROL Type" + cameraType); |
|
|
|
log.info("预置位NVR:" + ip + " channel:" + channel + " pointNum:" + pointNum + " cameraType:" + cameraType); |
|
|
|
//给camera赋值结束 |
|
|
|
PtzControlResult ptzControlResult = ptzDetailControl(camera, cameraType); |
|
|
|
|
|
|
|
try { |
|
|
|
//等待5s开始调用红外 |
|
|
|
Thread.sleep(5000); |
|
|
|
Camera cameraHw = new Camera(); |
|
|
|
cameraHw.setAddress(param.getAddress()); |
|
|
|
cameraHw.setIp(splitArray[7]); |
|
|
|
cameraHw.setPort(Integer.parseInt(splitArray[8])); |
|
|
|
cameraHw.setChannel(Integer.parseInt(splitArray[9])); |
|
|
|
cameraHw.setPresetId(Integer.parseInt(splitArray[3])); |
|
|
|
cameraHw.setCameraType(Integer.parseInt(splitArray[4])); |
|
|
|
cameraHw.setUserName(splitArray[10]); |
|
|
|
cameraHw.setPassword(splitArray[11]); |
|
|
|
//获取红外温度 |
|
|
|
retry(cameraHw,camera, cameraType); |
|
|
|
} catch (Exception e) { |
|
|
|
// throw new RuntimeException(e); |
|
|
|
} |
|
|
|
return ptzControlResult; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public PtzControlResult ptzDetailControl(Camera camera,int cameraType) { |
|
|
|
//大华预置位跳转 |
|
|
|
PtzControlResult ptzControlResult = null; |
|
|
|
if (ObjectUtil.equals(cameraType, 1)) { |
|
|
|
log.info("开始登录大华摄像头 进行跳转预置位"); |
|
|
|
log.info("开始登录大华NVR 进行跳转预置位"); |
|
|
|
String ajaxResult = dahuaService.cameraControl(camera, 0, 0, 0); |
|
|
|
PtzControlResult ptzControlResult = PtzControlResult.builder() |
|
|
|
ptzControlResult = PtzControlResult.builder() |
|
|
|
.resultCode(ajaxResult) |
|
|
|
.build(); |
|
|
|
return ptzControlResult; |
|
|
|
} |
|
|
|
//海康预置位跳转 |
|
|
|
log.info("开始登录海康摄像头 进行跳转预置位"); |
|
|
|
lUserID = (Integer) redisService.redisTemplate.opsForValue().get(camera.getIp() + "_userId"); |
|
|
|
if (ObjectUtil.isEmpty(lUserID)) { |
|
|
|
NvrInfo nvrInfo = new NvrInfo(); |
|
|
|
nvrInfo.setNvrIp(camera.getIp()); |
|
|
|
nvrInfo.setServerPort(camera.getPort()); |
|
|
|
nvrInfo.setAccount(camera.getUserName()); |
|
|
|
nvrInfo.setPassword(camera.getPassword()); |
|
|
|
login_V40(nvrInfo); |
|
|
|
} else { |
|
|
|
//海康预置位跳转 |
|
|
|
log.info("开始登录海康NVR 进行跳转预置位"); |
|
|
|
lUserID = (Integer) redisService.redisTemplate.opsForValue().get(camera.getIp() + "_userId"); |
|
|
|
log.info("相机:lUserID" + camera.getIp() + "_userId" + lUserID); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//参数:登录令牌,通道号,预置位跳转,跳转的预置位码 |
|
|
|
boolean gotoPreset = hcNetSDK.NET_DVR_PTZPreset_Other(lUserID, camera.getChannel(), HCNetSDK.GOTO_PRESET, camera.getPointNum()); |
|
|
|
if (!gotoPreset) { |
|
|
|
log.error("海康-获取设备预置位跳转设备参数失败,错误码:" + hcNetSDK.NET_DVR_GetLastError()); |
|
|
|
return PtzControlResult.builder() |
|
|
|
.resultCode("-1") |
|
|
|
if (ObjectUtil.isEmpty(lUserID)) { |
|
|
|
NvrInfo nvrInfo = new NvrInfo(); |
|
|
|
nvrInfo.setNvrIp(camera.getIp()); |
|
|
|
nvrInfo.setServerPort(camera.getPort()); |
|
|
|
nvrInfo.setAccount(camera.getUserName()); |
|
|
|
nvrInfo.setPassword(camera.getPassword()); |
|
|
|
login_V40(nvrInfo); |
|
|
|
lUserID = (Integer) redisService.redisTemplate.opsForValue().get(camera.getIp() + "_userId"); |
|
|
|
log.info("相机:lUserID" + camera.getIp() + "_userId" + lUserID); |
|
|
|
} |
|
|
|
//参数:登录令牌,通道号,预置位跳转,跳转的预置位码 |
|
|
|
boolean gotoPreset = hcNetSDK.NET_DVR_PTZPreset_Other(lUserID, camera.getChannel(), HCNetSDK.GOTO_PRESET, camera.getPointNum()); |
|
|
|
if (!gotoPreset) { |
|
|
|
log.error("海康-获取设备预置位跳转设备参数失败,错误码:" + hcNetSDK.NET_DVR_GetLastError()); |
|
|
|
return PtzControlResult.builder() |
|
|
|
.resultCode("-1") |
|
|
|
.build(); |
|
|
|
} else { |
|
|
|
log.info("海康-成功跳转到预置位!"); |
|
|
|
} |
|
|
|
ptzControlResult = PtzControlResult.builder() |
|
|
|
.resultCode("0") |
|
|
|
.build(); |
|
|
|
} else { |
|
|
|
log.info("海康-成功跳转到预置位!"); |
|
|
|
} |
|
|
|
PtzControlResult ptzControlResult = PtzControlResult.builder() |
|
|
|
.resultCode("0") |
|
|
|
.build(); |
|
|
|
return ptzControlResult; |
|
|
|
|
|
|
|
} |
|
|
|
/** |
|
|
|
* 大华红外读数多次读取 |
|
|
|
* @param camera |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public TemperatureData retry(Camera camera,Camera nvrCamera,int cameraType){ |
|
|
|
//添加重试机制 |
|
|
|
final int MAX_RETRIES = 3; // 最大重试次数 |
|
|
|
boolean success = false; |
|
|
|
int retryCount = 0; |
|
|
|
while (!success && retryCount < MAX_RETRIES) { |
|
|
|
try { |
|
|
|
retryCount++; |
|
|
|
log.info("第{}次尝试读取红外", retryCount); |
|
|
|
TemperatureData temperatureData = cameraController.cameraHong(camera); |
|
|
|
if(StringUtils.isNull(temperatureData)){ |
|
|
|
log.warn("第{}次读取红外失败", retryCount); |
|
|
|
ptzDetailControl(nvrCamera,cameraType); |
|
|
|
Thread.sleep(3000); // 等待2秒后重试 |
|
|
|
} else { |
|
|
|
success = true; |
|
|
|
return temperatureData; |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("第{}次抓图失败: {}", retryCount, e.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
//拍照 |
|
|
|
@ -189,6 +229,12 @@ public class IvsCameraServiceImpl implements IvsCameraService { |
|
|
|
return snapshotInfoListResult; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 下载图片 |
|
|
|
* |
|
|
|
* @param fileSessionId |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
public ByteArrayInputStream downloadFile(String fileSessionId) { |
|
|
|
final String rawString = StringHexConverter.fromHex(fileSessionId); |
|
|
|
@ -201,22 +247,9 @@ public class IvsCameraServiceImpl implements IvsCameraService { |
|
|
|
} |
|
|
|
} |
|
|
|
String[] cameraAddressInfos = rawString.split((":")); |
|
|
|
// if (cameraAddressInfos.length != 7) { |
|
|
|
// log.error("DOWNLOAD_FILE ADDRESS INFO ERROR rawString: {}", rawString); |
|
|
|
// try { |
|
|
|
// return new ByteArrayInputStream(loadDefaultImage()); |
|
|
|
// } catch (IOException e) { |
|
|
|
// throw new RuntimeException("测试环境生成错误图片失败2", e); |
|
|
|
// } |
|
|
|
// } |
|
|
|
// 从nvr或者camera下载图片, 开发中 |
|
|
|
Camera camera = new Camera(); |
|
|
|
//赋值 |
|
|
|
// camera.setIp("192.168.1.231"); |
|
|
|
// camera.setPort(8000); |
|
|
|
// camera.setUserName("admin"); |
|
|
|
// camera.setPassword("2016.h.BD"); |
|
|
|
// camera.setChannel(11); |
|
|
|
log.info("DOWNLOAD_FILE ip: {}, port: {}, channel: {}, pointName: {},cameraType: {}, username: {}, password: {}", |
|
|
|
cameraAddressInfos[0], |
|
|
|
cameraAddressInfos[1], |
|
|
|
@ -232,53 +265,64 @@ public class IvsCameraServiceImpl implements IvsCameraService { |
|
|
|
camera.setCameraType(Integer.parseInt(cameraAddressInfos[4])); |
|
|
|
camera.setUserName(cameraAddressInfos[5]); |
|
|
|
camera.setPassword(cameraAddressInfos[6]); |
|
|
|
if (ObjectUtil.equals(camera.getCameraType(), 1)){ |
|
|
|
if (ObjectUtil.equals(camera.getCameraType(), 1)) { |
|
|
|
log.info("大华相机拍照"); |
|
|
|
ByteArrayInputStream picture = dahuaService.Picture(camera); |
|
|
|
return picture; |
|
|
|
} |
|
|
|
//从redis中获取lUserID(存在时间200) |
|
|
|
lUserID = (Integer) redisService.redisTemplate.opsForValue().get(camera.getIp() + "_userId"); |
|
|
|
//判断lUserID是否为空 |
|
|
|
if (ObjectUtil.isEmpty(lUserID)) { |
|
|
|
//重新登录获取lUserID |
|
|
|
NvrInfo nvrInfo = new NvrInfo(); |
|
|
|
nvrInfo.setNvrIp(camera.getIp()); |
|
|
|
nvrInfo.setServerPort(camera.getPort()); |
|
|
|
nvrInfo.setAccount(camera.getUserName()); |
|
|
|
nvrInfo.setPassword(camera.getPassword()); |
|
|
|
login_V40(nvrInfo); |
|
|
|
} else { |
|
|
|
//从redis中获取lUserID(存在时间200) |
|
|
|
lUserID = (Integer) redisService.redisTemplate.opsForValue().get(camera.getIp() + "_userId"); |
|
|
|
} |
|
|
|
HCNetSDK.NET_DVR_JPEGPARA dvrJpegpara = new HCNetSDK.NET_DVR_JPEGPARA(); |
|
|
|
dvrJpegpara.wPicSize = 3; |
|
|
|
dvrJpegpara.wPicQuality = 0; |
|
|
|
dvrJpegpara.write(); |
|
|
|
// 2. 创建临时存储目录(系统临时目录更安全) |
|
|
|
Path tempDir = Paths.get(System.getProperty("java.io.tmpdir"), "hik_capture"); |
|
|
|
// 生成唯一文件名(IP+时间戳) |
|
|
|
String fileName = String.format("%s_%s.jpg", camera.getIp(), DateUtils.dateTimeNow("yyyyMMddHHmmss")); |
|
|
|
Path tempImagePath = tempDir.resolve(fileName); |
|
|
|
try { |
|
|
|
if (!Files.exists(tempDir)) { |
|
|
|
Files.createDirectories(tempDir); |
|
|
|
//判断lUserID是否为空 |
|
|
|
if (ObjectUtil.isEmpty(lUserID)) { |
|
|
|
//重新登录获取lUserID |
|
|
|
NvrInfo nvrInfo = new NvrInfo(); |
|
|
|
nvrInfo.setNvrIp(camera.getIp()); |
|
|
|
nvrInfo.setServerPort(camera.getPort()); |
|
|
|
nvrInfo.setAccount(camera.getUserName()); |
|
|
|
nvrInfo.setPassword(camera.getPassword()); |
|
|
|
login_V40(nvrInfo); |
|
|
|
lUserID = (Integer) redisService.redisTemplate.opsForValue().get(camera.getIp() + "_userId"); |
|
|
|
} |
|
|
|
boolean b = hcNetSDK.NET_DVR_CaptureJPEGPicture(lUserID, camera.getChannel(), dvrJpegpara, |
|
|
|
tempImagePath.toString().getBytes("GBK")); |
|
|
|
if (!b) { |
|
|
|
// System.out.println("设置设备进行抓图失败,错误码:" + hcNetSDK.NET_DVR_GetLastError()); |
|
|
|
log.error("设置设备进行抓图失败,错误码:" + hcNetSDK.NET_DVR_GetLastError()); |
|
|
|
HCNetSDK.NET_DVR_JPEGPARA dvrJpegpara = new HCNetSDK.NET_DVR_JPEGPARA(); |
|
|
|
dvrJpegpara.wPicSize = 3; |
|
|
|
dvrJpegpara.wPicQuality = 0; |
|
|
|
dvrJpegpara.write(); |
|
|
|
|
|
|
|
// 确保目录存在 |
|
|
|
File picDir = new File("D:\\pic"); |
|
|
|
if (!picDir.exists()) { |
|
|
|
picDir.mkdirs(); // 如果目录不存在,创建它 |
|
|
|
} |
|
|
|
//添加重试机制 |
|
|
|
final int MAX_RETRIES = 5; // 最大重试次数 |
|
|
|
boolean success = false; |
|
|
|
int retryCount = 0; |
|
|
|
while (!success && retryCount < MAX_RETRIES) { |
|
|
|
try { |
|
|
|
retryCount++; |
|
|
|
log.info("第{}次尝试抓图", retryCount); |
|
|
|
// 生成文件名(例如:camera_ip + timestamp) |
|
|
|
String fileName = "D:\\pic\\" + camera.getIp() + "_" + System.currentTimeMillis() + ".jpg"; |
|
|
|
Path tempImagePath = Paths.get(fileName); |
|
|
|
boolean b = hcNetSDK.NET_DVR_CaptureJPEGPicture(lUserID, camera.getChannel(), dvrJpegpara, |
|
|
|
tempImagePath.toString().getBytes("GBK")); |
|
|
|
if (!b) { |
|
|
|
log.error("设置设备进行抓图失败,错误码:" + hcNetSDK.NET_DVR_GetLastError()); |
|
|
|
log.warn("第{}次抓图超时", retryCount); |
|
|
|
} else { |
|
|
|
//抓图成功 |
|
|
|
InputStream inputStream = Files.newInputStream(tempImagePath); |
|
|
|
byte[] bytes = StreamUtils.copyToByteArray(inputStream); |
|
|
|
log.error("第{}次抓图成功: {}", retryCount); |
|
|
|
return new ByteArrayInputStream(bytes); |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("第{}次抓图失败: {}", retryCount, e.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
//图片流 |
|
|
|
InputStream inputStream = Files.newInputStream(tempImagePath); |
|
|
|
byte[] bytes = StreamUtils.copyToByteArray(inputStream); |
|
|
|
// 确保删除临时文件 |
|
|
|
Files.deleteIfExists(tempImagePath); |
|
|
|
return new ByteArrayInputStream(bytes); |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("抓图失败" + e.getMessage()); |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public byte[] loadDefaultImage() throws IOException { |
|
|
|
@ -333,3 +377,4 @@ public class IvsCameraServiceImpl implements IvsCameraService { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|