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
package com.iailab.netsdk.demo.frame.Attendance;
 
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
 
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
 
import com.sun.jna.Pointer;
 
import com.iailab.netsdk.common.FunctionList;
import com.iailab.netsdk.common.LoginPanel;
import com.iailab.netsdk.common.Res;
import com.iailab.netsdk.demo.module.AttendanceModule;
import com.iailab.netsdk.demo.module.LoginModule;
import com.iailab.netsdk.lib.NetSDKLib;
import com.iailab.netsdk.lib.NetSDKLib.LLong;
import com.iailab.netsdk.lib.ToolKits;
 
/**
 * 考勤机Demo:包含门禁事件订阅、人员操作、信息操作
 */
class AttendanceFrame  extends JFrame{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    // 设备断线通知回调
    private  DisConnect disConnect  = new DisConnect(); 
    
    // 获取界面窗口
    private static JFrame frame = new JFrame();
        
    public AttendanceFrame(){
        setTitle(Res.string().getAttendance());
        setLayout(new BorderLayout());
        pack();
        setSize(800, 555);
        setResizable(false);
        setLocationRelativeTo(null);  
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);   // 释放窗体
        
        LoginModule.init(disConnect, null);   // 打开工程,初始化 
 
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        loginPanel = new LoginPanel();
        showPanel = new AttendanceShowPanel();
        operatePanel = new AttendanceFunctionOperatePanel(showPanel);
        
        JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, loginPanel, operatePanel);
        splitPane.setDividerSize(0);
        splitPane.setBorder(null);
        add(splitPane, BorderLayout.NORTH);
        add(showPanel, BorderLayout.CENTER);
        
        loginPanel.addLoginBtnActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {               
                if(loginPanel.checkLoginText()) {
                    if(login()) {
                        frame = ToolKits.getFrame(e);
                        frame.setTitle(Res.string().getAttendance() + " : " + Res.string().getOnline());
                    }
                }
            }
        });
        
        loginPanel.addLogoutBtnActionListener(new ActionListener() {        
            @Override
            public void actionPerformed(ActionEvent e) {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        logout();
                    }
                });
            }
        });
        
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                AttendanceModule.stopRealLoadPicture();
                LoginModule.logout();
                LoginModule.cleanup();   // 关闭工程,释放资源
                dispose();    
                
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        FunctionList demo = new FunctionList();
                        demo.setVisible(true);
                    }
                });
            }
        });
    }
    
    // 设备断线回调: 通过 CLIENT_Init 设置该回调函数,当设备出现断线时,SDK会调用该函数
    private class DisConnect implements NetSDKLib.fDisConnect {
        public DisConnect() { }
        
        public void invoke(LLong m_hLoginHandle, String pchDVRIP, int nDVRPort, Pointer dwUser) {
            System.out.printf("Device[%s] Port[%d] DisConnect!\n", pchDVRIP, nDVRPort);
            // 断线提示
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JOptionPane.showMessageDialog(null, Res.string().getDisConnect(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
                    logout();
                }
            });
        }
    }
        
    // 登录
    public boolean login() {
        if(LoginModule.login(loginPanel.ipTextArea.getText(), 
                        Integer.parseInt(loginPanel.portTextArea.getText()), 
                        loginPanel.nameTextArea.getText(), 
                        new String(loginPanel.passwordTextArea.getPassword()))) {
    
            loginPanel.setButtonEnable(true);
            operatePanel.setButtonEnable(true);
            
        } else {
            JOptionPane.showMessageDialog(null, Res.string().getLoginFailed() + ", " + ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
            return false;
        }
        return true;
    }
        
    // 登出
    public void logout() {
        AttendanceModule.stopRealLoadPicture();
        LoginModule.logout();
        
        frame.setTitle(Res.string().getAttendance());
        loginPanel.setButtonEnable(false);
        operatePanel.setButtonEnable(false);
        showPanel.clearup();
    }
    
    private LoginPanel loginPanel;                            // 登陆面板
    private AttendanceFunctionOperatePanel operatePanel;    // 操作面板
    private AttendanceShowPanel showPanel;                    // 显示面板
}
 
public class Attendance {
    public static void main(String[] args) {    
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                AttendanceFrame demo = new AttendanceFrame();    
                demo.setVisible(true);
            }
        });
    }
}