fuliqi
2025-02-21 64efb660b2c119c00432434c0f651f8996483f18
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
package com.ycl.api.YS.maintenance;
 
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.NETDEV_CONFIG_COMMAND_E;
import com.ycl.api.YS.lib.NetDEVSDKLib.NETDEV_DEVICE_BASICINFO_S;
import com.ycl.api.YS.lib.NetDEVSDKLib.NETDEV_DEVICE_TYPE_E;
 
import javax.swing.*;
import java.util.regex.Pattern;
 
/**
 * @description  To export or import the file of configuration.
 * @introduction  Support IPC/NVR/VMS.
 */
public class Configuration {
    /**
     * @description  To export the file of configuration.
     * @introduction Calling the interface of NETDEV_GetDevConfig  NETDEV_GetConfigFile.
     */
    public static void Export() {
        if(null == NetDemo.lpUserID)
        {
            JOptionPane.showMessageDialog(null, "Please Login device first. error code"+NetDemo.netdevsdk.NETDEV_GetLastError());
            return;
        }
        NETDEV_DEVICE_BASICINFO_S stDeviceInfo = new NETDEV_DEVICE_BASICINFO_S();
        IntByReference dwBytesReturned = new IntByReference();
        boolean bRet = NetDemo.netdevsdk.NETDEV_GetDevConfig(NetDemo.lpUserID, NetDemo.ChannelID, NETDEV_CONFIG_COMMAND_E.NETDEV_GET_DEVICECFG, stDeviceInfo.getPointer(), stDeviceInfo.size(), dwBytesReturned);
        if(bRet != true)
        {
            System.out.printf("NETDEV_GetDevConfig failed:%d\n", NetDemo.netdevsdk.NETDEV_GetLastError());
            return;
        }
        stDeviceInfo.read();
        String strFileNameString = "";
        String strCurrentpathString = "";
        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
            ){
             strFileNameString = Common.byteArrayToString(stDeviceInfo.szDevModel) + "_" + NetDemo.strIPAddr + "_" + "config.tgz";
             strCurrentpathString = NetDemo.strConfigFilePath +  strFileNameString;
        }
        else if((NetDemo.pstDeviceInfo.dwDeviceType == NETDEV_DEVICE_TYPE_E.NETDEV_DTYPE_NVR)||
                (NetDemo.pstDeviceInfo.dwDeviceType == NETDEV_DEVICE_TYPE_E.NETDEV_DTYPE_NVR_BACKUP)||
                (NetDemo.pstDeviceInfo.dwDeviceType == NETDEV_DEVICE_TYPE_E.NETDEV_DTYPE_HNVR)){
              strFileNameString = Common.byteArrayToString(stDeviceInfo.szDevModel) + "_" + NetDemo.strIPAddr + "_" + "config.xml";
              strCurrentpathString = NetDemo.strConfigFilePath +  strFileNameString;  
        }
        boolean bRet2 = NetDemo.netdevsdk.NETDEV_GetConfigFile(NetDemo.lpUserID, strCurrentpathString);
        if(bRet2 != true)
        {
            System.out.printf("NETDEV_GetConfigFile failed:%d\n", NetDemo.netdevsdk.NETDEV_GetLastError());
            return;
        }
        else 
        {
            JOptionPane.showMessageDialog(null, "Export success,file path"+strCurrentpathString);
        }  
    }
    /**
     * @description To import the file of configuration.
     * @introduction Calling the interface of NETDEV_SetConfigFile.
     */
    public static void Import() {
        if(null == NetDemo.lpUserID)
        {
            JOptionPane.showMessageDialog(null, "Please Login device first. error code"+NetDemo.netdevsdk.NETDEV_GetLastError());
            return;
        }
        boolean bRet = NetDemo.netdevsdk.NETDEV_SetConfigFile(NetDemo.lpUserID, NetDemo.jTextFieldMaintenanceManualConfigurationImport.getText());
        if(bRet != true)
        {
            System.out.printf("NETDEV_SetConfigFile failed:%d\n", NetDemo.netdevsdk.NETDEV_GetLastError());
            JOptionPane.showMessageDialog(null, "Import file failed, please reselect file, error code"+NetDemo.netdevsdk.NETDEV_GetLastError());
            NetDemo.jTextFieldMaintenanceManualConfigurationImport.setText(null);
            return;
        }
        else 
        {
            JOptionPane.showMessageDialog(null, "Import file success");
        }  
    }
    /**
     * @description To browse the file of Configuration. 
     */
    public static void Browse() {
        JFileChooser fcFileChooser = new JFileChooser();
        fcFileChooser.setMultiSelectionEnabled(false);
        int result=fcFileChooser.showSaveDialog(null);
        if (result==JFileChooser.APPROVE_OPTION)
        {
            fcFileChooser.getSelectedFile();
            String strFilePath = fcFileChooser.getSelectedFile().getAbsolutePath();
            if(strFilePath==null||strFilePath.equals("")) 
            {
                System.out.println("No choose file");
                return;
            }
            String newpath=null;
            try {
                newpath=strFilePath.substring(strFilePath.lastIndexOf("."));
            }catch(Exception y) {
                JOptionPane.showMessageDialog(null, "File is not exist");
                return;
            }
            
            if(newpath==null||newpath.equals("")) 
            {
                System.out.println("No choose file");
                return;
            }
            String reg = "(tgz|xml)";
            Pattern p = Pattern.compile(reg);
            boolean boo = p.matcher(newpath).find();
            if(boo) 
            {
                JOptionPane.showMessageDialog(null, "Choose file ok");
                NetDemo.jTextFieldMaintenanceManualConfigurationImport.setText(strFilePath);
            }
            else 
            {
                JOptionPane.showMessageDialog(null, "Only support tgz file or xml file");
            }  
        }
    
    }
}