liusuyi
2024-07-10 016aa105789fca71e86cdbb0d26a181812f200f9
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
package com.ruoyi.utils.sdk.dhsdk.module;
 
import com.ruoyi.utils.sdk.dhsdk.lib.NetSDKLib;
import com.ruoyi.utils.sdk.dhsdk.lib.ToolKits;
import com.sun.jna.Memory;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.sun.jna.ptr.IntByReference;
import lombok.extern.slf4j.Slf4j;
 
import static com.ruoyi.utils.sdk.dhsdk.lib.ToolKits.getErrorCodePrint;
 
/**
 * @Description:
 * @ClassName: ConfigModule
 * @Author: 刘苏义
 * @Date: 2023年10月14日14:57:01
 **/
@Slf4j(topic = "dhSdk")
public class ConfigModule {
    public static NetSDKLib netsdk = NetSDKLib.NETSDK_INSTANCE;
    public static NetSDKLib configsdk = NetSDKLib.CONFIG_INSTANCE;
 
    /**
     * 查询设备状态
     */
    public static boolean queryDevState(NetSDKLib.LLong hLoginHandle, int nType, NetSDKLib.SdkStructure stuInfo) {
 
        IntByReference intRetLen = new IntByReference();
        stuInfo.write();
        if (!netsdk.CLIENT_QueryDevState(hLoginHandle, nType, stuInfo.getPointer(), stuInfo.size(), intRetLen, 3000)) {
            return false;
        }
        stuInfo.read();
        return true;
    }
 
    /**
     * 查询远程设备状态
     */
    public static boolean queryRemotDevState(NetSDKLib.LLong hLoginHandle, int nChn, int nType, NetSDKLib.SdkStructure stuInfo) {
 
        IntByReference intRetLen = new IntByReference();
        stuInfo.write();
        if (!netsdk.CLIENT_QueryRemotDevState(hLoginHandle, nType, nChn, stuInfo.getPointer(), stuInfo.size(), intRetLen, 3000)) {
            log.error("Config Failed!" + ToolKits.getErrorCodePrint());
            return false;
        }
        stuInfo.read();
        return true;
    }
 
    /**
     * 获取单个配置
     *
     * @param hLoginHandle 登陆句柄
     * @param nChn         通道号,-1 表示全通道
     * @param strCmd       配置名称
     * @param cmdObject    配置对应的结构体对象
     * @return 成功返回 true
     */
    public static boolean GetNewDevConfig(NetSDKLib.LLong hLoginHandle, int nChn, String strCmd, Structure cmdObject) {
        boolean result = false;
        IntByReference error = new IntByReference(0);
        int nBufferLen = 2 * 1024 * 1024;
        byte[] strBuffer = new byte[nBufferLen];
 
        if (netsdk.CLIENT_GetNewDevConfig(hLoginHandle, strCmd, nChn, strBuffer, nBufferLen, error, 6000)) {
            cmdObject.write();
            if (configsdk.CLIENT_ParseData(strCmd, strBuffer, cmdObject.getPointer(), cmdObject.size(), null)) {
                cmdObject.read();
                result = true;
            } else {
                log.error("Parse [" + strCmd + "] Config Failed!" + ToolKits.getErrorCodePrint());
                result = false;
            }
        } else {
            log.error("Get [" + strCmd + "] Config Failed!Last Error = " + getErrorCodePrint());
            result = false;
        }
 
        return result;
    }
    public static String getChannelName(NetSDKLib.LLong hLoginHandle,Integer channel) {
        String channelName = "";
        NetSDKLib.AV_CFG_ChannelName channelTitleName = new NetSDKLib.AV_CFG_ChannelName();
        if (ToolKits.GetDevConfig(hLoginHandle, channel, NetSDKLib.CFG_CMD_CHANNELTITLE, channelTitleName)) {
            try {
                channelName = new String(channelTitleName.szName, "GBK");
            } catch (Exception e) {
                System.err.println("getChannelName Failed!");
            }
        } else {
            System.err.println("Get Channel Name Failed." + ToolKits.getErrorCodePrint());
        }
        return channelName;
    }
    public static boolean GetDevConfig(NetSDKLib.LLong hLoginHandle, int nChn, String strCmd, Structure cmdObject) {
        boolean result = true;
        IntByReference error = new IntByReference(0);
        int nBufferLen = 2 * 1024 * 1024;
        byte[] strBuffer = new byte[nBufferLen];
        cmdObject.write();
        boolean bRet = netsdk.CLIENT_QueryNewSystemInfo(hLoginHandle, strCmd, nChn, strBuffer, cmdObject.size(), error, 3000);
        if (bRet) {
            cmdObject.read();
        } else {
            log.error("Get [" + strCmd + "] Config Failed!Last Error = " + getErrorCodePrint());
            result = false;
        }
        return result;
    }
 
    public static boolean queryCameraState(NetSDKLib.LLong hLoginHandle, Integer chanNum, Integer chanNo) {
        boolean bRet = false;
        try {
            NetSDKLib.NET_CAMERA_STATE_INFO[] arrCameraStatus = new NetSDKLib.NET_CAMERA_STATE_INFO[chanNum];
            for (int i = 0; i < arrCameraStatus.length; i++) {
                arrCameraStatus[i] = new NetSDKLib.NET_CAMERA_STATE_INFO();
            }
 
            // 入参
            NetSDKLib.NET_IN_GET_CAMERA_STATEINFO stIn = new NetSDKLib.NET_IN_GET_CAMERA_STATEINFO();
            stIn.bGetAllFlag = 0; // 全部
            stIn.nValidNum = chanNum;
            stIn.nChannels[chanNo - 1] = chanNo - 1;
 
            // 出参
            NetSDKLib.NET_OUT_GET_CAMERA_STATEINFO stOut = new NetSDKLib.NET_OUT_GET_CAMERA_STATEINFO();
            stOut.nMaxNum = chanNum;
            stOut.pCameraStateInfo = new Memory(arrCameraStatus[0].size() * chanNum);
            stOut.pCameraStateInfo.clear(arrCameraStatus[0].size() * chanNum);
            ToolKits.SetStructArrToPointerData(arrCameraStatus, stOut.pCameraStateInfo);  // 将数组内存拷贝到Pointer
 
            stIn.write();
            stOut.write();
 
            bRet = netsdk.CLIENT_QueryDevInfo(hLoginHandle, NetSDKLib.NET_QUERY_GET_CAMERA_STATE,
                    stIn.getPointer(), stOut.getPointer(), null, 3000);
            if (bRet) {
                stOut.read();
                ToolKits.GetPointerDataToStructArr(stOut.pCameraStateInfo, arrCameraStatus);  // 将Pointer拷贝到数组内存
                final String[] connectionState = {"未知", "正在连接", "已连接", "未连接", "通道未配置,无信息", "通道有配置,但被禁用"};
               log.debug(connectionState[arrCameraStatus[chanNo - 1].emConnectionState]);
                if (connectionState[arrCameraStatus[chanNo - 1].emConnectionState].equals("已连接")) {
                    log.debug("通道" + arrCameraStatus[chanNo - 1].nChannel + connectionState[arrCameraStatus[chanNo - 1].emConnectionState]);
                    bRet = true;
                } else {
                    bRet = false;
                }
            } else {
                log.error("Query Camera State Failed!" + getErrorCodePrint());
            }
        } catch (Exception ex) {
            log.error("Query Camera State Failed!" + ex.getMessage());
        }
        return bRet;
    }
 
    // 获取配置
    public static boolean GetConfig(NetSDKLib.LLong hLoginHandle, int nChn, int type, Structure cmdObject) {
        boolean result = false;
        // 获取
        cmdObject.write();
        if (netsdk.CLIENT_GetConfig(hLoginHandle, type, nChn, cmdObject.getPointer(), cmdObject.size(), 4000, null)) {
            cmdObject.read();
            result = true;
        } else {
            log.error("GetConfig Failed!" + getErrorCodePrint());
            result = false;
        }
        return result;
    }
 
    /**
     * 设置单个配置
     *
     * @param hLoginHandle 登陆句柄
     * @param nChn         通道号,-1 表示全通道
     * @param strCmd       配置名称
     * @param cmdObject    配置对应的结构体对象
     * @return 成功返回 true
     */
    public static boolean SetDevConfig(NetSDKLib.LLong hLoginHandle, int nChn, String strCmd, Structure cmdObject) {
        boolean result = false;
        int nBufferLen = 2 * 1024 * 1024;
        byte szBuffer[] = new byte[nBufferLen];
        for (int i = 0; i < nBufferLen; i++) szBuffer[i] = 0;
        IntByReference error = new IntByReference(0);
        IntByReference restart = new IntByReference(0);
        cmdObject.write();
        if (configsdk.CLIENT_PacketData(strCmd, cmdObject.getPointer(), cmdObject.size(), szBuffer, nBufferLen)) {
            cmdObject.read();
            if (netsdk.CLIENT_SetNewDevConfig(hLoginHandle, strCmd, nChn, szBuffer, nBufferLen, error, restart, 3000)) {
                result = true;
            } else {
                log.error("Set [" + strCmd + "] Config Failed! Last Error = " + getErrorCodePrint());
 
                result = false;
            }
        } else {
            log.error("Parse [" + strCmd + "] Config Failed!" + ToolKits.getErrorCodePrint());
            result = false;
        }
 
        return result;
    }
 
 
}