fuliqi
2024-08-29 4163c93761115c7524ef74a557a1f5e01eafb429
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package com.ycl.api.YS.config.basic;
 
 
import com.ycl.api.YS.NetDemo;
import com.ycl.api.YS.lib.NetDEVSDKLib.NETDEV_TIME_CFG_S;
import com.ycl.api.YS.lib.NetDEVSDKLib.NETDEV_TIME_ZONE_E;
 
import javax.swing.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
 
/**
 *
 * @introduction System time
 * @description Support IPC/NVR/VMS
 */
public class SystemTime {
 
    /**
     *
     * @introduction Get the system time
     * @description Calling the interface of NETDEV_GetSystemTimeCfg
     *
     */
    public static void getSystemTime() {
        if(null == NetDemo.lpUserID){
            JOptionPane.showMessageDialog(null, "Please Login device first.error code"+NetDemo.netdevsdk.NETDEV_GetLastError());
            return;
        }
        NetDemo.textFieldBasicTime.setText(null);
        NetDemo.textFieldSetBasicSystemTime.setText(null);
            try {
                NetDemo.runflag=false;
                NETDEV_TIME_CFG_S stTimeCfg = new NETDEV_TIME_CFG_S();
                stTimeCfg.write();
                boolean bRet = NetDemo.netdevsdk.NETDEV_GetSystemTimeCfg(NetDemo.lpUserID, stTimeCfg);
                if(bRet != true)
                {
                    JOptionPane.showMessageDialog(null, "Get Time failed,error code"+NetDemo.netdevsdk.NETDEV_GetLastError());
                    System.out.printf("NETDEV_GetSystemTimeCfg failed:%d\n", NetDemo.netdevsdk.NETDEV_GetLastError());
                }else {
                    stTimeCfg.read();
                    NetDemo.jComboBoxBasicTimeZone.setSelectedIndex(stTimeCfg.dwTimeZone);
                    String strTime = "" + stTimeCfg.stTime.dwYear+"-";
                    if(stTimeCfg.stTime.dwMonth < 10) {
                        strTime+= "0";
                    }
                    strTime = strTime + stTimeCfg.stTime.dwMonth + "-";
                    if(stTimeCfg.stTime.dwDay < 10) {
                        strTime+= "0";
                    }
                    strTime = strTime + stTimeCfg.stTime.dwDay + " ";
                    if(stTimeCfg.stTime.dwHour < 10) {
                        strTime+= "0";
                    }
                    strTime = strTime + stTimeCfg.stTime.dwHour + ":";
                    if(stTimeCfg.stTime.dwMinute < 10) {
                        strTime+= "0";
                    }
                    strTime = strTime + stTimeCfg.stTime.dwMinute + ":";
 
                    if(stTimeCfg.stTime.dwSecond < 10) {
                        strTime+= "0";
                    }
                    strTime = strTime + stTimeCfg.stTime.dwSecond;
 
                    NetDemo.lastTargetId=NetDemo.jComboBoxBasicTimeZone.getItemAt(NetDemo.jComboBoxBasicTimeZone.getSelectedIndex()).substring(3, 9);
                    NetDemo.textFieldBasicTime.setText(strTime);
                    NetDemo.textFieldSetBasicSystemTime.setText(strTime);
                }
 
                NetDemo.lastTargetId=NetDemo.jComboBoxBasicTimeZone.getItemAt(NetDemo.jComboBoxBasicTimeZone.getSelectedIndex()).substring(3, 9);
                Thread.sleep(1001);
                NetDemo.runflag=true;
                autoAddTime();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    }
 
    /**
     *
     * @introduction Set system time
     * @description Calling the interface of NETDEV_SetSystemTimeCfg
     *
     */
    public static void setSystemTime() {
        if(null == NetDemo.lpUserID)
        {
            JOptionPane.showMessageDialog(null, "Please Login device first. error code"+NetDemo.netdevsdk.NETDEV_GetLastError());
            return;
        }
        NetDemo.runflag=false;
        NETDEV_TIME_CFG_S stTimeCfg = new NETDEV_TIME_CFG_S();
        String strTime = NetDemo.textFieldSetBasicSystemTime.getText();
        if(strTime==null||strTime.equals("")) {
            JOptionPane.showMessageDialog(null, "Time is null");
            return;
        }
        int getSTyear=0;
        int getSTmonth=0;
        int getSTday=0;
        int getSThour=0;
        int getSTminute=0;
        int getSTsecond=0;
        try {
            getSTyear=Integer.parseInt(strTime.substring(0, 4));
        }catch(Exception e) {
            JOptionPane.showMessageDialog(null, "Year format error");
            return;
        }
        try {
            getSTmonth= Integer.parseInt(strTime.substring(5, 7));
        }catch(Exception e) {
            JOptionPane.showMessageDialog(null, "Month format error");
            return;
        }
        try {
            getSTday= Integer.parseInt(strTime.substring(8, 10));
        }catch(Exception e) {
            JOptionPane.showMessageDialog(null, "Day format error");
            return;
        }
        try {
            getSThour= Integer.parseInt(strTime.substring(11, 13));
        }catch(Exception e) {
            JOptionPane.showMessageDialog(null, "Hour format error");
            return;
        }
        try {
            getSTminute= Integer.parseInt(strTime.substring(14, 16));
        }catch(Exception e) {
            JOptionPane.showMessageDialog(null, "Minute format error");
            return;
        }
        try {
            getSTsecond= Integer.parseInt(strTime.substring(17, 19));
        }catch(Exception e) {
            JOptionPane.showMessageDialog(null, "Second format error");
            return;
        }
 
        stTimeCfg.stTime.dwYear = getSTyear;
        stTimeCfg.stTime.dwMonth = getSTmonth;
        stTimeCfg.stTime.dwDay = getSTday;
        stTimeCfg.stTime.dwHour = getSThour;
        stTimeCfg.stTime.dwMinute = getSTminute;
        stTimeCfg.stTime.dwSecond = getSTsecond;
        stTimeCfg.dwTimeZone = NetDemo.jComboBoxBasicTimeZone.getSelectedIndex();
        stTimeCfg.write();
        System.out.println(stTimeCfg.stTime.dwHour);
 
        boolean bRet = NetDemo.netdevsdk.NETDEV_SetSystemTimeCfg(NetDemo.lpUserID, stTimeCfg);
        if(bRet != true)
        {
            JOptionPane.showMessageDialog(null, "Set time failed,error code"+NetDemo.netdevsdk.NETDEV_GetLastError());
            System.out.printf("NETDEV_SetSystemTimeCfg failed:%d\n", NetDemo.netdevsdk.NETDEV_GetLastError());
            return;
        }else {
            JOptionPane.showMessageDialog(null, "Set Success");
        }
    }
 
    /**
     *
     * @introduction Select timezone
     * @description
     *
     */
    public static void changeComboxBasicTime() {
        try {
            if(NetDemo.textFieldSetBasicSystemTime.getText()==null||NetDemo.textFieldSetBasicSystemTime.getText().equals("")) {
                String sourceTime=getCheck();
                String targetId=NetDemo.jComboBoxBasicTimeZone.getItemAt(NetDemo.jComboBoxBasicTimeZone.getSelectedIndex()).substring(3, 9);
                comboxBasicTime(sourceTime,targetId);
            }else {
                String getsourceTime=NetDemo.textFieldSetBasicSystemTime.getText();
                String targetId=NetDemo.jComboBoxBasicTimeZone.getItemAt(NetDemo.jComboBoxBasicTimeZone.getSelectedIndex()).substring(3, 9);
                comboxBasicTime(getsourceTime,targetId);
                NetDemo.lastTargetId=NetDemo.jComboBoxBasicTimeZone.getItemAt(NetDemo.jComboBoxBasicTimeZone.getSelectedIndex()).substring(3, 9);
            }
        }catch(Exception e) {
        }
    }
 
    /**
     *
     * @introduction Synchronization computer time
     * @description Calling the interface of NETDEV_SetSystemTimeCfg
     *
     */
    public static void synchronizationComputerTime() {
        if(null == NetDemo.lpUserID)
        {
            JOptionPane.showMessageDialog(null, "Please Login device first. error code"+NetDemo.netdevsdk.NETDEV_GetLastError());
            return;
        }
        NetDemo.runflag=false;
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String getComputerTime=sdf.format(new Date());
        NETDEV_TIME_CFG_S stTimeCfg = new NETDEV_TIME_CFG_S();
 
        if(getComputerTime==null||getComputerTime.equals("")) {
            JOptionPane.showMessageDialog(null, "Time is null");
            return;
        }
 
        int getSTyear=0;
        int getSTmonth=0;
        int getSTday=0;
        int getSThour=0;
        int getSTminute=0;
        int getSTsecond=0;
        try {
            getSTyear=Integer.parseInt(getComputerTime.substring(0, 4));
        }catch(Exception e) {
            JOptionPane.showMessageDialog(null, "Year format error");
            return;
        }
        try {
            getSTmonth= Integer.parseInt(getComputerTime.substring(5, 7));
        }catch(Exception e) {
            JOptionPane.showMessageDialog(null, "Month format error");
            return;
        }
        try {
            getSTday= Integer.parseInt(getComputerTime.substring(8, 10));
        }catch(Exception e) {
            JOptionPane.showMessageDialog(null, "Day format error");
            return;
        }
        try {
            getSThour= Integer.parseInt(getComputerTime.substring(11, 13));
        }catch(Exception e) {
            JOptionPane.showMessageDialog(null, "Hour format error");
            return;
        }
        try {
            getSTminute= Integer.parseInt(getComputerTime.substring(14, 16));
        }catch(Exception e) {
            JOptionPane.showMessageDialog(null, "Minute format error");
            return;
        }
        try {
            getSTsecond= Integer.parseInt(getComputerTime.substring(17, 19));
        }catch(Exception e) {
            JOptionPane.showMessageDialog(null, "second format error");
            return;
        }
 
        stTimeCfg.stTime.dwYear = getSTyear;
        stTimeCfg.stTime.dwMonth = getSTmonth;
        stTimeCfg.stTime.dwDay = getSTday;
        stTimeCfg.stTime.dwHour = getSThour;
        stTimeCfg.stTime.dwMinute = getSTminute;
        stTimeCfg.stTime.dwSecond = getSTsecond;
        stTimeCfg.dwTimeZone = NETDEV_TIME_ZONE_E.NETDEV_TIME_ZONE_E0800;
        stTimeCfg.write();
 
        boolean bRet = NetDemo.netdevsdk.NETDEV_SetSystemTimeCfg(NetDemo.lpUserID, stTimeCfg);
        if(bRet != true)
        {
            JOptionPane.showMessageDialog(null, "set time failed,error code"+NetDemo.netdevsdk.NETDEV_GetLastError());
            System.out.printf("NETDEV_SetSystemTimeCfg failed:%d\n", NetDemo.netdevsdk.NETDEV_GetLastError());
            return;
        }else {
            stTimeCfg.read();
            String strTime = "" + stTimeCfg.stTime.dwYear+"-";
            if(stTimeCfg.stTime.dwMonth < 10) {
                strTime+= "0";
            }
            strTime = strTime + stTimeCfg.stTime.dwMonth + "-";
            if(stTimeCfg.stTime.dwDay < 10) {
                strTime+= "0";
            }
            strTime = strTime + stTimeCfg.stTime.dwDay + " ";
            if(stTimeCfg.stTime.dwHour < 10) {
                strTime+= "0";
            }
            strTime = strTime + stTimeCfg.stTime.dwHour + ":";
            if(stTimeCfg.stTime.dwMinute < 10) {
                strTime+= "0";
            }
            strTime = strTime + stTimeCfg.stTime.dwMinute + ":";
            if(stTimeCfg.stTime.dwSecond < 10) {
                strTime+= "0";
            }
            strTime = strTime + stTimeCfg.stTime.dwSecond;
 
            JOptionPane.showMessageDialog(null, "Set Success");
            NetDemo.textFieldBasicTime.setText(strTime);
            NetDemo.textFieldSetBasicSystemTime.setText(strTime);
            NetDemo.jComboBoxBasicTimeZone.setSelectedIndex(27);
            NetDemo.jButtonBasicGetSystemTime.doClick();
        }
    }
 
    public static void comboxBasicTime(String sourceTime,String targetId) {
        timeConvert(sourceTime,targetId);
    }
 
    /**
     *
     * @introduction Time format conversion
     * @description
     *
     * @param [sourceTime] [时区时间]
     * @param [targetId] [时差]
     * @return [String]
     */
    public static String timeConvert(String sourceTime,String targetId){
        if(null == NetDemo.lpUserID){
            JOptionPane.showMessageDialog(null, "Please Login device first.error code"+NetDemo.netdevsdk.NETDEV_GetLastError());
            return null;
        }
 
        NETDEV_TIME_CFG_S stTimeCfg = new NETDEV_TIME_CFG_S();
        stTimeCfg.write();
        boolean bRet = NetDemo.netdevsdk.NETDEV_GetSystemTimeCfg(NetDemo.lpUserID, stTimeCfg);
        if(bRet != true)
        {
            JOptionPane.showMessageDialog(null, "get Time failed,error code"+NetDemo.netdevsdk.NETDEV_GetLastError());
            System.out.printf("NETDEV_GetSystemTimeCfg failed:%d\n", NetDemo.netdevsdk.NETDEV_GetLastError());
            return null;
        }else {
            stTimeCfg.read();
            String reg = "^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$";
            if (!sourceTime.matches(reg)){
                return null;
            }
            targetId=NetDemo.jComboBoxBasicTimeZone.getItemAt(NetDemo.jComboBoxBasicTimeZone.getSelectedIndex()).substring(3, 9);
            try{
                //时间格式
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                //根据入参原时区id,获取对应的timezone对象
                TimeZone sourceTimeZone = TimeZone.getTimeZone("GMT"+NetDemo.lastTargetId);
                //设置SimpleDateFormat时区为原时区(否则是本地默认时区),目的:用来将字符串sourceTime转化成原时区对应的date对象
                df.setTimeZone(sourceTimeZone);
                //将字符串sourceTime转化成原时区对应的date对象
                Date sourceDate = df.parse(sourceTime);
                //开始转化时区:根据目标时区id设置目标TimeZone
                TimeZone targetTimeZone = TimeZone.getTimeZone("GMT"+targetId);
                //设置SimpleDateFormat时区为目标时区(否则是本地默认时区),目的:用来将字符串sourceTime转化成目标时区对应的date对象
                df.setTimeZone(targetTimeZone);
                //得到目标时间字符串
                String targetTime = df.format(sourceDate);
                NetDemo.textFieldSetBasicSystemTime.setText(targetTime);
                }
            catch (Exception e){
                e.printStackTrace();
                }
            }
        return null;
        }
 
    /**
     *
     * @introduction Check time format
     * @description
     *
     */
    public static String getCheck() {
        NETDEV_TIME_CFG_S stTimeCfg = new NETDEV_TIME_CFG_S();
        stTimeCfg.write();
        boolean bRet = NetDemo.netdevsdk.NETDEV_GetSystemTimeCfg(NetDemo.lpUserID, stTimeCfg);
        if(bRet != true)
        {
            JOptionPane.showMessageDialog(null, "get Time failed,error code"+NetDemo.netdevsdk.NETDEV_GetLastError());
            System.out.printf("NETDEV_GetSystemTimeCfg failed:%d\n", NetDemo.netdevsdk.NETDEV_GetLastError());
            return null;
        }else {
            stTimeCfg.read();
        String strTime = "" + stTimeCfg.stTime.dwYear+"-";
           if(stTimeCfg.stTime.dwMonth < 10)
           {
            strTime+= "0";
           }
           strTime = strTime + stTimeCfg.stTime.dwMonth + "-";
 
           if(stTimeCfg.stTime.dwDay < 10)
           {
            strTime+= "0";
           }
           strTime = strTime + stTimeCfg.stTime.dwDay + " ";
 
           if(stTimeCfg.stTime.dwHour < 10)
           {
            strTime+= "0";
           }
           strTime = strTime + stTimeCfg.stTime.dwHour + ":";
 
           if(stTimeCfg.stTime.dwMinute < 10)
           {
            strTime+= "0";
           }
           strTime = strTime + stTimeCfg.stTime.dwMinute + ":";
 
           if(stTimeCfg.stTime.dwSecond < 10)
           {
            strTime+= "0";
           }
           strTime = strTime + stTimeCfg.stTime.dwSecond;
        return strTime;
    }
    }
 
    /**
     *
     * @introduction Auto add time
     * @description
     *
     */
    public static void autoAddTime() {
        String strTime=NetDemo.textFieldSetBasicSystemTime.getText();
        NetDemo.textFieldBasicTime.setText(strTime);
        NetDemo.textFieldSetBasicSystemTime.setText(strTime);
        if(NetDemo.runflag==false) {
            return;
            }
        new Thread() {
            @Override
            public void run() {
                while(NetDemo.runflag) {
                    try {
                        String strTime2=NetDemo.textFieldBasicTime.getText();
                        Calendar c = Calendar.getInstance();
                        c.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(strTime2));
                        long sec=c.getTimeInMillis();
                        SimpleDateFormat sdd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        Thread.sleep(1000);
                        sec+=1000;
                        NetDemo.textFieldBasicTime.setText(sdd.format(new Date(sec)));
                        } catch (Exception e) {
                            e.printStackTrace();
                            }
                    }
                }
            }.start();
            }
    }