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