dongyukun
2024-11-05 e8ad669f7c97d45cd23630dc101180a130d6c17e
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package com.netsdk.demo.frame;
 
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
 
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
 
import com.sun.jna.Pointer;
 
import com.netsdk.common.BorderEx;
import com.netsdk.common.DateChooserJButton;
import com.netsdk.common.FunctionList;
import com.netsdk.common.LoginPanel;
import com.netsdk.common.Res;
import com.netsdk.demo.module.DeviceControlModule;
import com.netsdk.demo.module.LoginModule;
import com.netsdk.lib.NetSDKLib;
import com.netsdk.lib.NetSDKLib.LLong;
import com.netsdk.lib.ToolKits;
 
/**
 * Device Control Demo
 */
class DeviceControlFrame extends JFrame {
    private static final long serialVersionUID = 1L;
    
    // device disconnect callback instance
    private DisConnect disConnect = new DisConnect(); 
    
    // device control frame (this)
    private static JFrame frame = new JFrame();
    
    public DeviceControlFrame() {
        setTitle(Res.string().getDeviceControl());
        setLayout(new BorderLayout());
        pack();
        setSize(550, 350);
        setResizable(false);
        setLocationRelativeTo(null);
        LoginModule.init(disConnect, null);
        
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        } 
     
        loginPanel = new DeviceControlLoginPanel();
        deviceCtlPanel = new DeviceControlPanel();
        
        add(loginPanel, BorderLayout.NORTH);
        add(deviceCtlPanel, 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().getDeviceControl() + " : " + Res.string().getOnline());
                    }
                }
            }
        });
        
        loginPanel.addLogoutBtnActionListener(new ActionListener() {        
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.setTitle(Res.string().getDeviceControl());
                logout();    
            }
        });
        
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                LoginModule.logout();
                LoginModule.cleanup();
                dispose();    
                
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        FunctionList demo = new FunctionList();
                        demo.setVisible(true);
                    }
                });
            }
        });
    }
    
    /////////////////function///////////////////
    // device disconnect callback class 
    // set it's instance by call CLIENT_Init, when device disconnect sdk will call it.
    private class DisConnect implements NetSDKLib.fDisConnect {
        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);
                    frame.setTitle(Res.string().getDeviceControl());
                    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);
                deviceCtlPanel.setButtonEnabled(true);
                                
        }else {
            JOptionPane.showMessageDialog(null, Res.string().getLoginFailed() + ", " + ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
            return false;
        }
        
        return true;
    }
 
    public void logout() {
        
        LoginModule.logout();
        
        loginPanel.setButtonEnable(false);
        deviceCtlPanel.resetButtonEnabled();
    }
    
    private class DeviceControlPanel extends JPanel {
        private static final long serialVersionUID = 1L;
        
        public DeviceControlPanel() {
            BorderEx.set(this, Res.string().getDeviceControl(), 2);
            setLayout(new BorderLayout());
            setPreferredSize(new Dimension(350, 220));
            setResizable(false);
            JLabel nullLable = new JLabel();
            
            currentTimeCheckBox = new JCheckBox(Res.string().getCurrentTime());
            
            getDateChooser = new DateChooserJButton();
            setDateChooser = new DateChooserJButton(2000, 2037);
            
            rebootBtn = new JButton(Res.string().getReboot());
            getTimeBtn = new JButton(Res.string().getGetTime());
            setTimeBtn = new JButton(Res.string().getSetTime());
            
            nullLable.setPreferredSize(currentTimeCheckBox.getPreferredSize());
            getDateChooser.setPreferredSize(new Dimension(150, 20));
            setDateChooser.setPreferredSize(new Dimension(150, 20));
            rebootBtn.setPreferredSize(new Dimension(100, 20));
            getTimeBtn.setPreferredSize(new Dimension(100, 20));
            setTimeBtn.setPreferredSize(new Dimension(100, 20));
            
            JPanel rebootPanel = new JPanel();
            BorderEx.set(rebootPanel, Res.string().getDeviceReboot(), 2);
            
            rebootPanel.add(rebootBtn);
            
            JPanel timePanel = new JPanel(new GridLayout(2,1));
            BorderEx.set(timePanel, Res.string().getSyncTime(), 2);
            
            JPanel getPanel = new JPanel();
            JPanel setPanel = new JPanel();
            
            getPanel.add(nullLable);
            getPanel.add(getDateChooser);
            getPanel.add(getTimeBtn);
            
            setPanel.add(currentTimeCheckBox);
            setPanel.add(setDateChooser);
            setPanel.add(setTimeBtn);
            
            timePanel.add(getPanel);
            timePanel.add(setPanel);
            
            JSplitPane splitPane = new JSplitPane();
            splitPane.setDividerSize(0);
            splitPane.setBorder(null);
            splitPane.add(rebootPanel, JSplitPane.LEFT);
            splitPane.add(timePanel, JSplitPane.RIGHT);
             add(splitPane);
            
            getDateChooser.setEnabled(false);
            setButtonEnabled(false);
 
            rebootBtn.addActionListener(new ActionListener() {            
                @Override
                public void actionPerformed(ActionEvent e) {
                    
                    OptionDialog optionDialog = new OptionDialog();
                    optionDialog.setVisible(true);
                }
            });
            
            getTimeBtn.addActionListener(new ActionListener() {            
                @Override
                public void actionPerformed(ActionEvent e) {
                    String date = DeviceControlModule.getTime();
                    if (date == null) {
                        JOptionPane.showMessageDialog(null, ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
                    }else {
                        getDateChooser.setText(date);
                    }
                    
                }
            });
            
            setTimeBtn.addActionListener(new ActionListener() {            
                @Override
                public void actionPerformed(ActionEvent e) {
                    String date = null;
                    if (!currentTimeCheckBox.isSelected()) {
                        date = setDateChooser.getText();
                    }
                    if (!DeviceControlModule.setTime(date)) {
                        JOptionPane.showMessageDialog(null, ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
                    }
                    else {
                        JOptionPane.showMessageDialog(null, Res.string().getOperateSuccess(), Res.string().getPromptMessage(), JOptionPane.PLAIN_MESSAGE);
                    }
                }
            });
            
            currentTimeCheckBox.addItemListener(new ItemListener() {
                @Override
                public void itemStateChanged(ItemEvent e) {
                    JCheckBox jcb = (JCheckBox)e.getItem();
                    if (jcb.isSelected()) {
                        setDateChooser.setEnabled(false);
                    }else {
                        setDateChooser.setEnabled(true);
                    }
                }
            });
            
        }
        
        public void setButtonEnabled(boolean b) {
            
            currentTimeCheckBox.setEnabled(b);
            setDateChooser.setEnabled(b);
            rebootBtn.setEnabled(b);
            getTimeBtn.setEnabled(b);
            setTimeBtn.setEnabled(b);
        }
        
        public void resetButtonEnabled() {
            currentTimeCheckBox.setSelected(false);
            setButtonEnabled(false);
        }
        
        private class OptionDialog extends JDialog {
            private static final long serialVersionUID = 1L;
 
            public OptionDialog() {
                setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                pack();
                setSize(250, 100);
                setLocationRelativeTo(null);
                setModal(true);
                setTitle(Res.string().getDeviceReboot());
                
                JLabel messageLable = new JLabel(Res.string().getRebootTips()); 
                confirmBtn = new JButton(Res.string().getConfirm());
                cancelBtn = new JButton(Res.string().getCancel());
                
                JPanel messagePanel = new JPanel();
                messagePanel.add(messageLable);
                
                JPanel btnPanel = new JPanel();
                btnPanel.add(cancelBtn);
                btnPanel.add(confirmBtn);
                
                add(messagePanel, BorderLayout.NORTH);
                add(btnPanel, BorderLayout.CENTER);
                
                addListener();
                
            }
            
            private void addListener() {
              confirmBtn.addActionListener(new ActionListener() {
 
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        cancelBtn.setEnabled(false);
                        if (!DeviceControlModule.reboot()) {
                            JOptionPane.showMessageDialog(null, ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
                        }
                        else {
                            JOptionPane.showMessageDialog(null, Res.string().getOperateSuccess(), Res.string().getPromptMessage(), JOptionPane.PLAIN_MESSAGE);
                        }
                        dispose();
                    }
                });
                
                cancelBtn.addActionListener(new ActionListener() {
 
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        dispose();
                    }
                });
            }
            
            private JButton confirmBtn;
            private JButton cancelBtn;
            
        }
        
        private JButton rebootBtn;
        private DateChooserJButton getDateChooser;
        private JButton getTimeBtn;
        private JCheckBox currentTimeCheckBox;
        private DateChooserJButton setDateChooser;
        private JButton setTimeBtn; 
    }
 
    private class DeviceControlLoginPanel extends LoginPanel {
    
        private static final long serialVersionUID = 1L;
 
        public DeviceControlLoginPanel() {
            setLayout(new GridLayout(3, 1));
            removeAll();
            JPanel ipPanel = new JPanel();
            JPanel userPanel = new JPanel();
            JPanel btnPanel = new JPanel();
            
            resetSize();
    
            ipPanel.add(ipLabel);
            ipPanel.add(ipTextArea);
            ipPanel.add(portLabel);
            ipPanel.add(portTextArea);
            
            userPanel.add(nameLabel);
            userPanel.add(nameTextArea);
            userPanel.add(passwordLabel);
            userPanel.add(passwordTextArea);
                
            btnPanel.add(loginBtn);
            btnPanel.add(new JLabel("  "));
            btnPanel.add(logoutBtn);
 
            add(ipPanel);
            add(userPanel);
            add(btnPanel);
        }
        
        private void resetSize() {
            
            ipLabel.setPreferredSize(new Dimension(70, 25));
            portLabel.setPreferredSize(new Dimension(70, 25));
            nameLabel.setText(Res.string().getUserName());
            nameLabel.setPreferredSize(new Dimension(70, 25));
            passwordLabel.setPreferredSize(new Dimension(70, 25));
 
            loginBtn.setPreferredSize(new Dimension(100, 20));
            logoutBtn.setPreferredSize(new Dimension(100, 20));
            
            ipTextArea.setPreferredSize(new Dimension(100, 20));
            portTextArea.setPreferredSize(new Dimension(100, 20));
            nameTextArea.setPreferredSize(new Dimension(100, 20));
            passwordTextArea.setPreferredSize(new Dimension(100, 20));
        }
    }
    
    private DeviceControlLoginPanel loginPanel;
    
    private DeviceControlPanel deviceCtlPanel;
    
}
 
public class DeviceControl {  
    public static void main(String[] args) {    
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {    
                DeviceControlFrame demo = new DeviceControlFrame();
                demo.setVisible(true);
            }
        });        
    }
};