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