提交 | 用户 | 时间
|
149dd0
|
1 |
package com.iailab.netsdk.demo.frame.AutoRegister; |
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.io.UnsupportedEncodingException; |
|
11 |
import java.util.concurrent.ExecutorService; |
|
12 |
import java.util.concurrent.Executors; |
|
13 |
|
|
14 |
import javax.swing.JButton; |
|
15 |
import javax.swing.JCheckBox; |
|
16 |
import javax.swing.JDialog; |
|
17 |
import javax.swing.JLabel; |
|
18 |
import javax.swing.JOptionPane; |
|
19 |
import javax.swing.JPanel; |
|
20 |
import javax.swing.JTextField; |
|
21 |
|
|
22 |
import com.iailab.netsdk.common.BorderEx; |
|
23 |
import com.iailab.netsdk.common.LoginPanel; |
|
24 |
import com.iailab.netsdk.common.Res; |
|
25 |
import com.iailab.netsdk.demo.module.AutoRegisterModule; |
|
26 |
import com.iailab.netsdk.demo.module.LoginModule; |
|
27 |
import com.iailab.netsdk.lib.ToolKits; |
|
28 |
import com.iailab.netsdk.lib.NetSDKLib.CFG_DVRIP_INFO; |
|
29 |
|
|
30 |
/** |
|
31 |
* 主动注册网络配置 |
|
32 |
*/ |
|
33 |
public class DeviceConfigDialog extends JDialog{ |
|
34 |
private static final long serialVersionUID = 1L; |
|
35 |
|
|
36 |
private CFG_DVRIP_INFO info = null; |
|
37 |
private ExecutorService executorService = Executors.newSingleThreadExecutor(); |
|
38 |
|
|
39 |
public DeviceConfigDialog(){ |
|
40 |
setTitle(Res.string().getDeviceConfig()); |
|
41 |
setLayout(new BorderLayout()); |
|
42 |
setModal(true); |
|
43 |
pack(); |
|
44 |
setSize(300, 380); |
|
45 |
setResizable(false); |
|
46 |
setLocationRelativeTo(null); |
|
47 |
setDefaultCloseOperation(DISPOSE_ON_CLOSE); // 释放窗体 |
|
48 |
|
|
49 |
loginDevicePanel = new LoginDevicePanel(); |
|
50 |
ConfigDevicePanel configDevicePanel = new ConfigDevicePanel(); |
|
51 |
|
|
52 |
add(loginDevicePanel, BorderLayout.NORTH); |
|
53 |
add(configDevicePanel, BorderLayout.CENTER); |
|
54 |
|
|
55 |
addWindowListener(new WindowAdapter() { |
|
56 |
public void windowClosing(WindowEvent e){ |
|
57 |
if(!executorService.isShutdown()) { |
|
58 |
executorService.shutdown(); |
|
59 |
} |
|
60 |
|
|
61 |
LoginModule.logout(); |
|
62 |
dispose(); |
|
63 |
} |
|
64 |
}); |
|
65 |
} |
|
66 |
|
|
67 |
/* |
|
68 |
* 登陆设备面板 |
|
69 |
*/ |
|
70 |
private class LoginDevicePanel extends LoginPanel { |
|
71 |
private static final long serialVersionUID = 1L; |
|
72 |
|
|
73 |
public LoginDevicePanel() { |
|
74 |
BorderEx.set(this, Res.string().getLogin(), 2); |
|
75 |
setLayout(new FlowLayout()); |
|
76 |
Dimension dimension = new Dimension(); |
|
77 |
dimension.height = 180; |
|
78 |
setPreferredSize(dimension); |
|
79 |
|
|
80 |
ipLabel.setPreferredSize(new Dimension(100, 21)); |
|
81 |
portLabel.setPreferredSize(new Dimension(100, 21)); |
|
82 |
nameLabel.setPreferredSize(new Dimension(100, 21)); |
|
83 |
passwordLabel.setPreferredSize(new Dimension(100, 21)); |
|
84 |
|
|
85 |
ipLabel.setHorizontalAlignment(JLabel.CENTER); |
|
86 |
portLabel.setHorizontalAlignment(JLabel.CENTER); |
|
87 |
nameLabel.setHorizontalAlignment(JLabel.CENTER); |
|
88 |
passwordLabel.setHorizontalAlignment(JLabel.CENTER); |
|
89 |
|
|
90 |
ipTextArea.setPreferredSize(new Dimension(140, 21)); |
|
91 |
portTextArea.setPreferredSize(new Dimension(140, 21)); |
|
92 |
nameTextArea.setPreferredSize(new Dimension(140, 21)); |
|
93 |
passwordTextArea.setPreferredSize(new Dimension(140, 21)); |
|
94 |
loginBtn.setPreferredSize(new Dimension(120, 21)); |
|
95 |
logoutBtn.setPreferredSize(new Dimension(120, 21)); |
|
96 |
|
|
97 |
add(ipLabel); |
|
98 |
add(ipTextArea); |
|
99 |
add(portLabel); |
|
100 |
add(portTextArea); |
|
101 |
add(nameLabel); |
|
102 |
add(nameTextArea); |
|
103 |
add(passwordLabel); |
|
104 |
add(passwordTextArea); |
|
105 |
add(loginBtn); |
|
106 |
add(logoutBtn); |
|
107 |
|
|
108 |
// 登陆 |
|
109 |
loginBtn.addActionListener(new ActionListener() { |
|
110 |
@Override |
|
111 |
public void actionPerformed(ActionEvent arg0) { |
|
112 |
login(); |
|
113 |
} |
|
114 |
}); |
|
115 |
|
|
116 |
// 登出 |
|
117 |
logoutBtn.addActionListener(new ActionListener() { |
|
118 |
@Override |
|
119 |
public void actionPerformed(ActionEvent arg0) { |
|
120 |
logout(); |
|
121 |
} |
|
122 |
}); |
|
123 |
} |
|
124 |
} |
|
125 |
|
|
126 |
/* |
|
127 |
* 配置设备面板 |
|
128 |
*/ |
|
129 |
private class ConfigDevicePanel extends JPanel { |
|
130 |
private static final long serialVersionUID = 1L; |
|
131 |
|
|
132 |
public ConfigDevicePanel() { |
|
133 |
BorderEx.set(this, Res.string().getDeviceConfig(), 2); |
|
134 |
setLayout(new FlowLayout()); |
|
135 |
|
|
136 |
enableCheckBox = new JCheckBox(Res.string().getEnable()); |
|
137 |
JLabel nullLabel = new JLabel(); |
|
138 |
JLabel autoRegisterIpLabel = new JLabel(Res.string().getRegisterAddress(), JLabel.CENTER); |
|
139 |
JLabel autoRegisterPortLabel = new JLabel(Res.string().getRegisterPort(), JLabel.CENTER); |
|
140 |
JLabel deviceIdLabel = new JLabel(Res.string().getDeviceID(), JLabel.CENTER); |
|
141 |
|
|
142 |
enableCheckBox.setPreferredSize(new Dimension(80, 21)); |
|
143 |
nullLabel.setPreferredSize(new Dimension(120, 21)); |
|
144 |
autoRegisterIpLabel.setPreferredSize(new Dimension(100, 21)); |
|
145 |
autoRegisterPortLabel.setPreferredSize(new Dimension(100, 21)); |
|
146 |
deviceIdLabel.setPreferredSize(new Dimension(100, 21)); |
|
147 |
|
|
148 |
autoRegisterIpTextField = new JTextField(); |
|
149 |
autoRegisterPortTextField = new JTextField(); |
|
150 |
deviceIdTextField = new JTextField(); |
|
151 |
|
|
152 |
autoRegisterIpTextField.setPreferredSize(new Dimension(140, 21)); |
|
153 |
autoRegisterPortTextField.setPreferredSize(new Dimension(140, 21)); |
|
154 |
deviceIdTextField.setPreferredSize(new Dimension(140, 21)); |
|
155 |
|
|
156 |
getBtn = new JButton(Res.string().getGet()); |
|
157 |
setBtn = new JButton(Res.string().getSet()); |
|
158 |
|
|
159 |
getBtn.setPreferredSize(new Dimension(120, 21)); |
|
160 |
setBtn.setPreferredSize(new Dimension(120, 21)); |
|
161 |
|
|
162 |
add(enableCheckBox); |
|
163 |
add(nullLabel); |
|
164 |
add(autoRegisterIpLabel); |
|
165 |
add(autoRegisterIpTextField); |
|
166 |
add(autoRegisterPortLabel); |
|
167 |
add(autoRegisterPortTextField); |
|
168 |
add(deviceIdLabel); |
|
169 |
add(deviceIdTextField); |
|
170 |
add(getBtn); |
|
171 |
add(setBtn); |
|
172 |
|
|
173 |
enableCheckBox.setSelected(true); |
|
174 |
enableCheckBox.setEnabled(false); |
|
175 |
getBtn.setEnabled(false); |
|
176 |
setBtn.setEnabled(false); |
|
177 |
autoRegisterIpTextField.setEnabled(false); |
|
178 |
autoRegisterPortTextField.setEnabled(false); |
|
179 |
deviceIdTextField.setEnabled(false); |
|
180 |
|
|
181 |
// 获取 |
|
182 |
getBtn.addActionListener(new ActionListener() { |
|
183 |
@Override |
|
184 |
public void actionPerformed(ActionEvent arg0) { |
|
185 |
executorService.execute(new Runnable() { |
|
186 |
@Override |
|
187 |
public void run() { |
|
188 |
getBtn.setEnabled(false); |
|
189 |
} |
|
190 |
}); |
|
191 |
|
|
192 |
executorService.execute(new Runnable() { |
|
193 |
@Override |
|
194 |
public void run() { |
|
195 |
getConfig(); |
|
196 |
} |
|
197 |
}); |
|
198 |
} |
|
199 |
}); |
|
200 |
|
|
201 |
// 设置 |
|
202 |
setBtn.addActionListener(new ActionListener() { |
|
203 |
@Override |
|
204 |
public void actionPerformed(ActionEvent arg0) { |
|
205 |
executorService.execute(new Runnable() { |
|
206 |
@Override |
|
207 |
public void run() { |
|
208 |
setBtn.setEnabled(false); |
|
209 |
} |
|
210 |
}); |
|
211 |
|
|
212 |
executorService.execute(new Runnable() { |
|
213 |
@Override |
|
214 |
public void run() { |
|
215 |
setConfig(); |
|
216 |
} |
|
217 |
}); |
|
218 |
} |
|
219 |
}); |
|
220 |
} |
|
221 |
} |
|
222 |
|
|
223 |
// 登陆 |
|
224 |
private void login() { |
|
225 |
if(loginDevicePanel.checkLoginText()) { |
|
226 |
if(LoginModule.login(loginDevicePanel.ipTextArea.getText(), |
|
227 |
Integer.parseInt(loginDevicePanel.portTextArea.getText()), |
|
228 |
loginDevicePanel.nameTextArea.getText(), |
|
229 |
new String(loginDevicePanel.passwordTextArea.getPassword()))) { |
|
230 |
loginDevicePanel.setButtonEnable(true); |
|
231 |
enableCheckBox.setEnabled(true); |
|
232 |
getBtn.setEnabled(true); |
|
233 |
setBtn.setEnabled(true); |
|
234 |
autoRegisterIpTextField.setEnabled(true); |
|
235 |
autoRegisterPortTextField.setEnabled(true); |
|
236 |
deviceIdTextField.setEnabled(true); |
|
237 |
} else { |
|
238 |
JOptionPane.showMessageDialog(null, Res.string().getLoginFailed() + ", " + ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE); |
|
239 |
} |
|
240 |
} |
|
241 |
} |
|
242 |
|
|
243 |
// 登出 |
|
244 |
private void logout() { |
|
245 |
LoginModule.logout(); |
|
246 |
loginDevicePanel.setButtonEnable(false); |
|
247 |
enableCheckBox.setEnabled(false); |
|
248 |
getBtn.setEnabled(false); |
|
249 |
setBtn.setEnabled(false); |
|
250 |
autoRegisterIpTextField.setEnabled(false); |
|
251 |
autoRegisterPortTextField.setEnabled(false); |
|
252 |
deviceIdTextField.setEnabled(false); |
|
253 |
autoRegisterIpTextField.setText(""); |
|
254 |
autoRegisterPortTextField.setText(""); |
|
255 |
deviceIdTextField.setText(""); |
|
256 |
} |
|
257 |
|
|
258 |
// 获取 |
|
259 |
private void getConfig() { |
|
260 |
info = AutoRegisterModule.getDVRIPConfig(LoginModule.m_hLoginHandle); |
|
261 |
if(info == null) { |
|
262 |
autoRegisterIpTextField.setText(""); |
|
263 |
autoRegisterPortTextField.setText(""); |
|
264 |
deviceIdTextField.setText(""); |
|
265 |
JOptionPane.showMessageDialog(null, Res.string().getGet() + Res.string().getFailed() + ", " + ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE); |
|
266 |
} else { |
|
267 |
if(info.stuRegisters[0].bEnable == 1) { |
|
268 |
enableCheckBox.setSelected(true); |
|
269 |
} else { |
|
270 |
enableCheckBox.setSelected(false); |
|
271 |
} |
|
272 |
|
|
273 |
autoRegisterIpTextField.setText(new String(info.stuRegisters[0].stuServers[0].szAddress).trim()); |
|
274 |
autoRegisterPortTextField.setText(String.valueOf(info.stuRegisters[0].stuServers[0].nPort)); |
|
275 |
try { |
|
276 |
deviceIdTextField.setText(new String(info.stuRegisters[0].szDeviceID, "GBK").trim()); |
|
277 |
} catch (UnsupportedEncodingException e) { |
|
278 |
e.printStackTrace(); |
|
279 |
} |
|
280 |
|
|
281 |
JOptionPane.showMessageDialog(null, Res.string().getSucceed(), Res.string().getPromptMessage(), JOptionPane.INFORMATION_MESSAGE); |
|
282 |
} |
|
283 |
getBtn.setEnabled(true); |
|
284 |
} |
|
285 |
|
|
286 |
/** |
|
287 |
* 设置(在获取的基础上配置) |
|
288 |
*/ |
|
289 |
private void setConfig() { |
|
290 |
info = AutoRegisterModule.getDVRIPConfig(LoginModule.m_hLoginHandle); |
|
291 |
if(autoRegisterIpTextField.getText().equals("")) { |
|
292 |
setBtn.setEnabled(true); |
|
293 |
JOptionPane.showMessageDialog(null, Res.string().getInput() + Res.string().getRegisterAddress(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE); |
|
294 |
return; |
|
295 |
} |
|
296 |
|
|
297 |
if(autoRegisterPortTextField.getText().equals("")) { |
|
298 |
setBtn.setEnabled(true); |
|
299 |
JOptionPane.showMessageDialog(null, Res.string().getInput() + Res.string().getRegisterPort(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE); |
|
300 |
return; |
|
301 |
} |
|
302 |
|
|
303 |
if(deviceIdTextField.getText().equals("")) { |
|
304 |
setBtn.setEnabled(true); |
|
305 |
JOptionPane.showMessageDialog(null, Res.string().getInput() + Res.string().getDeviceID(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE); |
|
306 |
return; |
|
307 |
} |
|
308 |
|
|
309 |
// win下,中文需要转换为GBK |
|
310 |
byte[] deviceId = null; |
|
311 |
try { |
|
312 |
deviceId = deviceIdTextField.getText().getBytes("GBK"); |
|
313 |
} catch (UnsupportedEncodingException e) { |
|
314 |
e.printStackTrace(); |
|
315 |
} |
|
316 |
|
|
317 |
System.out.println("登录句柄"+LoginModule.m_hLoginHandle + "是否启用" + enableCheckBox.isSelected() + "ip" + autoRegisterIpTextField.getText() + "端口" + autoRegisterPortTextField.getText() + "设备id" + new String(deviceId)); |
|
318 |
|
|
319 |
if(AutoRegisterModule.setDVRIPConfig(LoginModule.m_hLoginHandle, |
|
320 |
enableCheckBox.isSelected(), |
|
321 |
autoRegisterIpTextField.getText(), |
|
322 |
Integer.parseInt(autoRegisterPortTextField.getText()), |
|
323 |
deviceId, |
|
324 |
info)) { |
|
325 |
JOptionPane.showMessageDialog(null, Res.string().getSucceed(), Res.string().getPromptMessage(), JOptionPane.INFORMATION_MESSAGE); |
|
326 |
} else { |
|
327 |
JOptionPane.showMessageDialog(null, Res.string().getSet() + Res.string().getFailed() + ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE); |
|
328 |
} |
|
329 |
|
|
330 |
setBtn.setEnabled(true); |
|
331 |
} |
|
332 |
|
|
333 |
private LoginDevicePanel loginDevicePanel; |
|
334 |
|
|
335 |
private JTextField autoRegisterIpTextField; |
|
336 |
private JTextField autoRegisterPortTextField; |
|
337 |
private JTextField deviceIdTextField; |
|
338 |
private JCheckBox enableCheckBox; |
|
339 |
private JButton getBtn; |
|
340 |
private JButton setBtn; |
|
341 |
} |