dongyukun
8 天以前 f6eecba7ffb1535a2748f3f31ca255e2e0743267
提交 | 用户 | 时间
149dd0 1 package com.iailab.netsdk.demo.frame.AutoRegister;
H 2
3 import java.awt.BorderLayout;
4 import java.awt.Color;
5 import java.awt.Component;
6 import java.awt.Dimension;
7 import java.awt.FlowLayout;
8 import java.awt.Panel;
9 import java.awt.event.ActionEvent;
10 import java.awt.event.ActionListener;
11 import java.awt.event.MouseEvent;
12 import java.awt.event.MouseListener;
13 import java.awt.event.WindowAdapter;
14 import java.awt.event.WindowEvent;
15 import java.awt.image.BufferedImage;
16 import java.io.BufferedReader;
17 import java.io.BufferedWriter;
18 import java.io.ByteArrayInputStream;
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileNotFoundException;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.InputStreamReader;
25 import java.io.OutputStreamWriter;
26 import java.io.UnsupportedEncodingException;
27 import java.net.InetAddress;
28 import java.net.UnknownHostException;
29 import java.util.concurrent.ExecutionException;
30
31 import javax.imageio.ImageIO;
32 import javax.swing.BorderFactory;
33 import javax.swing.ImageIcon;
34 import javax.swing.JButton;
35 import javax.swing.JFileChooser;
36 import javax.swing.JFrame;
37 import javax.swing.JLabel;
38 import javax.swing.JMenuItem;
39 import javax.swing.JOptionPane;
40 import javax.swing.JPanel;
41 import javax.swing.JPopupMenu;
42 import javax.swing.JScrollPane;
43 import javax.swing.JTextField;
44 import javax.swing.JTree;
45 import javax.swing.SwingUtilities;
46 import javax.swing.SwingWorker;
47 import javax.swing.UIManager;
48 import javax.swing.filechooser.FileFilter;
49 import javax.swing.tree.DefaultMutableTreeNode;
50 import javax.swing.tree.DefaultTreeCellRenderer;
51 import javax.swing.tree.DefaultTreeModel;
52 import javax.swing.tree.TreePath;
53 import javax.swing.tree.TreeSelectionModel;
54
55 import com.sun.jna.Pointer;
56
57 import com.iailab.netsdk.common.BorderEx;
58 import com.iailab.netsdk.common.DeviceManagerListener;
59 import com.iailab.netsdk.common.FunctionList;
60 import com.iailab.netsdk.common.PaintPanel;
61 import com.iailab.netsdk.common.Res;
62 import com.iailab.netsdk.common.SavePath;
63 import com.iailab.netsdk.demo.module.*;
64 import com.iailab.netsdk.lib.NetSDKLib;
65 import com.iailab.netsdk.lib.ToolKits;
66 import com.iailab.netsdk.lib.NetSDKLib.*;
67
68 /**
69  * 主动注册Demo:包含主动注册、实时预览、对讲、主动注册网络配置
70  */
71 class AutoRegisterFrame extends JFrame {
72     private static final long serialVersionUID = 1L;
73     
74     // 设备断线通知回调
75     private DisConnect disConnectCallback = new DisConnect(); 
76     
77     // 主动注册监听回调
78     private ServiceCB servicCallback = new ServiceCB();
79     
80     // 抓图回调
81     public CaptureReceiveCB  captureCallback = new CaptureReceiveCB();
82     
83     private boolean isExist = false;
84     
85     private TreeCellRender treeCellRender = new TreeCellRender();
86     
87     // 预览句柄
88     private LLong realplayHandle = new LLong(0);
89     
90     // 预览节点
91     private ChannelTreeNode realplayChannelTreeNode = null;
92     
93     // 对讲节点
94     private DeviceTreeNode talkDeviceTreeNode = null;
95     
96     public AutoRegisterFrame() {
97         setTitle(Res.string().getAutoRegister());
98         setLayout(new BorderLayout());
99         pack();
100         setSize(780, 540);
101         setResizable(false);
102         setLocationRelativeTo(null);
103         
104         // 打开工程,初始化,设置断线回调
105         LoginModule.init(disConnectCallback, null);   
106         
107         // 设置抓图回调
108         AutoRegisterModule.setSnapRevCallBack(captureCallback);
109         
110         try {
111             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
112         } catch (Exception e) {
113             e.printStackTrace();
114         } 
115         
116         // 注册列表面板
117         AutoRegisterTreePanel autoRegisterTreePanel = new AutoRegisterTreePanel();
118         
119         // 实时预览面板
120         RealPlayPanel realplayPanel = new RealPlayPanel();           
121         
122         // 监听和设备管理面板
123         ListenDeviceManagerPanel listenDeviceManagerPanel = new ListenDeviceManagerPanel();
124
125         add(autoRegisterTreePanel, BorderLayout.WEST);
126         add(realplayPanel, BorderLayout.CENTER);
127         add(listenDeviceManagerPanel, BorderLayout.EAST);
128         
129         addWindowListener(new WindowAdapter() {
130             public void windowClosing(WindowEvent e) {
131                 // 停止对讲
132                 if(AutoRegisterModule.m_hTalkHandle.longValue() != 0) {
133                     AutoRegisterModule.stopTalk(AutoRegisterModule.m_hTalkHandle);                                    
134                     talkDeviceTreeNode = null;
135                 }    
136                 
137                 // 停止拉流
138                 if(realplayHandle.longValue() != 0) {
139                     AutoRegisterModule.stopRealPlay(realplayHandle);
140                     realplayWindowPanel.repaint();
141                     
142                     realplayChannelTreeNode = null;                        
143                 }            
144                 
145                 // 登出所有设备
146                 for(int i = 0; i < rootNode.getChildCount(); i++) {
147                     DeviceTreeNode deviceTreeNode = (DeviceTreeNode)rootNode.getChildAt(i);
148                     if(deviceTreeNode.getDeviceInfo().getLoginHandle().longValue() != 0) {
149                         AutoRegisterModule.logout(deviceTreeNode.getDeviceInfo().getLoginHandle());
150                     }
151                 }
152     
153                 // 停止服务
154                 AutoRegisterModule.stopServer();
155                 
156                 LoginModule.cleanup();   // 关闭工程,释放资源
157                 dispose();    
158                 
159                 SwingUtilities.invokeLater(new Runnable() {
160                     public void run() {
161                         FunctionList demo = new FunctionList();
162                         demo.setVisible(true);
163                     }
164                 });
165             }
166         });
167     }
168     
169     /////////////////面板///////////////////
170     // 设备断线回调: 通过 CLIENT_Init 设置该回调函数,当设备出现断线时,SDK会调用该函数
171     private class DisConnect implements fDisConnect {
172         public void invoke(LLong m_hLoginHandle, String pchDVRIP, int nDVRPort, Pointer dwUser) {
173             System.out.printf("Device[%s] Port[%d] DisConnect!\n", pchDVRIP, nDVRPort);
174                 
175             for(int i = 0; i < rootNode.getChildCount(); i++) {
176                 DeviceTreeNode deviceTreeNode = (DeviceTreeNode)rootNode.getChildAt(i);            
177             
178                 // 根据设备IP判断断线设备
179                 if(pchDVRIP.equals(deviceTreeNode.getDeviceInfo().getDeviceIp())
180                         && nDVRPort == deviceTreeNode.getDeviceInfo().getDevicePort()) {                        
181                 
182                     synchronized (this) {
183                         // 停止断线设备的对讲, 主动注册中,对讲要用同步,不能在另开的线程里停止对讲,否则会出现句柄无效的错误                    
184                         if(deviceTreeNode == talkDeviceTreeNode) {
185                             AutoRegisterModule.stopTalk(AutoRegisterModule.m_hTalkHandle);                    
186
187                             talkDeviceTreeNode = null;
188                         }
189
190                         // 停止断线设备的拉流
191                         for(int j = 0; j < deviceTreeNode.getChildCount(); j++) {
192                             ChannelTreeNode channelTreeNode = (ChannelTreeNode)deviceTreeNode.getChildAt(j);
193                             if(channelTreeNode == realplayChannelTreeNode) {
194                                 AutoRegisterModule.stopRealPlay(realplayHandle);
195                                 realplayWindowPanel.repaint();
196                                 
197                                 realplayChannelTreeNode = null;
198
199                                 break;
200                             }                            
201                         }        
202                         
203                         // 登出
204                         if(deviceTreeNode.getDeviceInfo().getLoginHandle().longValue() != 0) {
205                             AutoRegisterModule.logout(deviceTreeNode.getDeviceInfo().getLoginHandle());
206                         }
207                         
208                         SwingUtilities.invokeLater(new DisConnectRunnable(deviceTreeNode));
209                     }        
210                     
211                     break;
212                 }    
213             }
214                     
215         }
216     }
217     
218     private class DisConnectRunnable implements Runnable {
219         DeviceTreeNode devicetTreeNode;
220         
221         public DisConnectRunnable(DeviceTreeNode devicetTreeNode) {
222             this.devicetTreeNode = devicetTreeNode;
223         }
224     
225         @Override
226         public void run() {            
227             devicetTreeNode.setUserObject(devicetTreeNode.getDeviceInfo().getDevcieId());                
228             devicetTreeNode.removeAllChildren();                          
229                         
230             tree.updateUI();                
231         }    
232     }
233     
234     private class AutoRegisterTreePanel extends JPanel {
235         /**
236          * 
237          */
238         private static final long serialVersionUID = 1L;
239
240         public AutoRegisterTreePanel() {
241             setLayout(new BorderLayout());
242             Dimension dimension = new Dimension();
243             dimension.width = 210;
244             setPreferredSize(dimension);
245             
246             JPanel configPanel = new JPanel();
247             JPanel deviceListPanel = new JPanel();
248             
249             add(configPanel, BorderLayout.NORTH);
250             add(deviceListPanel, BorderLayout.CENTER);
251             
252             ////// 配置面板  ////////
253             configPanel.setBorder(BorderFactory.createTitledBorder(Res.string().getOperate()));
254             configPanel.setPreferredSize(new Dimension(200, 55));
255             configPanel.setLayout(new FlowLayout());
256             
257             JButton configBtn = new JButton(Res.string().getDeviceConfig());
258             configBtn.setPreferredSize(new Dimension(180, 21));
259             
260             configPanel.add(configBtn);
261             
262             ////// 设备列表面板 ///////
263             deviceListPanel.setBorder(BorderFactory.createTitledBorder(Res.string().getDeviceList()));
264             deviceListPanel.setLayout(new BorderLayout());
265             
266             rootNode = new DefaultMutableTreeNode();             
267             treeModel = new DefaultTreeModel(rootNode);
268             tree = new JTree(treeModel);
269
270             tree.setBackground(Color.LIGHT_GRAY);
271             tree.setForeground(Color.BLACK);
272             tree.setEditable(false);
273             tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);  
274             tree.setRootVisible(false);
275             tree.expandPath(new TreePath(rootNode));
276             tree.setShowsRootHandles(true);
277         
278             JScrollPane jScrollPane = new JScrollPane(tree);        
279             deviceListPanel.add(jScrollPane, BorderLayout.CENTER);
280           
281             // 配置设备
282             configBtn.addActionListener(new ActionListener() {            
283                 @Override
284                 public void actionPerformed(ActionEvent arg0) {
285                     DeviceConfigDialog deviceConfigDialog = new DeviceConfigDialog();
286                     deviceConfigDialog.setVisible(true);        
287                 }
288             });
289             
290             tree.addMouseListener(new MouseListener() {        
291                 @Override
292                 public void mouseReleased(MouseEvent e) {
293
294                 }
295                 
296                 @Override
297                 public void mousePressed(MouseEvent e) {
298                     // 右键
299                     if(SwingUtilities.isRightMouseButton(e)) {
300                         TreePath treePath = tree.getPathForLocation(e.getX(), e.getY());
301                         tree.setSelectionPath(treePath);
302                         
303                         showPopupMenu(e.getComponent(), e.getX(), e.getY());
304                     }    
305                 }
306                 
307                 @Override
308                 public void mouseExited(MouseEvent e) {
309     
310                 }
311                 
312                 @Override
313                 public void mouseEntered(MouseEvent e) {
314
315                 }
316                 
317                 @Override
318                 public void mouseClicked(MouseEvent e) {
319                     // 双击
320                     if(e.getClickCount() > 1) {
321                         if(tree.getLastSelectedPathComponent() instanceof ChannelTreeNode) {
322                             ChannelTreeNode channelTreeNode = (ChannelTreeNode)tree.getLastSelectedPathComponent();
323                             
324                             if(channelTreeNode != realplayChannelTreeNode) {
325                                 startRealplay();
326                             }    
327                         }    
328                     }
329                 }
330             });
331         }
332     }
333     
334     /**
335      * 树的右键弹出菜单
336      */
337     @SuppressWarnings("static-access")
338     private void showPopupMenu(    Component component, int x, int y) {
339         JPopupMenu popupMenu = new JPopupMenu();
340         
341         JMenuItem logoutMenuItem = new JMenuItem(Res.string().getLogout());
342         JMenuItem addDeviceMenuItem = new JMenuItem(Res.string().getAddDevice());
343         JMenuItem modifyDeviceMenuItem = new JMenuItem(Res.string().getModifyDevice());
344         JMenuItem deleteDeviceMenuItem = new JMenuItem(Res.string().getDeleteDevice());
345         JMenuItem clearDeviceMenuItem = new JMenuItem(Res.string().getClearDevice());
346         JMenuItem importDeviceMenuItem = new JMenuItem(Res.string().getImportDevice());
347         JMenuItem exportDeviceMenuItem = new JMenuItem(Res.string().getExportDevice());
348         JMenuItem realplayMenuItem = new JMenuItem(Res.string().getStartRealPlay());
349         JMenuItem stopRealplayMenuItem = new JMenuItem(Res.string().getStopRealPlay());
350         JMenuItem startTalkMenuItem = new JMenuItem(Res.string().getStartTalk());
351         JMenuItem stopTalkMenuItem = new JMenuItem(Res.string().getStopTalk());
352         JMenuItem captureMenuItem = new JMenuItem(Res.string().getRemoteCapture());
353         
354         if(tree.getLastSelectedPathComponent() instanceof DeviceTreeNode) {  // 右击设备节点
355             DeviceTreeNode deviceTreeNode = (DeviceTreeNode)tree.getLastSelectedPathComponent();    
356             
357             if(deviceTreeNode!=null&&deviceTreeNode.getDeviceInfo().getLoginHandle().longValue() != 0) { // 登陆
358                 popupMenu.add(logoutMenuItem);
359                 popupMenu.add(deleteDeviceMenuItem);
360                 
361                 if(deviceTreeNode == talkDeviceTreeNode) {  // 对讲
362                     popupMenu.add(stopTalkMenuItem);
363                 } else {  // 停止对讲
364                     popupMenu.add(startTalkMenuItem);
365                 }            
366             } else {  // 未登陆
367                 popupMenu.add(modifyDeviceMenuItem);
368                 popupMenu.add(deleteDeviceMenuItem);
369             }
370         } else if(tree.getLastSelectedPathComponent() instanceof ChannelTreeNode) {  // 右击通道节点
371             ChannelTreeNode channelTreeNode = (ChannelTreeNode)tree.getLastSelectedPathComponent();        
372             
373             if(channelTreeNode == realplayChannelTreeNode) { // 拉流
374                 popupMenu.add(stopRealplayMenuItem);
375             } else {  // 未拉流
376                 popupMenu.add(realplayMenuItem);
377             }
378             
379             popupMenu.add(captureMenuItem);
380         } else { // 右键空白区域
381             popupMenu.add(addDeviceMenuItem);
382             popupMenu.add(clearDeviceMenuItem);
383             popupMenu.add(importDeviceMenuItem);
384             popupMenu.add(exportDeviceMenuItem);
385             
386             logoutBtn.setEnabled(false);
387             modifyDeviceBtn.setEnabled(false);
388             deleteDeviceBtn.setEnabled(false);
389             startRealPlayBtn.setEnabled(false);
390             stopRealPlayBtn.setEnabled(false);
391             startTalkBtn.setEnabled(false);
392             stopTalkBtn.setEnabled(false);
393             captureBtn.setEnabled(false);
394         }
395         
396         popupMenu.setDefaultLightWeightPopupEnabled(false);
397         popupMenu.show(component, x, y);
398         
399         // 登出
400         logoutMenuItem.addActionListener(new ActionListener() {            
401             @Override
402             public void actionPerformed(ActionEvent arg0) {
403                 SwingUtilities.invokeLater(new Runnable() {            
404                     @Override
405                     public void run() {
406                         logout();
407                     }
408                 });
409             }
410         });
411         
412         // 添加设备
413         addDeviceMenuItem.addActionListener(new ActionListener() {            
414             @Override
415             public void actionPerformed(ActionEvent arg0) {
416                 addDevice();
417             }
418         });
419         
420         // 修改设备
421         modifyDeviceMenuItem.addActionListener(new ActionListener() {            
422             @Override
423             public void actionPerformed(ActionEvent arg0) {
424                 modifyDevice();
425             }
426         });
427         
428         // 删除设备
429         deleteDeviceMenuItem.addActionListener(new ActionListener() {            
430             @Override
431             public void actionPerformed(ActionEvent arg0) {
432                 deleteDevice();
433             }
434         });
435         
436         // 清空设备
437         clearDeviceMenuItem.addActionListener(new ActionListener() {            
438             @Override
439             public void actionPerformed(ActionEvent arg0) {
440                 clearDevice();
441             }
442         });
443         
444         // 导入设备
445         importDeviceMenuItem.addActionListener(new ActionListener() {            
446             @Override
447             public void actionPerformed(ActionEvent arg0) {
448                 importDevice();
449             }
450         });
451         
452         // 导出设备
453         exportDeviceMenuItem.addActionListener(new ActionListener() {            
454             @Override
455             public void actionPerformed(ActionEvent arg0) {
456                 exportDevice();
457             }
458         });
459         
460         // 开始预览
461         realplayMenuItem.addActionListener(new ActionListener() {            
462             @Override
463             public void actionPerformed(ActionEvent arg0) {
464                 SwingUtilities.invokeLater(new Runnable() {                    
465                     @Override
466                     public void run() {
467                         startRealplay();
468                     }
469                 });
470             }
471         });
472         
473         // 停止预览
474         stopRealplayMenuItem.addActionListener(new ActionListener() {            
475             @Override
476             public void actionPerformed(ActionEvent arg0) {
477                 SwingUtilities.invokeLater(new Runnable() {            
478                     @Override
479                     public void run() {
480                         stopRealplay();
481                     }
482                 });
483             }
484         });
485         
486         // 开始对讲
487         startTalkMenuItem.addActionListener(new ActionListener() {            
488             @Override
489             public void actionPerformed(ActionEvent arg0) {
490                 SwingUtilities.invokeLater(new Runnable() {                
491                     @Override
492                     public void run() {
493                         startTalk();
494                     }
495                 });
496             }
497         });
498         
499         // 停止对讲
500         stopTalkMenuItem.addActionListener(new ActionListener() {            
501             @Override
502             public void actionPerformed(ActionEvent arg0) {
503                 SwingUtilities.invokeLater(new Runnable() {        
504                     @Override
505                     public void run() {
506                         stopTalk();
507                     }
508                 });
509             }
510         });
511         
512         // 远程抓图
513         captureMenuItem.addActionListener(new ActionListener() {            
514             @Override
515             public void actionPerformed(ActionEvent arg0) {
516                 capture();
517             }
518         });
519     }
520     
521     /**
522      * 实时预览面板
523      */
524     private class RealPlayPanel extends JPanel {
525         private static final long serialVersionUID = 1L;
526         
527         public RealPlayPanel() {
528             BorderEx.set(this, "", 0);
529             setLayout(new BorderLayout());
530             
531             JPanel realplayPanel = new JPanel();
532             JPanel picPanel = new JPanel();
533             
534             add(realplayPanel, BorderLayout.NORTH);
535             add(picPanel, BorderLayout.CENTER);
536             
537             // 实时预览面板
538             realplayPanel.setBorder(BorderFactory.createTitledBorder(Res.string().getRealplay()));
539             Dimension dimension = new Dimension();
540             dimension.height = 250;
541             realplayPanel.setPreferredSize(dimension);
542             realplayPanel.setLayout(new BorderLayout());
543             
544             realplayWindowPanel = new Panel();
545             realplayWindowPanel.setBackground(Color.GRAY);
546             realplayPanel.add(realplayWindowPanel, BorderLayout.CENTER);
547             
548             // 抓图面板
549             picPanel.setBorder(BorderFactory.createTitledBorder(Res.string().getCapturePicture()));
550             picPanel.setLayout(new BorderLayout());
551             
552             capturePanel = new PaintPanel();
553
554             picPanel.add(capturePanel, BorderLayout.CENTER);    
555         }
556     }
557     
558     /**
559      * 监听和设备管理面板
560      */
561     private class ListenDeviceManagerPanel extends JPanel {
562         private static final long serialVersionUID = 1L;
563         
564         public ListenDeviceManagerPanel() {
565             setLayout(new BorderLayout());
566             Dimension dimension = new Dimension();
567             dimension.width = 230;
568             setPreferredSize(dimension);
569             
570             JPanel listenPanel = new JPanel();
571             JPanel deviceManagerPanel = new JPanel();
572             JPanel functionOperatePanel = new JPanel();
573             
574             add(listenPanel, BorderLayout.NORTH);
575             add(deviceManagerPanel, BorderLayout.CENTER);
576             add(functionOperatePanel, BorderLayout.SOUTH);
577             
578             ///// 监听面板 /////
579             listenPanel.setBorder(BorderFactory.createTitledBorder(Res.string().getAutoRegisterListen()));
580             listenPanel.setPreferredSize(new Dimension(200, 110));
581             listenPanel.setLayout(new FlowLayout());
582             
583             JLabel ipLabel = new JLabel(Res.string().getRegisterAddress(), JLabel.CENTER);
584             JLabel portLabel = new JLabel(Res.string().getRegisterPort(), JLabel.CENTER);
585             
586             ipLabel.setPreferredSize(new Dimension(100, 21));
587             portLabel.setPreferredSize(new Dimension(100, 21));
588             
589             ipTextField = new JTextField(getHostAddress());
590             portTextField = new JTextField("9500");
591             
592             ipTextField.setPreferredSize(new Dimension(100, 21));
593             portTextField.setPreferredSize(new Dimension(100, 21));
594             
595             startListenBtn = new JButton(Res.string().getStartListen());
596             stopListenBtn = new JButton(Res.string().getStopListen());
597             
598             startListenBtn.setPreferredSize(new Dimension(105, 21));
599             stopListenBtn.setPreferredSize(new Dimension(100, 21));
600             stopListenBtn.setEnabled(false);
601                     
602             listenPanel.add(ipLabel);
603             listenPanel.add(ipTextField);
604             listenPanel.add(portLabel);
605             listenPanel.add(portTextField);
606             listenPanel.add(startListenBtn);
607             listenPanel.add(stopListenBtn);
608             
609             ///// 设备管理  ////////
610             deviceManagerPanel.setBorder(BorderFactory.createTitledBorder(Res.string().getDeviceManager()));
611             deviceManagerPanel.setLayout(new FlowLayout());
612             
613             logoutBtn = new JButton(Res.string().getLogout());
614             addDeviceBtn = new JButton(Res.string().getAddDevice());
615             modifyDeviceBtn = new JButton(Res.string().getModifyDevice());
616             deleteDeviceBtn = new JButton(Res.string().getDeleteDevice());
617             clearDeviceBtn = new JButton(Res.string().getClearDevice());
618             importDeviceBtn = new JButton(Res.string().getImportDevice());
619             exportDeviceBtn = new JButton(Res.string().getExportDevice());
620             
621             logoutBtn.setPreferredSize(new Dimension(210, 21));
622             addDeviceBtn.setPreferredSize(new Dimension(210, 21));
623             modifyDeviceBtn.setPreferredSize(new Dimension(210, 21));
624             deleteDeviceBtn.setPreferredSize(new Dimension(210, 21));
625             clearDeviceBtn.setPreferredSize(new Dimension(210, 21));
626             importDeviceBtn.setPreferredSize(new Dimension(210, 21));
627             exportDeviceBtn.setPreferredSize(new Dimension(210, 21));
628             
629             logoutBtn.setEnabled(false);
630             modifyDeviceBtn.setEnabled(false);
631             deleteDeviceBtn.setEnabled(false);
632             
633             deviceManagerPanel.add(logoutBtn);
634             deviceManagerPanel.add(addDeviceBtn);
635             deviceManagerPanel.add(modifyDeviceBtn);
636             deviceManagerPanel.add(deleteDeviceBtn);
637             deviceManagerPanel.add(clearDeviceBtn);
638             deviceManagerPanel.add(importDeviceBtn);
639             deviceManagerPanel.add(exportDeviceBtn);
640             
641             ///// 基本操作  ////////
642             functionOperatePanel.setBorder(BorderFactory.createTitledBorder(Res.string().getFunctionOperate()));
643             functionOperatePanel.setLayout(new FlowLayout());
644             Dimension dimension1 = new Dimension();
645             dimension1.height = 160;
646             functionOperatePanel.setPreferredSize(dimension1);
647             
648             startRealPlayBtn = new JButton(Res.string().getStartRealPlay());
649             stopRealPlayBtn = new JButton(Res.string().getStopRealPlay());
650             startTalkBtn = new JButton(Res.string().getStartTalk());
651             stopTalkBtn = new JButton(Res.string().getStopTalk());
652             captureBtn = new JButton(Res.string().getRemoteCapture());
653         
654             startRealPlayBtn.setPreferredSize(new Dimension(210, 21));
655             stopRealPlayBtn.setPreferredSize(new Dimension(210, 21));
656             startTalkBtn.setPreferredSize(new Dimension(210, 21));
657             stopTalkBtn.setPreferredSize(new Dimension(210, 21));
658             captureBtn.setPreferredSize(new Dimension(210, 21));
659             
660             startRealPlayBtn.setEnabled(false);
661             stopRealPlayBtn.setEnabled(false);
662             startTalkBtn.setEnabled(false);
663             stopTalkBtn.setEnabled(false);
664             captureBtn.setEnabled(false);
665             
666             functionOperatePanel.add(startRealPlayBtn);
667             functionOperatePanel.add(stopRealPlayBtn);
668             functionOperatePanel.add(startTalkBtn);
669             functionOperatePanel.add(stopTalkBtn);
670             functionOperatePanel.add(captureBtn);
671             
672             // 开启监听服务
673             startListenBtn.addActionListener(new ActionListener() {        
674                 @Override
675                 public void actionPerformed(ActionEvent arg0) {
676                     if(ipTextField.getText().equals("")) {
677                         JOptionPane.showMessageDialog(null, Res.string().getInput() + Res.string().getRegisterAddress(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
678                         return;
679                     }
680                     
681                     if(portTextField.getText().equals("")) {
682                         JOptionPane.showMessageDialog(null, Res.string().getInput() + Res.string().getRegisterPort(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
683                         return;
684                     }
685                     
686                     if(AutoRegisterModule.startServer(ipTextField.getText(), 
687                                                       Integer.parseInt(portTextField.getText()), 
688                                                       servicCallback)) {
689                         startListenBtn.setEnabled(false);
690                         stopListenBtn.setEnabled(true);
691                     }
692                 }
693             });
694             
695             // 停止监听服务
696             stopListenBtn.addActionListener(new ActionListener() {        
697                 @Override
698                 public void actionPerformed(ActionEvent arg0) {
699                     if(AutoRegisterModule.stopServer()) {
700                         startListenBtn.setEnabled(true);
701                         stopListenBtn.setEnabled(false);
702                     }
703                 }
704             });
705             
706             // 添加设备
707             addDeviceBtn.addActionListener(new ActionListener() {            
708                 @Override
709                 public void actionPerformed(ActionEvent arg0) {
710                     addDevice();        
711                 }
712             });
713             
714             // 修改设备
715             modifyDeviceBtn.addActionListener(new ActionListener() {            
716                 @Override
717                 public void actionPerformed(ActionEvent arg0) {
718                     modifyDevice();
719                 }
720             });    
721             
722             // 删除单个
723             deleteDeviceBtn.addActionListener(new ActionListener() {            
724                 @Override
725                 public void actionPerformed(ActionEvent arg0) {    
726                     deleteDevice();                
727                 }
728             });
729             
730             // 清空设备
731             clearDeviceBtn.addActionListener(new ActionListener() {                
732                 @Override
733                 public void actionPerformed(ActionEvent arg0) {
734                     clearDevice();
735                 }
736             });
737
738             // 导入文件
739             importDeviceBtn.addActionListener(new ActionListener() {
740                 public void actionPerformed(ActionEvent e) {
741                     importDevice();
742                 }
743             });
744             
745             // 导出文件
746             exportDeviceBtn.addActionListener(new ActionListener() {
747                 public void actionPerformed(ActionEvent e) {
748                     exportDevice();
749                 }
750             });
751             
752             // 登出
753             logoutBtn.addActionListener(new ActionListener() {        
754                 @Override
755                 public void actionPerformed(ActionEvent arg0) {
756                     SwingUtilities.invokeLater(new Runnable() {                    
757                         @Override
758                         public void run() {
759                             logout();    
760                         }
761                     });
762                 }
763             });
764             
765             // 开始拉流
766             startRealPlayBtn.addActionListener(new ActionListener() {            
767                 @Override
768                 public void actionPerformed(ActionEvent arg0) {
769                     SwingUtilities.invokeLater(new Runnable() {                    
770                         @Override
771                         public void run() {
772                             startRealplay();
773                         }
774                     });
775                 }
776             });
777             
778             // 停止拉流
779             stopRealPlayBtn.addActionListener(new ActionListener() {                
780                 @Override
781                 public void actionPerformed(ActionEvent arg0) {
782                     SwingUtilities.invokeLater(new Runnable() {                    
783                         @Override
784                         public void run() {
785                             stopRealplay();
786                         }
787                     });
788                 }
789             });
790             
791             // 开始对讲
792             startTalkBtn.addActionListener(new ActionListener() {        
793                 @Override
794                 public void actionPerformed(ActionEvent arg0) {
795                     SwingUtilities.invokeLater(new Runnable() {                    
796                         @Override
797                         public void run() {
798                             startTalk();
799                         }
800                     });
801                 }
802             });
803             
804             // 停止对讲
805             stopTalkBtn.addActionListener(new ActionListener() {        
806                 @Override
807                 public void actionPerformed(ActionEvent arg0) {
808                     SwingUtilities.invokeLater(new Runnable() {                    
809                         @Override
810                         public void run() {
811                             stopTalk();
812                         }
813                     });
814                 }
815             });
816             
817             // 抓图
818             captureBtn.addActionListener(new ActionListener() {        
819                 @Override
820                 public void actionPerformed(ActionEvent arg0) {
821                     capture();
822                 }
823             });
824         }
825     }
826     
827     /**
828      * 添加设备
829      */
830     private void addDevice() {
831         isExist = false;
832         
833         AddDeviceDialog addDeviceDialog = new AddDeviceDialog();
834
835         addDeviceDialog.addDeviceManagerListener(new DeviceManagerListener() {                        
836             @Override
837             public void onDeviceManager(String deviceId, String username,
838                                         String password) {                    
839                 if(!deviceId.equals("")) {
840                     // 最大支持导入100个
841                     if(rootNode.getChildCount() >= 100) {
842                         JOptionPane.showMessageDialog(null, Res.string().getMaximumSupport(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
843                         return;
844                     }
845     
846                     // 判断设备是否存在
847                     for(int i = 0; i < rootNode.getChildCount(); i++) {
848                         DeviceTreeNode childNode = (DeviceTreeNode)rootNode.getChildAt(i);
849                         
850                         if(deviceId.equals(childNode.getDeviceInfo().getDevcieId())) {
851                             isExist = true;
852                             JOptionPane.showMessageDialog(null, Res.string().getAlreadyExisted(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
853                             break;
854                         }
855                     }
856                     
857                     // 不存在,可以添加
858                     if(!isExist) {
859                         DEVICE_INFO deviceInfo = new DEVICE_INFO();
860                         deviceInfo.setDevcieId(deviceId);
861                         deviceInfo.setUsername(username);
862                         deviceInfo.setPassword(password);
863                         
864                         DeviceTreeNode childNode = new DeviceTreeNode();                    
865                         childNode.setDeviceInfo(deviceInfo);
866                         childNode.setUserObject(deviceId);
867                         
868                         rootNode.add(childNode);
869                         
870                         tree.expandPath(new TreePath(rootNode));                        
871                         tree.updateUI();
872                         tree.setCellRenderer(treeCellRender);
873                         
874                         JOptionPane.showMessageDialog(null, Res.string().getSucceed(), Res.string().getPromptMessage(), JOptionPane.INFORMATION_MESSAGE);
875                     }
876                 }                
877             }
878         });        
879         
880         addDeviceDialog.setVisible(true);
881     }
882     
883     /**
884      * 修改设备
885      */
886     private void modifyDevice() {
887         isExist = false;
888
889         // 选中的设备节点
890         final DeviceTreeNode deviceTreeNode = (DeviceTreeNode)tree.getLastSelectedPathComponent();
891         
892         if(deviceTreeNode == null) {
893             return;
894         }
895         
896         ModifyDeviceDialog modifyDeviceDialog = new ModifyDeviceDialog(deviceTreeNode.getDeviceInfo().devcieId, deviceTreeNode.getDeviceInfo().username, deviceTreeNode.getDeviceInfo().password);
897         
898         modifyDeviceDialog.addDeviceManagerListener(new DeviceManagerListener() {                        
899             @Override
900             public void onDeviceManager(String deviceId, String username, String password) {    
901                 if(deviceTreeNode.getDeviceInfo().getLoginHandle().longValue() != 0) {
902                     JOptionPane.showMessageDialog(null, Res.string().getDeviceLogined(), Res.string().getErrorMessage(), JOptionPane.INFORMATION_MESSAGE);    
903                     return;
904                 }                
905                 
906                 if(!deviceId.equals("")) {                
907                     // 设备ID变化,循环检查,查看是否已存在。    设备ID不变,即修改用户名和密码
908                     if(!deviceId.equals(deviceTreeNode.getDeviceInfo().devcieId)) {
909                         // 判断设备是否存在
910                         for(int i = 0; i < rootNode.getChildCount(); i++) {
911                             DeviceTreeNode childNode = (DeviceTreeNode)rootNode.getChildAt(i);
912                             
913                             if(deviceId.equals(childNode.getDeviceInfo().getDevcieId())) {
914                                 isExist = true;
915                                 JOptionPane.showMessageDialog(null, Res.string().getAlreadyExisted(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
916                                 break;
917                             }
918                         }
919                     }
920                     
921                     if(!isExist) {
922                         // 修改信息
923                         deviceTreeNode.getDeviceInfo().setDevcieId(deviceId);                            
924                         deviceTreeNode.getDeviceInfo().setUsername(username);
925                         deviceTreeNode.getDeviceInfo().setPassword(password);            
926
927                         deviceTreeNode.setUserObject(deviceId);                
928                         deviceTreeNode.removeAllChildren();  
929                         
930                         tree.updateUI();    
931             
932                         JOptionPane.showMessageDialog(null, Res.string().getSucceed(), Res.string().getPromptMessage(), JOptionPane.INFORMATION_MESSAGE);    
933                     }
934                 }
935             }
936         });
937         
938         modifyDeviceDialog.setVisible(true);
939     }
940     
941     /**
942      * 删除单个设备
943      */
944     private void deleteDevice() {
945         // 判断是否选中
946         DeviceTreeNode deviceTreeNode = (DeviceTreeNode)tree.getLastSelectedPathComponent();
947
948         if(deviceTreeNode == null) {
949             return;
950         }
951         
952         // 停止当前登录设备的对讲
953         if(deviceTreeNode == talkDeviceTreeNode) {
954             AutoRegisterModule.stopTalk(AutoRegisterModule.m_hTalkHandle);                    
955
956             talkDeviceTreeNode = null;
957         }
958         
959         // 停止当前登录设备的拉流
960         for(int i = 0; i < deviceTreeNode.getChildCount(); i++) {
961             ChannelTreeNode channelTreeNode = (ChannelTreeNode)deviceTreeNode.getChildAt(i);
962             if(channelTreeNode == realplayChannelTreeNode) {
963                 AutoRegisterModule.stopRealPlay(realplayHandle);
964                 realplayWindowPanel.repaint();
965
966                 realplayChannelTreeNode = null;
967                 
968                 break;
969             }    
970         }    
971         
972         // 登出
973         if(deviceTreeNode.getDeviceInfo().getLoginHandle().longValue() != 0) {
974             AutoRegisterModule.logout(deviceTreeNode.getDeviceInfo().getLoginHandle());
975
976             logoutBtn.setEnabled(false);
977             modifyDeviceBtn.setEnabled(true);
978         }
979                 
980         deviceTreeNode.setUserObject(deviceTreeNode.getDeviceInfo().getDevcieId());                
981         
982         treeModel.removeNodeFromParent(deviceTreeNode);
983         tree.updateUI();
984         
985         modifyDeviceBtn.setEnabled(false);
986         deleteDeviceBtn.setEnabled(false);
987     }
988     
989     /**
990      * 清空设备
991      */
992     private void clearDevice() {
993         // 停止拉流
994         if(realplayHandle.longValue() != 0) {
995             AutoRegisterModule.stopRealPlay(realplayHandle);
996             realplayWindowPanel.repaint();
997             
998             realplayChannelTreeNode = null;
999         }
1000         
1001         // 停止对讲
1002         if(AutoRegisterModule.m_hTalkHandle.longValue() != 0) {
1003             AutoRegisterModule.stopTalk(AutoRegisterModule.m_hTalkHandle);    
1004
1005             talkDeviceTreeNode = null;                    
1006         }    
1007         
1008         // 登出所有设备
1009         for(int i = 0; i < rootNode.getChildCount(); i++) {
1010             DeviceTreeNode deviceTreeNode = (DeviceTreeNode)rootNode.getChildAt(i);
1011             if(deviceTreeNode.getDeviceInfo().getLoginHandle().longValue() != 0) {
1012                 AutoRegisterModule.logout(deviceTreeNode.getDeviceInfo().getLoginHandle());
1013             }
1014         }
1015         
1016         capturePanel.setOpaque(true);
1017         capturePanel.repaint();    
1018
1019         rootNode.removeAllChildren();
1020         tree.updateUI();    
1021         
1022         logoutBtn.setEnabled(false);
1023         modifyDeviceBtn.setEnabled(false);
1024         deleteDeviceBtn.setEnabled(false);
1025         startRealPlayBtn.setEnabled(false);
1026         stopRealPlayBtn.setEnabled(false);
1027         startTalkBtn.setEnabled(false);
1028         stopTalkBtn.setEnabled(false);
1029         captureBtn.setEnabled(false);
1030     }
1031     
1032     /**
1033      * 导入文件
1034      */
1035     private void importDevice() {
1036         jfc = new JFileChooser("./");
1037         jfc.setMultiSelectionEnabled(false);   //不可以拖选多个文件
1038         jfc.setAcceptAllFileFilterUsed(false); //关掉显示所有
1039         jfc.setDialogTitle(Res.string().getImportDevice());
1040     
1041         //添加过滤器
1042         jfc.addChoosableFileFilter(new FileFilter() {
1043             public boolean accept(File f) {
1044                 if(f.getName().toLowerCase().endsWith("csv")
1045                         || f.isDirectory()) {
1046                     return true;
1047                 }
1048                 return false;
1049             }
1050             public String getDescription() {
1051                 return ".csv";
1052             }
1053         });
1054         
1055         if(jfc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {    
1056             String path = jfc.getSelectedFile().getAbsolutePath();
1057             
1058             if(!path.endsWith("csv")) {
1059                 path += ".csv";
1060             }
1061             importFile(path, "GB2312");    
1062         } 
1063     }
1064     
1065     /**
1066      * 导出文件
1067      */
1068     private void exportDevice() {
1069         jfc = new JFileChooser("./");
1070         jfc.setMultiSelectionEnabled(false);   //不可以拖选多个文件
1071         jfc.setAcceptAllFileFilterUsed(false); //关掉显示所有
1072         jfc.setDialogTitle(Res.string().getExportDevice());
1073
1074         //添加过滤器
1075         jfc.addChoosableFileFilter(new FileFilter() {
1076             public boolean accept(File f) {
1077                 if(f.getName().toLowerCase().endsWith("csv")
1078                         || f.isDirectory()) {
1079                     return true;
1080                 }
1081                 return false;
1082             }
1083             public String getDescription() {
1084                 return ".csv";
1085             }
1086         });
1087                                 
1088         if(jfc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
1089
1090             String path = jfc.getSelectedFile().toString();
1091             if(!path.endsWith("csv")) {
1092                 path += ".csv";
1093             }
1094             System.out.println(path);
1095             File file = new File(path);
1096             
1097             if(file.exists()) {  // 已存在文件
1098                 int result = JOptionPane.showConfirmDialog(null, Res.string().getWhetherNoToCover(), Res.string().getPromptMessage(), JOptionPane.YES_NO_OPTION);
1099                 if(result == JOptionPane.YES_OPTION) {  // 覆盖
1100                     if(!file.renameTo(file)) {  // 被使用
1101                         JOptionPane.showMessageDialog(null, Res.string().getFileOpened(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
1102                     } else {   // 未被使用
1103                         exportFile(file);    
1104                     }            
1105                 } 
1106             } else {  // 新建文件                            
1107                 exportFile(file);        
1108             }
1109         }             
1110     }
1111     
1112     /**
1113      * 登出设备功能
1114      */
1115     private void logout() {
1116         // 选中的设备节点
1117         DeviceTreeNode deviceTreeNode = (DeviceTreeNode)tree.getLastSelectedPathComponent();
1118         
1119         if(deviceTreeNode == null) {
1120             return;
1121         }
1122         
1123         // 停止当前登录设备的对讲
1124         if(deviceTreeNode == talkDeviceTreeNode) {
1125             AutoRegisterModule.stopTalk(AutoRegisterModule.m_hTalkHandle);                    
1126
1127             talkDeviceTreeNode = null;
1128         }
1129         
1130         // 停止当前登录设备的拉流
1131         for(int i = 0; i < deviceTreeNode.getChildCount(); i++) {
1132             ChannelTreeNode channelTreeNode = (ChannelTreeNode)deviceTreeNode.getChildAt(i);
1133             if(channelTreeNode == realplayChannelTreeNode) {
1134                 AutoRegisterModule.stopRealPlay(realplayHandle);
1135                 realplayWindowPanel.repaint();
1136
1137                 realplayChannelTreeNode = null;
1138                 
1139                 break;
1140             }    
1141         }    
1142         
1143         // 登出
1144         if(deviceTreeNode.getDeviceInfo().getLoginHandle().longValue() != 0) {
1145             AutoRegisterModule.logout(deviceTreeNode.getDeviceInfo().getLoginHandle());
1146
1147             logoutBtn.setEnabled(false);
1148             modifyDeviceBtn.setEnabled(true);
1149         }
1150                 
1151         deviceTreeNode.setUserObject(deviceTreeNode.getDeviceInfo().getDevcieId());                
1152         deviceTreeNode.removeAllChildren();  
1153         
1154         tree.updateUI();
1155     }
1156     
1157     /**
1158      * 开始拉流功能
1159      */
1160     private void startRealplay() {
1161         // 选中的通道节点
1162         ChannelTreeNode channelTreeNode = (ChannelTreeNode)tree.getLastSelectedPathComponent();
1163         
1164         if(channelTreeNode == null) {
1165             return;
1166         }
1167         
1168         // 通道节点对应的设备节点
1169         DeviceTreeNode deviceTreeNode = (DeviceTreeNode)channelTreeNode.getParent();
1170         
1171         // 判断所有设备有没有拉流
1172         if(realplayHandle.longValue() != 0) {
1173             AutoRegisterModule.stopRealPlay(realplayHandle);                    
1174             realplayWindowPanel.repaint();
1175
1176             realplayChannelTreeNode = null;
1177         }    
1178
1179         // 选中通道拉流
1180         realplayHandle = AutoRegisterModule.startRealPlay(deviceTreeNode.getDeviceInfo().getLoginHandle(), channelTreeNode.getChn() - 1, 0, realplayWindowPanel);
1181     
1182         if(realplayHandle.longValue() != 0) {
1183             startRealPlayBtn.setEnabled(false);
1184             stopRealPlayBtn.setEnabled(true);
1185             
1186             realplayChannelTreeNode = channelTreeNode;                    
1187         } else {
1188             JOptionPane.showMessageDialog(null, Res.string().getRealplay() + Res.string().getFailed() + ", " + ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
1189         }
1190         
1191         tree.updateUI();
1192     }
1193     
1194     /**
1195      * 停止拉流功能
1196      */
1197     private void stopRealplay() {
1198         if(realplayHandle.longValue() != 0) {
1199             AutoRegisterModule.stopRealPlay(realplayHandle);
1200             realplayWindowPanel.repaint();
1201             
1202             realplayChannelTreeNode = null;
1203
1204             startRealPlayBtn.setEnabled(true);
1205             stopRealPlayBtn.setEnabled(false);
1206         }
1207         tree.updateUI();
1208     }
1209     
1210     /**
1211      * 开始对讲
1212      */
1213     private void startTalk() {
1214         // 选中的设备节点
1215         DeviceTreeNode deviceTreeNode = (DeviceTreeNode)tree.getLastSelectedPathComponent();
1216         
1217         if(deviceTreeNode == null) {
1218             return;
1219         }
1220         
1221         // 判断所有设备有没有对讲
1222         if(AutoRegisterModule.m_hTalkHandle.longValue() != 0) {
1223             AutoRegisterModule.stopTalk(AutoRegisterModule.m_hTalkHandle);                    
1224             
1225             talkDeviceTreeNode = null;
1226         }    
1227
1228         // 选中设备对讲
1229         if(AutoRegisterModule.startTalk(deviceTreeNode.getDeviceInfo().getLoginHandle())) {        
1230             startTalkBtn.setEnabled(false);
1231             stopTalkBtn.setEnabled(true);
1232             
1233             talkDeviceTreeNode = deviceTreeNode;                
1234         } else {
1235             JOptionPane.showMessageDialog(null, Res.string().getTalk() + Res.string().getFailed() + ", " +  ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
1236         }
1237         
1238         tree.updateUI();
1239     }
1240     
1241     /**
1242      * 停止对讲
1243      */
1244     private void stopTalk() {
1245         // 判断设备的通道有没有对讲
1246         if(AutoRegisterModule.m_hTalkHandle.longValue() != 0) {
1247             AutoRegisterModule.stopTalk(AutoRegisterModule.m_hTalkHandle);                    
1248
1249             talkDeviceTreeNode = null;
1250
1251             startTalkBtn.setEnabled(true);
1252             stopTalkBtn.setEnabled(false);
1253         }    
1254
1255         tree.updateUI();
1256     }
1257     
1258     /**
1259      * 抓图功能
1260      */
1261     private void capture() {
1262         // 选中的通道节点
1263         ChannelTreeNode channelTreeNode = (ChannelTreeNode)tree.getLastSelectedPathComponent();
1264         
1265         if(channelTreeNode == null) {
1266             return;
1267         }
1268         
1269         // 通道节点对应的设备节点
1270         DeviceTreeNode deviceTreeNode = (DeviceTreeNode)channelTreeNode.getParent();
1271         
1272         AutoRegisterModule.snapPicture(deviceTreeNode.getDeviceInfo().getLoginHandle(), channelTreeNode.getChn() - 1);
1273     }
1274     
1275     /**
1276      * 侦听服务器回调函数
1277      */
1278     public class ServiceCB implements fServiceCallBack {
1279         @Override
1280         public int invoke(LLong lHandle, final String pIp, final int wPort,
1281                 int lCommand, Pointer pParam, int dwParamLen,
1282                 Pointer dwUserData) {
1283
1284             // 将 pParam 转化为序列号
1285             byte[] buffer = new byte[dwParamLen];
1286             pParam.read(0, buffer, 0, dwParamLen);
1287             String deviceId = "";
1288             try {
1289                 deviceId = new String(buffer, "GBK").trim();
1290             } catch (UnsupportedEncodingException e) {
1291                 e.printStackTrace();
1292             }
1293             
1294             System.out.printf("Register Device Info [Device address %s][port %s][DeviceID %s] \n", pIp, wPort, deviceId);
1295             switch(lCommand) {
1296                 case EM_LISTEN_TYPE.NET_DVR_DISCONNECT: {  // 验证期间设备断线回调
1297                     for(int i = 0; i < rootNode.getChildCount(); i++) {
1298                         if(deviceId.equals(((DeviceTreeNode)rootNode.getChildAt(i)).getDeviceInfo().getDevcieId())) {                            
1299                             DeviceTreeNode deviceTreeNode = (DeviceTreeNode)rootNode.getChildAt(i);                
1300                             
1301                             deviceTreeNode.getDeviceInfo().setDeviceIp("");
1302                             deviceTreeNode.getDeviceInfo().setDevicePort(0);
1303                             
1304                             break;
1305                         }
1306                     }    
1307                     
1308                     break;
1309                 }            
1310                 case EM_LISTEN_TYPE.NET_DVR_SERIAL_RETURN: { // 设备注册携带序列号
1311                     for(int i = 0; i < rootNode.getChildCount(); i++) {
1312                         if(deviceId.equals(((DeviceTreeNode)rootNode.getChildAt(i)).getDeviceInfo().getDevcieId())) {                            
1313                             final DeviceTreeNode deviceTreeNode = (DeviceTreeNode)rootNode.getChildAt(i);                
1314                             
1315                             deviceTreeNode.getDeviceInfo().setDeviceIp(pIp);
1316                             deviceTreeNode.getDeviceInfo().setDevicePort(wPort);
1317                             
1318                             try {
1319                                 Thread.sleep(20);
1320                             } catch (InterruptedException e1) {
1321                                 e1.printStackTrace();
1322                             }
1323                             
1324                             new SwingWorker<LLong, String>() {                    
1325                                 @Override
1326                                 protected LLong doInBackground() {
1327                                     return login(deviceTreeNode);                        
1328                                 }
1329                                 
1330                                 @Override
1331                                 protected void done() {
1332                                     try {
1333                                         if(get() == null) {
1334                                             return;
1335                                         }
1336                                         
1337                                         if(get().longValue() != 0) {
1338 //                                            deviceTreeNode.getDeviceInfo().setDeviceIp(pIp);
1339 //                                            deviceTreeNode.getDeviceInfo().setDevicePort(wPort);
1340                                             deviceTreeNode.getDeviceInfo().setLoginHandle(get());
1341                                             
1342                                             for(int i = 0; i < AutoRegisterModule.m_stDeviceInfo.byChanNum; i++) {
1343                                                 ChannelTreeNode chnNode = new ChannelTreeNode();
1344                                                 chnNode.setChn(i + 1);
1345                                                 chnNode.setUserObject(Res.string().getChannel() + " " + String.valueOf(i + 1));
1346                                                 
1347                                                 deviceTreeNode.add(chnNode);
1348                                             }
1349                                             
1350                                             deviceTreeNode.setUserObject(deviceTreeNode.getDeviceInfo().getDevcieId() + " (" + deviceTreeNode.getDeviceInfo().getDeviceIp() + ")");    
1351                                             tree.expandPath(new TreePath(rootNode));
1352                                             tree.setCellRenderer(treeCellRender);    
1353                                             tree.updateUI();
1354                                         }        
1355                                     } catch (InterruptedException e) {
1356                                         e.printStackTrace();
1357                                     } catch (ExecutionException e) {
1358                                         e.printStackTrace();
1359                                     }                    
1360                                 }
1361                             }.execute();
1362                             
1363                             break;
1364                         }
1365                     }    
1366                     
1367                     break;
1368                 }
1369                 default:
1370                     break;
1371             
1372             }            
1373
1374             return 0;
1375         }
1376     }
1377     
1378     /**
1379      * 登录设备
1380      * @param deviceIp 登录设备IP
1381      * @param port 登录设备端口号
1382      * @param deviceTreeNode 登录设备设备节点
1383      */
1384     private LLong login(DeviceTreeNode deviceTreeNode) {                
1385         // 判断设备是否登录
1386         if(deviceTreeNode.getDeviceInfo().getLoginHandle().longValue() != 0) {
1387             return null;
1388         }
1389         
1390         LLong loginHandleLong = AutoRegisterModule.login(deviceTreeNode.getDeviceInfo().getDeviceIp(), 
1391                                                          deviceTreeNode.getDeviceInfo().getDevicePort(), 
1392                                                          deviceTreeNode.getDeviceInfo().getUsername(), 
1393                                                          deviceTreeNode.getDeviceInfo().getPassword(), 
1394                                                          deviceTreeNode.getDeviceInfo().getDevcieId());
1395         if(loginHandleLong.longValue() != 0) {
1396             System.out.printf("Login Success [Device IP %s][port %d][DeviceID %s]\n", deviceTreeNode.getDeviceInfo().getDeviceIp(), 
1397                     deviceTreeNode.getDeviceInfo().getDevicePort(), deviceTreeNode.getDeviceInfo().getDevcieId());
1398         }  else {
1399             System.err.printf("Login Failed[Device IP %s] [Port %d][DeviceID %s] %s", deviceTreeNode.getDeviceInfo().getDeviceIp(), 
1400                     deviceTreeNode.getDeviceInfo().getDevicePort(), deviceTreeNode.getDeviceInfo().getDevcieId(), ToolKits.getErrorCodePrint());
1401         }
1402         
1403         return loginHandleLong;
1404     } 
1405
1406     /**
1407      * 设备节点
1408      */
1409     private class DeviceTreeNode extends DefaultMutableTreeNode {
1410         private static final long serialVersionUID = 1L;
1411         
1412         private DEVICE_INFO deviceInfo; // 信息
1413         
1414         public DeviceTreeNode() {
1415             
1416         }
1417         
1418         @Override
1419         public Object getUserObject() {
1420             return super.getUserObject();
1421         }
1422
1423         @Override
1424         public void setUserObject(Object arg0) {
1425             super.setUserObject(arg0);
1426         }
1427
1428         public DEVICE_INFO getDeviceInfo() {
1429             return deviceInfo;
1430         }
1431         
1432         public void setDeviceInfo(DEVICE_INFO deviceInfo) {
1433             this.deviceInfo = deviceInfo;
1434         }
1435     }
1436     
1437     /**
1438      * 通道节点
1439      */
1440     private class ChannelTreeNode extends DefaultMutableTreeNode {
1441         private static final long serialVersionUID = 1L;
1442
1443         private int nChn = 0;
1444         
1445         public ChannelTreeNode() {
1446             
1447         }
1448         
1449         public int getChn() {
1450             return nChn;
1451         }
1452         public void setChn(int nChn) {
1453             this.nChn = nChn;
1454         }
1455     }
1456     
1457     /**
1458      * 设备信息
1459      */
1460     private class DEVICE_INFO {
1461         private String devcieId = "";
1462         private String username = "";
1463         private String password = "";
1464         private String deviceIp = "";
1465         private int port = 0;
1466         private LLong loginHandle = new LLong(0);
1467         
1468         public String getDevcieId() {
1469             return devcieId;
1470         }
1471         public void setDevcieId(String devcieId) {
1472             this.devcieId = devcieId;
1473         }
1474         
1475         public String getDeviceIp() {
1476             return deviceIp;
1477         }
1478         public void setDeviceIp(String deviceIp) {
1479             this.deviceIp = deviceIp;
1480         }
1481         
1482         public int getDevicePort() {
1483             return port;
1484         }
1485         public void setDevicePort(int port) {
1486             this.port = port;
1487         }
1488         
1489         public String getUsername() {
1490             return username;
1491         }
1492         public void setUsername(String username) {
1493             this.username = username;
1494         }
1495         
1496         public String getPassword() {
1497             return password;
1498         }
1499         public void setPassword(String password) {
1500             this.password = password;
1501         }
1502         
1503         public LLong getLoginHandle() {
1504             return loginHandle;
1505         }
1506         public void setLoginHandle(LLong loginHandle) {
1507             this.loginHandle = loginHandle;
1508         }    
1509     }
1510
1511     /**
1512      * 自定义树的渲染器
1513      */
1514     private class TreeCellRender extends DefaultTreeCellRenderer {
1515         private static final long serialVersionUID = 1L;
1516
1517         public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, 
1518                 boolean expanded, boolean leaf, int row, boolean hasFocus) {
1519             
1520             super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
1521         
1522             if(value instanceof DeviceTreeNode) {
1523                 setIcon(new ImageIcon("./libs/device.png"));
1524                 
1525                 DeviceTreeNode deviceTreeNode = (DeviceTreeNode)value;
1526         
1527                 // 设备节点的字体颜色
1528                 if(deviceTreeNode.getDeviceInfo().getLoginHandle().longValue() != 0) {                    
1529                     if(deviceTreeNode == talkDeviceTreeNode) { 
1530                         setForeground(Color.RED);
1531                     } else {
1532                         setForeground(Color.GREEN);
1533                     }
1534                 } else {
1535                     setForeground(Color.BLACK);
1536                 }    
1537                 
1538                 if(selected) {    
1539                     // 如果设备节点被选中,拉流按钮使能
1540                     startRealPlayBtn.setEnabled(false);
1541                     stopRealPlayBtn.setEnabled(false);
1542                     captureBtn.setEnabled(false);
1543                     deleteDeviceBtn.setEnabled(true);
1544                     
1545                     // 设备节点被选中,登录时的使能
1546                     if(deviceTreeNode.getDeviceInfo().getLoginHandle().longValue() != 0) {
1547                         logoutBtn.setEnabled(true);
1548                         modifyDeviceBtn.setEnabled(false);
1549                                     
1550                         if(deviceTreeNode == talkDeviceTreeNode) { 
1551                             startTalkBtn.setEnabled(false);
1552                             stopTalkBtn.setEnabled(true);                        
1553                         } else {
1554                             startTalkBtn.setEnabled(true);
1555                             stopTalkBtn.setEnabled(false);
1556                         }
1557                     } else {    // 登出时的使能                    
1558                         logoutBtn.setEnabled(false);
1559                         modifyDeviceBtn.setEnabled(true);
1560                         startTalkBtn.setEnabled(false);
1561                         stopTalkBtn.setEnabled(false);
1562                     }    
1563                 } 
1564             } else if(value instanceof ChannelTreeNode) {
1565                 setIcon(new ImageIcon("./libs/camera.png"));
1566                 
1567                 ChannelTreeNode channelTreeNode = (ChannelTreeNode)value;
1568                 
1569                 // 设置通道节点的字体颜色    
1570                 if(channelTreeNode == realplayChannelTreeNode) {
1571                     setForeground(Color.RED);
1572                 } else {
1573                     setForeground(Color.BLACK);                    
1574                 }
1575
1576                 if(selected) {        
1577                     // 如果通道节点被选中,登出置灰
1578                     logoutBtn.setEnabled(false);
1579                     captureBtn.setEnabled(true);
1580                     modifyDeviceBtn.setEnabled(false);
1581                     deleteDeviceBtn.setEnabled(false);
1582                     startTalkBtn.setEnabled(false);
1583                     stopTalkBtn.setEnabled(false);    
1584                     
1585                     if(channelTreeNode == realplayChannelTreeNode) {
1586                         startRealPlayBtn.setEnabled(false);
1587                         stopRealPlayBtn.setEnabled(true);
1588                     } else {
1589                         startRealPlayBtn.setEnabled(true);
1590                         stopRealPlayBtn.setEnabled(false);                    
1591                     }
1592                 } 
1593             }
1594
1595             return this;
1596         }
1597     }
1598     
1599     /**
1600      * 导入csv文件
1601      */
1602     private void importFile(String filePath, String encoding) {        
1603         File file = new File(filePath);
1604
1605         if(!file.exists()) {
1606             JOptionPane.showMessageDialog(null, Res.string().getFileNotExist(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
1607             return;
1608         }
1609         
1610           FileInputStream input = null;
1611         InputStreamReader reader = null;
1612         BufferedReader bReader = null;
1613         
1614         try {
1615             input = new FileInputStream(file);
1616
1617             if(encoding == null) {
1618                 reader = new InputStreamReader(input);
1619             } else {
1620                 reader = new InputStreamReader(input, encoding);
1621             }
1622             bReader = new BufferedReader(reader);
1623             
1624             // 头标题
1625             String line = bReader.readLine();
1626             String everyLine = "";               
1627             
1628             while((line = bReader.readLine()) != null) {
1629                 everyLine = line;
1630                 
1631                 // 最大支持导入100个
1632                 if(rootNode.getChildCount() >= 100) {
1633                     return;
1634                 }
1635                 
1636                 String[] infos = everyLine.split(",");
1637
1638                 isExist = false;
1639                 if(!infos[0].equals("")) {                
1640                     // 判断是否存在
1641                     for(int i = 0; i < rootNode.getChildCount(); i++) {                       
1642                         DeviceTreeNode childNode = (DeviceTreeNode)rootNode.getChildAt(i);                    
1643                         if(infos[0].equals(childNode.getDeviceInfo().getDevcieId())) {
1644                             isExist = true;                                        
1645                             break;
1646                         }
1647                     }
1648                     
1649                     // 不存在, 可以添加
1650                     if(!isExist) {
1651                            final DEVICE_INFO deviceInfo = new DEVICE_INFO();
1652                         deviceInfo.setDevcieId(infos[0]);
1653                         if(infos.length == 1) {
1654                             deviceInfo.setUsername("");
1655                             deviceInfo.setPassword("");
1656                         } else if(infos.length == 2) {
1657                             deviceInfo.setUsername(infos[1]);
1658                             deviceInfo.setPassword("");
1659                         } else if(infos.length == 3){
1660                             deviceInfo.setUsername(infos[1]);
1661                             deviceInfo.setPassword(infos[2]);
1662                         }                   
1663                         
1664                         DeviceTreeNode childNode = new DeviceTreeNode();                    
1665                         childNode.setDeviceInfo(deviceInfo);
1666                         childNode.setUserObject(infos[0]);
1667                         
1668                         rootNode.add(childNode);
1669                         
1670                         tree.expandPath(new TreePath(rootNode));
1671                         tree.updateUI();
1672                     }
1673                 }                
1674             } 
1675         } catch (FileNotFoundException e) {
1676             e.printStackTrace();
1677         } catch (Exception e) {
1678             e.printStackTrace();
1679         } finally {
1680             if(null != bReader) {
1681                 try {
1682                     bReader.close();
1683                 } catch (IOException e) {
1684                     e.printStackTrace();
1685                 }
1686             }
1687             tree.setCellRenderer(treeCellRender);
1688             JOptionPane.showMessageDialog(null, Res.string().getImportCompletion(), Res.string().getPromptMessage(), JOptionPane.INFORMATION_MESSAGE);
1689         }
1690     }
1691
1692     /**
1693      * 导出csv文件
1694      */
1695     private void exportFile(File file) {
1696         FileOutputStream output = null;
1697         OutputStreamWriter osWriter = null;
1698         BufferedWriter bWriter = null;
1699         
1700         try {        
1701             output = new FileOutputStream(file);
1702             osWriter = new OutputStreamWriter(output, "GB2312");    
1703             bWriter = new BufferedWriter(osWriter);
1704             
1705             // 写头标题
1706             bWriter.write("DeviceId" + "," + "UserName" + "," + "Password");
1707             bWriter.newLine();
1708             
1709             // 写内容
1710             for(int i = 0; i < rootNode.getChildCount(); i++) {                       
1711                 DeviceTreeNode childNode = (DeviceTreeNode)rootNode.getChildAt(i);    
1712                 String deviceId = childNode.getDeviceInfo().getDevcieId();
1713                 String username = childNode.getDeviceInfo().getUsername();
1714                 String password = childNode.getDeviceInfo().getPassword();
1715                 
1716                 bWriter.write(deviceId + "," + username + "," + password);
1717                 bWriter.newLine();
1718             }                
1719         } catch (FileNotFoundException e) {
1720             e.printStackTrace();
1721         } catch (UnsupportedEncodingException e) {
1722             e.printStackTrace();
1723         } catch (Exception e) {
1724             e.printStackTrace();        
1725         } finally {
1726             if(null != bWriter) {
1727                 try {
1728                     bWriter.close();
1729                 } catch (IOException e) {
1730                     e.printStackTrace();
1731                 }
1732             }
1733             JOptionPane.showMessageDialog(null, Res.string().getExportCompletion(), Res.string().getPromptMessage(), JOptionPane.INFORMATION_MESSAGE);
1734         }
1735     }
1736     
1737     /**
1738      * 抓图回调函数
1739      */
1740     public class CaptureReceiveCB implements NetSDKLib.fSnapRev{
1741         BufferedImage bufferedImage = null;
1742         public void invoke( LLong lLoginID, Pointer pBuf, int RevLen, int EncodeType, int CmdSerial, Pointer dwUser) {    
1743             if(pBuf != null && RevLen > 0) {                    
1744                 String strFileName = SavePath.getSavePath().getSaveCapturePath(); 
1745
1746                 System.out.println("strFileName = " + strFileName);
1747
1748                 byte[] buf = pBuf.getByteArray(0, RevLen);
1749                 ByteArrayInputStream byteArrInput = new ByteArrayInputStream(buf);
1750                 try {
1751                     bufferedImage = ImageIO.read(byteArrInput);
1752                     if(bufferedImage == null) {
1753                         return;
1754                     }
1755                     ImageIO.write(bufferedImage, "jpg", new File(strFileName));    
1756                 } catch (IOException e) {
1757                     e.printStackTrace();
1758                 }    
1759                 
1760                 // 界面显示抓图     
1761                 SwingUtilities.invokeLater(new Runnable() {    
1762                     @Override
1763                     public void run() {            
1764                         capturePanel.setOpaque(false);
1765                         capturePanel.setImage(bufferedImage);
1766                         capturePanel.repaint();                
1767                     }
1768                 });
1769             }
1770         }
1771     }
1772     
1773     /**
1774      * 获取本地地址
1775      * @return
1776      */
1777     private String getHostAddress() {
1778         String address = "";
1779         try {
1780             InetAddress inetAddress = InetAddress.getLocalHost();
1781             address = inetAddress.getHostAddress();
1782         } catch (UnknownHostException e) {
1783             e.printStackTrace();
1784         }
1785         
1786         return address;
1787     }
1788     
1789     private DefaultMutableTreeNode rootNode;      
1790     private DefaultTreeModel treeModel;
1791     private JTree tree;
1792     
1793     private Panel realplayWindowPanel;
1794     private PaintPanel capturePanel;
1795     
1796     private JTextField ipTextField;
1797     private JTextField portTextField;
1798     private JButton startListenBtn;
1799     private JButton stopListenBtn;    
1800     private JButton addDeviceBtn;
1801     private JButton modifyDeviceBtn;
1802     private JButton deleteDeviceBtn;
1803     private JButton clearDeviceBtn;
1804     private JButton importDeviceBtn;
1805     private JButton exportDeviceBtn;
1806     private JButton logoutBtn;
1807
1808     private JButton startRealPlayBtn;
1809     private JButton stopRealPlayBtn;
1810     private JButton startTalkBtn;
1811     private JButton stopTalkBtn;
1812     private JButton captureBtn;
1813     
1814     private JFileChooser jfc;
1815 }
1816
1817 public class AutoRegister {  
1818     public static void main(String[] args) {    
1819         SwingUtilities.invokeLater(new Runnable() {
1820             public void run() {
1821                 AutoRegisterFrame demo = new AutoRegisterFrame();
1822                 demo.setVisible(true);
1823             }
1824         });        
1825     }
1826 };