kongdeqiang
2024-07-05 3b787780c12b802d3cc5f6ff9983b2822a018c79
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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<Integer> startList = getDateString(start);
        List<Integer> 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<Integer> getDateString(String dateString){
        String[] s = dateString.split(" ");
        List<Integer> 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");
    }
 
}