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
package com.ycl.api.YS.config.network;
 
import com.sun.jna.ptr.IntByReference;
import com.ycl.api.YS.NetDemo;
import com.ycl.api.YS.util.Common;
import com.ycl.api.YS.lib.NetDEVSDKLib;
import com.ycl.api.YS.lib.NetDEVSDKLib.NETDEV_CONFIG_COMMAND_E;
import com.ycl.api.YS.lib.NetDEVSDKLib.NETDEV_DEVICE_TYPE_E;
import com.ycl.api.YS.lib.NetDEVSDKLib.NETDEV_SYSTEM_IPADDR_INFO_S;
import com.ycl.api.YS.lib.NetDEVSDKLib.NETDEV_SYSTEM_NTP_INFO_LIST_S;
 
import javax.swing.*;
import java.util.InputMismatchException;
 
/**
 *
 * @introduction NTP
 * @description Support IPC/NVR
 */
public class NTP {
 
    /**
     *
     * @introduction Get NTP information
     * @description  Calling the interface of NETDEV_GetDevConfig
     *
     */
    public static void getNTPInfo() {
        if(null == NetDemo.lpUserID){
            JOptionPane.showMessageDialog(null, "Please Login device first. error code"+NetDemo.netdevsdk.NETDEV_GetLastError());
            return;
        }
        if(NetDemo.ChannelID == 0){
            JOptionPane.showMessageDialog(null, "Please select an online channel after login.");
            return;
        }
 
        NETDEV_SYSTEM_NTP_INFO_LIST_S stNTPInfo = new NETDEV_SYSTEM_NTP_INFO_LIST_S();
        for(int i =0; i<NetDEVSDKLib.NETDEV_NTP_SERVER_LIST_NUM; i++)
        {
            stNTPInfo.astNTPServerInfoList[i] = new NETDEV_SYSTEM_IPADDR_INFO_S();
        }
        stNTPInfo.write();
 
        IntByReference dwBytesReturned = new IntByReference();
        boolean bRet = NetDemo.netdevsdk.NETDEV_GetDevConfig(NetDemo.lpUserID, NetDemo.ChannelID, NETDEV_CONFIG_COMMAND_E.NETDEV_GET_NTPCFG_EX, stNTPInfo.getPointer(), stNTPInfo.size(), dwBytesReturned );
        if(bRet != true)
        {
            JOptionPane.showMessageDialog(null, "Get data failed. error code"+NetDemo.netdevsdk.NETDEV_GetLastError());
            System.out.printf("NETDEV_GetDevConfig failed:%d\n", NetDemo.netdevsdk.NETDEV_GetLastError());
            return;
        }
        stNTPInfo.read();
 
        for(int i= 0; i< stNTPInfo.ulNum; i++)
        {
            if(stNTPInfo.astNTPServerInfoList[i].bEnabled ==1)
            {
                NetDemo.chckbxConfigNetWorkNTPEnable.setSelected(true);
            }else {
                NetDemo.chckbxConfigNetWorkNTPEnable.setSelected(false);
            }
            NetDemo.textFieldConfigNetWorkNTPDomainName.setText(Common.byteArrayToString(stNTPInfo.astNTPServerInfoList[i].szDomainName));
 
            if(stNTPInfo.astNTPServerInfoList[i].ulAddressType == 0)
            {
                NetDemo.jComboBoxConfigNetWorkNTPIPType.setSelectedIndex(0);
                NetDemo.textFieldConfigNetWorkNTPDomainName.setEnabled(false);
            }
            else if(stNTPInfo.astNTPServerInfoList[i].ulAddressType == 1)
            {
                NetDemo.jComboBoxConfigNetWorkNTPIPType.setSelectedIndex(1);
            }
            else if(stNTPInfo.astNTPServerInfoList[i].ulAddressType == 2)
            {
                NetDemo.jComboBoxConfigNetWorkNTPIPType.setSelectedIndex(2);
                NetDemo.textFieldConfigNetWorkNTPServerIP.setEnabled(false);
            }
            NetDemo.textFieldConfigNetWorkNTPServerIP.setText(Common.byteArrayToString(stNTPInfo.astNTPServerInfoList[i].szIPAddress));
            NetDemo.textFieldConfigNetWorkNTPPort.setText(String.valueOf(stNTPInfo.astNTPServerInfoList[i].ulPort));
            NetDemo.textFieldConfigNetWorkNTPSynchronizeInterval.setText(String.valueOf(stNTPInfo.astNTPServerInfoList[i].ulSynchronizeInterval));
        }
    }
 
    /**
     *
     * @introduction Set NTP information
     * @description Calling the interface of NETDEV_SetDevConfig
     *
     */
    public static void setNTPInfo() {
        if(null == NetDemo.lpUserID){
            JOptionPane.showMessageDialog(null, "Please Login device first. error code"+NetDemo.netdevsdk.NETDEV_GetLastError());
            return;
        }
        if(NetDemo.ChannelID == 0){
            JOptionPane.showMessageDialog(null, "Please select an online channel after login.");
            return;
        }
 
        NETDEV_SYSTEM_NTP_INFO_LIST_S stNTPInfo = new NETDEV_SYSTEM_NTP_INFO_LIST_S();
        for(int i =0; i<NetDEVSDKLib.NETDEV_NTP_SERVER_LIST_NUM; i++)
        {
            stNTPInfo.astNTPServerInfoList[i] = new NETDEV_SYSTEM_IPADDR_INFO_S();
        }
        stNTPInfo.write();
 
        IntByReference dwBytesReturned = new IntByReference();
 
        boolean bRet = NetDemo.netdevsdk.NETDEV_GetDevConfig(NetDemo.lpUserID, NetDemo.ChannelID, NETDEV_CONFIG_COMMAND_E.NETDEV_GET_NTPCFG_EX, stNTPInfo.getPointer(), stNTPInfo.size(), dwBytesReturned );
        if(bRet != true) {
            JOptionPane.showMessageDialog(null, "Get data failed. error code"+NetDemo.netdevsdk.NETDEV_GetLastError());
            System.out.printf("NETDEV_GetDevConfig failed:%d\n", NetDemo.netdevsdk.NETDEV_GetLastError());
            return;
        }else {
            stNTPInfo.read();
 
            if(NetDemo.chckbxConfigNetWorkNTPEnable.isSelected() == true) {
                stNTPInfo.astNTPServerInfoList[0].bEnabled = 1;
            }else {
                stNTPInfo.astNTPServerInfoList[0].bEnabled = 0;
            }
            stNTPInfo.astNTPServerInfoList[0].ulAddressType = NetDemo.jComboBoxConfigNetWorkNTPIPType.getSelectedIndex();
 
            if(NetDemo.jComboBoxConfigNetWorkNTPIPType.getSelectedIndex()==0) {
                NetDemo.textFieldConfigNetWorkNTPDomainName.setEnabled(false);
                if(NetDemo.textFieldConfigNetWorkNTPServerIP.getText().equals("0.0.0.0")) {
                    System.arraycopy(NetDemo.textFieldConfigNetWorkNTPServerIP.getText().getBytes(), 0, stNTPInfo.astNTPServerInfoList[0].szIPAddress, 0, NetDemo.textFieldConfigNetWorkNTPServerIP.getText().getBytes().length);
                }else if(Common.isIP(NetDemo.textFieldConfigNetWorkNTPServerIP.getText())) {
                System.arraycopy(NetDemo.textFieldConfigNetWorkNTPServerIP.getText().getBytes(), 0, stNTPInfo.astNTPServerInfoList[0].szIPAddress, 0, NetDemo.textFieldConfigNetWorkNTPServerIP.getText().getBytes().length);
                }else {
                    JOptionPane.showMessageDialog(null, "Ip is error");
                    return;
                    }
                }
 
            if(NetDemo.jComboBoxConfigNetWorkNTPIPType.getSelectedIndex()==2) {
                NetDemo.textFieldConfigNetWorkNTPServerIP.setEnabled(false);
                if(NetDemo.textFieldConfigNetWorkNTPDomainName.getText()==null||NetDemo.textFieldConfigNetWorkNTPDomainName.getText().equals("")) {
                    JOptionPane.showMessageDialog(null, "Please consider domain name is right  cannot be null");
                    return;
                }
                System.arraycopy(NetDemo.textFieldConfigNetWorkNTPDomainName.getText().getBytes(), 0, stNTPInfo.astNTPServerInfoList[0].szDomainName, 0, NetDemo.textFieldConfigNetWorkNTPDomainName.getText().getBytes().length);
            }
 
            String  FieldConfigNetWorkNTPPort= NetDemo.textFieldConfigNetWorkNTPPort.getText();
            if(FieldConfigNetWorkNTPPort==null||FieldConfigNetWorkNTPPort.equals("")) {
                JOptionPane.showMessageDialog(null, "Please input ntp port");
                return;
            }
 
            int getFieldConfigNetWorkNTPPort=0;
            try {
                getFieldConfigNetWorkNTPPort=Integer.parseInt(FieldConfigNetWorkNTPPort);
            }catch(NumberFormatException |InputMismatchException e2) {
                JOptionPane.showMessageDialog(null, "NTP port is null or transformation Exception");
                NetDemo.textFieldConfigNetWorkNTPPort.setText(null);
                return;
            }
 
 
            if(getFieldConfigNetWorkNTPPort==0) {
                JOptionPane.showMessageDialog(null, "NTP port can not use");
                NetDemo.textFieldConfigNetWorkNTPPort.setText(null);
                return;
            }
            if(getFieldConfigNetWorkNTPPort<0 || getFieldConfigNetWorkNTPPort>65535) {
                JOptionPane.showMessageDialog(null, "NTP port  should between 0 and 65535");
                NetDemo.textFieldConfigNetWorkNTPPort.setText(null);
                return;
            }
 
            stNTPInfo.astNTPServerInfoList[0].ulPort = getFieldConfigNetWorkNTPPort;
 
            String SynchronizeInterval=NetDemo.textFieldConfigNetWorkNTPSynchronizeInterval.getText();
 
            int Interval=0;
            try {
                Interval=Integer.valueOf(SynchronizeInterval);
            }catch(Exception t) {
                JOptionPane.showMessageDialog(null, "Interval is null or transformation Exception");
                return;
            }
 
            stNTPInfo.astNTPServerInfoList[0].ulSynchronizeInterval = Interval;
 
            stNTPInfo.write();
 
            boolean bRet2 = NetDemo.netdevsdk.NETDEV_SetDevConfig(NetDemo.lpUserID, NetDemo.ChannelID, NETDEV_CONFIG_COMMAND_E.NETDEV_SET_NTPCFG_EX, stNTPInfo.getPointer(), stNTPInfo.size());
            if(bRet2 != true)
            {
                JOptionPane.showMessageDialog(null, "Set data failed,error code"+NetDemo.netdevsdk.NETDEV_GetLastError());
                System.out.printf("NETDEV_SetDevConfig failed:%d\n", NetDemo.netdevsdk.NETDEV_GetLastError());
                return;
            }else {
                JOptionPane.showMessageDialog(null, "Set success");
                }
            }
        }
 
    /**
     *
     * @introduction Choose IP type
     * @description
     *
     */
    public static void changeIPType() {
        if(NetDemo.pstDeviceInfo.dwDeviceType == NETDEV_DEVICE_TYPE_E.NETDEV_DTYPE_IPC ||
           NetDemo.pstDeviceInfo.dwDeviceType == NETDEV_DEVICE_TYPE_E.NETDEV_DTYPE_IPC_FISHEYE ||
           NetDemo.pstDeviceInfo.dwDeviceType == NETDEV_DEVICE_TYPE_E.NETDEV_DTYPE_IPC_ECONOMIC_FISHEYE ||
           NetDemo.pstDeviceInfo.dwDeviceType == NETDEV_DEVICE_TYPE_E.NETDEV_DTYPE_IPC_ACS)
        {
            if(NetDemo.jComboBoxConfigNetWorkNTPIPType.getSelectedIndex()==1)
            {
            NetDemo.jComboBoxConfigNetWorkNTPIPType.setSelectedIndex(0);
            NetDemo.textFieldConfigNetWorkNTPDomainName.setEnabled(false);
            NetDemo.textFieldConfigNetWorkNTPServerIP.setEnabled(true);
            JOptionPane.showMessageDialog(null, "Not support ipv6,please wait other version,please choose IPV4 or domain name");
            return;
            }
        }
 
        if(NetDemo.jComboBoxConfigNetWorkNTPIPType.getSelectedIndex()==0) {
            NetDemo.textFieldConfigNetWorkNTPDomainName.setEnabled(false);
            NetDemo.textFieldConfigNetWorkNTPServerIP.setEnabled(true);
            }else if(NetDemo.jComboBoxConfigNetWorkNTPIPType.getSelectedIndex()==2) {
                NetDemo.textFieldConfigNetWorkNTPDomainName.setEnabled(true);
                NetDemo.textFieldConfigNetWorkNTPServerIP.setEnabled(false);
                }else {
                    NetDemo.jComboBoxConfigNetWorkNTPIPType.setSelectedIndex(0);
                    NetDemo.textFieldConfigNetWorkNTPDomainName.setEnabled(false);
                    NetDemo.textFieldConfigNetWorkNTPServerIP.setEnabled(true);
                    JOptionPane.showMessageDialog(null, "Not support ipv6,please wait other version,please choose IPV4 or domain name");
                    return;
                    }
        }
    }