Jay
2024-11-08 02722a3f9eca857ce7fffea352e9f7ee692a1b71
提交 | 用户 | 时间
149dd0 1 package com.iailab.netsdk.demo.frame.scada;
H 2
3 import com.iailab.netsdk.common.FunctionList;
4 import com.iailab.netsdk.common.Res;
5 import com.iailab.netsdk.demo.frame.vto.DefaultDisConnect;
6 import com.iailab.netsdk.demo.frame.vto.DefaultHaveReconnect;
7 import com.iailab.netsdk.demo.frame.vto.OperateManager;
8 import com.iailab.netsdk.demo.module.LoginModule;
9 import com.iailab.netsdk.lib.NetSDKLib;
10 import com.iailab.netsdk.lib.ToolKits;
11 import com.iailab.netsdk.lib.Utils;
12 import com.iailab.netsdk.lib.structure.NET_ATTRIBUTE_INFO;
13 import com.iailab.netsdk.lib.structure.NET_GET_CONDITION_INFO;
14 import com.iailab.netsdk.lib.structure.NET_IN_SCADA_GET_ATTRIBUTE_INFO;
15 import com.iailab.netsdk.lib.structure.NET_OUT_SCADA_GET_ATTRIBUTE_INFO;
16 import com.sun.jna.Memory;
17 import com.sun.jna.NativeLong;
18 import com.sun.jna.Pointer;
19 import com.sun.jna.ptr.IntByReference;
20
21 import javax.swing.*;
22 import javax.swing.border.EmptyBorder;
23 import javax.swing.border.TitledBorder;
24 import javax.swing.table.DefaultTableCellRenderer;
25 import javax.swing.table.DefaultTableModel;
26 import java.awt.*;
27 import java.awt.event.*;
28 import java.io.UnsupportedEncodingException;
29 import java.nio.charset.Charset;
30 import java.util.HashMap;
31 import java.util.Map;
32 import java.util.Vector;
33
34 import static com.iailab.netsdk.demo.module.LoginModule.m_hLoginHandle;
35
36 public class SCADADemo extends JFrame {
37
38     private static final long serialVersionUID = 1L;
39
40     private JPanel contentPane;
41     private JTextField ipTextField;
42     private JTextField portTextField;
43     private JTextField userNameTextField;
44     private JPasswordField passwordField;
45
46     private JButton btnLogin; // 登录
47     private JButton btnLogout; // 登出
48     private JButton btnGetDeviceList; // 获取设备列表
49     private JButton btnAlarmAttachInfo; // 遥信,订阅报警
50     private JButton btnAttachInfo; // 遥测,订阅信息
51     private JButton btnAlarmAttachInfoStop; // 遥信,取消订阅报警
52     private JButton btnAttachInfoStop; // 遥测,取消订阅信息
53
54     private JButton btnStartListen; // 停止监听
55     private JButton btnStopListen; // 停止监听
56
57
58     private JTable devicesTable; // 设备列表
59     private JTable pointListTable; // 设备点位列表
60     private JTable alarmAttachInfoTable;
61     private JScrollPane deviceScrollPane;
62
63
64     private JButton btnGetSCADAAttribute; // 获取点位信息
65     private boolean isListen = false;
66
67
68     ///////////////////// 主面板 /////////////////////
69
70     private static JFrame mainFrame = new JFrame();
71     private OperateManager manager = new OperateManager();
72
73     /**
74      * Launch the application.
75      */
76     public static void main(String[] args) {
77         EventQueue.invokeLater(new Runnable() {
78             public void run() {
79                 try {
80                     SCADADemo frame = new SCADADemo();
81                     frame.setVisible(true);
82                 } catch (Exception e) {
83                     e.printStackTrace();
84                 }
85             }
86         });
87     }
88
89     /**
90      * enable button
91      *
92      * @param enable
93      */
94     public void setButtonEnable(boolean enable) {
95         btnLogin.setEnabled(enable);
96         btnLogout.setEnabled(enable);
97         btnGetDeviceList.setEnabled(enable);
98         btnAlarmAttachInfo.setEnabled(enable);
99         btnAttachInfo.setEnabled(enable);
100         btnGetSCADAAttribute.setEnabled(enable);
101         btnStartListen.setEnabled(enable);
102
103     }
104
105
106     private boolean isAlarmAttachInfo = false;
107     private boolean isAttachInfo = false;
108
109     /**
110      * 登录
111      */
112     public boolean login() {
113         if (LoginModule.login(ipTextField.getText(), Integer.parseInt(portTextField.getText()),
114                 userNameTextField.getText(), new String(passwordField.getPassword()))) {
115             setButtonEnable(true); // login succeed,enable related button
116             btnLogin.setEnabled(false);
117             // 监听按钮使能
118         } else {
119             JOptionPane.showMessageDialog(null, Res.string().getLoginFailed() + ", " + ToolKits.getErrorCodeShow(),
120                     Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
121             return false;
122         }
123         return true;
124     }
125
126     /**
127      * 登出
128      */
129     public void logout() {
130         if (LoginModule.logout()) {
131             setButtonEnable(false); // 所有按钮置灰
132             btnLogin.setEnabled(true);
133             if (isListen) {
134                 stopListen();
135                 isListen = false;
136             }
137
138             if (isAlarmAttachInfo) {
139                 scadaDetachInfo();
140                 isAlarmAttachInfo = false;
141             }
142             if (isAttachInfo) {
143                 scadaAlarmDetachInfo();
144                 isAttachInfo = false;
145             }
146
147             //清空表格
148             //普通事件table清空数据
149             ((DefaultTableModel) alarmTable.getModel()).setRowCount(0);
150
151             // 设备列表table清空数据
152             ((DefaultTableModel) devicesDataTable.getModel()).setRowCount(0);
153
154             // 监测点位实时信息table清空数据
155             ((DefaultTableModel) attachInfoDataTable.getModel()).setRowCount(0);
156
157             // 监测点位报警table清空数据
158             ((DefaultTableModel) alarmAttachInfoDataTable.getModel()).setRowCount(0);
159
160             // 点位信息table清空数据
161             ((DefaultTableModel) attributeDataTable.getModel()).setRowCount(0);
162         }
163     }
164
165
166     /**
167      * 获取当前主机接入的外部设备ID
168      */
169     Vector<String> deviceIds = new Vector<String>();
170     // 多平台 编码
171     private final static Charset encode = Charset.forName(Utils.getPlatformEncode());
172
173     public boolean getDeviceIdList() {
174         deviceIds.clear();
175
176         // model.setColumnCount(0);
177         int nCount = 64; //
178
179         NetSDKLib.NET_SCADA_DEVICE_ID_INFO[] stuDeviceIDList = new NetSDKLib.NET_SCADA_DEVICE_ID_INFO[nCount];
180         for (int i = 0; i < stuDeviceIDList.length; ++i) {
181             stuDeviceIDList[i] = new NetSDKLib.NET_SCADA_DEVICE_ID_INFO();
182         }
183
184         NetSDKLib.NET_SCADA_DEVICE_LIST stuSCADADeviceInfo = new NetSDKLib.NET_SCADA_DEVICE_LIST();
185         stuSCADADeviceInfo.nMax = nCount;
186         int nSize = stuDeviceIDList[0].size() * nCount;
187         stuSCADADeviceInfo.pstuDeviceIDInfo = new Memory(nSize);   // 监测设备信息
188         stuSCADADeviceInfo.pstuDeviceIDInfo.clear(nSize);
189         ToolKits.SetStructArrToPointerData(stuDeviceIDList, stuSCADADeviceInfo.pstuDeviceIDInfo);
190         if (queryDevState(NetSDKLib.NET_DEVSTATE_SCADA_DEVICE_LIST, stuSCADADeviceInfo)) {
191             JOptionPane.showMessageDialog(null, Res.string().getLoginFailed() + ", " + ToolKits.getErrorCodeShow(),
192                     Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
193             return false;
194         }
195
196         if (stuSCADADeviceInfo.nRet == 0) {
197             System.out.println("当前主机接入的外部设备ID有效个数为0.");                        // 外部设备没有有效ID
198             return false;
199         }
200
201         // 从 Pointer 提取数据
202         ToolKits.GetPointerDataToStructArr(stuSCADADeviceInfo.pstuDeviceIDInfo, stuDeviceIDList);
203         // 打印数据并更新设备ID
204         System.out.println("获取当前主机接入的外部设备ID的有效个数:" + stuSCADADeviceInfo.nRet);
205         if (null != devicesDataTable) {
206             DefaultTableModel model = (DefaultTableModel) devicesDataTable.getModel();
207             model.getDataVector().clear();// 清空所有数据
208             for (int i = 0; i < stuSCADADeviceInfo.nRet; ++i) {
209                 String deviceID = "";
210                 String deviceName = "";
211
212                 deviceName = new String(stuDeviceIDList[i].szDevName, encode).trim();
213                 deviceID = new String(stuDeviceIDList[i].szDeviceID, encode).trim();
214                 System.out.printf("外部设备[%d] 设备id[%s] 设备名称[%s]\n", i,
215                         new String(stuDeviceIDList[i].szDeviceID, encode).trim(), deviceName);
216
217
218                 deviceIds.add(deviceID);
219                 model.addRow(new Object[]{deviceID, deviceName});
220             }
221         }
222
223         return true;
224     }
225
226     /**
227      * 查询设备状态
228      */
229     public boolean queryDevState(int nType, NetSDKLib.SdkStructure stuInfo) {
230
231         IntByReference intRetLen = new IntByReference();
232         stuInfo.write();
233         if (!LoginModule.netsdk.CLIENT_QueryDevState(m_hLoginHandle, nType, stuInfo.getPointer(), stuInfo.size(), intRetLen, 3000)) {
234             return true;
235         }
236         stuInfo.read();
237         return false;
238     }
239
240
241     /**
242      * 订阅监测点位报警
243      */
244     public void scadaAlarmAttachInfo() {
245
246         if (!isAlarmAttachInfo) {
247             // 入参
248             NetSDKLib.NET_IN_SCADA_ALARM_ATTACH_INFO stIn = new NetSDKLib.NET_IN_SCADA_ALARM_ATTACH_INFO();
249             stIn.cbCallBack = SCADAAlarmAttachInfoCallBack.getINSTANCE(alarmAttachInfoDataTable);
250             // 出参
251             NetSDKLib.NET_OUT_SCADA_ALARM_ATTACH_INFO stOut = new NetSDKLib.NET_OUT_SCADA_ALARM_ATTACH_INFO();
252
253             alarmAttachInfoHandle = LoginModule.netsdk.CLIENT_SCADAAlarmAttachInfo(m_hLoginHandle, stIn, stOut, 3000);
254
255             if (alarmAttachInfoHandle.longValue() != 0) {
256                 contentPane.repaint();
257                 isAlarmAttachInfo = true;
258                 btnAlarmAttachInfo.setText(Res.string().getCancel()); // 取消
259             }
260         } else {
261             scadaAlarmDetachInfo();
262         }
263
264     }
265
266     /**
267      * 取消订阅监测点位报警信息
268      */
269     public void scadaAlarmDetachInfo() {
270         if (alarmAttachInfoHandle.longValue() != 0) {
271             LoginModule.netsdk.CLIENT_SCADAAlarmDetachInfo(alarmAttachInfoHandle);
272             contentPane.repaint();
273             isAlarmAttachInfo = false;
274             alarmAttachInfoHandle.setValue(0);
275             alarmAttachInfoDataModel.getDataVector().clear();
276             btnAlarmAttachInfo.setText(Res.string().getSCADAAttach()); // 订阅
277
278         }
279     }
280
281     private NetSDKLib.LLong attachInfoHandle = new NetSDKLib.LLong(0);  // 监测点位信息,订阅句柄
282     private NetSDKLib.LLong alarmAttachInfoHandle = new NetSDKLib.LLong(0);  // 监测点位报警,订阅句柄
283
284     /**
285      * 普通报警监听回调
286      */
287     private static class MessCallback implements NetSDKLib.fMessCallBack {
288         private static MessCallback INSTANCE;
289         private JTable table;
290
291         private MessCallback(JTable table) {
292             this.table = table;
293         }
294
295         public static MessCallback getInstance(JTable table) {
296             if (INSTANCE == null) {
297                 INSTANCE = new MessCallback(table);
298             }
299             if (table != null) {
300                 INSTANCE.table = table;
301             }
302
303             return INSTANCE;
304         }
305
306         @Override
307         public boolean invoke(int lCommand, NetSDKLib.LLong lLoginID, Pointer pStuEvent,
308                               int dwBufLen, String strDeviceIP, NativeLong nDevicePort,
309                               Pointer dwUser) {
310             switch (lCommand) {
311
312                 case NetSDKLib.NET_ALARM_SCADA_DEV_ALARM: {  // 12706 检测采集设备报警事件   "SCADADevAlarm"
313
314                     NetSDKLib.ALARM_SCADA_DEV_INFO msg = new NetSDKLib.ALARM_SCADA_DEV_INFO();
315                     ToolKits.GetPointerData(pStuEvent, msg);
316                     String deviceId = new String(msg.szDevID,encode).trim();
317                     System.out.println("[检测采集设备报警事件] nChannel :" + msg.nChannel);
318                     System.out.println("设备ID :" + deviceId);
319                     String description = new String(msg.szDesc, encode).trim();
320                     String deviceName = new String(msg.szDevName,encode).trim();
321                     System.out.println( " nAction :" + msg.nAction +
322                             " nAlarmFlag :" + msg.nAlarmFlag + "\n" + "故障设备名称" + deviceName
323                     + "\n" + "描述" + description + "时间" + msg.stuTime);
324                     if (table != null) {
325 //                        NetSDKLib.ALARM_SCADA_DEV_INFO msg = new NetSDKLib.ALARM_SCADA_DEV_INFO();
326 //                        ToolKits.GetPointerData(pStuEvent, msg);
327                         DefaultTableModel model = (DefaultTableModel) table.getModel();
328                         model.addRow(new Object[]{deviceId, deviceName, msg.nChannel, description});
329                     }
330                     break;
331                 }
332                 default:
333                     System.out.println("What's lCommand: " + lCommand);
334                     break;
335
336             }
337
338             return true;
339         }
340     }
341
342     /**
343      * 订阅监测点位实时信息
344      */
345     public void scadaAttachInfo() {
346         if (!isBtnAttachInfo) {
347
348             // 入参
349             NetSDKLib.NET_IN_SCADA_ATTACH_INFO stIn = new NetSDKLib.NET_IN_SCADA_ATTACH_INFO();
350             stIn.cbCallBack = SCADAAttachInfoCallBack.getInstance(attachInfoDataTable);
351             // 出参
352             System.err.println("登录句柄: " + m_hLoginHandle);
353             NetSDKLib.NET_OUT_SCADA_ATTACH_INFO stOut = new NetSDKLib.NET_OUT_SCADA_ATTACH_INFO();
354             attachInfoHandle = LoginModule.netsdk.CLIENT_SCADAAttachInfo(m_hLoginHandle, stIn, stOut, 3000);
355             if (attachInfoHandle.longValue() != 0) {
356                 contentPane.repaint();
357                 isBtnAttachInfo = true;
358                 btnAttachInfo.setText(Res.string().getCancel()); // 取消
359                 System.out.println("CLIENT_SCADAAttachInfo: 订阅监测点位实时信息成功!");
360             } else {
361                 JOptionPane.showMessageDialog(null, ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(),
362                         JOptionPane.ERROR_MESSAGE);
363             }
364         } else {
365             scadaDetachInfo();
366         }
367     }
368
369
370     /**
371      * 获取设备点位信息
372      */
373     public void getDeviceAttribute() throws UnsupportedEncodingException {
374         Map<Integer, String> num2PointType = new HashMap<Integer, String>(6);
375         num2PointType.put(0, "UNKNOWN");
376         num2PointType.put(1, "ALL");
377         num2PointType.put(2, "YC");
378         num2PointType.put(3, "YX");
379         num2PointType.put(4, "YT");
380         num2PointType.put(5, "YK");
381         DefaultTableModel model = (DefaultTableModel) attributeDataTable.getModel();
382         model.getDataVector().clear(); // 清空所有数据
383         for (int i = 0; i < deviceIds.size(); i++) {
384             String deviceId = deviceIds.get(i);
385             int nMaxAttributeInfoNum = 20;
386             NET_IN_SCADA_GET_ATTRIBUTE_INFO inParam = new NET_IN_SCADA_GET_ATTRIBUTE_INFO(); // 获取设备点位信息 入参
387             NET_OUT_SCADA_GET_ATTRIBUTE_INFO outParam = new NET_OUT_SCADA_GET_ATTRIBUTE_INFO(); // 获取设备点位信息 出参
388             NET_GET_CONDITION_INFO conditionInfo = new NET_GET_CONDITION_INFO(); // 获取条件
389             conditionInfo.szDeviceID = deviceIds.get(i).getBytes();
390             conditionInfo.bIsSendID = 1;
391             outParam.nMaxAttributeInfoNum = nMaxAttributeInfoNum;
392             NET_ATTRIBUTE_INFO attributeInfo = new NET_ATTRIBUTE_INFO(); // 设备点位信息(内存由用户申请)
393
394             outParam.pstuAttributeInfo = new Memory(nMaxAttributeInfoNum * attributeInfo.size());
395             inParam.stuCondition = conditionInfo;
396             inParam.write();
397             outParam.write();
398             boolean ret = LoginModule.netsdk.CLIENT_SCADAGetAttributeInfo(m_hLoginHandle, inParam.getPointer(), outParam.getPointer(), 3000);
399             inParam.read();
400             outParam.read();
401
402             if (ret) {
403                 System.out.println("SCADAGetAttributeInfo succeed!");
404                 outParam.read();
405                 int retAttributeInfoNum = outParam.nRetAttributeInfoNum;
406                 System.out.println(outParam);
407
408                 NET_ATTRIBUTE_INFO infos[] = new NET_ATTRIBUTE_INFO[retAttributeInfoNum];
409                 for (int j = 0; j < retAttributeInfoNum; j++) {
410                     infos[j] = new NET_ATTRIBUTE_INFO();
411                 }
412                 System.err.println("infos 大小: " + infos.length);
413                 ToolKits.GetPointerDataToStructArr(outParam.pstuAttributeInfo, infos);
414
415                 for (int n = 0; n < retAttributeInfoNum; n++) {
416                     NET_ATTRIBUTE_INFO out = new NET_ATTRIBUTE_INFO();
417                     out = infos[n];
418                     model.addRow(new Object[]{deviceId, new String(out.szSignalName, encode).trim(), out.nDelay, out.bIsValid, num2PointType.get(out.emPointType)});
419
420                     System.out.println("设备ID:" + deviceId
421                             + "\n" + "点位类型: " + num2PointType.get(out.emPointType)
422                             + "\n" + "点位名称: " + new String(out.szSignalName, encode).trim()
423                             + "\n" + "时延: " + out.nDelay);
424
425                 }
426             } else {
427                 JOptionPane.showMessageDialog(null, ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(),
428                         JOptionPane.ERROR_MESSAGE);
429             }
430         }
431
432
433     }
434
435     /**
436      * 取消订阅监测点位实时信息
437      */
438     public void scadaDetachInfo() {
439         if (attachInfoHandle.longValue() != 0) {
440             LoginModule.netsdk.CLIENT_SCADADetachInfo(attachInfoHandle);
441             //attachInfoHandle.setValue(0);
442             contentPane.repaint();
443             isBtnAttachInfo = false;
444             attachInfoDataModel.getDataVector().clear();
445             btnAttachInfo.setText(Res.string().getSCADAAttach()); // 订阅
446             System.out.println("CLIENT_SCADADetachInfo: 取消订阅监测点位实时信息成功!");
447         }
448     }
449
450
451     /**
452      * 订阅报警信息
453      */
454     public void startListenEx() {
455         if (!isListen) {
456
457             LoginModule.netsdk.CLIENT_SetDVRMessCallBack(MessCallback.getInstance(alarmTable), null); // set alarm listen callback
458
459             boolean b = LoginModule.netsdk.CLIENT_StartListenEx(m_hLoginHandle);
460             if (b) {
461                 isListen = true;
462                 contentPane.repaint();
463                 btnStartListen.setText(Res.string().getCancel());
464                 //btnStopListen.setEnabled(true);
465                 System.out.println("CLIENT_StartListenEx success.");
466             }
467         } else {
468             stopListen();
469         }
470
471     }
472
473     public JTable tableInit(Object[][] data, String[] columnName) {
474         JTable table;
475         DefaultTableModel model;
476         model = new DefaultTableModel(data, columnName);
477         table = new JTable(model) {
478             @Override // 不可编辑
479             public boolean isCellEditable(int row, int column) {
480                 return false;
481             }
482         };
483
484         table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // 只能选中一行
485         // 列表显示居中
486         DefaultTableCellRenderer dCellRenderer = new DefaultTableCellRenderer();
487         dCellRenderer.setHorizontalAlignment(JLabel.CENTER);
488         table.setDefaultRenderer(Object.class, dCellRenderer);
489         return table;
490     }
491
492
493     /**
494      * 停止监听
495      */
496     public void stopListen() {
497         if (isListen) {
498             if (LoginModule.netsdk.CLIENT_StopListen(m_hLoginHandle)) {
499                 isListen = false;
500                 contentPane.repaint();
501                 alarmModel.getDataVector().clear();
502                 btnStartListen.setText(Res.string().getStart());
503
504             }
505         }
506
507     }
508
509     // 获取界面窗口
510     private static JFrame frame = new JFrame();
511
512
513     /**
514      * Create Frame
515      */
516     public SCADADemo() {
517         setTitle(Res.string().getSCADA());
518         setBounds(100, 100, 920, 750);
519         contentPane = new JPanel();
520         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
521         setContentPane(contentPane);
522         contentPane.setLayout(null);
523         setResizable(true);
524         try {
525             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
526         } catch (Exception e) {
527             e.printStackTrace();
528         }
529         LoginModule.init(DefaultDisConnect.GetInstance(), DefaultHaveReconnect.getINSTANCE());
530         JPanel panel = new JPanel();
531         panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), Res.string().getLogin(),
532                 TitledBorder.LEFT, TitledBorder.TOP, null, new Color(0, 0, 0)));
533         panel.setBounds(0, 0, 905, 46);
534         contentPane.add(panel);
535         panel.setLayout(null);
536
537         JLabel ipLabel = new JLabel(Res.string().getIp());
538         ipLabel.setBounds(10, 15, 44, 21);
539         panel.add(ipLabel);
540
541         ipTextField = new JTextField();
542         ipTextField.setText("172.3.0.223"); // 172.24.31.185 172.3.4.104 172.3.4.101
543         ipTextField.setBounds(64, 15, 89, 21);
544         panel.add(ipTextField);
545         ipTextField.setColumns(10);
546
547         JLabel portLabel = new JLabel(Res.string().getPort());
548         portLabel.setBounds(174, 15, 44, 21);
549         panel.add(portLabel);
550
551         portTextField = new JTextField();
552         portTextField.setText("37777");
553         portTextField.setColumns(10);
554         portTextField.setBounds(228, 15, 66, 21);
555         panel.add(portTextField);
556
557         JLabel lblName = new JLabel(Res.string().getUserName());
558         lblName.setBounds(316, 15, 66, 21);
559         panel.add(lblName);
560
561         userNameTextField = new JTextField();
562         userNameTextField.setText("admin");
563         userNameTextField.setColumns(10);
564         userNameTextField.setBounds(383, 15, 87, 21);
565         panel.add(userNameTextField);
566
567         JLabel lblPassword = new JLabel(Res.string().getPassword());
568         lblPassword.setBounds(492, 15, 66, 21);
569         panel.add(lblPassword);
570
571         passwordField = new JPasswordField();
572         passwordField.setBounds(568, 15, 112, 21);
573         passwordField.setText("admin123");
574         panel.add(passwordField);
575
576         btnLogin = new JButton(Res.string().getLogin());
577         btnLogin.setBounds(684, 14, 99, 23);
578         panel.add(btnLogin);
579         btnLogin.addMouseListener(new MouseAdapter() {
580             @Override
581             public void mouseClicked(MouseEvent e) {
582                 super.mouseClicked(e);
583                 login();
584             }
585         });
586
587         btnLogout = new JButton(Res.string().getLogout());
588         btnLogout.setBounds(785, 14, 110, 23);
589         panel.add(btnLogout);
590         btnLogout.setEnabled(false);
591         btnLogout.addMouseListener(new MouseAdapter() {
592             @Override
593             public void mouseClicked(MouseEvent e) {
594                 super.mouseClicked(e);
595                 logout();
596
597
598                 LoginModule.cleanup();   // 关闭工程,释放资源
599                 dispose();
600                 // 返回主菜单
601                 SwingUtilities.invokeLater(new Runnable() {
602                     public void run() {
603                         FunctionList demo = new FunctionList();
604                         demo.setVisible(true);
605                     }
606                 });
607             }
608         });
609
610         // 设备列表
611         JPanel devicesPanel = new JPanel();
612         devicesPanel.setBorder(new TitledBorder(null, Res.string().getSCADADeviceList(), TitledBorder.LEFT, TitledBorder.TOP, null, new Color(0, 0, 0)));
613         devicesPanel.setBounds(0, 50, 450, 260);
614         contentPane.add(devicesPanel);
615         devicesPanel.setLayout(new BorderLayout(0, 0));
616
617         btnGetDeviceList = new JButton(Res.string().getListBtn());
618         btnGetDeviceList.setBounds(260, 315, 100, 29);
619         contentPane.add(btnGetDeviceList);
620         btnGetDeviceList.addMouseListener(
621
622                 new MouseAdapter() {
623                     @Override
624                     public void mouseClicked(MouseEvent e) {
625                         super.mouseClicked(e);
626                         getDeviceIdList();
627
628                     }
629                 });
630         devicesData = new Object[0][3];
631         devicesDataTable = tableInit(devicesData, devicesDataTitle);
632         devicesDataModel = (DefaultTableModel) devicesDataTable.getModel();
633         JScrollPane deviceScrollPane = new JScrollPane(devicesDataTable);
634         deviceScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
635         devicesPanel.add(deviceScrollPane, BorderLayout.CENTER);
636
637
638         // 点位信息
639         JPanel pointListPanel = new JPanel();
640         pointListPanel.setBorder(new TitledBorder(null, Res.string().getSCADAPointList(), TitledBorder.LEFT, TitledBorder.TOP, null, new Color(0, 0, 0)));
641         pointListPanel.setBounds(0, 350, 450, 260);
642         contentPane.add(pointListPanel);
643         pointListPanel.setLayout(new BorderLayout(0, 0));
644         btnGetSCADAAttribute = new JButton(Res.string().getListBtn());
645         btnGetSCADAAttribute.setBounds(260, 615, 100, 29);
646         contentPane.add(btnGetSCADAAttribute);
647         btnGetSCADAAttribute.addMouseListener(new MouseAdapter() {
648             @Override
649             public void mouseClicked(MouseEvent e) {
650                 super.mouseClicked(e);
651                 try {
652                     getDeviceAttribute();
653                 } catch (UnsupportedEncodingException unsupportedEncodingException) {
654                     unsupportedEncodingException.printStackTrace();
655                 }
656             }
657         });
658         attributeData = new Object[0][3];
659         attributeDataTable = tableInit(attributeData, attributeDataTitle);
660         attributeDataModel = (DefaultTableModel) attributeDataTable.getModel();
661         JScrollPane attributeDataScrollPane = new JScrollPane(attributeDataTable);
662         attributeDataScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
663         pointListPanel.add(attributeDataScrollPane, BorderLayout.CENTER);
664
665         // 遥测
666         JPanel attachAlarmPanel = new JPanel();
667         attachAlarmPanel.setBorder(new TitledBorder(null, Res.string().getSCADAAlarmAttachInfo(), TitledBorder.LEFT, TitledBorder.TOP, null, new Color(0, 0, 0)));
668         attachAlarmPanel.setBounds(450, 50, 450, 170);
669         contentPane.add(attachAlarmPanel);
670         attachAlarmPanel.setLayout(new BorderLayout(0, 0));
671         btnAlarmAttachInfo = new JButton(Res.string().getSCADAAttach());
672         btnAlarmAttachInfo.setBounds(450, 225, 100, 29);
673         contentPane.add(btnAlarmAttachInfo);
674         btnAlarmAttachInfo.addActionListener(new ActionListener() {
675             @Override
676             public void actionPerformed(ActionEvent arg0) {
677                 scadaAlarmAttachInfo();
678             }
679         });
680         alarmAttachInfoData = new Object[0][3];
681         alarmAttachInfoDataTable = tableInit(alarmAttachInfoData, alarmAttachInfoDataTitle);
682         alarmAttachInfoDataModel = (DefaultTableModel) alarmAttachInfoDataTable.getModel();
683         JScrollPane alarmAttachInfoDataScrollPane = new JScrollPane(alarmAttachInfoDataTable);
684         alarmAttachInfoDataScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
685         attachAlarmPanel.add(alarmAttachInfoDataScrollPane, BorderLayout.CENTER);
686
687         // 普通报警
688         JPanel attachPanel = new JPanel();
689         attachPanel.setBorder(new TitledBorder(null, Res.string().getSCADAAttach(), TitledBorder.LEFT, TitledBorder.TOP, null, new Color(0, 0, 0)));
690         attachPanel.setBounds(450, 270, 450, 170);
691         contentPane.add(attachPanel);
692         attachPanel.setLayout(new BorderLayout(0, 0));
693         btnStartListen = new JButton(Res.string().getSCADAAttach());
694         btnStartListen.setBounds(450, 445, 100, 29);
695         contentPane.add(btnStartListen);
696         btnStartListen.addActionListener(new ActionListener() {
697             @Override
698             public void actionPerformed(ActionEvent arg0) {
699                 startListenEx();
700             }
701         });
702 //        btnStartListen.addMouseListener(new MouseAdapter() {
703 //            @Override
704 //            public void mouseClicked(MouseEvent e) {
705 //                super.mouseClicked(e);
706 //                isBtnStartListen = !isBtnStartListen;
707 //                if (isBtnStartListen) {
708 //                    startListenEx();
709 //                    btnStartListen.setText(Res.string().getCancel());
710 //                } else {
711 //                    stopListen();
712 //                    btnStartListen.setText(Res.string().getSCADAAttach());
713 //                }
714 //                contentPane.add(btnStartListen);
715 //                contentPane.setOpaque(true);
716 //                contentPane.repaint();
717 //            }
718 //        });
719         alarmData = new Object[0][3];
720         alarmTable = tableInit(alarmData, alarmTableTitle);
721         alarmModel = (DefaultTableModel) alarmTable.getModel();
722         JScrollPane scrollPane = new JScrollPane(alarmTable);
723         scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
724         attachPanel.add(scrollPane, BorderLayout.CENTER);
725
726         // 遥信 订阅监测点位实时信息
727         boolean isBtnGetDeviceList = false;
728         JPanel attachInfoPanel = new JPanel();
729         attachInfoPanel.setBorder(new TitledBorder(null, Res.string().getSCADAAttachInfo(), TitledBorder.LEFT, TitledBorder.TOP, null, new Color(0, 0, 0)));
730         attachInfoPanel.setBounds(450, 480, 450, 170);
731         contentPane.add(attachInfoPanel);
732         attachInfoPanel.setLayout(new BorderLayout(0, 0));
733         btnAttachInfo = new JButton(Res.string().getSCADAAttach()); // 订阅
734         btnAttachInfo.setBounds(450, 655, 100, 29);
735         contentPane.add(btnAttachInfo);
736         btnAttachInfo.addActionListener(new ActionListener() {
737             @Override
738             public void actionPerformed(ActionEvent arg0) {
739                 scadaAttachInfo();
740             }
741         });
742 //        btnAttachInfo.addMouseListener(new MouseAdapter() {
743 //            @Override
744 //            public void mouseClicked(MouseEvent e) {
745 //                super.mouseClicked(e);
746 //                scadaAttachInfo();
747 //            }
748 //        });
749
750         attachInfoData = new Object[0][3];
751         attachInfoDataTable = tableInit(attachInfoData, attachInfoDataTitle);
752         attachInfoDataModel = (DefaultTableModel) attachInfoDataTable.getModel();
753         JScrollPane attachInfoDataScrollPane = new JScrollPane(attachInfoDataTable);
754         attachInfoDataScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
755         attachInfoPanel.add(attachInfoDataScrollPane, BorderLayout.CENTER);
756
757         setButtonEnable(false);
758         btnLogin.setEnabled(true);
759         addWindowListener(new WindowAdapter() {
760             @Override
761             public void windowClosing(WindowEvent e) {
762                 if (LoginModule.m_hLoginHandle.longValue() != 0) {
763                     logout();
764                 }
765                 LoginModule.cleanup();
766                 dispose();
767                 SwingUtilities.invokeLater(new Runnable() {
768                     public void run() {
769                         FunctionList demo = new FunctionList();
770                         demo.setVisible(true);
771                     }
772                 });
773             }
774         });
775     }
776
777     private boolean isBtnGetDeviceList = false;
778     private boolean isBtnStartListen = false;
779     private boolean isBtnAlarmAttachInfo = false;
780     private boolean isBtnAttachInfo = false;
781     private boolean isBtnPointList = false;
782
783     // 普通订阅数据表
784     private Object[][] alarmData;
785     private JTable alarmTable;
786     private final String[] alarmTableTitle = {Res.string().getDeviceID(), Res.string().getDeviceName(), Res.string().getChannel(), Res.string().getAlarmDescribe()};
787     private DefaultTableModel alarmModel;
788
789     // 设备列表
790     private Object[][] devicesData;
791     private JTable devicesDataTable;
792     private final String[] devicesDataTitle = {Res.string().getDeviceID(), Res.string().getDeviceName()};
793     private DefaultTableModel devicesDataModel;
794
795     // 报警列表
796     private Object[][] alarmAttachInfoData;
797     private JTable alarmAttachInfoDataTable;
798     private final String[] alarmAttachInfoDataTitle = {Res.string().getDeviceID(), Res.string().getPointID(), Res.string().getAlarmDescribe(), Res.string().getCollectTime(), Res.string().getAlarmLevel()};
799     private DefaultTableModel alarmAttachInfoDataModel;
800
801
802     // 实时信息列表
803     private Object[][] attachInfoData;
804     private JTable attachInfoDataTable;
805     private final String[] attachInfoDataTitle = {Res.string().getDeviceName(), Res.string().getCollectTime(), Res.string().getPointID()};
806     private DefaultTableModel attachInfoDataModel;
807
808     // 点位信息列表
809     private Object[][] attributeData;
810     private JTable attributeDataTable;
811     private final String[] attributeDataTitle = {Res.string().getDeviceID(), Res.string().getPointName(), Res.string().getAlarmDelay(), Res.string().getIfValidSignalPoint(), Res.string().getAlarmType()};
812     private DefaultTableModel attributeDataModel;
813
814     // 设备断线通知回调
815     private DisConnect disConnectCallback = new DisConnect();
816
817     /////////////////面板///////////////////
818     // 设备断线回调: 通过 CLIENT_Init 设置该回调函数,当设备出现断线时,SDK会调用该函数
819     private static class DisConnect implements NetSDKLib.fDisConnect {
820         public void invoke(NetSDKLib.LLong m_hLoginHandle, String pchDVRIP, int nDVRPort, Pointer dwUser) {
821             System.out.printf("Device[%s] Port[%d] DisConnect!\n", pchDVRIP, nDVRPort);
822             // 断线提示
823             SwingUtilities.invokeLater(new Runnable() {
824                 public void run() {
825                     frame.setTitle(Res.string().getSCADA() + " : " + Res.string().getDisConnectReconnecting());
826                 }
827             });
828         }
829     }
830
831 //    // 网络连接恢复,设备重连成功回调
832 //    // 通过 CLIENT_SetAutoReconnect 设置该回调函数,当已断线的设备重连成功时,SDK会调用该函数
833 //    private static class HaveReConnect implements NetSDKLib.fHaveReConnect {
834 //        public void invoke(NetSDKLib.LLong m_hLoginHandle, String pchDVRIP, int nDVRPort, Pointer dwUser) {
835 //            System.out.printf("ReConnect Device[%s] Port[%d]\n", pchDVRIP, nDVRPort);
836 //
837 //            // 重连提示
838 //            SwingUtilities.invokeLater(new Runnable() {
839 //                public void run() {
840 //                    frame.setTitle(Res.string().getPowerEnvironmentMonitor() + " : " + Res.string().getOnline());
841 //                }
842 //            });
843 //        }
844 //    }
845
846     /**
847      * 订阅监测点位信息回调
848      */
849     private static class SCADAAttachInfoCallBack implements NetSDKLib.fSCADAAttachInfoCallBack {
850         private JTable attachInfoTable;
851         private static SCADAAttachInfoCallBack INSTANCE;
852
853         private SCADAAttachInfoCallBack(JTable attachInfoTable) {
854             this.attachInfoTable = attachInfoTable;
855         }
856
857         public static final SCADAAttachInfoCallBack getInstance(JTable table) {
858             if (INSTANCE == null) {
859                 INSTANCE = new SCADAAttachInfoCallBack(table);
860             }
861             if (table != null) {
862                 INSTANCE.attachInfoTable = table;
863             }
864             return INSTANCE;
865         }
866
867         @Override
868         public void invoke(NetSDKLib.LLong lLoginID, NetSDKLib.LLong lAttachHandle,
869                            NetSDKLib.NET_SCADA_NOTIFY_POINT_INFO_LIST pInfo, int nBufLen, Pointer dwUser) {
870             System.out.println("————————————————————【订阅监测点位信息回调】————————————————————");
871 //            for (int i = 0; i < pInfo.nList; i++) {
872 //                System.out.println(" 设备名称:" + new String(pInfo.stuList[i].szDevName).trim());
873 //                System.out.println(" 点位名(与点位表的取值一致):" + new String(pInfo.stuList[i].szPointName).trim());
874 //                System.out.println(" 现场监控单元ID:" + new String(pInfo.stuList[i].szFSUID).trim());
875 //                System.out.println(" 点位ID:" + new String(pInfo.stuList[i].szID).trim());
876 //                System.out.println(" 探测器ID:" + new String(pInfo.stuList[i].szSensorID).trim());
877 //                System.out.println(" 点位类型:" + pInfo.stuList[i].emPointType);
878 //                System.out.println(" 采集时间 : " + pInfo.stuList[i].stuCollectTime.toStringTime());
879 //            }
880
881             //更新列表
882             if (attachInfoTable != null) {
883                 DefaultTableModel model = (DefaultTableModel) attachInfoTable.getModel();
884                 for (int i = 0; i < pInfo.nList; i++) {
885                     String deviceName = new String(pInfo.stuList[i].szDevName, encode).trim();
886                     String time = pInfo.stuList[i].stuCollectTime.toStringTime();
887                     System.out.println(" 设备名称:" + deviceName);
888                     System.out.println(" 采集时间:" + time);
889                     System.out.println(" 点位ID:" + new String(pInfo.stuList[i].szID).trim());
890                     model.addRow(new Object[]{deviceName, time, new String(pInfo.stuList[i].szID).trim()});
891                 }
892             }
893
894             System.out.println("————————————————————【订阅监测点位信息回调】————————————————————");
895
896         }
897     }
898 }