zxl
1 天以前 3b0516a2959e25576e4f3fda697a3b025d06c8c9
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package com.ycl.api.YS.playback.play;
 
import com.sun.jna.ptr.LongByReference;
import com.ycl.api.YS.NetDemo;
import com.ycl.api.YS.util.Common;
import com.ycl.api.YS.lib.NetDEVSDKLib.*;
 
import javax.swing.*;
import java.io.File;
 
/**
 * 
 * @introduction Download Record
 * @description Support NVR/VMS
 */
public class DownloadRecord {
    /**
     * 
     * @introduction Download Record
     * @description Calling the interface of NETDEV_GetFileByTime to download the record by time
     *
     */
    public static void startDownload() {
        if(null == NetDemo.lpUserID)
        {
            JOptionPane.showMessageDialog(null, "Please Login device first. error code"+NetDemo.netdevsdk.NETDEV_GetLastError());
            return;
        }
        if(NetDemo.RecordTable.getSelectedRow()<0) {
            JOptionPane.showMessageDialog(null, "No file,please choose a file");
            return;
        }
 
        NetDemo.jButtonStartdownload.setEnabled(false);
        int row=NetDemo.RecordTable.getSelectedRow();
        String Beginstr=(String)NetDemo.RecordTable.getValueAt(row, 0);
        String Endstr=(String)NetDemo.RecordTable.getValueAt(row, 1);
        String strSeparator = File.separator;
        NetDemo.strRecordPath = System.getProperty("user.dir").replaceAll("%20"," ") + strSeparator+"Record"+strSeparator+"DownloadFile" + Common.timeStamp()+ "_D"+NetDemo.ChannelID;    
        NETDEV_PLAYBACKCOND_S pstPlayBackCond = new NETDEV_PLAYBACKCOND_S();
        pstPlayBackCond.dwChannelID = NetDemo.ChannelID;
        pstPlayBackCond.dwDownloadSpeed = NETDEV_E_DOWNLOAD_SPEED_E.NETDEV_DOWNLOAD_SPEED_EIGHT;
        pstPlayBackCond.tBeginTime = Common.date2TimeStamp(Beginstr, NetDemo.DateFormat);
        pstPlayBackCond.tEndTime = Common.date2TimeStamp(Endstr, NetDemo.DateFormat);
        
        NetDemo.lpDownloadHandle = NetDemo.netdevsdk.NETDEV_GetFileByTime(NetDemo.lpUserID, pstPlayBackCond, NetDemo.strRecordPath, NETDEV_MEDIA_FILE_FORMAT_E.NETDEV_MEDIA_FILE_MP4);
        if(null != NetDemo.lpDownloadHandle){
            NetDemo.DownloadProgress.setMaximum((int)pstPlayBackCond.tEndTime);
            NetDemo.DownloadProgress.setMinimum((int)pstPlayBackCond.tBeginTime);
            Thread singledownloadThread=new Thread(new Runnable() {
                @Override
                public void run() {
                    int nCount = 0;
                    long getFileTime=0;
                    int failCount=0;
                    while(true)
                    {
                        LongByReference PlayTime = new LongByReference();
                        boolean bRet = NetDemo.netdevsdk.NETDEV_PlayBackControl(NetDemo.lpDownloadHandle, NETDEV_VOD_PLAY_CTRL_E.NETDEV_PLAY_CTRL_GETPLAYTIME, PlayTime);
                        if(bRet){
                            long lTime = PlayTime.getValue();
                            NetDemo.DownloadProgress.setValue((int)lTime);
                            if(lTime!=getFileTime) {
                                getFileTime=lTime;
                                failCount=0;
                            }else {
                                failCount++;
                                if(failCount==5) {
                                    NetDemo.netdevsdk.NETDEV_StopGetFile(NetDemo.lpDownloadHandle);
                                    NetDemo.DownloadProgress.setValue(NetDemo.DownloadProgress.getMaximum());
                                    NetDemo.lpDownloadHandle = null;
                                    break;
                                }
                            }
                        }
                        else{
                            nCount++;
                            System.out.println("Get playback time failed");
                        }
                        if((long)NetDemo.DownloadProgress.getMaximum() == PlayTime.getValue() || PlayTime.getValue() > (long)NetDemo.DownloadProgress.getMaximum()
                                || nCount>3){
                            NetDemo.netdevsdk.NETDEV_StopGetFile(NetDemo.lpDownloadHandle);
                            NetDemo.DownloadProgress.setValue(NetDemo.DownloadProgress.getMaximum());
                            NetDemo.lpDownloadHandle = null;
                            NetDemo.jButtonStartdownload.setEnabled(true);
                            break;
                        }
                        try{
                            Thread.sleep(700);
                        }catch(InterruptedException e){
                            e.printStackTrace();
                        }
                    }
                }
            });
            NetDemo.singleLiveThread.execute(singledownloadThread);
        }
    }
 
    /**
     * 
     * @introduction Download Record by name
     * @description Calling the interface of NETDEV_GetFileByName to download the record by time
     *
     */
    public static void startDownloadByName() {
        if(null == NetDemo.lpUserID)
        {
            JOptionPane.showMessageDialog(null, "Please Login device first. error code"+NetDemo.netdevsdk.NETDEV_GetLastError());
            return;
        }
        if(NetDemo.RecordTable.getSelectedRow()<0) {
            JOptionPane.showMessageDialog(null, "No file,please choose a file");
            return;
        }
        
        NetDemo.jButtonStartdownloadByName.setEnabled(false);
        String strSeparator = File.separator;
        NetDemo.strRecordPath = System.getProperty("user.dir").replaceAll("%20"," ") + strSeparator +"Record"+strSeparator+"DownloadFile" + String.valueOf(Common.timeStamp())+ "_D"+NetDemo.ChannelID;
        NETDEV_PLAYBACKINFO_S pstPlayBackCond = new NETDEV_PLAYBACKINFO_S();
        int row=NetDemo.RecordTable.getSelectedRow();
        /* 获取文件名 */
        String str=(String)NetDemo.RecordTable.getValueAt(row, 2);
        System.arraycopy(str.getBytes(), 0, pstPlayBackCond.szName, 0, str.getBytes().length);
        String Beginstr=(String)NetDemo.RecordTable.getValueAt(row, 0);
        String Endstr=(String)NetDemo.RecordTable.getValueAt(row, 1);
        pstPlayBackCond.dwDownloadSpeed = NETDEV_E_DOWNLOAD_SPEED_E.NETDEV_DOWNLOAD_SPEED_EIGHT;
        pstPlayBackCond.tBeginTime =Common.date2TimeStamp(Beginstr, NetDemo.DateFormat);
        pstPlayBackCond.tEndTime = Common.date2TimeStamp(Endstr, NetDemo.DateFormat);
        NetDemo.lpDownloadHandleByName = NetDemo.netdevsdk.NETDEV_GetFileByName(NetDemo.lpUserID, pstPlayBackCond, NetDemo.strRecordPath, NETDEV_MEDIA_FILE_FORMAT_E.NETDEV_MEDIA_FILE_MP4);
        
        if(null != NetDemo.lpDownloadHandleByName){
            NetDemo.DownloadProgress.setMaximum((int)pstPlayBackCond.tEndTime);
            NetDemo.DownloadProgress.setMinimum((int)pstPlayBackCond.tBeginTime);
            Thread singledownloadByNameThread=new Thread(new Runnable() {
                @Override
                public void run() {
                    int nCount = 0;
                    long getFileTime=0;
                    int failCount=0;
                    while(true)
                    {
                        LongByReference PlayTime = new LongByReference();
                        boolean bRet = NetDemo.netdevsdk.NETDEV_PlayBackControl(NetDemo.lpDownloadHandleByName, NETDEV_VOD_PLAY_CTRL_E.NETDEV_PLAY_CTRL_GETPLAYTIME, PlayTime);
                        if(bRet){
                            long lTime = PlayTime.getValue();
                            NetDemo.DownloadProgress.setValue((int)lTime);
                            if(lTime!=getFileTime) {
                                getFileTime=lTime;
                                failCount=0;
                            }else {
                                failCount++;
                                if(failCount==5) {
                                    NetDemo.netdevsdk.NETDEV_StopGetFile(NetDemo.lpDownloadHandleByName);
                                    NetDemo.DownloadProgress.setValue(NetDemo.DownloadProgress.getMaximum());
                                    NetDemo.lpDownloadHandleByName = null;
                                    break;
                                }
                            }
                        }
                        else{
                            nCount++;
                            System.out.println("Get playback time failed");
                        }
                        if((long)NetDemo.DownloadProgress.getMaximum() == PlayTime.getValue() || PlayTime.getValue() > (long)NetDemo.DownloadProgress.getMaximum()
                                || nCount>3){
                            NetDemo.netdevsdk.NETDEV_StopGetFile(NetDemo.lpDownloadHandleByName);
                            NetDemo.DownloadProgress.setValue(NetDemo.DownloadProgress.getMaximum());
                            NetDemo.lpDownloadHandleByName = null;
                            NetDemo.jButtonStartdownloadByName.setEnabled(true);
                            break;
                        }
                        try{
                            Thread.sleep(700);
                        }catch(InterruptedException e){
                            e.printStackTrace();
                        }
                    }
 
                }
            });
            NetDemo.singleLiveThread.execute(singledownloadByNameThread);
        }
    }
 
    /**
     * 
     * @introduction Stop download
     * @description Calling the interface of NETDEV_StopGetFile
     *
     */
    public static void stopDownload() {
        NetDemo.jButtonStartdownload.setEnabled(true);
        if(null != NetDemo.lpDownloadHandle){
            NetDemo.netdevsdk.NETDEV_StopGetFile(NetDemo.lpDownloadHandle);
            NetDemo.lpDownloadHandle = null;
            NetDemo.DownloadProgress.setValue(NetDemo.DownloadProgress.getMaximum());
        }else {
            JOptionPane.showMessageDialog(null, "No file is downloading,or you use wrong button.");
        }
    
    }
 
    /**
     * 
     * @introduction Stop download by name
     * @description Calling the interface of NETDEV_StopGetFile
     *
     */
    public static void stopDownloadByName() {
        NetDemo.jButtonStartdownloadByName.setEnabled(true);
        if(null != NetDemo.lpDownloadHandleByName){
            NetDemo.netdevsdk.NETDEV_StopGetFile(NetDemo.lpDownloadHandleByName);
            NetDemo.lpDownloadHandleByName = null;
            NetDemo.DownloadProgress.setValue(NetDemo.DownloadProgress.getMaximum());
        }else {
            JOptionPane.showMessageDialog(null, "No file is downloading,or you use wrong button.");
        }
    
    }
    
}