dongyukun
2024-11-05 e8ad669f7c97d45cd23630dc101180a130d6c17e
提交 | 用户 | 时间
ce910c 1 package com.netsdk.demo.frame;
H 2
3 import java.awt.BorderLayout;
4 import java.awt.Dimension;
5 import java.awt.GridLayout;
6
7 import java.awt.FlowLayout;
8
9 import java.awt.event.ActionEvent;
10 import java.awt.event.ActionListener;
11 import java.awt.event.WindowAdapter;
12 import java.awt.event.WindowEvent;
13 import java.util.Vector;
14
15 import javax.swing.DefaultComboBoxModel;
16 import javax.swing.JButton;
17 import javax.swing.JComboBox;
18 import javax.swing.JFrame;
19 import javax.swing.JLabel;
20 import javax.swing.JOptionPane;
21 import javax.swing.JPanel;
22 import javax.swing.JProgressBar;
23 import javax.swing.JScrollPane;
24 import javax.swing.JTable;
25 import javax.swing.ListSelectionModel;
26 import javax.swing.SwingUtilities;
27 import javax.swing.UIManager;
28 import javax.swing.border.EmptyBorder;
29 import javax.swing.table.DefaultTableCellRenderer;
30 import javax.swing.table.DefaultTableModel;
31
32 import com.netsdk.common.*;
33 import com.netsdk.demo.module.DownLoadRecordModule;
34 import com.netsdk.demo.module.LoginModule;
35 import com.netsdk.lib.*;
36 import com.netsdk.lib.NetSDKLib.LLong;
37
38 import com.sun.jna.CallbackThreadInitializer;
39 import com.sun.jna.Native;
40 import com.sun.jna.Pointer;
41 import com.sun.jna.ptr.IntByReference;
42
43 /*
44  * 下载录像Demo
45  */
46 class DownLoadRecordFrame extends JFrame{
47     private static final long serialVersionUID = 1L;
48     private Vector<String> chnlist = new Vector<String>(); 
49     
50     private DefaultTableModel model;
51     private LLong m_hDownLoadByTimeHandle = new LLong(0);   // 按时间下载句柄
52     private LLong m_hDownLoadByFileHandle = new LLong(0);   // 按文件下载句柄
53     
54     private boolean b_downloadByTime = false;
55     private boolean b_downloadByFile = false;
56     private IntByReference nFindCount = new IntByReference(0);
57     
58     // 设备断线通知回调
59     private DisConnect disConnect       = new DisConnect(); 
60     
61     // 网络连接恢复
62     private static HaveReConnect haveReConnect = new HaveReConnect(); 
63     
64     // 开始时间
65     private NetSDKLib.NET_TIME stTimeStart = new NetSDKLib.NET_TIME(); 
66     
67     // 结束时间
68     private NetSDKLib.NET_TIME stTimeEnd = new NetSDKLib.NET_TIME();
69     
70     // 录像文件信息
71     private NetSDKLib.NET_RECORDFILE_INFO[] stFileInfo = (NetSDKLib.NET_RECORDFILE_INFO[])new NetSDKLib.NET_RECORDFILE_INFO().toArray(2000);
72
73     Object[][] data = null;
74     
75     // 获取界面窗口
76     private static JFrame frame = new JFrame();   
77     
78     public DownLoadRecordFrame() {
79         setTitle(Res.string().getDownloadRecord());
80         setLayout(new BorderLayout());
81         pack();
82         setSize(800, 560);
83         setResizable(false);
84         setLocationRelativeTo(null);
85         LoginModule.init(disConnect, haveReConnect);   // 打开工程,初始化
86         
87         try {
88             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
89         } catch (Exception e) {
90             e.printStackTrace();
91         } 
92                 
93         loginPanel = new LoginPanel();
94         downloadRecordPanel = new DownLoadRecordPanel(); 
95     
96         add(loginPanel, BorderLayout.NORTH);
97         add(downloadRecordPanel, BorderLayout.CENTER);
98         
99         loginPanel.addLoginBtnActionListener(new ActionListener() {
100             @Override
101             public void actionPerformed(ActionEvent e) {
102                 if(loginPanel.checkLoginText()) {
103                     if(login()) {
104                         frame = ToolKits.getFrame(e);
105                         frame.setTitle(Res.string().getDownloadRecord() + " : " + Res.string().getOnline());
106                     }    
107                 }
108             }
109         });
110         
111         loginPanel.addLogoutBtnActionListener(new ActionListener() {        
112             @Override
113             public void actionPerformed(ActionEvent e) {
114                 frame.setTitle(Res.string().getDownloadRecord());
115                 logout();
116             }
117         });
118         
119         addWindowListener(new WindowAdapter() {
120             public void windowClosing(WindowEvent e) {
121                 DownLoadRecordModule.stopDownLoadRecordFile(m_hDownLoadByFileHandle);
122                 DownLoadRecordModule.stopDownLoadRecordFile(m_hDownLoadByTimeHandle);
123                 LoginModule.logout();
124                 LoginModule.cleanup();   // 关闭工程,释放资源
125                 dispose();        
126                 
127                 SwingUtilities.invokeLater(new Runnable() {
128                     public void run() {
129                         FunctionList demo = new FunctionList();
130                         demo.setVisible(true);
131                     }
132                 });
133             }
134         });
135     }
136     
137     /////////////////面板//////////////////
138     // 设备断线回调: 通过 CLIENT_Init 设置该回调函数,当设备出现断线时,SDK会调用该函数
139     private class DisConnect implements NetSDKLib.fDisConnect {
140         public void invoke(LLong m_hLoginHandle, String pchDVRIP, int nDVRPort, Pointer dwUser) {
141             System.out.printf("Device[%s] Port[%d] DisConnect!\n", pchDVRIP, nDVRPort);
142             // 断线提示
143             SwingUtilities.invokeLater(new Runnable() {
144                 public void run() {
145                     frame.setTitle(Res.string().getDownloadRecord() + " : " + Res.string().getDisConnectReconnecting());
146
147                     setButtonEnable(true);
148                     b_downloadByFile = false;
149                     downloadByFileBtn.setText(Res.string().getDownload());
150                     b_downloadByTime = false;
151                     downloadByTimeBtn.setText(Res.string().getDownload());
152                     DownLoadRecordModule.stopDownLoadRecordFile(m_hDownLoadByFileHandle);
153                     DownLoadRecordModule.stopDownLoadRecordFile(m_hDownLoadByTimeHandle);
154                 }
155             });    
156         }
157     }
158     
159     // 网络连接恢复,设备重连成功回调
160     // 通过 CLIENT_SetAutoReconnect 设置该回调函数,当已断线的设备重连成功时,SDK会调用该函数
161     private static class HaveReConnect implements NetSDKLib.fHaveReConnect {
162         public void invoke(LLong m_hLoginHandle, String pchDVRIP, int nDVRPort, Pointer dwUser) {
163             System.out.printf("ReConnect Device[%s] Port[%d]\n", pchDVRIP, nDVRPort);
164             
165             // 重连提示
166             SwingUtilities.invokeLater(new Runnable() {
167                 public void run() {
168                     frame.setTitle(Res.string().getDownloadRecord() + " : " + Res.string().getOnline());
169                 }
170             });
171         }
172     }
173     
174     // 登录
175     public boolean login() {
176         Native.setCallbackThreadInitializer(m_DownLoadPosByFile, 
177                                             new CallbackThreadInitializer(false, false, "downloadbyfile callback thread")); 
178         Native.setCallbackThreadInitializer(m_DownLoadPosByTime, 
179                                             new CallbackThreadInitializer(false, false, "downloadbytime callback thread")); 
180         if(LoginModule.login(loginPanel.ipTextArea.getText(), 
181                         Integer.parseInt(loginPanel.portTextArea.getText()), 
182                         loginPanel.nameTextArea.getText(), 
183                         new String(loginPanel.passwordTextArea.getPassword()))) {
184             loginPanel.setButtonEnable(true);
185             setButtonEnable(true);              
186             
187             for(int i = 1; i < LoginModule.m_stDeviceInfo.byChanNum + 1; i++) {
188                 chnlist.add(Res.string().getChannel() + " " + String.valueOf(i));
189             }
190             
191             // 默认设置主辅码流
192             DownLoadRecordModule.setStreamType(streamComboBoxByFile.getSelectedIndex());
193             
194             // 登陆成功,将通道添加到控件
195             chnComboBoxByFile.setModel(new DefaultComboBoxModel(chnlist));
196             chnComboBoxByTime.setModel(new DefaultComboBoxModel(chnlist));      
197         } else {
198             JOptionPane.showMessageDialog(null, Res.string().getLoginFailed() + ", " + ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
199             return false;
200         }
201         
202         return true;
203     }
204     
205     //登出
206     public void logout() {
207         LoginModule.logout();
208         loginPanel.setButtonEnable(false);
209         setButtonEnable(false);
210         
211         // 列表清空
212         data = new Object[14][5];
213         table.setModel(new DefaultTableModel(data, Res.string().getDownloadTableName()));
214         table.getColumnModel().getColumn(0).setPreferredWidth(23);
215         table.getColumnModel().getColumn(1).setPreferredWidth(28);
216         table.getColumnModel().getColumn(2).setPreferredWidth(50);                  
217             
218         for(int i = 0; i < LoginModule.m_stDeviceInfo.byChanNum; i++) {
219             chnlist.clear();
220         }
221         
222         chnComboBoxByFile.setModel(new DefaultComboBoxModel());
223         chnComboBoxByTime.setModel(new DefaultComboBoxModel());
224         
225         b_downloadByFile = false;
226         downloadByFileBtn.setText(Res.string().getDownload());
227         b_downloadByTime = false;
228         downloadByTimeBtn.setText(Res.string().getDownload());
229     }
230     
231     /*
232      * 下载录像面板
233      */
234     private class DownLoadRecordPanel extends JPanel {
235         private static final long serialVersionUID = 1L;
236         
237         public DownLoadRecordPanel() {
238             BorderEx.set(this, Res.string().getDownloadRecord(), 2);
239             setLayout(new GridLayout(1, 2));
240             
241             downloadByTimePanel = new DownLoadByTimePanel(); // 按时间下载
242             downloadByFilePanel = new DownLoadByFilePanel();  // 按文件下载
243             
244             add(downloadByTimePanel);
245             add(downloadByFilePanel);
246         }
247     }
248     
249     /*
250      * 按文件下载面板
251      */
252     private class DownLoadByFilePanel extends JPanel {
253         private static final long serialVersionUID = 1L;
254         
255         public DownLoadByFilePanel() {
256             BorderEx.set(this, Res.string().getDownloadByFile(), 2);
257             setLayout(new BorderLayout());
258             
259             downloadByFileSetPanel = new JPanel(); // 设置
260             queryPanel = new JPanel(); // 查询
261             downByFilePanel = new JPanel();  // 下载
262             
263             add(downloadByFileSetPanel, BorderLayout.NORTH);
264             add(queryPanel, BorderLayout.CENTER);
265             add(downByFilePanel, BorderLayout.SOUTH);
266             
267             /******** 设置面板***********/
268             JPanel startTimeByFile = new JPanel();
269             JPanel endTimeByFile = new JPanel();
270             JPanel chnByFile = new JPanel();
271             JPanel streamByFile = new JPanel();
272             
273             downloadByFileSetPanel.setLayout(new GridLayout(2, 2));
274             
275             downloadByFileSetPanel.add(startTimeByFile);
276             downloadByFileSetPanel.add(endTimeByFile);
277             downloadByFileSetPanel.add(chnByFile);
278             downloadByFileSetPanel.add(streamByFile);
279             
280             // 开始时间设置
281             startTimeByFile.setBorder(new EmptyBorder(5, 5, 5, 20));
282             startTimeByFile.setLayout(new GridLayout(2, 1));
283             JLabel startLabel = new JLabel(Res.string().getStartTime());
284             dateChooserStartByFile = new DateChooserJButton();
285             
286             Dimension dimension = new Dimension();
287             dimension.height = 20;
288             dateChooserStartByFile.setPreferredSize(dimension);
289             
290             startTimeByFile.add(startLabel);
291             startTimeByFile.add(dateChooserStartByFile);
292             
293             // 结束时间设置
294             endTimeByFile.setBorder(new EmptyBorder(5, 20, 5, 5));
295             endTimeByFile.setLayout(new GridLayout(2, 1));
296             JLabel endLabel = new JLabel(Res.string().getEndTime());
297             dateChooserEndByFile = new DateChooserJButton();
298             dateChooserEndByFile.setPreferredSize(dimension);
299             
300             endTimeByFile.add(endLabel);
301             endTimeByFile.add(dateChooserEndByFile);
302             
303             // 通道设置
304             chnByFile.setBorder(new EmptyBorder(5, 10, 0, 5));
305             chnByFile.setLayout(new FlowLayout());
306             chnlabel = new JLabel(Res.string().getChannel());
307             chnComboBoxByFile = new JComboBox();            
308             chnComboBoxByFile.setPreferredSize(new Dimension(115, 20));  
309             chnByFile.add(chnlabel);
310             chnByFile.add(chnComboBoxByFile);
311             
312             // 码流设置
313             streamByFile.setBorder(new EmptyBorder(5, 10, 0, 5));
314             streamByFile.setLayout(new FlowLayout());
315             streamLabel = new JLabel(Res.string().getStreamType());
316             String[] stream = {Res.string().getMasterAndSub(), Res.string().getMasterStream(), Res.string().getSubStream()};
317             streamComboBoxByFile = new JComboBox(stream);    
318             streamComboBoxByFile.setModel(new DefaultComboBoxModel(stream));
319             streamComboBoxByFile.setPreferredSize(new Dimension(115, 20));  
320             streamByFile.add(streamLabel);
321             streamByFile.add(streamComboBoxByFile);
322
323             /******** 查询面板***********/
324             queryPanel.setLayout(new BorderLayout());
325             queryPanel.setBorder(new EmptyBorder(0, 5, 5, 5));
326             
327             data = new Object[14][5];
328             defaultmodel = new DefaultTableModel(data, Res.string().getDownloadTableName());
329             table = new JTable(defaultmodel){
330                 private static final long serialVersionUID = 1L;
331                 @Override
332                 public boolean isCellEditable(int row, int column) {
333                     return false;
334                 }
335             };
336             table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);  // 只能选中一行    
337             
338             table.getColumnModel().getColumn(0).setPreferredWidth(20);
339             table.getColumnModel().getColumn(1).setPreferredWidth(20);
340             table.getColumnModel().getColumn(2).setPreferredWidth(50);
341             
342             DefaultTableCellRenderer dCellRenderer = new DefaultTableCellRenderer();
343             dCellRenderer.setHorizontalAlignment(JLabel.CENTER);
344             table.setDefaultRenderer(Object.class, dCellRenderer);
345             
346             queryPanel.add(new JScrollPane(table), BorderLayout.CENTER);
347             
348             /******** 下载面板***********/
349             downByFilePanel.setLayout(new BorderLayout());
350             downByFilePanel.setBorder(new EmptyBorder(5, 5, 5, 5));
351             
352             JPanel btnPanel1 = new JPanel();
353             downloadByFileProgressBar = new JProgressBar(0, 100);
354             
355             downloadByFileProgressBar.setPreferredSize(new Dimension(100, 20)); 
356             downloadByFileProgressBar.setStringPainted(true);
357             
358             downByFilePanel.add(btnPanel1, BorderLayout.CENTER);
359             downByFilePanel.add(downloadByFileProgressBar, BorderLayout.SOUTH);
360             
361             // 查询、下载按钮
362             queryRecordBtn = new JButton(Res.string().getQuery());
363             downloadByFileBtn = new JButton(Res.string().getDownload());    
364             
365             queryRecordBtn.setPreferredSize(new Dimension(175, 20)); 
366             downloadByFileBtn.setPreferredSize(new Dimension(175, 20)); 
367             
368             btnPanel1.setLayout(new FlowLayout());
369             btnPanel1.add(queryRecordBtn);
370             btnPanel1.add(downloadByFileBtn);
371             
372             queryRecordBtn.setEnabled(false);
373             downloadByFileBtn.setEnabled(false);
374             downloadByFileProgressBar.setEnabled(false);
375             chnComboBoxByFile.setEnabled(false);
376             streamComboBoxByFile.setEnabled(false);
377             dateChooserStartByFile.setEnabled(false);
378             dateChooserEndByFile.setEnabled(false);
379             
380             streamComboBoxByFile.addActionListener(new ActionListener() {        
381                 @Override
382                 public void actionPerformed(ActionEvent e) {
383                     DownLoadRecordModule.setStreamType(streamComboBoxByFile.getSelectedIndex());
384                 }
385             });
386             
387             queryRecordBtn.addActionListener(new ActionListener() {
388                 @Override
389                 public void actionPerformed(ActionEvent arg0) {
390                     int i = 1; // 列表序号
391                     int time = 0;
392                     
393                     System.out.println(dateChooserStartByFile.getText() + "\n" + dateChooserEndByFile.getText());
394                     // 开始时间
395                     String[] dateStartByFile = dateChooserStartByFile.getText().split(" ");
396                     String[] dateStart1 = dateStartByFile[0].split("-");
397                     String[] dateStart2 = dateStartByFile[1].split(":");
398                     
399                     stTimeStart.dwYear = Integer.parseInt(dateStart1[0]);
400                     stTimeStart.dwMonth = Integer.parseInt(dateStart1[1]);
401                     stTimeStart.dwDay = Integer.parseInt(dateStart1[2]);
402                     
403                     stTimeStart.dwHour = Integer.parseInt(dateStart2[0]);
404                     stTimeStart.dwMinute = Integer.parseInt(dateStart2[1]);
405                     stTimeStart.dwSecond = Integer.parseInt(dateStart2[2]);
406                     
407                     // 结束时间
408                     String[] dateEndByFile = dateChooserEndByFile.getText().split(" ");
409                     String[] dateEnd1 = dateEndByFile[0].split("-");
410                     String[] dateEnd2 = dateEndByFile[1].split(":");
411                     
412                     stTimeEnd.dwYear = Integer.parseInt(dateEnd1[0]);
413                     stTimeEnd.dwMonth = Integer.parseInt(dateEnd1[1]);
414                     stTimeEnd.dwDay = Integer.parseInt(dateEnd1[2]);
415                     
416                     stTimeEnd.dwHour = Integer.parseInt(dateEnd2[0]);
417                     stTimeEnd.dwMinute = Integer.parseInt(dateEnd2[1]);
418                     stTimeEnd.dwSecond = Integer.parseInt(dateEnd2[2]);
419                         
420                     if(stTimeStart.dwYear != stTimeEnd.dwYear
421                        || stTimeStart.dwMonth != stTimeEnd.dwMonth
422                        || (stTimeEnd.dwDay - stTimeStart.dwDay > 1)) {
423                         JOptionPane.showMessageDialog(null, Res.string().getSelectTimeAgain(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
424                         return;                
425                     }
426                     
427                     if(stTimeEnd.dwDay - stTimeStart.dwDay == 1) {
428                         time = (24 + stTimeEnd.dwHour)*60*60 + stTimeEnd.dwMinute*60 + stTimeEnd.dwSecond -
429                                    stTimeStart.dwHour*60*60 - stTimeStart.dwMinute*60 - stTimeStart.dwSecond;
430                     } else {
431                         time = stTimeEnd.dwHour*60*60 + stTimeEnd.dwMinute*60 + stTimeEnd.dwSecond -
432                                    stTimeStart.dwHour*60*60 - stTimeStart.dwMinute*60 - stTimeStart.dwSecond;
433                     }
434
435                     if(time > 6 * 60 * 60 
436                        || time <= 0) {
437                         JOptionPane.showMessageDialog(null, Res.string().getSelectTimeAgain(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
438                         return;    
439                     }
440                                         
441                     if(!DownLoadRecordModule.queryRecordFile(chnComboBoxByFile.getSelectedIndex(), 
442                                                stTimeStart, 
443                                                stTimeEnd, 
444                                                stFileInfo,
445                                                nFindCount)) {
446                         // 列表清空
447                         data = new Object[14][5];
448                         table.setModel(new DefaultTableModel(data, Res.string().getDownloadTableName()));
449                         table.getColumnModel().getColumn(0).setPreferredWidth(23);
450                         table.getColumnModel().getColumn(1).setPreferredWidth(28);
451                         table.getColumnModel().getColumn(2).setPreferredWidth(50);     
452                         JOptionPane.showMessageDialog(null, ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
453                     } else {
454                         System.out.println(nFindCount.getValue());
455                           int count = 0;
456                           if(nFindCount.getValue() > 14) {
457                               count = nFindCount.getValue();
458                           } else {
459                               count = 14;
460                           }
461                           data = new Object[count][5];
462                           table.setModel(new DefaultTableModel(data, Res.string().getDownloadTableName()));
463                         table.getColumnModel().getColumn(0).setPreferredWidth(23);
464                         table.getColumnModel().getColumn(1).setPreferredWidth(28);
465                         table.getColumnModel().getColumn(2).setPreferredWidth(50);
466                         
467                         if(nFindCount.getValue() == 0) {     
468                             return;
469                         }
470                         
471                         model = (DefaultTableModel)table.getModel();
472                         
473                         for(int j = 0; j < nFindCount.getValue(); j++) {
474                             model.setValueAt(String.valueOf(i), j, 0);
475                             model.setValueAt(String.valueOf(stFileInfo[j].ch + 1), j, 1);    // 设备返回的通道加1
476                             model.setValueAt(Res.string().getRecordTypeStr(stFileInfo[j].nRecordFileType), j, 2);
477                             model.setValueAt(stFileInfo[j].starttime.toStringTime(), j, 3);
478                             model.setValueAt(stFileInfo[j].endtime.toStringTime(), j, 4);
479                             
480                             i++;
481                         }
482                     }
483                 }
484             });
485             
486             downloadByFileBtn.addActionListener(new ActionListener() {            
487                 @Override
488                 public void actionPerformed(ActionEvent e) {    
489                     int row = -1;
490                     row = table.getSelectedRow(); //获得所选的单行
491         
492                     if(model == null) {
493                         JOptionPane.showMessageDialog(null, Res.string().getQueryRecord(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
494                         return;
495                     }
496                     
497                     if(row < 0) {
498                         JOptionPane.showMessageDialog(null, Res.string().getSelectRowWithData(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
499                         return;
500                     }
501                     
502                     for(int m = 1; m < 5; m++) {
503                         if(model.getValueAt(row, m) == null || String.valueOf(model.getValueAt(row, m)).trim().equals("")) {
504                             JOptionPane.showMessageDialog(null, Res.string().getSelectRowWithData(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
505                             return;
506                         }
507                     }                    
508                     
509                     // 开始时间
510                     String[] dateStart = String.valueOf(model.getValueAt(row, 3)).split(" ");
511                     String[] dateStartByFile1 = dateStart[0].split("/");
512                     String[] dateStartByFile2 = dateStart[1].split(":");
513                     
514                     stTimeStart.dwYear = Integer.parseInt(dateStartByFile1[0]);
515                     stTimeStart.dwMonth = Integer.parseInt(dateStartByFile1[1]);
516                     stTimeStart.dwDay = Integer.parseInt(dateStartByFile1[2]);
517                     
518                     stTimeStart.dwHour = Integer.parseInt(dateStartByFile2[0]);
519                     stTimeStart.dwMinute = Integer.parseInt(dateStartByFile2[1]);
520                     stTimeStart.dwSecond = Integer.parseInt(dateStartByFile2[2]);
521                     
522                     // 结束时间
523                     String[] dateEnd = String.valueOf(model.getValueAt(row, 4)).split(" ");
524                     String[] dateEndByFile1 = dateEnd[0].split("/");
525                     String[] dateEndByFile2 = dateEnd[1].split(":");
526                     
527                     stTimeEnd.dwYear = Integer.parseInt(dateEndByFile1[0]);
528                     stTimeEnd.dwMonth = Integer.parseInt(dateEndByFile1[1]);
529                     stTimeEnd.dwDay = Integer.parseInt(dateEndByFile1[2]);
530                     
531                     stTimeEnd.dwHour = Integer.parseInt(dateEndByFile2[0]);
532                     stTimeEnd.dwMinute = Integer.parseInt(dateEndByFile2[1]);
533                     stTimeEnd.dwSecond = Integer.parseInt(dateEndByFile2[2]);
534                     
535                     if(!b_downloadByFile) {
536                         System.out.println("ByFile" + String.valueOf(model.getValueAt(row, 3)) + "\n" + String.valueOf(model.getValueAt(row, 4)));
537                         SwingUtilities.invokeLater(new Runnable() {                
538                             @Override
539                             public void run() {
540                                 downloadByFileProgressBar.setValue(0);
541                             }
542                         });    
543                         m_hDownLoadByFileHandle = DownLoadRecordModule.downloadRecordFile(Integer.parseInt(String.valueOf(model.getValueAt(row, 1))) - 1, 
544                                                                               Res.string().getRecordTypeInt(String.valueOf(model.getValueAt(row, 2))), 
545                                                                               stTimeStart, 
546                                                                               stTimeEnd, 
547                                                                               SavePath.getSavePath().getSaveRecordFilePath(),
548                                                                               m_DownLoadPosByFile);
549                         if(m_hDownLoadByFileHandle.longValue() != 0) {
550                             b_downloadByFile = true;
551                             downloadByFileBtn.setText(Res.string().getStopDownload());
552                         } else {
553                             JOptionPane.showMessageDialog(null, ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
554                         }
555                     } else {
556                         DownLoadRecordModule.stopDownLoadRecordFile(m_hDownLoadByFileHandle);            
557                         b_downloadByFile = false;
558                         downloadByFileBtn.setText(Res.string().getDownload());
559                         SwingUtilities.invokeLater(new Runnable() {                
560                             @Override
561                             public void run() {
562                                 downloadByFileProgressBar.setValue(0);
563                             }
564                         });
565                     }
566                 }            
567             });
568         }
569     }
570     
571     /*
572      * 按时间下载面板
573      */
574     private class DownLoadByTimePanel extends JPanel {
575         private static final long serialVersionUID = 1L;
576         
577         public DownLoadByTimePanel() {
578             BorderEx.set(this, Res.string().getDownloadByTime(), 2);
579             setLayout(new BorderLayout());
580             
581             downloadByTimeSetPanel = new JPanel(); // 设置
582             downByTimePanel = new JPanel();  // 下载
583             
584             add(downloadByTimeSetPanel, BorderLayout.NORTH);
585             add(downByTimePanel, BorderLayout.CENTER);
586             
587             /******** 设置面板***********/
588             JPanel startTimeByTime = new JPanel();
589             JPanel endTimeByTime = new JPanel();
590             JPanel chnByTime = new JPanel();
591             JPanel streamByTime = new JPanel();
592             
593             downloadByTimeSetPanel.setLayout(new GridLayout(2, 2));
594             
595             downloadByTimeSetPanel.add(startTimeByTime);
596             downloadByTimeSetPanel.add(endTimeByTime);
597             downloadByTimeSetPanel.add(chnByTime);
598             downloadByTimeSetPanel.add(streamByTime);
599             
600             // 开始时间设置
601             startTimeByTime.setBorder(new EmptyBorder(5, 5, 5, 20));
602             startTimeByTime.setLayout(new GridLayout(2, 1));
603             JLabel startLabel = new JLabel(Res.string().getStartTime());
604             dateChooserStartByTime = new DateChooserJButton();
605             Dimension dimension = new Dimension();
606             dimension.height = 20;
607             dateChooserStartByTime.setPreferredSize(dimension);
608             
609             startTimeByTime.add(startLabel);
610             startTimeByTime.add(dateChooserStartByTime);
611             
612             // 结束时间设置
613             endTimeByTime.setBorder(new EmptyBorder(5, 20, 5, 5));
614             endTimeByTime.setLayout(new GridLayout(2, 1));
615             JLabel endLabel = new JLabel(Res.string().getEndTime());
616             dateChooserEndByTime = new DateChooserJButton();
617             dateChooserEndByTime.setPreferredSize(dimension);
618             
619             endTimeByTime.add(endLabel);
620             endTimeByTime.add(dateChooserEndByTime);
621             
622             // 通道设置
623             chnByTime.setBorder(new EmptyBorder(5, 10, 0, 5));
624             chnByTime.setLayout(new FlowLayout());
625             chnlabel = new JLabel(Res.string().getChannel());
626             chnComboBoxByTime = new JComboBox();    
627             chnComboBoxByTime.setPreferredSize(new Dimension(115, 20));  
628             chnByTime.add(chnlabel);
629             chnByTime.add(chnComboBoxByTime);
630             
631             // 码流设置
632             streamByTime.setBorder(new EmptyBorder(5, 10, 0, 5));
633             streamByTime.setLayout(new FlowLayout());
634             streamLabel = new JLabel(Res.string().getStreamType());
635             String[] stream = {Res.string().getMasterAndSub(), Res.string().getMasterStream(), Res.string().getSubStream()};
636             streamComboBoxByTime = new JComboBox();    
637             streamComboBoxByTime.setModel(new DefaultComboBoxModel(stream));
638             streamComboBoxByTime.setPreferredSize(new Dimension(115, 20));  
639             streamByTime.add(streamLabel);
640             streamByTime.add(streamComboBoxByTime);
641
642             /******** 下载面板***********/
643             downByTimePanel.setLayout(new FlowLayout());
644             downByTimePanel.setBorder(new EmptyBorder(0, 5, 0, 5));
645             
646             JPanel btnPanel2 = new JPanel();
647             downloadByTimeProgressBar = new JProgressBar(0, 100);
648             
649             downloadByTimeProgressBar.setPreferredSize(new Dimension(355, 20)); 
650             downloadByTimeProgressBar.setStringPainted(true);
651             
652             downByTimePanel.add(btnPanel2);
653             downByTimePanel.add(downloadByTimeProgressBar);
654             
655             // 下载按钮
656             downloadByTimeBtn = new JButton(Res.string().getDownload());
657             JLabel nullLabel = new JLabel();
658             nullLabel.setPreferredSize(new Dimension(180, 20)); 
659             downloadByTimeBtn.setPreferredSize(new Dimension(170, 20)); 
660             
661             btnPanel2.setLayout(new FlowLayout());
662             btnPanel2.add(downloadByTimeBtn);
663             btnPanel2.add(nullLabel);
664
665             downloadByTimeBtn.setEnabled(false);
666             downloadByTimeProgressBar.setEnabled(false);
667             chnComboBoxByTime.setEnabled(false);
668             streamComboBoxByTime.setEnabled(false);
669             dateChooserStartByTime.setEnabled(false);
670             dateChooserEndByTime.setEnabled(false);
671             
672             streamComboBoxByTime.addActionListener(new ActionListener() {
673                 @Override
674                 public void actionPerformed(ActionEvent e) {
675                     DownLoadRecordModule.setStreamType(streamComboBoxByTime.getSelectedIndex());
676                 }
677             });
678             
679             downloadByTimeBtn.addActionListener(new ActionListener() {    
680                 @Override
681                 public void actionPerformed(ActionEvent e) {
682                     int time = 0;
683                     // 开始时间
684                     String[] dateStartByTime = dateChooserStartByTime.getText().split(" ");
685                     String[] dateStart1 = dateStartByTime[0].split("-");
686                     String[] dateStart2 = dateStartByTime[1].split(":");
687                     
688                     stTimeStart.dwYear = Integer.parseInt(dateStart1[0]);
689                     stTimeStart.dwMonth = Integer.parseInt(dateStart1[1]);
690                     stTimeStart.dwDay = Integer.parseInt(dateStart1[2]);
691                     
692                     stTimeStart.dwHour = Integer.parseInt(dateStart2[0]);
693                     stTimeStart.dwMinute = Integer.parseInt(dateStart2[1]);
694                     stTimeStart.dwSecond = Integer.parseInt(dateStart2[2]);
695                     
696                     // 结束时间
697                     String[] dateEndByTime = dateChooserEndByTime.getText().split(" ");
698                     String[] dateEnd1 = dateEndByTime[0].split("-");
699                     String[] dateEnd2 = dateEndByTime[1].split(":");
700                     
701                     stTimeEnd.dwYear = Integer.parseInt(dateEnd1[0]);
702                     stTimeEnd.dwMonth = Integer.parseInt(dateEnd1[1]);
703                     stTimeEnd.dwDay = Integer.parseInt(dateEnd1[2]);
704                     
705                     stTimeEnd.dwHour = Integer.parseInt(dateEnd2[0]);
706                     stTimeEnd.dwMinute = Integer.parseInt(dateEnd2[1]);
707                     stTimeEnd.dwSecond = Integer.parseInt(dateEnd2[2]);
708                     
709                     if(stTimeStart.dwYear != stTimeEnd.dwYear
710                        || stTimeStart.dwMonth != stTimeEnd.dwMonth
711                        || (stTimeEnd.dwDay - stTimeStart.dwDay) > 1) {
712                         JOptionPane.showMessageDialog(null, Res.string().getSelectTimeAgain(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
713                         return;                
714                     }
715                     
716                     if(stTimeEnd.dwDay - stTimeStart.dwDay == 1) {
717                         time = (24 + stTimeEnd.dwHour)*60*60 + stTimeEnd.dwMinute*60 + stTimeEnd.dwSecond -
718                                    stTimeStart.dwHour*60*60 - stTimeStart.dwMinute*60 - stTimeStart.dwSecond;
719                     } else {
720                         time = stTimeEnd.dwHour*60*60 + stTimeEnd.dwMinute*60 + stTimeEnd.dwSecond -
721                                    stTimeStart.dwHour*60*60 - stTimeStart.dwMinute*60 - stTimeStart.dwSecond;
722                     }
723                     System.out.println("time :" + time);
724                     if(time > 6 * 60 * 60 
725                        || time <= 0) {
726                         JOptionPane.showMessageDialog(null, Res.string().getSelectTimeAgain(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
727                         return;    
728                     }
729                     
730                     if(!b_downloadByTime) {
731                         System.out.println("ByTime" + dateChooserStartByTime.getText() + "\n" + dateChooserEndByTime.getText());
732                         SwingUtilities.invokeLater(new Runnable() {                
733                             @Override
734                             public void run() {
735                                 downloadByTimeProgressBar.setValue(0);
736                             }
737                         });
738                         m_hDownLoadByTimeHandle = DownLoadRecordModule.downloadRecordFile(chnComboBoxByTime.getSelectedIndex(), 
739                                                                             0, 
740                                                                             stTimeStart, 
741                                                                             stTimeEnd, 
742                                                                             SavePath.getSavePath().getSaveRecordFilePath(),
743                                                                             m_DownLoadPosByTime);
744                         if(m_hDownLoadByTimeHandle.longValue() != 0) {
745                             b_downloadByTime = true;
746                             downloadByTimeBtn.setText(Res.string().getStopDownload());
747                             chnComboBoxByTime.setEnabled(false);
748                             streamComboBoxByTime.setEnabled(false);
749                             dateChooserStartByTime.setEnabled(false);
750                             dateChooserEndByTime.setEnabled(false);
751                         } else {
752                             JOptionPane.showMessageDialog(null, ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
753                         }
754                     } else {
755                         DownLoadRecordModule.stopDownLoadRecordFile(m_hDownLoadByTimeHandle);
756                         b_downloadByTime = false;
757                         downloadByTimeBtn.setText(Res.string().getDownload());
758                         chnComboBoxByTime.setEnabled(true);
759                         streamComboBoxByTime.setEnabled(true);
760                         dateChooserStartByTime.setEnabled(true);
761                         dateChooserEndByTime.setEnabled(true);
762                         
763                         SwingUtilities.invokeLater(new Runnable() {                
764                             @Override
765                             public void run() {
766                                 downloadByTimeProgressBar.setValue(0);
767                             }
768                         });
769                     }
770                 }
771             });
772         }
773     }
774     
775     /*
776      * 按文件下载回调
777      */
778     private DownLoadPosCallBackByFile m_DownLoadPosByFile = new DownLoadPosCallBackByFile(); // 录像下载进度
779     class DownLoadPosCallBackByFile implements NetSDKLib.fTimeDownLoadPosCallBack{
780         public void invoke(LLong lLoginID, final int dwTotalSize, final int dwDownLoadSize, int index, NetSDKLib.NET_RECORDFILE_INFO.ByValue recordfileinfo, Pointer dwUser) {    
781             SwingUtilities.invokeLater(new Runnable() {    
782                 @Override
783                 public void run() {
784 //                    System.out.println("ByFile " + dwDownLoadSize + " / " + dwTotalSize);
785                     downloadByFileProgressBar.setValue(dwDownLoadSize*100 / dwTotalSize);
786                     if(dwDownLoadSize == -1) {
787                         downloadByFileProgressBar.setValue(100);
788                         DownLoadRecordModule.stopDownLoadRecordFile(m_hDownLoadByFileHandle);
789                         b_downloadByFile = false;
790                         downloadByFileBtn.setText(Res.string().getDownload());
791                         JOptionPane.showMessageDialog(null, Res.string().getDownloadCompleted(), Res.string().getPromptMessage(), JOptionPane.INFORMATION_MESSAGE);
792                     }
793                 }
794             });
795         }
796     }
797     
798     /*
799      * 按时间下载回调
800      */
801     private DownLoadPosCallBackByTime m_DownLoadPosByTime = new DownLoadPosCallBackByTime(); // 录像下载进度
802     class DownLoadPosCallBackByTime implements NetSDKLib.fTimeDownLoadPosCallBack{
803         public void invoke(LLong lLoginID, final int dwTotalSize, final int dwDownLoadSize, int index, NetSDKLib.NET_RECORDFILE_INFO.ByValue recordfileinfo, Pointer dwUser) {    
804             SwingUtilities.invokeLater(new Runnable() {
805                 @Override
806                 public void run() {
807 //                    System.out.println("ByTime " + dwDownLoadSize + " / " + dwTotalSize);
808                     downloadByTimeProgressBar.setValue(dwDownLoadSize*100 / dwTotalSize);
809                     if(dwDownLoadSize == -1) {
810                         downloadByTimeProgressBar.setValue(100);
811                         DownLoadRecordModule.stopDownLoadRecordFile(m_hDownLoadByTimeHandle);
812                         b_downloadByTime = false;
813                         downloadByTimeBtn.setText(Res.string().getDownload());
814                         chnComboBoxByTime.setEnabled(true);
815                         streamComboBoxByTime.setEnabled(true);
816                         dateChooserStartByTime.setEnabled(true);
817                         dateChooserEndByTime.setEnabled(true);
818                         JOptionPane.showMessageDialog(null, Res.string().getDownloadCompleted(), Res.string().getPromptMessage(), JOptionPane.INFORMATION_MESSAGE);
819                     }
820                 }
821             });
822         }
823     }
824     
825     private void setButtonEnable(boolean bln) {
826         queryRecordBtn.setEnabled(bln);
827         downloadByFileBtn.setEnabled(bln);
828         downloadByFileProgressBar.setValue(0);
829         downloadByFileProgressBar.setEnabled(bln);
830         downloadByTimeBtn.setEnabled(bln);
831         downloadByTimeProgressBar.setValue(0);
832         downloadByTimeProgressBar.setEnabled(bln);     
833         chnComboBoxByFile.setEnabled(bln);
834         streamComboBoxByFile.setEnabled(bln);
835         chnComboBoxByTime.setEnabled(bln);
836         streamComboBoxByTime.setEnabled(bln);
837         dateChooserStartByFile.setEnabled(bln);
838         dateChooserEndByFile.setEnabled(bln);
839         dateChooserStartByTime.setEnabled(bln);
840         dateChooserEndByTime.setEnabled(bln);    
841     }
842         
843     //登录组件
844     private LoginPanel loginPanel;
845     // 下载
846     private DownLoadRecordPanel downloadRecordPanel;
847     
848     // 按文件下载
849     private DownLoadByTimePanel downloadByTimePanel; 
850     private JPanel downloadByFileSetPanel;
851     private JPanel queryPanel;
852     private JPanel downByFilePanel;
853     private JButton queryRecordBtn;
854     private JButton downloadByFileBtn;
855     private JProgressBar downloadByFileProgressBar;
856     private JButton downloadByTimeBtn;
857     private JProgressBar downloadByTimeProgressBar;
858     private JTable table;
859     private DefaultTableModel defaultmodel;
860     private JLabel chnlabel;
861     private JComboBox chnComboBoxByFile;    
862     private JComboBox chnComboBoxByTime;    
863     private JLabel streamLabel;
864     private JComboBox streamComboBoxByFile;
865     private JComboBox streamComboBoxByTime;
866     
867     private DateChooserJButton dateChooserStartByFile;
868     private DateChooserJButton dateChooserEndByFile;
869     
870      // 按文件下载
871     private DownLoadByFilePanel downloadByFilePanel; 
872     private JPanel downloadByTimeSetPanel;
873     private JPanel downByTimePanel;
874     
875     private DateChooserJButton dateChooserStartByTime;
876     private DateChooserJButton dateChooserEndByTime;
877 }
878
879 public class DownLoadRecord {  
880     public static void main(String[] args) {    
881         SwingUtilities.invokeLater(new Runnable() {
882             public void run() {
883                 DownLoadRecordFrame demo = new DownLoadRecordFrame();
884                 demo.setVisible(true);
885             }
886         });        
887     }
888 };
889