提交 | 用户 | 时间
|
149dd0
|
1 |
package com.iailab.netsdk.demo.frame; |
H |
2 |
|
|
3 |
import java.awt.*; |
|
4 |
import java.awt.event.*; |
|
5 |
import java.text.SimpleDateFormat; |
|
6 |
import java.util.*; |
|
7 |
|
|
8 |
import javax.swing.*; |
|
9 |
import javax.swing.table.*; |
|
10 |
|
|
11 |
import com.sun.jna.NativeLong; |
|
12 |
import com.sun.jna.Pointer; |
|
13 |
|
|
14 |
import com.iailab.netsdk.common.BorderEx; |
|
15 |
import com.iailab.netsdk.common.FunctionList; |
|
16 |
import com.iailab.netsdk.common.LoginPanel; |
|
17 |
import com.iailab.netsdk.common.Res; |
|
18 |
import com.iailab.netsdk.demo.module.AlarmListenModule; |
|
19 |
import com.iailab.netsdk.demo.module.LoginModule; |
|
20 |
import com.iailab.netsdk.lib.NetSDKLib; |
|
21 |
import com.iailab.netsdk.lib.NetSDKLib.LLong; |
|
22 |
import com.iailab.netsdk.lib.ToolKits; |
|
23 |
|
|
24 |
/** |
|
25 |
* Alarm Listen Demo |
|
26 |
*/ |
|
27 |
class AlarmListenFrame extends JFrame{ |
|
28 |
private static final long serialVersionUID = 1L; |
|
29 |
|
|
30 |
// device disconnect callback instance |
|
31 |
private DisConnect disConnect = new DisConnect(); |
|
32 |
|
|
33 |
// device reconnect callback instance |
|
34 |
private static HaveReConnect haveReConnect = new HaveReConnect(); |
|
35 |
|
|
36 |
// alarm listen frame (this) |
|
37 |
private static JFrame frame = new JFrame(); |
|
38 |
|
|
39 |
private Component target = this; |
|
40 |
|
|
41 |
// alarm event info list |
|
42 |
Vector<AlarmEventInfo> data = new Vector<AlarmEventInfo>(); |
|
43 |
|
|
44 |
public AlarmListenFrame() { |
|
45 |
setTitle(Res.string().getAlarmListen()); |
|
46 |
setLayout(new BorderLayout()); |
|
47 |
pack(); |
|
48 |
setSize(800, 530); |
|
49 |
setResizable(false); |
|
50 |
setLocationRelativeTo(null); |
|
51 |
LoginModule.init(disConnect, haveReConnect); |
|
52 |
|
|
53 |
try { |
|
54 |
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); |
|
55 |
} catch (Exception e) { |
|
56 |
e.printStackTrace(); |
|
57 |
} |
|
58 |
|
|
59 |
loginPanel = new LoginPanel(); |
|
60 |
alarmListenPanel = new AlarmListenPanel(); |
|
61 |
showAlarmPanel = new ShowAlarmEventPanel(); |
|
62 |
|
|
63 |
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, loginPanel, alarmListenPanel); |
|
64 |
splitPane.setDividerSize(0); |
|
65 |
add(splitPane, BorderLayout.NORTH); |
|
66 |
add(showAlarmPanel, BorderLayout.CENTER); |
|
67 |
|
|
68 |
loginPanel.addLoginBtnActionListener(new ActionListener() { |
|
69 |
@Override |
|
70 |
public void actionPerformed(ActionEvent e) { |
|
71 |
if(loginPanel.checkLoginText()) { |
|
72 |
if(login()) { |
|
73 |
frame = ToolKits.getFrame(e); |
|
74 |
frame.setTitle(Res.string().getAlarmListen() + " : " + Res.string().getOnline()); |
|
75 |
} |
|
76 |
} |
|
77 |
} |
|
78 |
}); |
|
79 |
|
|
80 |
loginPanel.addLogoutBtnActionListener(new ActionListener() { |
|
81 |
@Override |
|
82 |
public void actionPerformed(ActionEvent e) { |
|
83 |
frame.setTitle(Res.string().getAlarmListen()); |
|
84 |
logout(); |
|
85 |
} |
|
86 |
}); |
|
87 |
|
|
88 |
addWindowListener(new WindowAdapter() { |
|
89 |
public void windowClosing(WindowEvent e) { |
|
90 |
AlarmListenModule.stopListen(); |
|
91 |
LoginModule.logout(); |
|
92 |
LoginModule.cleanup(); |
|
93 |
dispose(); |
|
94 |
|
|
95 |
SwingUtilities.invokeLater(new Runnable() { |
|
96 |
public void run() { |
|
97 |
FunctionList demo = new FunctionList(); |
|
98 |
demo.setVisible(true); |
|
99 |
} |
|
100 |
}); |
|
101 |
} |
|
102 |
}); |
|
103 |
} |
|
104 |
|
|
105 |
/////////////////function/////////////////// |
|
106 |
// device disconnect callback class |
|
107 |
// set it's instance by call CLIENT_Init, when device disconnect sdk will call it. |
|
108 |
private class DisConnect implements NetSDKLib.fDisConnect { |
|
109 |
public DisConnect() { } |
|
110 |
|
|
111 |
public void invoke(LLong m_hLoginHandle, String pchDVRIP, int nDVRPort, Pointer dwUser) { |
|
112 |
System.out.printf("Device[%s] Port[%d] DisConnect!\n", pchDVRIP, nDVRPort); |
|
113 |
|
|
114 |
SwingUtilities.invokeLater(new Runnable() { |
|
115 |
public void run() { |
|
116 |
frame.setTitle(Res.string().getAlarmListen() + " : " + Res.string().getDisConnectReconnecting()); |
|
117 |
} |
|
118 |
}); |
|
119 |
} |
|
120 |
} |
|
121 |
|
|
122 |
// device reconnect(success) callback class |
|
123 |
// set it's instance by call CLIENT_SetAutoReconnect, when device reconnect success sdk will call it. |
|
124 |
private static class HaveReConnect implements NetSDKLib.fHaveReConnect { |
|
125 |
public void invoke(LLong m_hLoginHandle, String pchDVRIP, int nDVRPort, Pointer dwUser) { |
|
126 |
System.out.printf("ReConnect Device[%s] Port[%d]\n", pchDVRIP, nDVRPort); |
|
127 |
|
|
128 |
SwingUtilities.invokeLater(new Runnable() { |
|
129 |
public void run() { |
|
130 |
frame.setTitle(Res.string().getAlarmListen() + " : " + Res.string().getOnline()); |
|
131 |
} |
|
132 |
}); |
|
133 |
} |
|
134 |
} |
|
135 |
|
|
136 |
public boolean login() { |
|
137 |
if(LoginModule.login(loginPanel.ipTextArea.getText(), |
|
138 |
Integer.parseInt(loginPanel.portTextArea.getText()), |
|
139 |
loginPanel.nameTextArea.getText(), |
|
140 |
new String(loginPanel.passwordTextArea.getPassword()))) { |
|
141 |
|
|
142 |
loginPanel.setButtonEnable(true); |
|
143 |
alarmListenPanel.setButtonEnable(true); |
|
144 |
|
|
145 |
} else { |
|
146 |
JOptionPane.showMessageDialog(null, Res.string().getLoginFailed() + ", " + ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE); |
|
147 |
return false; |
|
148 |
} |
|
149 |
return true; |
|
150 |
} |
|
151 |
|
|
152 |
public void logout() { |
|
153 |
AlarmListenModule.stopListen(); |
|
154 |
LoginModule.logout(); |
|
155 |
|
|
156 |
loginPanel.setButtonEnable(false); |
|
157 |
alarmListenPanel.initButtonEnable(); |
|
158 |
showAlarmPanel.clean(); |
|
159 |
} |
|
160 |
|
|
161 |
public Vector<String> convertAlarmEventInfo(AlarmEventInfo alarmEventInfo) { |
|
162 |
Vector<String> vector = new Vector<String>(); |
|
163 |
|
|
164 |
vector.add(String.valueOf(alarmEventInfo.id)); |
|
165 |
vector.add(formatDate(alarmEventInfo.date)); |
|
166 |
vector.add(String.valueOf(alarmEventInfo.chn)); |
|
167 |
String status = null; |
|
168 |
if (alarmEventInfo.status == AlarmStatus.ALARM_START) { |
|
169 |
status = Res.string().getStart(); |
|
170 |
}else { |
|
171 |
status = Res.string().getStop(); |
|
172 |
} |
|
173 |
vector.add(alarmMessageMap.get(alarmEventInfo.type) + status); |
|
174 |
|
|
175 |
return vector; |
|
176 |
} |
|
177 |
|
|
178 |
private String formatDate(Date date) { |
|
179 |
final SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
180 |
return simpleDate.format(date); |
|
181 |
} |
|
182 |
|
|
183 |
private fAlarmDataCB cbMessage = new fAlarmDataCB(); |
|
184 |
// alarm listen data callback |
|
185 |
private class fAlarmDataCB implements NetSDKLib.fMessCallBack{ |
|
186 |
private final EventQueue eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue(); |
|
187 |
|
|
188 |
@Override |
|
189 |
public boolean invoke(int lCommand, LLong lLoginID, |
|
190 |
Pointer pStuEvent, int dwBufLen, String strDeviceIP, |
|
191 |
NativeLong nDevicePort, Pointer dwUser) { |
|
192 |
|
|
193 |
switch (lCommand) { |
|
194 |
case NetSDKLib.NET_ALARM_ALARM_EX: |
|
195 |
case NetSDKLib.NET_MOTION_ALARM_EX: |
|
196 |
case NetSDKLib.NET_VIDEOLOST_ALARM_EX: |
|
197 |
case NetSDKLib.NET_SHELTER_ALARM_EX: |
|
198 |
case NetSDKLib.NET_DISKFULL_ALARM_EX: |
|
199 |
case NetSDKLib.NET_DISKERROR_ALARM_EX: { |
|
200 |
byte []alarm = new byte[dwBufLen]; |
|
201 |
pStuEvent.read(0, alarm, 0, dwBufLen); |
|
202 |
for (int i = 0; i < dwBufLen; i++) { |
|
203 |
if (alarm[i] == 1) { |
|
204 |
AlarmEventInfo alarmEventInfo = new AlarmEventInfo(i, lCommand, AlarmStatus.ALARM_START); |
|
205 |
if (!data.contains(alarmEventInfo)) { |
|
206 |
data.add(alarmEventInfo); |
|
207 |
eventQueue.postEvent(new AlarmListenEvent(target, alarmEventInfo)); |
|
208 |
} |
|
209 |
}else { |
|
210 |
AlarmEventInfo alarmEventInfo = new AlarmEventInfo(i, lCommand, AlarmStatus.ALARM_STOP); |
|
211 |
if (data.remove(alarmEventInfo)) { |
|
212 |
eventQueue.postEvent(new AlarmListenEvent(target, alarmEventInfo)); |
|
213 |
} |
|
214 |
} |
|
215 |
} |
|
216 |
break; |
|
217 |
} |
|
218 |
default: |
|
219 |
break; |
|
220 |
|
|
221 |
} |
|
222 |
|
|
223 |
return true; |
|
224 |
} |
|
225 |
|
|
226 |
} |
|
227 |
|
|
228 |
// alarm listen event |
|
229 |
class AlarmListenEvent extends AWTEvent { |
|
230 |
private static final long serialVersionUID = 1L; |
|
231 |
public static final int EVENT_ID = AWTEvent.RESERVED_ID_MAX + 1; |
|
232 |
|
|
233 |
private AlarmEventInfo alarmEventInfo; |
|
234 |
|
|
235 |
public AlarmListenEvent(Object target, |
|
236 |
AlarmEventInfo alarmEventInfo) { |
|
237 |
super(target,EVENT_ID); |
|
238 |
|
|
239 |
this.alarmEventInfo = alarmEventInfo; |
|
240 |
++AlarmEventInfo.index; |
|
241 |
this.alarmEventInfo.id = AlarmEventInfo.index; |
|
242 |
} |
|
243 |
|
|
244 |
public AlarmEventInfo getAlarmEventInfo() { |
|
245 |
return alarmEventInfo; |
|
246 |
} |
|
247 |
} |
|
248 |
|
|
249 |
@Override |
|
250 |
protected void processEvent( AWTEvent event) { |
|
251 |
if ( event instanceof AlarmListenEvent) { |
|
252 |
AlarmEventInfo alarmEventInfo = ((AlarmListenEvent)event).getAlarmEventInfo(); |
|
253 |
showAlarmPanel.insert(alarmEventInfo); |
|
254 |
} else { |
|
255 |
super.processEvent(event); |
|
256 |
} |
|
257 |
} |
|
258 |
|
|
259 |
// alarm listen control panel |
|
260 |
private class AlarmListenPanel extends JPanel { |
|
261 |
private static final long serialVersionUID = 1L; |
|
262 |
|
|
263 |
public AlarmListenPanel() { |
|
264 |
BorderEx.set(this, Res.string().getAlarmListen(), 2); |
|
265 |
setLayout(new FlowLayout()); |
|
266 |
|
|
267 |
btnStartListen = new JButton(Res.string().getStartListen()); |
|
268 |
btnStopListen = new JButton(Res.string().getStopListen()); |
|
269 |
|
|
270 |
btnStartListen.setPreferredSize(new Dimension(150, 20)); |
|
271 |
btnStopListen.setPreferredSize(new Dimension(150, 20)); |
|
272 |
|
|
273 |
add(btnStartListen); |
|
274 |
add(new JLabel(" ")); |
|
275 |
add(btnStopListen); |
|
276 |
|
|
277 |
initButtonEnable(); |
|
278 |
|
|
279 |
btnStartListen.addActionListener(new ActionListener(){ |
|
280 |
@Override |
|
281 |
public void actionPerformed(ActionEvent e) { |
|
282 |
if (AlarmListenModule.startListen(cbMessage)) { |
|
283 |
setButtonEnable(false); |
|
284 |
}else { |
|
285 |
JOptionPane.showMessageDialog(null, Res.string().getAlarmListenFailed() + "," + ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE); |
|
286 |
} |
|
287 |
} |
|
288 |
}); |
|
289 |
|
|
290 |
btnStopListen.addActionListener(new ActionListener(){ |
|
291 |
@Override |
|
292 |
public void actionPerformed(ActionEvent e) { |
|
293 |
if (AlarmListenModule.stopListen()) { |
|
294 |
showAlarmPanel.clean(); |
|
295 |
setButtonEnable(true); |
|
296 |
}else { |
|
297 |
JOptionPane.showMessageDialog(null, ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE); |
|
298 |
} |
|
299 |
} |
|
300 |
}); |
|
301 |
} |
|
302 |
|
|
303 |
public void setButtonEnable(boolean b) { |
|
304 |
btnStartListen.setEnabled(b); |
|
305 |
btnStopListen.setEnabled(!b); |
|
306 |
} |
|
307 |
|
|
308 |
public void initButtonEnable() { |
|
309 |
btnStartListen.setEnabled(false); |
|
310 |
btnStopListen.setEnabled(false); |
|
311 |
} |
|
312 |
|
|
313 |
private JButton btnStartListen; |
|
314 |
private JButton btnStopListen; |
|
315 |
} |
|
316 |
|
|
317 |
// alarm listen event show panel |
|
318 |
private class ShowAlarmEventPanel extends JPanel { |
|
319 |
private static final long serialVersionUID = 1L; |
|
320 |
private final static int MIN_SHOW_LINES = 20; |
|
321 |
private final static int MAX_SHOW_LINES = 100; |
|
322 |
private int currentRowNums = 0; |
|
323 |
|
|
324 |
public ShowAlarmEventPanel() { |
|
325 |
BorderEx.set(this, Res.string().getShowAlarmEvent(), 2); |
|
326 |
setLayout(new BorderLayout()); |
|
327 |
|
|
328 |
Vector<String> columnNames = new Vector<String>(); |
|
329 |
columnNames.add(Res.string().getIndex()); // index |
|
330 |
columnNames.add(Res.string().getEventTime()); // event time |
|
331 |
columnNames.add(Res.string().getChannel()); // channel |
|
332 |
columnNames.add(Res.string().getAlarmMessage()); // alarm message |
|
333 |
|
|
334 |
tableModel = new DefaultTableModel(null, columnNames); |
|
335 |
table = new JTable(tableModel) { |
|
336 |
private static final long serialVersionUID = 1L; |
|
337 |
|
|
338 |
public boolean isCellEditable(int rowIndex, int columnIndex) { |
|
339 |
return false; |
|
340 |
} |
|
341 |
}; |
|
342 |
|
|
343 |
tableModel.setRowCount(MIN_SHOW_LINES); // set min show lines |
|
344 |
|
|
345 |
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
|
346 |
|
|
347 |
table.getColumnModel().getColumn(0).setPreferredWidth(90); |
|
348 |
table.getColumnModel().getColumn(1).setPreferredWidth(200); |
|
349 |
table.getColumnModel().getColumn(2).setPreferredWidth(80); |
|
350 |
table.getColumnModel().getColumn(3).setPreferredWidth(400); |
|
351 |
|
|
352 |
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); |
|
353 |
table.setAutoscrolls(false); |
|
354 |
|
|
355 |
table.getTableHeader().setReorderingAllowed(false); |
|
356 |
// table.getTableHeader().setResizingAllowed(false); |
|
357 |
|
|
358 |
JScrollPane scrollPane = new JScrollPane(table); |
|
359 |
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); |
|
360 |
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); |
|
361 |
add(scrollPane, BorderLayout.CENTER); |
|
362 |
} |
|
363 |
|
|
364 |
public void insert(AlarmEventInfo alarmEventInfo) { |
|
365 |
tableModel.insertRow(0,convertAlarmEventInfo(alarmEventInfo)); |
|
366 |
if (currentRowNums < MAX_SHOW_LINES) { |
|
367 |
++currentRowNums; |
|
368 |
} |
|
369 |
|
|
370 |
if (currentRowNums <= MIN_SHOW_LINES) { |
|
371 |
tableModel.setRowCount(MIN_SHOW_LINES); |
|
372 |
}else if (currentRowNums == MAX_SHOW_LINES) { |
|
373 |
tableModel.setRowCount(MAX_SHOW_LINES); |
|
374 |
} |
|
375 |
|
|
376 |
table.updateUI(); |
|
377 |
} |
|
378 |
|
|
379 |
public void clean() { |
|
380 |
currentRowNums = 0; |
|
381 |
data.clear(); |
|
382 |
AlarmEventInfo.index=0; |
|
383 |
tableModel.setRowCount(0); |
|
384 |
tableModel.setRowCount(MIN_SHOW_LINES); |
|
385 |
table.updateUI(); |
|
386 |
} |
|
387 |
|
|
388 |
private JTable table = null; |
|
389 |
private DefaultTableModel tableModel = null; |
|
390 |
} |
|
391 |
|
|
392 |
private static HashMap<Integer, String> alarmMessageMap = new HashMap<Integer, String>() { |
|
393 |
|
|
394 |
private static final long serialVersionUID = 1L; |
|
395 |
|
|
396 |
{ |
|
397 |
put(NetSDKLib.NET_ALARM_ALARM_EX, Res.string().getExternalAlarm()); |
|
398 |
put(NetSDKLib.NET_MOTION_ALARM_EX, Res.string().getMotionAlarm()); |
|
399 |
put(NetSDKLib.NET_VIDEOLOST_ALARM_EX, Res.string().getVideoLostAlarm()); |
|
400 |
put(NetSDKLib.NET_SHELTER_ALARM_EX, Res.string().getShelterAlarm()); |
|
401 |
put(NetSDKLib.NET_DISKFULL_ALARM_EX, Res.string().getDiskFullAlarm()); |
|
402 |
put(NetSDKLib.NET_DISKERROR_ALARM_EX, Res.string().getDiskErrorAlarm()); |
|
403 |
} |
|
404 |
}; |
|
405 |
|
|
406 |
private LoginPanel loginPanel; |
|
407 |
private AlarmListenPanel alarmListenPanel; |
|
408 |
private ShowAlarmEventPanel showAlarmPanel; |
|
409 |
|
|
410 |
enum AlarmStatus { |
|
411 |
ALARM_START, ALARM_STOP |
|
412 |
} |
|
413 |
|
|
414 |
// struct of alarm event |
|
415 |
static class AlarmEventInfo { |
|
416 |
public static long index = 0; |
|
417 |
public long id; |
|
418 |
public int chn; |
|
419 |
public int type; |
|
420 |
public Date date; |
|
421 |
public AlarmStatus status; |
|
422 |
|
|
423 |
public AlarmEventInfo(int chn, int type, AlarmStatus status) { |
|
424 |
this.chn = chn; |
|
425 |
this.type = type; |
|
426 |
this.status = status; |
|
427 |
this.date = new Date(); |
|
428 |
} |
|
429 |
|
|
430 |
@Override |
|
431 |
public boolean equals(Object o) { |
|
432 |
if (this == o) return true; |
|
433 |
if (o == null || getClass() != o.getClass()) return false; |
|
434 |
AlarmEventInfo showInfo = (AlarmEventInfo) o; |
|
435 |
return chn == showInfo.chn && type == showInfo.type; |
|
436 |
} |
|
437 |
} |
|
438 |
|
|
439 |
} |
|
440 |
|
|
441 |
public class AlarmListen { |
|
442 |
public static void main(String[] args) { |
|
443 |
SwingUtilities.invokeLater(new Runnable() { |
|
444 |
public void run() { |
|
445 |
AlarmListenFrame demo = new AlarmListenFrame(); |
|
446 |
demo.setVisible(true); |
|
447 |
} |
|
448 |
}); |
|
449 |
} |
|
450 |
}; |