package com.boying.service; import com.boying.entity.VideoRecordDto; import com.boying.service.HCNetSDK; import com.boying.service.SdkInitService; import com.sun.jna.ptr.IntByReference; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Timer; @Component public class VideoJob { @Autowired private RedisTemplate redisTemplate; @Value("${allFilePath.videoPath}") private String videoPath; private Boolean runFlag = false; private String VIDEO_EQ_KEY="VIDEO_EQ_KEY:"; int lUserID; int m_lLoadHandle; Timer downloadtimer;//下载用定时器 private static HCNetSDK hCNetSDK = SdkInitService.hcNetSDK; @Scheduled(cron = "0/1 * * * * *") public void work() throws InterruptedException { if(runFlag){ return; } VideoRecordDto videoRecordDto = (VideoRecordDto) redisTemplate.boundListOps(VIDEO_EQ_KEY).rightPop(); if(videoRecordDto == null){ return; } String loginStatus = (String)redisTemplate.opsForValue().get("Login_Status:"+videoRecordDto.getDiskUrl()); if("true".equals(loginStatus)){ return; } if(videoRecordDto != null){ try { runFlag = true; redisTemplate.opsForValue().set("Login_Status:"+videoRecordDto.getDiskUrl(),"true"); HCNetSDK.NET_DVR_DEVICEINFO_V30 m_strDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V30(); lUserID = hCNetSDK.NET_DVR_Login_V30(videoRecordDto.getDiskUrl(),(short)8000,videoRecordDto.getDiskUser(),videoRecordDto.getDiskPassword(), m_strDeviceInfo); long userID = lUserID; if (userID == -1) { System.out.println("hksdk(视频)-海康sdk登录失败!"); return; } dowmloadRecordByTime(lUserID,videoRecordDto); Thread.sleep(0); }catch (Exception e){ runFlag = false; }finally { runFlag = false; redisTemplate.opsForValue().set("Login_Status:"+videoRecordDto.getDiskUrl(),"false"); } } } //按时间下载录像(不支持转码3GP格式) public void dowmloadRecordByTime(int userID,VideoRecordDto dto) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String start = sdf.format(dto.getStartTime()); String end = sdf.format(dto.getEndTime()); List startList = getDateString(start); List endList = getDateString(end); HCNetSDK.NET_DVR_PLAYCOND net_dvr_playcond = new HCNetSDK.NET_DVR_PLAYCOND(); net_dvr_playcond.read(); net_dvr_playcond.dwChannel=dto.getEqPort(); //通道号 NVR设备路数小于32路的起始通道号从33开始,依次增加 //开始时间 net_dvr_playcond.struStartTime.dwYear=startList.get(0); net_dvr_playcond.struStartTime.dwMonth=startList.get(1); net_dvr_playcond.struStartTime.dwDay=startList.get(2); net_dvr_playcond.struStartTime.dwHour=startList.get(3); net_dvr_playcond.struStartTime.dwMinute=startList.get(4); net_dvr_playcond.struStartTime.dwSecond=startList.get(5); //停止时间 net_dvr_playcond.struStopTime.dwYear=endList.get(0); net_dvr_playcond.struStopTime.dwMonth=endList.get(1); net_dvr_playcond.struStopTime.dwDay=endList.get(2); net_dvr_playcond.struStopTime.dwHour=endList.get(3); net_dvr_playcond.struStopTime.dwMinute=endList.get(4); net_dvr_playcond.struStopTime.dwSecond=endList.get(5); net_dvr_playcond.write(); HCNetSDK.NET_DVR_FINDDATA_V40 struFindData = new HCNetSDK.NET_DVR_FINDDATA_V40(); struFindData.read(); String strFileName = null; try { strFileName=new String(struFindData.sFileName,"UTF-8").trim(); }catch (Exception e){ e.printStackTrace(); } String sFileName = videoPath+dto.getVideoName(); System.out.println(sFileName); //m_lLoadHandle=hCNetSDK.NET_DVR_GetFileByName(userID,strFileName,sFileName.getBytes()); m_lLoadHandle = hCNetSDK.NET_DVR_GetFileByTime_V40(userID,sFileName,net_dvr_playcond); if (m_lLoadHandle >= 0) { hCNetSDK.NET_DVR_PlayBackControl(m_lLoadHandle, HCNetSDK.NET_DVR_PLAYSTART, 0, null); /* IntByReference intP=new IntByReference(5*8*1024); IntByReference intInlen=new IntByReference(0); boolean b_PlayBack=ClientDemo.hCNetSDK.NET_DVR_PlayBackControl_V40(m_lLoadHandle,24,intP.getPointer(),4,Pointer.NULL,intInlen); if (!b_PlayBack) { System.out.println("设置下载速度失败,错误码为" + ClientDemo.hCNetSDK.NET_DVR_GetLastError()); return; }*/ Date nowTime = new Date(System.currentTimeMillis()); SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss"); System.out.println("开始下载时间:"+sdFormatter.format(nowTime)); downloadtimer = new Timer();//新建定时器 downloadtimer.schedule(new downloadTask(), 0, 5000);//0秒后开始响应函数 } else { System.out.println("按时间下载失败"); System.out.println("laste error " + hCNetSDK.NET_DVR_GetLastError()); return; } } class downloadTask extends java.util.TimerTask { //定时器函数 @Override public void run() { IntByReference nPos = new IntByReference(0); hCNetSDK.NET_DVR_PlayBackControl(m_lLoadHandle, HCNetSDK.NET_DVR_PLAYGETPOS, 0, nPos); if (nPos.getValue() > 100) { m_lLoadHandle=-1; downloadtimer.cancel(); System.out.println( "由于网络原因或DVR忙,下载异常终止!"); } if (nPos.getValue() == 100) { Date nowTime = new Date(System.currentTimeMillis()); SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss"); System.out.println("结束下载时间:"+sdFormatter.format(nowTime)); m_lLoadHandle=-1; downloadtimer.cancel(); System.out.println("按时间下载结束!"); //退出程序时调用,每一台设备分别注销 if (hCNetSDK.NET_DVR_Logout(lUserID)) { System.out.println("注销成功"); } // hCNetSDK.NET_DVR_Cleanup(); } } } static List getDateString(String dateString){ String[] s = dateString.split(" "); List list = new ArrayList<>(); for (int i = 0; i < s.length; i++) { String s1 = s[i]; if(i==0){ for (String s2 : s1.split("-")) { list.add(Integer.parseInt(s2)); } }else { for (String s2 : s1.split(":")) { list.add(Integer.parseInt(s2)); } } } return list; } public static void main(String[] args) { getDateString("2024-07-04 09:37:00"); } }