dengzedong
2024-12-19 85b2001c0ec2f1adc598db3bf47ad457dcca7074
提交 | 用户 | 时间
149dd0 1 package com.iailab.netsdk.demo.frame.Attendance;
H 2
3 import java.awt.BorderLayout;
4 import java.awt.Dimension;
5 import java.awt.FlowLayout;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8 import java.awt.event.WindowAdapter;
9 import java.awt.event.WindowEvent;
10 import java.util.Timer;
11 import java.util.TimerTask;
12 import java.util.concurrent.locks.ReentrantLock;
13
14 import javax.swing.JButton;
15 import javax.swing.JDialog;
16 import javax.swing.JLabel;
17 import javax.swing.JOptionPane;
18 import javax.swing.JPanel;
19 import javax.swing.SwingUtilities;
20
21 import com.iailab.netsdk.common.BorderEx;
22 import com.iailab.netsdk.common.Res;
23 import com.iailab.netsdk.demo.module.AlarmListenModule;
24 import com.iailab.netsdk.demo.module.AttendanceModule;
25 import com.iailab.netsdk.lib.NetSDKLib;
26 import com.iailab.netsdk.lib.NetSDKLib.ALARM_CAPTURE_FINGER_PRINT_INFO;
27 import com.iailab.netsdk.lib.NetSDKLib.LLong;
28 import com.iailab.netsdk.lib.NetSDKLib.fMessCallBack;
29 import com.iailab.netsdk.lib.ToolKits;
30
31 import com.sun.jna.NativeLong;
32 import com.sun.jna.Pointer;
33
34 /**
35  * 添加信息信息
36  */
37 public class AddFingerPrintDialog extends JDialog{
38     
39     /**
40      * 
41      */
42     private static final long serialVersionUID = 1L;
43     private static final int CHANNEL_ID = 0;   // 门禁序号
44     private static final String READER_ID = "1";    // 读卡器ID
45     private static final long TIMER_DELAY = 30000;    // 定时器超时时间
46     
47     private String userID = null;
48     private byte []collectionData = null;
49     private Timer timer = new Timer();    // 信息采集定时器
50     private ReentrantLock lock = new ReentrantLock();
51     
52     public AddFingerPrintDialog(String userId) { 
53         
54         setTitle(Res.string().getAddFingerPrint());
55         setLayout(new BorderLayout());
56         setModal(true);
57         pack();
58         setSize(300, 180);
59         setResizable(false);
60         setLocationRelativeTo(null);
61         setDefaultCloseOperation(DISPOSE_ON_CLOSE);
62         
63         //////////采集面板  /////////////////
64         JPanel collectionPanel = new JPanel();
65         BorderEx.set(collectionPanel, Res.string().getcFingerPrintCollection(), 4);
66         collectionPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 35, 25));
67         collectionBtn = new JButton(Res.string().getStartCollection());
68         collectionBtn.setPreferredSize(new Dimension(150, 20));
69         promptLabel = new JLabel();
70         promptLabel.setPreferredSize(new Dimension(150, 20));
71         promptLabel.setHorizontalAlignment(JLabel.CENTER);
72
73         collectionPanel.add(collectionBtn);
74         collectionPanel.add(promptLabel);
75         
76         //////////功能面板  /////////////////
77         JPanel functionPanel = new JPanel();
78         addBtn = new JButton(Res.string().getAdd());
79         cancelBtn = new JButton(Res.string().getCancel());
80         addBtn.setPreferredSize(new Dimension(100, 20));
81         cancelBtn.setPreferredSize(new Dimension(100, 20));
82         
83         functionPanel.add(addBtn);
84         functionPanel.add(cancelBtn);
85         
86         add(collectionPanel, BorderLayout.CENTER);
87         add(functionPanel, BorderLayout.SOUTH);
88         
89         addBtn.setEnabled(false);
90         userID = userId;
91         
92         cbMessage = new fCollectionDataCB();
93         
94         collectionBtn.addActionListener(new ActionListener() {
95
96             @Override
97             public void actionPerformed(ActionEvent e) {
98                 collectionFinger();
99             }
100         });
101         
102         addBtn.addActionListener(new ActionListener() {
103
104             @Override
105             public void actionPerformed(ActionEvent e) {
106                 if (AttendanceModule.insertFingerByUserId(userID, collectionData)) {
107                     JOptionPane.showMessageDialog(null, Res.string().getSucceed(), Res.string().getPromptMessage(), JOptionPane.INFORMATION_MESSAGE);
108                 } else {
109                     JOptionPane.showMessageDialog(null, Res.string().getFailed() , Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
110                 }
111                 dispose();
112             }
113             
114         });
115         
116         cancelBtn.addActionListener(new ActionListener() {
117
118             @Override
119             public void actionPerformed(ActionEvent e) {
120                 AlarmListenModule.stopListen();
121                 timer.cancel();
122                 dispose();
123             }
124         });
125         
126         addWindowListener(new WindowAdapter() {
127             public void windowClosing(WindowEvent e) {
128                 AlarmListenModule.stopListen();
129                 timer.cancel();
130                 dispose();
131             }
132         });
133     }
134     
135     public void collectionFinger() {
136         
137         if (!AlarmListenModule.startListen(cbMessage)) {
138             JOptionPane.showMessageDialog(null, Res.string().getCollectionFailed() + "," + ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
139             return;
140         }
141         
142         collectionData = null;
143         if (!AttendanceModule.collectionFinger(CHANNEL_ID, READER_ID)) {
144             JOptionPane.showMessageDialog(null, Res.string().getCollectionFailed() + "," + ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
145             return;
146         }
147
148         SwingUtilities.invokeLater(new Runnable() {
149             public void run() {
150                 promptLabel.setText(Res.string().getInCollection());
151                 collectionBtn.setEnabled(false);
152             }
153         });
154         
155         timer.schedule(new TimerTask() {
156             public void run() {
157                 lock.lock();
158                 if (collectionData == null) {
159                     AlarmListenModule.stopListen();
160                     promptLabel.setText(Res.string().getCollectionFailed());
161                     collectionBtn.setEnabled(true);
162                 }
163                 lock.unlock();
164             }
165             
166         }, TIMER_DELAY);
167     }
168     
169    /**
170      *  信息采集监听回调
171      **/
172     private class fCollectionDataCB implements fMessCallBack{
173         
174         @Override
175         public boolean invoke(int lCommand, LLong lLoginID,
176                 Pointer pStuEvent, int dwBufLen, String strDeviceIP,
177                 NativeLong nDevicePort, Pointer dwUser) {
178              
179             if (lCommand == NetSDKLib.NET_ALARM_FINGER_PRINT) {
180                 lock.lock();
181                 if (collectionData == null) {
182                     timer.cancel();
183                     ALARM_CAPTURE_FINGER_PRINT_INFO msg = new ALARM_CAPTURE_FINGER_PRINT_INFO();
184                       ToolKits.GetPointerData(pStuEvent, msg);
185                     collectionData = new byte[msg.nPacketLen * msg.nPacketNum];
186                     msg.szFingerPrintInfo.read(0, collectionData, 0, msg.nPacketLen * msg.nPacketNum);
187                     SwingUtilities.invokeLater(new Runnable() {
188                         public void run() {
189                             AlarmListenModule.stopListen();
190                             promptLabel.setText(Res.string().getcCompleteCollection());
191                             addBtn.setEnabled(true);
192                         }
193                     });
194                 }
195                 lock.unlock();
196             }
197             
198             return true;
199         }
200         
201     }
202     
203     private fMessCallBack cbMessage;     // 信息采集回调
204     private JLabel promptLabel;         // 提示信息
205     private JButton collectionBtn;        // 采集按钮
206     private JButton addBtn;                // 添加按钮
207     private JButton cancelBtn;            // 取消按钮
208 }