|
|
|
@ -1,18 +1,36 @@ |
|
|
|
package com.inspect.nvr.service.impl; |
|
|
|
|
|
|
|
import cn.hutool.core.util.ObjectUtil; |
|
|
|
import com.inspect.nvr.domain.Infrared.Camera; |
|
|
|
import com.inspect.nvr.domain.Infrared.NvrInfo; |
|
|
|
import com.inspect.nvr.domain.device.*; |
|
|
|
import com.inspect.nvr.hikVision.utils.AjaxResult; |
|
|
|
import com.inspect.nvr.hikVision.utils.StringUtils; |
|
|
|
import com.inspect.nvr.hikVision.utils.jna.HCNetSDK; |
|
|
|
import com.inspect.nvr.hikVision.utils.jna.HikVisionUtils; |
|
|
|
import com.inspect.nvr.service.IvsCameraService; |
|
|
|
import com.inspect.nvr.utils.DateUtils; |
|
|
|
import com.inspect.nvr.utils.redis.RedisService; |
|
|
|
import com.sun.jna.ptr.IntByReference; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.core.io.ClassPathResource; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.util.StreamUtils; |
|
|
|
|
|
|
|
import javax.annotation.Resource; |
|
|
|
import java.io.ByteArrayInputStream; |
|
|
|
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.concurrent.TimeUnit; |
|
|
|
|
|
|
|
@Service |
|
|
|
public class IvsCameraServiceImpl implements IvsCameraService { |
|
|
|
@ -21,16 +39,103 @@ public class IvsCameraServiceImpl implements IvsCameraService { |
|
|
|
@Value("${test-mode:false}") |
|
|
|
private boolean testMode; |
|
|
|
|
|
|
|
@Resource |
|
|
|
private RedisService redisService; |
|
|
|
|
|
|
|
private Integer lUserID; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private HCNetSDK hcNetSDK; |
|
|
|
|
|
|
|
//跳转预置位 |
|
|
|
@Override |
|
|
|
public PtzControlResult ptzControl(PtzControlParam param) { |
|
|
|
|
|
|
|
Camera camera = new Camera(); |
|
|
|
//赋值 |
|
|
|
|
|
|
|
|
|
|
|
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); |
|
|
|
lUserID = (Integer) redisService.redisTemplate.opsForValue().get(camera.getIp() + "_userId"); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//参数:登录令牌,通道号,预置位跳转,跳转的预置位码 |
|
|
|
boolean gotoPreset = hcNetSDK.NET_DVR_PTZPreset_Other(lUserID, camera.getChannel(), HCNetSDK.GOTO_PRESET, camera.getPointNum()); |
|
|
|
if (!gotoPreset) { |
|
|
|
|
|
|
|
log.error("海康-获取设备预置位跳转设备参数失败,错误码:" + hcNetSDK.NET_DVR_GetLastError()); |
|
|
|
|
|
|
|
} else { |
|
|
|
log.info("海康-成功跳转到预置位!"); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
PtzControlResult ptzControlResult = PtzControlResult.builder() |
|
|
|
.resultCode("0") |
|
|
|
.build(); |
|
|
|
return ptzControlResult; |
|
|
|
} |
|
|
|
|
|
|
|
//拍照 |
|
|
|
@Override |
|
|
|
public SnapshotCommandResult platformSnapshot(String cameraCode, String domainCode, SnapshotCommandParam param) { |
|
|
|
|
|
|
|
Camera camera = new Camera(); |
|
|
|
//赋值 |
|
|
|
|
|
|
|
//从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); |
|
|
|
|
|
|
|
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"); |
|
|
|
try { |
|
|
|
if (!Files.exists(tempDir)) { |
|
|
|
Files.createDirectories(tempDir); |
|
|
|
} |
|
|
|
// 3. 生成唯一文件名(IP+时间戳) |
|
|
|
String fileName = String.format("%s_%s.jpg", camera.getIp(), DateUtils.dateTimeNow("yyyyMMddHHmmss")); |
|
|
|
Path tempImagePath = tempDir.resolve(fileName); |
|
|
|
|
|
|
|
boolean b = hcNetSDK.NET_DVR_CaptureJPEGPicture(lUserID, camera.getChannel(), dvrJpegpara, |
|
|
|
tempImagePath.toString().getBytes("GBK")); |
|
|
|
if (!b) { |
|
|
|
System.out.println("设置设备进行抓图失败,错误码:" + hcNetSDK.NET_DVR_GetLastError()); |
|
|
|
} |
|
|
|
//图片流 |
|
|
|
InputStream inputStream = Files.newInputStream(tempImagePath); |
|
|
|
|
|
|
|
// 确保删除临时文件 |
|
|
|
Files.deleteIfExists(tempImagePath); |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
log.error("抓图失败"+e.getMessage()); |
|
|
|
} |
|
|
|
SnapshotCommandResult snapshotCommandResult = |
|
|
|
SnapshotCommandResult.builder() |
|
|
|
.taskID("0123456789") |
|
|
|
@ -74,4 +179,51 @@ public class IvsCameraServiceImpl implements IvsCameraService { |
|
|
|
ClassPathResource imgFile = new ClassPathResource("images/infrared_default.jpg"); |
|
|
|
return StreamUtils.copyToByteArray(imgFile.getInputStream()); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public AjaxResult login_V40(NvrInfo nvrInfo) { |
|
|
|
|
|
|
|
HCNetSDK.NET_DVR_USER_LOGIN_INFO m_strLoginInfo = HikVisionUtils.login_V40(nvrInfo.getNvrIp(), nvrInfo.getServerPort().shortValue(), nvrInfo.getAccount(), nvrInfo.getPassword());//设备登录信息 |
|
|
|
HCNetSDK.NET_DVR_DEVICEINFO_V40 m_strDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V40();//设备信息 |
|
|
|
|
|
|
|
int lUserID = hcNetSDK.NET_DVR_Login_V40(m_strLoginInfo, m_strDeviceInfo); |
|
|
|
if (lUserID == -1) { |
|
|
|
System.out.println("登录失败,错误码为" + hcNetSDK.NET_DVR_GetLastError()); |
|
|
|
return AjaxResult.error(hcNetSDK.NET_DVR_GetErrorMsg(new IntByReference(hcNetSDK.NET_DVR_GetLastError()))); |
|
|
|
} else { |
|
|
|
System.out.println(":设备登录成功!" + lUserID); |
|
|
|
// birdNvrMapper.updateNvrInfo(nvrInfo); |
|
|
|
// redisTemplate.opsForValue().set(nvrInfo.getNvrIp() + "_userId", lUserID); |
|
|
|
redisService.setCacheObject(nvrInfo.getNvrIp() + "_userId", lUserID,200L, TimeUnit.SECONDS); |
|
|
|
//若果设备序列号为空时,即为第一次登陆,写入基础数据 |
|
|
|
if (nvrInfo.getNvrSerial() == null || nvrInfo.getNvrSerial().equals("")) { |
|
|
|
m_strDeviceInfo.read(); |
|
|
|
HCNetSDK.NET_DVR_DEVICEINFO_V30 deviceinfo_v30 = m_strDeviceInfo.struDeviceV30; |
|
|
|
nvrInfo.setNvrSerial(StringUtils.bytetoString(deviceinfo_v30.sSerialNumber, "GB2312")); |
|
|
|
//模拟通道不为0,并且数字通道为0,即为模拟通道 |
|
|
|
if (deviceinfo_v30.byChanNum != 0 && deviceinfo_v30.byIPChanNum == 0) { |
|
|
|
nvrInfo.setChannelType(0); |
|
|
|
nvrInfo.setChannelNumber(deviceinfo_v30.byChanNum + deviceinfo_v30.byHighDChanNum * 256); |
|
|
|
nvrInfo.setStartChannel((int) deviceinfo_v30.byStartChan); |
|
|
|
} |
|
|
|
//模拟通道为0,并且数字通道不为0,即为数字通道 |
|
|
|
if (deviceinfo_v30.byChanNum == 0 && deviceinfo_v30.byIPChanNum != 0) { |
|
|
|
nvrInfo.setChannelType(1); |
|
|
|
nvrInfo.setChannelNumber(deviceinfo_v30.byIPChanNum + deviceinfo_v30.byHighDChanNum * 256); |
|
|
|
nvrInfo.setStartChannel((int) deviceinfo_v30.byStartDChan); |
|
|
|
} |
|
|
|
//模拟通道不为0,并且数字通道不为0,即为双通道。 |
|
|
|
if (deviceinfo_v30.byChanNum != 0 && deviceinfo_v30.byIPChanNum != 0) { |
|
|
|
nvrInfo.setChannelType(2); |
|
|
|
nvrInfo.setChannelNumber(deviceinfo_v30.byChanNum + deviceinfo_v30.byHighDChanNum * 256); |
|
|
|
nvrInfo.setStartChannel((int) deviceinfo_v30.byStartChan); |
|
|
|
} |
|
|
|
nvrInfo.setCalicheNumber((int) deviceinfo_v30.byDiskNum); |
|
|
|
nvrInfo.setCharEncodeType((int) m_strDeviceInfo.byCharEncodeType); |
|
|
|
nvrInfo.setSyncDate(new Date()); |
|
|
|
// nvrInfoMapper.updateNvrInfo(nvrInfo); |
|
|
|
} |
|
|
|
return AjaxResult.success(hcNetSDK.NET_DVR_GetErrorMsg(new IntByReference(hcNetSDK.NET_DVR_GetLastError()))); |
|
|
|
} |
|
|
|
} |
|
|
|
} |