‘liusuyi’
2023-06-15 4aa86f1386d20c9ad811e2d18b7e56f00b06e25c
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
package com.ruoyi.device.hiksdk.service.impl;
 
import com.ruoyi.common.annotation.SdkOperate;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.constant.CamPriority;
import com.ruoyi.device.camera.domain.CameraCmd;
import com.ruoyi.device.camera.mapper.ArdCamerasMapper;
import com.ruoyi.device.hiksdk.common.GlobalVariable;
import com.ruoyi.device.camera.domain.ArdCameras;
import com.ruoyi.device.hiksdk.config.MinioClientSingleton;
import com.ruoyi.device.hiksdk.util.hikSdkUtil.GisUtil;
import com.ruoyi.device.hiksdk.util.hikSdkUtil.HCNetSDK;
import com.ruoyi.device.hiksdk.service.IHikClientService;
import com.ruoyi.device.hiksdk.util.hikSdkUtil.LoginResultCallBack;
import com.ruoyi.device.hiksdk.util.minio.MinioUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import com.sun.jna.Native;
import com.sun.jna.Platform;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Encoder;
 
import javax.annotation.Resource;
import java.io.*;
import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
 
import static com.ruoyi.device.hiksdk.util.hikSdkUtil.HCNetSDK.*;
 
/**
 * @ClassName: hikClientServiceImpl
 * @Description: 海康操作客户端实现类
 * @Author: Administrator
 * @Date: 2023年01月17日 11:25
 * @Version: 1.2
 **/
@Slf4j(topic = "hikSdk")
@Service
public class HikClientServiceImpl implements IHikClientService {
 
    @Resource
    ArdCamerasMapper ardCamerasMapper;
 
    @Resource
    SysUserMapper sysUserMapper;
 
    private static HCNetSDK hCNetSDK;
 
    @Override
    public void loadHCNetSDKLib() {
        try {
            log.debug("开始加载sdk库文件路径");
            if (Platform.isWindows()) {
                String WIN_PATH = System.getProperty("user.dir") + File.separator + "lib" + File.separator + "HCNetSDK.dll";
                log.debug("当前Windows平台的sdk库路径:" + WIN_PATH);
                hCNetSDK = (HCNetSDK) Native.loadLibrary(WIN_PATH, HCNetSDK.class);
            } else {
                log.debug("Linux平台");
                String LINUX_PATH = System.getProperty("user.dir") + File.separator + "hiklib" + File.separator + "libhcnetsdk.so";
                log.debug("当前Linux平台的sdk库路径:" + LINUX_PATH);
                hCNetSDK = (HCNetSDK) Native.loadLibrary(LINUX_PATH, HCNetSDK.class);
 
                //设置HCNetSDKCom组件库所在路径
                //libhcnetsdk.so
                String strPathCom = "/home/hiklib";
                NET_DVR_LOCAL_SDK_PATH struComPath = new NET_DVR_LOCAL_SDK_PATH();
                System.arraycopy(strPathCom.getBytes(), 0, struComPath.sPath, 0, strPathCom.length());
                struComPath.write();
                hCNetSDK.NET_DVR_SetSDKInitCfg(2, struComPath.getPointer());
 
                //设置libcrypto.so所在路径
                BYTE_ARRAY ptrByteArrayCrypto = new BYTE_ARRAY(256);
                String strPathCrypto = "/home/hiklib/libcrypto.so.1.1";
                System.arraycopy(strPathCrypto.getBytes(), 0, ptrByteArrayCrypto.byValue, 0, strPathCrypto.length());
                ptrByteArrayCrypto.write();
                hCNetSDK.NET_DVR_SetSDKInitCfg(3, ptrByteArrayCrypto.getPointer());
 
                //设置libssl.so所在路径
                BYTE_ARRAY ptrByteArraySsl = new BYTE_ARRAY(256);
                String strPathSsl = "/home/hiklib/libssl.so.1.1";
                System.arraycopy(strPathSsl.getBytes(), 0, ptrByteArraySsl.byValue, 0, strPathSsl.length());
                ptrByteArraySsl.write();
                hCNetSDK.NET_DVR_SetSDKInitCfg(4, ptrByteArraySsl.getPointer());
            }
        } catch (Exception ex) {
            log.error("加载库文件异常:" + ex.getMessage());
        }
    }
 
    /**
     * @描述 注册登录 只支持同步登陆,且官方不建议直接在此接口下写耗时操作
     * @参数 [dvrLogin]
     * @返回值 java.lang.Integer
     * @创建人 刘苏义
     * @创建时间 2023/1/17 16:12
     * @修改人和其它信息
     */
    public ArdCameras login1(ArdCameras camera) {
        // 初始化
        if (!hCNetSDK.NET_DVR_Init()) {
            log.error("SDK初始化失败");
        }
        //打印海康sdk日志
        if (Platform.isWindows()) {
            String WIN_PATH = System.getProperty("user.dir") + File.separator + "ardLog" + File.separator + "logs" + File.separator;
            hCNetSDK.NET_DVR_SetLogToFile(3, WIN_PATH, true);
        } else {
            hCNetSDK.NET_DVR_SetLogToFile(3, "/home/ardLog/hiklog", true);
        }
        String m_sDeviceIP = camera.getIp();
        String m_sUsername = camera.getUsername();
        String m_sPassword = camera.getPassword();
        short m_sPort = camera.getPort().shortValue();
        //设置连接时间与重连时间
        hCNetSDK.NET_DVR_SetConnectTime(2000, 1);
        hCNetSDK.NET_DVR_SetReconnect(100000, true);
        //设备信息, 输出参数
        NET_DVR_DEVICEINFO_V30 m_strDeviceInfo = new NET_DVR_DEVICEINFO_V30();
        int lUserID = hCNetSDK.NET_DVR_Login_V30(m_sDeviceIP, m_sPort, m_sUsername, m_sPassword, m_strDeviceInfo);
        if (lUserID < 0) {
            //释放SDK资源
            hCNetSDK.NET_DVR_Cleanup();
            camera.setLoginId(-1);
        }
        if (GlobalVariable.loginMap.containsKey(camera.getId())) {
            GlobalVariable.loginMap.remove(camera.getId());
        }
        GlobalVariable.loginMap.put(camera.getId(), lUserID);
 
        camera.setLoginId(lUserID);
        camera.setChannel((int) m_strDeviceInfo.byStartChan);
        return camera;
    }
 
    /**
     * @描述 注册登录 集成于NET_DVR_Login_V30,支持同步和异步登录
     * @参数 [dvrLogin]
     * @返回值 java.lang.Integer
     * @创建人 刘苏义
     * @创建时间 2023/1/17 16:12
     * @修改人和其它信息
     */
    @Override
    public void login(ArdCameras camera) {
        // 初始化
        if (!hCNetSDK.NET_DVR_Init()) {
            log.error("SDK初始化失败");
        }
        //打印海康sdk日志
        if (Platform.isWindows()) {
            String WIN_PATH = System.getProperty("user.dir") + File.separator + "ardLog" + File.separator + "logs" + File.separator;
            hCNetSDK.NET_DVR_SetLogToFile(3, WIN_PATH, true);
        } else {
            hCNetSDK.NET_DVR_SetLogToFile(3, "/home/ardLog/hiklog", true);
        }
        String m_sDeviceIP = camera.getIp();
        String m_sUsername = camera.getUsername();
        String m_sPassword = camera.getPassword();
        short m_sPort = camera.getPort().shortValue();
        //设置连接时间与重连时间
        hCNetSDK.NET_DVR_SetConnectTime(2000, 1);
        hCNetSDK.NET_DVR_SetReconnect(100000, true);
        //设备信息, 输出参数
        HCNetSDK.NET_DVR_DEVICEINFO_V40 m_strDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V40();
        HCNetSDK.NET_DVR_USER_LOGIN_INFO m_strLoginInfo = new HCNetSDK.NET_DVR_USER_LOGIN_INFO();
 
        // 注册设备-登录参数,包括设备地址、登录用户、密码等
        m_strLoginInfo.sDeviceAddress = new byte[HCNetSDK.NET_DVR_DEV_ADDRESS_MAX_LEN];
        System.arraycopy(m_sDeviceIP.getBytes(), 0, m_strLoginInfo.sDeviceAddress, 0, m_sDeviceIP.length());
        m_strLoginInfo.sUserName = new byte[HCNetSDK.NET_DVR_LOGIN_USERNAME_MAX_LEN];
        System.arraycopy(m_sUsername.getBytes(), 0, m_strLoginInfo.sUserName, 0, m_sUsername.length());
        m_strLoginInfo.sPassword = new byte[HCNetSDK.NET_DVR_LOGIN_PASSWD_MAX_LEN];
        System.arraycopy(m_sPassword.getBytes(), 0, m_strLoginInfo.sPassword, 0, m_sPassword.length());
        m_strLoginInfo.wPort = m_sPort;
        m_strLoginInfo.byVerifyMode = 0;
        m_strLoginInfo.byLoginMode = 0;
        //是否异步登录:0- 否,1- 是  windowsSDK里是true和false
        m_strLoginInfo.bUseAsynLogin = true;
        //异步登录回调
        m_strLoginInfo.cbLoginResult = new LoginResultCallBack(camera);
        m_strLoginInfo.write();
        int i = hCNetSDK.NET_DVR_Login_V40(m_strLoginInfo, m_strDeviceInfo);
        if (i < 0) {
            int errorCode = hCNetSDK.NET_DVR_GetLastError();
            log.info("登录异常:" + errorCode);
        }
    }
 
    /**
     * @描述 登录所有相机
     * @参数 []
     * @返回值 void
     * @创建人 刘苏义
     * @创建时间 2023/2/3 10:10
     * @修改人和其它信息
     */
    @Override
    public void loginAll() {
        try {
            log.debug("加载lib完成!");
            List<ArdCameras> ardCameras = ardCamerasMapper.selectArdCamerasListNoDataScope(new ArdCameras());
            for (ArdCameras camera : ardCameras) {
                Thread.sleep(100);
                login(camera);
            }
        } catch (Exception ex) {
            log.error("初始化登录相机异常:" + ex.getMessage());
        }
    }
 
    /**
     * @描述 注销登录
     * @参数 [dvrLogin]
     * @返回值 java.lang.Integer
     * @创建人 刘苏义
     * @创建时间 2023/1/17 16:12
     * @修改人和其它信息
     */
    @Override
    public boolean loginOut(String cameraId) {
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return false;
        }
        Integer userId = GlobalVariable.loginMap.get(cameraId);
        boolean b = hCNetSDK.NET_DVR_Logout(userId);
        if (b) {
            GlobalVariable.loginMap.remove(cameraId);
        }
        return b;
    }
 
    /**
     * 是否在线
     *
     * @param cmd
     */
    @Override
    public boolean isOnLine(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return false;
        }
        Integer userId = GlobalVariable.loginMap.get(cameraId);
        boolean isOnLine = hCNetSDK.NET_DVR_RemoteControl(userId, HCNetSDK.NET_DVR_CHECK_USER_STATUS, null, 0);
        return isOnLine;
    }
 
    /**
     * @描述 带速度的云台控制操作
     * Code:1-左上 2-上 3-右上 4-左 5-巡航 6-右 7-左下 8-下 9-右下 10-焦距变大 11-焦距变小
     * 12-焦点前调 13-焦点后调 14-光圈扩大 15-光圈缩小 16-雨刷开启
     * @参数 [userId, channelNum, speed]
     * @返回值 boolean
     * @创建人 刘苏义
     * @创建时间 2023/1/17 16:14
     * @修改人和其它信息
     */
    @Override
    @SdkOperate
    public boolean pTZControlWithSpeed(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        boolean enable = cmd.isEnable();
        Integer channelNum = cmd.getChannelNum();
        Integer speed = cmd.getSpeed();
        Integer code = cmd.getCode();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return false;
        }
        Integer userId = GlobalVariable.loginMap.get(cameraId);
        Integer dwStop;
        if (enable) {
            dwStop = 0;//开启
        } else {
            dwStop = 1;//关闭
        }
        Integer dwPTZCommand = -1;
        switch (code) {
            /*方向*/
            case 1:
                dwPTZCommand = HCNetSDK.UP_LEFT;
                break;
            case 2:
                dwPTZCommand = HCNetSDK.TILT_UP;
                break;
            case 3:
                dwPTZCommand = HCNetSDK.UP_RIGHT;
                break;
            case 4:
                dwPTZCommand = HCNetSDK.PAN_LEFT;
                break;
            case 5:
                dwPTZCommand = HCNetSDK.RUN_SEQ;
                break;
            case 6:
                dwPTZCommand = HCNetSDK.PAN_RIGHT;
                break;
            case 7:
                dwPTZCommand = HCNetSDK.DOWN_LEFT;
                break;
            case 8:
                dwPTZCommand = HCNetSDK.TILT_DOWN;
                break;
            case 9:
                dwPTZCommand = HCNetSDK.DOWN_RIGHT;
                break;
            /*焦距*/
            case 10:
                dwPTZCommand = HCNetSDK.ZOOM_IN;
                break;
            case 11:
                dwPTZCommand = HCNetSDK.ZOOM_OUT;
                break;
            /*焦点*/
            case 12:
                dwPTZCommand = HCNetSDK.FOCUS_NEAR;
                break;
            case 13:
                dwPTZCommand = HCNetSDK.FOCUS_FAR;
                break;
            /*光圈*/
            case 14:
                dwPTZCommand = HCNetSDK.IRIS_OPEN;
                break;
            case 15:
                dwPTZCommand = HCNetSDK.IRIS_CLOSE;
                break;
            /*雨刷*/
            case 16:
                dwPTZCommand = HCNetSDK.WIPER_PWRON;
                break;
        }
        boolean bool = hCNetSDK.NET_DVR_PTZControlWithSpeed_Other(userId, channelNum, dwPTZCommand, dwStop, speed);
        if (!bool) {
            int errorCode = hCNetSDK.NET_DVR_GetLastError();
            log.info("控制失败,请稍后重试" + errorCode);
        }
        return bool;
    }
 
    /**
     * 设置聚焦值
     *
     * @param cmd
     */
    @Override
    @SdkOperate
    public boolean setFocusPos(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer channelNum = cmd.getChannelNum();
        Integer dwFocusPos = cmd.getDwFocusPos();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return false;
        }
        Integer userId = GlobalVariable.loginMap.get(cameraId);
 
        NET_DVR_FOCUSMODE_CFG focusmodeCfg = new NET_DVR_FOCUSMODE_CFG();
        Pointer point = focusmodeCfg.getPointer();
        IntByReference ibrBytesReturned = new IntByReference(0);
        focusmodeCfg.dwFocusPos = dwFocusPos;
        boolean bool = hCNetSDK.NET_DVR_GetDVRConfig(userId, NET_DVR_GET_FOCUSMODECFG, channelNum, point, focusmodeCfg.size(), ibrBytesReturned);
        if (!bool) {
            int code = hCNetSDK.NET_DVR_GetLastError();
            log.info("设置聚焦值失败,请稍后重试" + code);
        }
        return bool;
    }
 
    /**
     * 获取聚焦值
     *
     * @param cmd
     */
    @Override
    public Map<String, Object> getFocusPos(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer channelNum = cmd.getChannelNum();
 
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return new HashMap<>();
        }
        Integer userId = GlobalVariable.loginMap.get(cameraId);
 
        NET_DVR_FOCUSMODE_CFG focusmodeCfg = new NET_DVR_FOCUSMODE_CFG();
        Pointer point = focusmodeCfg.getPointer();
        IntByReference ibrBytesReturned = new IntByReference(0);
        boolean bool = hCNetSDK.NET_DVR_GetDVRConfig(userId, NET_DVR_GET_FOCUSMODECFG, channelNum, point, focusmodeCfg.size(), ibrBytesReturned);
        if (bool) {
            focusmodeCfg.read();
            Map<String, Object> map = new HashMap<>();
            map.put("dwFocusPos", focusmodeCfg.dwFocusPos);
            map.put("byFocusDefinitionDisplay ", focusmodeCfg.byFocusDefinitionDisplay);
            map.put("dwRelativeFocusPos", focusmodeCfg.dwRelativeFocusPos);
            return map;
        } else {
            int code = hCNetSDK.NET_DVR_GetLastError();
            log.info("获取聚焦值失败,请稍后重试" + code);
            return new HashMap<>();
        }
    }
 
    /**
     * 设置预置点
     *
     * @param cmd
     */
    @Override
    @SdkOperate
    public boolean setPreset(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer channelNum = cmd.getChannelNum();
        Integer PresetIndex = cmd.getPresetIndex();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return false;
        }
        Integer userId = GlobalVariable.loginMap.get(cameraId);
        boolean bool = hCNetSDK.NET_DVR_PTZPreset_Other(userId, channelNum, SET_PRESET, PresetIndex);
        if (!bool) {
            int code = hCNetSDK.NET_DVR_GetLastError();
            log.info("预置点设置失败,请稍后重试" + code);
        }
        return bool;
    }
 
    /**
     * 转到预置点
     *
     * @param cmd
     */
    @Override
    @SdkOperate
    public boolean gotoPreset(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer channelNum = cmd.getChannelNum();
        Integer PresetIndex = cmd.getPresetIndex();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return false;
        }
        Integer userId = GlobalVariable.loginMap.get(cameraId);
        boolean bool = hCNetSDK.NET_DVR_PTZPreset_Other(userId, channelNum, GOTO_PRESET, PresetIndex);
        if (!bool) {
            int code = hCNetSDK.NET_DVR_GetLastError();
            log.info("预置点设置失败,请稍后重试" + code);
        }
        return bool;
    }
 
    /**
     * @描述 获取分辨率
     * @参数 [cameraId, channelNum]
     * @返回值 java.lang.String
     * @创建人 刘苏义
     * @创建时间 2023/2/3 8:36
     * @修改人和其它信息
     */
    @Override
    public String getVideoResolution(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer channelNum = cmd.getChannelNum();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return " ";
        }
        Integer userId = GlobalVariable.loginMap.get(cameraId);
 
        String ResResolution = "";
        NET_DVR_COMPRESSIONCFG_V30 resolution = new NET_DVR_COMPRESSIONCFG_V30();
        resolution.write();
        Pointer pioint = resolution.getPointer();
        IntByReference ibrBytesReturned = new IntByReference(0);
        try {
            boolean bool = hCNetSDK.NET_DVR_GetDVRConfig(userId, HCNetSDK.NET_DVR_GET_COMPRESSCFG_V30, channelNum, pioint, resolution.size(), ibrBytesReturned);
            if (bool) {
                resolution.read();
                //视频输出口分辨率:0- 1024x768,1- 1280x720,2-1280x1024,3- 1680x1050,4- 1920x1080,5- 3840*2160
                byte byStreamType = resolution.struNormHighRecordPara.byStreamType;
                int dwVideoBitrate = resolution.struNormHighRecordPara.dwVideoBitrate;
                int byResolution = resolution.struNormHighRecordPara.byResolution;
                switch (byResolution) {
                    case 0:
                        ResResolution = "DCIF(528*384/528*320)";
                        break;
                    case 1:
                        ResResolution = "CIF(352*288/352*240)";
                        break;
                    case 2:
                        ResResolution = "QCIF(176*144/176*120)";
                        break;
                    case 3:
                        ResResolution = "4CIF(704*576/704*480)";
                        break;
                    case 4:
                        ResResolution = "2CIF(704*288/704*240)";
                        break;
                    case 6:
                        ResResolution = "QVGA(320*240)";
                        break;
                    case 7:
                        ResResolution = "QQVGA(160*120)";
                        break;
                    case 16:
                        ResResolution = "VGA(640*480)";
                        break;
                    case 17:
                        ResResolution = "UXGA(1600*1200)";
                        break;
                    case 18:
                        ResResolution = "SVGA(800*600)";
                        break;
                    case 19:
                        ResResolution = "HD720P(1280*720)";
                        break;
                    case 20:
                        ResResolution = "XVGA(1280*960)";
                        break;
                    case 21:
                        ResResolution = "HD900P(1600*900)";
                        break;
                    case 22:
                        ResResolution = "1360*1024";
                        break;
                    case 23:
                        ResResolution = "1536*1536";
                        break;
                    case 24:
                        ResResolution = "1920*1920";
                        break;
                    case 27:
                        ResResolution = "1920*1080p";
                        break;
                    case 28:
                        ResResolution = "2560*1920";
                        break;
                    case 29:
                        ResResolution = "1600*304";
                        break;
                    case 30:
                        ResResolution = "2048*1536";
                        break;
                    default:
                        ResResolution = "不在当前分辨率索引,请联系管理员添加";
                        break;
                }
            } else {
                int code = hCNetSDK.NET_DVR_GetLastError();
                System.out.println("控制失败,请稍后重试" + code);
            }
        } catch (Exception ex) {
            log.error(ex.getMessage());
        }
        return ResResolution;
    }
 
    /**
     * @return
     * @描述 获取ptz信息
     * @参数 [userId, channelNum]
     * @返回值 boolean
     * @创建人 刘苏义
     * @创建时间 2023/1/17 16:36
     * @修改人和其它信息
     */
    @Override
    public Map<String, Object> getPtz(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer channelNum = cmd.getChannelNum();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return null;
        }
        Integer userId = GlobalVariable.loginMap.get(cameraId);
 
        NET_DVR_PTZPOS m_ptzPosCurrent = new NET_DVR_PTZPOS();
        Pointer pioint = m_ptzPosCurrent.getPointer();
        IntByReference ibrBytesReturned = new IntByReference(0);
        m_ptzPosCurrent.write();
        boolean bool = hCNetSDK.NET_DVR_GetDVRConfig(userId, HCNetSDK.NET_DVR_GET_PTZPOS, channelNum, pioint, m_ptzPosCurrent.size(), ibrBytesReturned);
        if (bool) {
            m_ptzPosCurrent.read();
            DecimalFormat df = new DecimalFormat("0.0");//设置保留位数
            //16进制转Integer后除10,保留小数点1位
            //实际显示的PTZ值是获取到的十六进制值的十分之一,
            //如获取的水平参数P的值是0x1750,实际显示的P值为175度;
            //获取到的垂直参数T的值是0x0789,实际显示的T值为78.9度;
            //获取到的变倍参数Z的值是0x1100,实际显示的Z值为110倍。
            BigDecimal b = new BigDecimal((float) Integer.parseInt(Integer.toHexString(m_ptzPosCurrent.wPanPos)) / 10);
            BigDecimal c = new BigDecimal((float) Integer.parseInt(Integer.toHexString(m_ptzPosCurrent.wTiltPos)) / 10);
            BigDecimal d = new BigDecimal((float) Integer.parseInt(Integer.toHexString(m_ptzPosCurrent.wZoomPos)) / 10);
            double p = b.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
            double t = c.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
            double z = d.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
            log.debug("T垂直参数为: " + p + "P水平参数为: " + t + "Z变倍参数为: " + z);
            Map<String, Object> ptzMap = new HashMap<>();
            ptzMap.put("p", p);
            ptzMap.put("t", t);
            ptzMap.put("z", z);
            return ptzMap;
        } else {
            int code = hCNetSDK.NET_DVR_GetLastError();
            log.info("控制失败,请稍后重试" + code);
            return null;
        }
 
    }
 
    /**
     * @描述 设置ptz信息
     * @参数 [userId, channelNum]
     * @返回值 boolean
     * @创建人 刘苏义
     * @创建时间 2023/1/17 16:36
     * @修改人和其它信息 注意俯仰角度负值需要加上360得到的正值进行设置
     */
    @Override
    @SdkOperate
    public boolean setPtz(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer channelNum = cmd.getChannelNum();
        Map<String, Double> ptz = cmd.getPtzMap();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return false;
        }
        Integer userId = GlobalVariable.loginMap.get(cameraId);
        NET_DVR_PTZPOS m_ptzPosCurrent = new NET_DVR_PTZPOS();
        m_ptzPosCurrent.wAction = 1;
        try {
            String p = String.valueOf((int) ((double) ptz.get("p") * 10));
            String t = String.valueOf((int) ((double) ptz.get("t") * 10));
            String z = String.valueOf((int) ((double) ptz.get("z") * 10));
            m_ptzPosCurrent.wPanPos = (short) (Integer.parseInt(p, 16));
            m_ptzPosCurrent.wTiltPos = (short) (Integer.parseInt(t, 16));
            m_ptzPosCurrent.wZoomPos = (short) (Integer.parseInt(z, 16));
            Pointer point = m_ptzPosCurrent.getPointer();
            m_ptzPosCurrent.write();
            boolean bool = hCNetSDK.NET_DVR_SetDVRConfig(userId, NET_DVR_SET_PTZPOS, channelNum, point, m_ptzPosCurrent.size());
            if (!bool) {
                int code = hCNetSDK.NET_DVR_GetLastError();
                log.info("设置ptz失败,请稍后重试" + code);
            }
            return bool;
        } catch (Exception ex) {
            log.error(ex.getMessage());
            return false;
        }
    }
 
    @Override
    @SdkOperate
    public boolean guideTargetPosition(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer channelNum = cmd.getChannelNum();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return false;
        }
        Integer userId = GlobalVariable.loginMap.get(cameraId);
        NET_DVR_PTZPOS m_ptzPosCurrent = new NET_DVR_PTZPOS();
        m_ptzPosCurrent.wAction = 1;
        try {
            ArdCameras cameras = ardCamerasMapper.selectArdCamerasById(cameraId);
            double[] cameraPositon = new double[]{cameras.getLongitude(), cameras.getLatitude(), cameras.getAltitude()};
            double[] targetPositions = cmd.getTargetPosition();
            double[] cameraPTZ = GisUtil.getCameraPTZ(cameraPositon, targetPositions, 20, 150);
            String p = String.valueOf((int) (cameraPTZ[0] * 10));
            String t = String.valueOf((int) (cameraPTZ[1] * 10));
            String z = String.valueOf((int) (cameraPTZ[2] * 10));
            m_ptzPosCurrent.wPanPos = (short) (Integer.parseInt(p, 16));
            m_ptzPosCurrent.wTiltPos = (short) (Integer.parseInt(t, 16));
            m_ptzPosCurrent.wZoomPos = (short) (Integer.parseInt(z, 16));
            Pointer point = m_ptzPosCurrent.getPointer();
            m_ptzPosCurrent.write();
            boolean bool = hCNetSDK.NET_DVR_SetDVRConfig(userId, NET_DVR_SET_PTZPOS, channelNum, point, m_ptzPosCurrent.size());
            if (!bool) {
                int code = hCNetSDK.NET_DVR_GetLastError();
                log.info("设置ptz失败,请稍后重试" + code);
            }
            return bool;
        } catch (Exception ex) {
            log.error("引导异常:" + ex.getMessage());
            return false;
        }
    }
 
    /**
     * @描述 操控锁定
     * @参数 [userId, channelNum]
     * @返回值 boolean
     * @创建人 刘苏义
     * @创建时间 2023/1/17 16:36
     * @修改人和其它信息 0-解锁 1-锁定
     */
    @Override
    public boolean controlLock(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();//申请锁的相机
        ArdCameras ardCameras = ardCamerasMapper.selectArdCamerasById(cameraId);
        if (StringUtils.isNull(ardCameras)) {
            return false;//找不到相机拒绝操控
        }
        Integer expired = cmd.getExpired();//申请控制时长
        String operator = cmd.getOperator();//申请者
        String currentOperator = ardCameras.getOperatorId();//相机当前控制者
        Date currentExpired = ardCameras.getOperatorExpired();//相机当前过期时间
        if (currentExpired == null) {
            //设置当前操作用户ID
            ardCameras.setOperatorId(operator);
            //设置当前过期时间
            Date now = new Date();
            now.setTime(now.getTime() + expired * 1000);
            ardCameras.setOperatorExpired(now);
            ardCamerasMapper.updateArdCameras(ardCameras);
        } else {
            //如果过期时间有值
            //如果是本人直接修改
            if (currentOperator.equals(operator)) {
                //设置当前过期时间
                Date now = new Date();
                now.setTime(now.getTime() + expired * 1000);
                ardCameras.setOperatorExpired(now);
                ardCamerasMapper.updateArdCameras(ardCameras);
            } else {
                //如果非本人比较优先级
                Integer currentLevel = 0;//当前操作者的优先级
                if (CamPriority.priorityMap.containsKey(currentOperator)) {
                    /*当前控制者为系统报警用户*/
                    currentLevel = (Integer) CamPriority.priorityMap.get(currentOperator);
                } else {
                    /*当前控制者为普通用户*/
                    SysUser sysUser = sysUserMapper.selectUserById(currentOperator);
                    currentLevel = sysUser.getCameraPriority();
                }
                Integer operatorLevel = 0;//获取申请者的优先级
                if (CamPriority.priorityMap.containsKey(operator)) {
                    /*包含说明当前申请控制者为系统报警用户*/
                    operatorLevel = (Integer) CamPriority.priorityMap.get(operator);
                } else {
                    /*否则申请控制者为当前登录用户*/
                    LoginUser loginUser = SecurityUtils.getLoginUser();
                    SysUser user = loginUser.getUser();//获取登录用户的信息
                    operatorLevel = user.getCameraPriority();
                }
                //判断优先级
                if (operatorLevel > currentLevel) {
                    Date now = new Date();
                    now.setTime(now.getTime() + expired * 60);
                    ardCameras.setOperatorExpired(now);//设置当前过期时间
                    ardCameras.setOperatorId(operator);//设置当前用户
                    ardCamerasMapper.updateArdCameras(ardCameras);
                } else {
                    return false;//优先级低无法上锁
                }
            }
        }
        return true;
    }
 
    /**
     * @描述 获取ptz锁定信息
     * @参数 [userId, channelNum]
     * @返回值 boolean
     * @创建人 刘苏义
     * @创建时间 2023/1/17 16:36
     * @修改人和其它信息 0-解锁 1-锁定
     */
    @Override
    public int getPTZLockInfo(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer channelNum = cmd.getChannelNum();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return -1;
        }
        Integer userId = GlobalVariable.loginMap.get(cameraId);
        NET_DVR_PTZ_LOCKCFG netDvrPtzLockcfg = new NET_DVR_PTZ_LOCKCFG();
        Pointer point = netDvrPtzLockcfg.getPointer();
        IntByReference ibrBytesReturned = new IntByReference(0);
        netDvrPtzLockcfg.write();
        boolean bool = hCNetSDK.NET_DVR_GetDVRConfig(userId, NET_DVR_GET_PTZLOCKCFG, channelNum, point, netDvrPtzLockcfg.size(), ibrBytesReturned);
        if (!bool) {
            int code = hCNetSDK.NET_DVR_GetLastError();
            log.info("获取ptz锁定信息失败,请稍后重试" + code);
            return -1;
        } else {
            netDvrPtzLockcfg.read();
            int byWorkMode = netDvrPtzLockcfg.byWorkMode;
            return byWorkMode;
        }
    }
 
    /**
     * @描述 设置零方位角
     * @参数 [userId, channelNum]
     * @返回值 boolean
     * @创建人 刘苏义
     * @创建时间 2023/1/17 16:36
     * @修改人和其它信息 注意俯仰角度负值需要加上360得到的正值进行设置
     */
    @Override
    @SdkOperate
    public boolean setZeroPtz(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer channelNum = cmd.getChannelNum();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return false;
        }
        Integer userId = GlobalVariable.loginMap.get(cameraId);
        NET_DVR_INITIALPOSITIONCTRL initialpositionctrl = new NET_DVR_INITIALPOSITIONCTRL();
 
        initialpositionctrl.dwSize = initialpositionctrl.size();
        initialpositionctrl.byWorkMode = 0;
        initialpositionctrl.dwChan = Short.parseShort(channelNum.toString());
 
        Pointer point = initialpositionctrl.getPointer();
        initialpositionctrl.write();
        boolean bool = hCNetSDK.NET_DVR_RemoteControl(userId, NET_DVR_PTZ_INITIALPOSITIONCTRL, point, initialpositionctrl.size());
        if (!bool) {
            int i = hCNetSDK.NET_DVR_GetLastError();
            log.error("错误码:" + i);
        }
        return bool;
    }
 
    /**
     * @描述 获取球机PTZ参数取值范围
     * @参数 [cameraId, channelNum]
     * @返回值 java.util.Map<java.lang.String, java.lang.Object>
     * @创建人 刘苏义
     * @创建时间 2023/2/8 10:58
     * @修改人和其它信息
     */
    @Override
    public Map<String, Object> getPtzScope(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer channelNum = cmd.getChannelNum();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return new HashMap<>();
        }
        Integer userId = GlobalVariable.loginMap.get(cameraId);
        NET_DVR_PTZSCOPE m_ptzPosCurrent = new NET_DVR_PTZSCOPE();
        Pointer point = m_ptzPosCurrent.getPointer();
        IntByReference ibrBytesReturned = new IntByReference(0);
        m_ptzPosCurrent.write();
        boolean bool = hCNetSDK.NET_DVR_GetDVRConfig(userId, NET_DVR_GET_PTZSCOPE, channelNum, point, m_ptzPosCurrent.size(), ibrBytesReturned);
        if (!bool) {
            int code = hCNetSDK.NET_DVR_GetLastError();
            log.info("设置ptz失败,请稍后重试" + code);
            return new HashMap<>();
        } else {
            m_ptzPosCurrent.read();
            DecimalFormat df = new DecimalFormat("0.0");//设置保留位数
 
            String wPanPosMax = df.format((float) Integer.parseInt(Integer.toHexString(m_ptzPosCurrent.wPanPosMax)) / 10);
            String wPanPosMin = df.format((float) Integer.parseInt(Integer.toHexString(m_ptzPosCurrent.wTiltPosMax)) / 10);
            String wTiltPosMax = df.format((float) Integer.parseInt(Integer.toHexString(m_ptzPosCurrent.wTiltPosMax)) / 10);
            String wTiltPosMin = df.format((float) Integer.parseInt(Integer.toHexString(m_ptzPosCurrent.wTiltPosMin)) / 10);
            String wZoomPosMax = df.format((float) Integer.parseInt(Integer.toHexString(m_ptzPosCurrent.wZoomPosMax)) / 10);
            String wZoomPosMin = df.format((float) Integer.parseInt(Integer.toHexString(m_ptzPosCurrent.wZoomPosMin)) / 10);
            Map<String, Object> ptzScopeMap = new HashMap<>();
            ptzScopeMap.put("pMax", wPanPosMax);
            ptzScopeMap.put("pMin", wPanPosMin);
            ptzScopeMap.put("tMax", wTiltPosMax);
            ptzScopeMap.put("tMin", wTiltPosMin);
            ptzScopeMap.put("zMax", wZoomPosMax);
            ptzScopeMap.put("zMin", wZoomPosMin);
            return ptzScopeMap;
        }
 
    }
 
    /**
     * @描述 透雾开关
     * @参数 [userId, channelNum, enable]
     * @返回值 boolean
     * @创建人 刘苏义
     * @创建时间 2023/1/18 13:07
     * @修改人和其它信息
     */
    @Override
    @SdkOperate
    public boolean controlDefogcfg(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        boolean enable = cmd.isEnable();
        Integer channelNum = cmd.getChannelNum();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return false;
        }
        Integer userId = GlobalVariable.loginMap.get(cameraId);
        NET_DVR_CAMERAPARAMCFG_EX struCameraParam = new NET_DVR_CAMERAPARAMCFG_EX();
        Pointer point = struCameraParam.getPointer();
        IntByReference ibrBytesReturned = new IntByReference(0);
        boolean b_GetCameraParam = hCNetSDK.NET_DVR_GetDVRConfig(userId, NET_DVR_GET_CCDPARAMCFG_EX, channelNum, point, struCameraParam.size(), ibrBytesReturned);
        if (!b_GetCameraParam) {
            log.error("获取前端参数失败,错误码:" + hCNetSDK.NET_DVR_GetLastError());
        }
        struCameraParam.read();
        log.info("是否开启透雾:" + struCameraParam.struDefogCfg.byMode);
 
        NET_DVR_DEFOGCFG defogcfg = new NET_DVR_DEFOGCFG();
        if (enable) {
            defogcfg.byMode = 2;//0-不启用 1-自动模式 2-常开模式
            defogcfg.byLevel = 100;//取值范围0-100
        } else {
            defogcfg.byMode = 0;//0-不启用 1-自动模式 2-常开模式
        }
        struCameraParam.struDefogCfg = defogcfg;
        struCameraParam.write();
        boolean bool = hCNetSDK.NET_DVR_SetDVRConfig(userId, NET_DVR_SET_CCDPARAMCFG_EX, channelNum, point, struCameraParam.size());
        if (!bool) {
            int code = hCNetSDK.NET_DVR_GetLastError();
            log.info("设置透雾失败,请稍后重试" + code);
            return false;
        }
        log.info("设置透雾成功");
        return bool;
    }
 
    /**
     * @描述 红外开关
     * @参数 [userId, channelNum, enable]
     * @返回值 boolean
     * @创建人 刘苏义
     * @创建时间 2023/1/18 13:07
     * @修改人和其它信息
     */
    @Override
    @SdkOperate
    public boolean controlInfrarecfg(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        boolean enable = cmd.isEnable();
        Integer channelNum = cmd.getChannelNum();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return false;
        }
        Integer userId = GlobalVariable.loginMap.get(cameraId);
        NET_DVR_CAMERAPARAMCFG_EX struDayNigh = new NET_DVR_CAMERAPARAMCFG_EX();
        Pointer point = struDayNigh.getPointer();
        IntByReference ibrBytesReturned = new IntByReference(0);
        boolean b_GetCameraParam = hCNetSDK.NET_DVR_GetDVRConfig(userId, NET_DVR_GET_CCDPARAMCFG_EX, channelNum, point, struDayNigh.size(), ibrBytesReturned);
        if (!b_GetCameraParam) {
            log.error("获取前端参数失败,错误码:" + hCNetSDK.NET_DVR_GetLastError());
        }
        struDayNigh.read();
        log.info("是否开启夜视:" + struDayNigh.struDayNight.byDayNightFilterType);
 
        NET_DVR_DAYNIGHT daynight = new NET_DVR_DAYNIGHT();
        if (enable) {
            daynight.byDayNightFilterType = 1;//夜晚
 
        } else {
            daynight.byDayNightFilterType = 0;//白天
        }
        daynight.bySwitchScheduleEnabled = 1;
        daynight.byDayNightFilterTime = 60;
        struDayNigh.struDayNight = daynight;
        struDayNigh.write();
        boolean bool = hCNetSDK.NET_DVR_SetDVRConfig(userId, NET_DVR_SET_CCDPARAMCFG_EX, channelNum, point, struDayNigh.size());
        if (!bool) {
            int code = hCNetSDK.NET_DVR_GetLastError();
            log.info("设置夜视失败,请稍后重试" + code);
            return false;
        }
        log.info("设置夜视成功");
        return bool;
    }
 
    /**
     * @描述 聚焦开关
     * @参数 [userId, channelNum, enable]
     * @返回值 boolean
     * @创建人 刘苏义
     * @创建时间 2023/1/18 13:07
     * @修改人和其它信息
     */
    @Override
    @SdkOperate
    public boolean controlFocusMode(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        boolean enable = cmd.isEnable();
        Integer channelNum = cmd.getChannelNum();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return false;
        }
        Integer userId = GlobalVariable.loginMap.get(cameraId);
        NET_DVR_FOCUSMODE_CFG struFocusMode = new NET_DVR_FOCUSMODE_CFG();
        Pointer point = struFocusMode.getPointer();
        IntByReference ibrBytesReturned = new IntByReference(0);
        boolean b_GetCameraParam = hCNetSDK.NET_DVR_GetDVRConfig(userId, NET_DVR_GET_FOCUSMODECFG, channelNum, point, struFocusMode.size(), ibrBytesReturned);
        if (!b_GetCameraParam) {
            System.out.println("获取前端参数失败,错误码:" + hCNetSDK.NET_DVR_GetLastError());
        }
        struFocusMode.read();
        log.info("当前聚焦模式:" + struFocusMode.byFocusMode);
 
        if (enable) {
            struFocusMode.byFocusMode = 1;//手动聚焦
        } else {
            struFocusMode.byAutoFocusMode = 1;
            struFocusMode.byFocusMode = 2;//自动聚焦
        }
        struFocusMode.byFocusDefinitionDisplay = 1;
        struFocusMode.byFocusSpeedLevel = 3;
        struFocusMode.write();
        boolean bool = hCNetSDK.NET_DVR_SetDVRConfig(userId, NET_DVR_SET_FOCUSMODECFG, channelNum, point, struFocusMode.size());
        if (!bool) {
            int code = hCNetSDK.NET_DVR_GetLastError();
            log.info("设置聚焦模式失败,请稍后重试" + code);
            return false;
        }
        log.info("设置聚焦模式成功");
        return bool;
    }
 
    /**
     * @描述 云台加热开关
     * @参数 [userId, channelNum, enable]
     * @返回值 boolean
     * @创建人 刘苏义
     * @创建时间 2023/1/18 13:07
     * @修改人和其它信息
     */
    @Override
    @SdkOperate
    public boolean controlPTHeateRpwron(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        boolean enable = cmd.isEnable();
        Integer channelNum = cmd.getChannelNum();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return false;
        }
        Integer userId = GlobalVariable.loginMap.get(cameraId);
        Integer dwStop;
        if (enable) {
            dwStop = 0;//开启
        } else {
            dwStop = 1;//关闭
        }
        boolean bool = hCNetSDK.NET_DVR_PTZControl_Other(userId, channelNum, HEATER_PWRON, dwStop);
        if (!bool) {
            int code = hCNetSDK.NET_DVR_GetLastError();
            log.info("设置云台加热失败,请稍后重试" + code);
            return false;
        }
        log.info("设置云台加热成功");
        return bool;
    }
 
    /**
     * @描述 镜头加热开关
     * @参数 [userId, channelNum, enable]
     * @返回值 boolean
     * @创建人 刘苏义
     * @创建时间 2023/1/18 13:07
     * @修改人和其它信息
     */
    @Override
    @SdkOperate
    public boolean controlCameraDeicing(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        boolean enable = cmd.isEnable();
        Integer channelNum = cmd.getChannelNum();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return false;
        }
        Integer userId = GlobalVariable.loginMap.get(cameraId);
        NET_DVR_DEVSERVER_CFG struDeicing = new NET_DVR_DEVSERVER_CFG();
        Pointer point = struDeicing.getPointer();
        IntByReference ibrBytesReturned = new IntByReference(0);
        boolean b_GetCameraParam = hCNetSDK.NET_DVR_GetDVRConfig(userId, NET_DVR_GET_DEVSERVER_CFG, channelNum, point, struDeicing.size(), ibrBytesReturned);
        if (!b_GetCameraParam) {
            log.error("获取前端参数失败,错误码:" + hCNetSDK.NET_DVR_GetLastError());
        }
        struDeicing.read();
        log.info("是否开启除冰:" + struDeicing.byEnableDeicing);
 
        if (enable) {
            struDeicing.byEnableDeicing = 1;//开启
        } else {
            struDeicing.byEnableDeicing = 0;//关闭
        }
        struDeicing.write();
        boolean bool = hCNetSDK.NET_DVR_SetDVRConfig(userId, NET_DVR_SET_DEVSERVER_CFG, channelNum, point, struDeicing.size());
        if (!bool) {
            int code = hCNetSDK.NET_DVR_GetLastError();
            log.info("设置镜头除冰失败,请稍后重试" + code);
            return false;
        }
        log.info("设置镜头除冰成功");
        return bool;
    }
 
    /**
     * 截图 返给前端
     *
     * @param cmd
     */
    public String captureJPEGPicture(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer channelNum = cmd.getChannelNum();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return "";
        }
        Integer userId = GlobalVariable.loginMap.get(cameraId);
        NET_DVR_WORKSTATE_V30 devwork = new NET_DVR_WORKSTATE_V30();
        if (!hCNetSDK.NET_DVR_GetDVRWorkState_V30(userId, devwork)) {
            // 返回Boolean值,判断是否获取设备能力
            log.error("抓图失败,请稍后重试");
        }
        //图片质量
        NET_DVR_JPEGPARA jpeg = new NET_DVR_JPEGPARA();
        //设置图片分辨率
        jpeg.wPicSize = 0;
        //设置图片质量
        jpeg.wPicQuality = 0;
        IntByReference a = new IntByReference();
        //设置图片大小
        ByteBuffer jpegBuffer = ByteBuffer.allocate(1024 * 1024);
        // 抓图到内存,单帧数据捕获并保存成JPEG存放在指定的内存空间中
        boolean is = hCNetSDK.NET_DVR_CaptureJPEGPicture_NEW(userId, channelNum, jpeg, jpegBuffer, 1024 * 1024, a);
        log.info("-----------这里开始图片存入内存----------" + is);
 
 
        BASE64Encoder encoder = new BASE64Encoder();
        String png_base64 = encoder.encodeBuffer(jpegBuffer);//转换成base64串
        png_base64 = png_base64.replaceAll("\n", "").replaceAll("\r", "");//删除 \r\n
        log.info("-----------处理完成截图数据----------");
        return png_base64;
 
//        ByteArrayInputStream in = new ByteArrayInputStream(jpegBuffer.array(), 0, a.getValue());
//        OutputStream outputStream = null;
//        try {
//            //1、设置response 响应头 //设置页面不缓存,清空buffer
//            response.reset();
//            //字符编码
//            response.setCharacterEncoding("UTF-8");
//            //二进制传输数据
//            response.setContentType("multipart/form-data");
//            //设置响应头
//            response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".jpeg", "UTF-8"));
//
//            outputStream = response.getOutputStream();
//            // LoginUser loginUser = LoginContext.me().getLoginUser();
//            waterMarkUtil.markImageByIO("", in, outputStream, null, "jpeg");
//            outputStream.flush();
//        } catch (Exception e) {
//            e.printStackTrace();
//            log.error("抓图失败,请稍后重试");
//        } finally {
//            try {
//                outputStream.close();
//            } catch (IOException e) {
//                log.error("抓图失败,请稍后重试");
//            }
//        }
 
 
    }
 
    /**
     * @描述 截图 存服务器
     * @参数 [cameraId, channelNum]
     * @返回值 java.lang.String
     * @创建人 刘苏义
     * @创建时间 2023/2/2 14:59
     * @修改人和其它信息
     */
    @Override
    @SdkOperate
    public String picCutCate(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer channelNum = cmd.getChannelNum();
        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
            return "";
        }
        Integer userId = GlobalVariable.loginMap.get(cameraId);
        //图片信息
        NET_DVR_JPEGPARA jpeg = new NET_DVR_JPEGPARA();
        //设置图片分辨率
        //图片尺寸:0-CIF(352*288/352*240),1-QCIF(176*144/176*120),2-4CIF(704*576/704*480)或D1(720*576/720*486),3-UXGA(1600*1200),
        // 4-SVGA(800*600),5-HD720P(1280*720),6-VGA(640*480),7-XVGA(1280*960),8-HD900P(1600*900),9-HD1080P(1920*1080),10-2560*1920,
        // 11-1600*304,12-2048*1536,13-2448*2048,14-2448*1200,15-2448*800,16-XGA(1024*768),17-SXGA(1280*1024),18-WD1(960*576/960*480),
        // 19-1080I(1920*1080),20-576*576,21-1536*1536,22-1920*1920,23-320*240,24-720*720,25-1024*768,26-1280*1280,27-1600*600,
        // 28-2048*768,29-160*120,75-336*256,78-384*256,79-384*216,80-320*256,82-320*192,83-512*384,127-480*272,128-512*272, 161-288*320,
        // 162-144*176,163-480*640,164-240*320,165-120*160,166-576*720,167-720*1280,168-576*960,180-180*240, 181-360*480, 182-540*720,
        // 183-720*960, 184-960*1280, 185-1080*1440, 500-384*288, 0xff-Auto(使用当前码流分辨率)
        jpeg.wPicSize = 0;
        //设置图片质量:0-最好,1-较好,2-一般
        jpeg.wPicQuality = 0;
        IntByReference a = new IntByReference();
        //设置图片大小
        ByteBuffer jpegBuffer = ByteBuffer.allocate(1024 * 1024);
        // 抓图到内存,单帧数据捕获并保存成JPEG存放在指定的内存空间中
        log.info("-----------这里开始封装 NET_DVR_CaptureJPEGPicture_NEW---------");
        boolean is = hCNetSDK.NET_DVR_CaptureJPEGPicture_NEW(userId, channelNum, jpeg, jpegBuffer, 1024 * 1024, a);
        log.info("-----------这里开始图片存入内存----------" + is);
        if (is) {
            log.info("hksdk(抓图)-结果状态值(0表示成功):" + hCNetSDK.NET_DVR_GetLastError());
            byte[] array = jpegBuffer.array();
            //存储到minio
            String BucketName = "pic";
            String uuid = UUID.randomUUID().toString().replace("-", "");
            String time = new SimpleDateFormat("yyyyMMdd").format(new Date());
            String ObjectName = time + "/" + uuid + ".jpeg";
            String ContentType = "image/JPEG";
            InputStream input = new ByteArrayInputStream(array);
            String url = "";
            try {
                boolean b = MinioUtils.uploadObject(BucketName, ObjectName, input, input.available(), ContentType);
                if (b) {
                    url = MinioUtils.getBucketObjectUrl(BucketName, ObjectName);
                    log.info("上传文件成功!" + url);
                }
            } catch (IOException ex) {
                log.error("上传文件异常:" + ex.getMessage());
            }
            return url;
        } else {
            int code = hCNetSDK.NET_DVR_GetLastError();
            log.info("抓图失败,请稍后重试" + code);
            return "";
        }
    }
 
    /**
     * @描述 短时录像
     * @参数 [userId, channelNum, enable]
     * @返回值 void
     * @创建人 刘苏义
     * @创建时间 2023/1/20 11:18
     * @修改人和其它信息
     */
    @Override
    public String record(CameraCmd cmd) {
        try {
            String cameraId = cmd.getCameraId();
            Integer channelNum = cmd.getChannelNum();
            String path = FileUtils.createFile("D:/recordTemp/" + cameraId + ".mp4");
            boolean enable = cmd.isEnable();
            if (!GlobalVariable.loginMap.containsKey(cameraId)) {
                return "";
            }
            Integer userId = GlobalVariable.loginMap.get(cameraId);
            //强制I帧结构体对象
            HCNetSDK.NET_DVR_I_FRAME netDvrIFrame = new HCNetSDK.NET_DVR_I_FRAME();   //新建结构体对象
            netDvrIFrame.read();
            netDvrIFrame.dwChannel = channelNum;//因为上文代码中设置了通道号,按照上文中的设置
            netDvrIFrame.byStreamType = 0;
            netDvrIFrame.dwSize = netDvrIFrame.size();
            netDvrIFrame.write();
            if (!hCNetSDK.NET_DVR_RemoteControl(userId, 3402, netDvrIFrame.getPointer(), netDvrIFrame.dwSize)) {
                log.error("强制I帧 错误码为:  " + hCNetSDK.NET_DVR_GetLastError());
            }
            //预览参数
            NET_DVR_PREVIEWINFO previewinfo = new NET_DVR_PREVIEWINFO();
            previewinfo.read();
            previewinfo.lChannel = channelNum;
            previewinfo.dwStreamType = 0;//码流类型:0-主码流,1-子码流,2-三码流,3-虚拟码流,以此类推
            previewinfo.dwLinkMode = 0;//连接方式:0-TCP方式,1-UDP方式,2-多播方式,3-RTP方式,4-RTP/RTSP,5-RTP/HTTP,6-HRUDP(可靠传输),7-RTSP/HTTPS,8-NPQ
            previewinfo.hPlayWnd = null;//播放窗口的句柄,为NULL表示不解码显示。
            previewinfo.bBlocked = 0;//0- 非阻塞取流,1-阻塞取流
            previewinfo.byNPQMode = 0;//NPQ模式:0-直连模式,1-过流媒体模式
            previewinfo.write();
            String url = "";
            if (enable) {
                if (!GlobalVariable.previewMap.containsKey(cameraId)) {
                    int lRealHandle = hCNetSDK.NET_DVR_RealPlay_V40(userId, previewinfo, null, null);
                    if (lRealHandle == -1) {
                        log.error("取流失败" + hCNetSDK.NET_DVR_GetLastError());
                        return "";
                    }
                    log.info("取流成功");
                    GlobalVariable.previewMap.put(cameraId, lRealHandle);
                }
                if (!hCNetSDK.NET_DVR_SaveRealData_V30(GlobalVariable.previewMap.get(cameraId), 2, path)) {
                    log.error("保存视频文件到临时文件夹失败 错误码为:  " + hCNetSDK.NET_DVR_GetLastError());
                    return "";
                }
                log.info("录像开始");
            } else {
                if (GlobalVariable.previewMap.containsKey(cameraId)) {
                    Integer lRealHandle = GlobalVariable.previewMap.get(cameraId);
                    hCNetSDK.NET_DVR_StopRealPlay(lRealHandle);
                    GlobalVariable.previewMap.remove(cameraId);
                }
                log.info("录像停止");
                //存入minio
                String BucketName = cmd.getRecordBucketName();
                String ObjectName = cmd.getRecordObjectName();
                String ContentType = "video/MP4";
                FileInputStream stream = new FileInputStream(path);
                boolean b = MinioUtils.uploadObject(BucketName, ObjectName, stream, stream.available(), ContentType);
                if (b) {
                    url = MinioUtils.getBucketObjectUrl(BucketName, ObjectName);
                    log.info("上传文件成功!" + MinioClientSingleton.domainUrl + "/" + BucketName + "/" + ObjectName);
                }
            }
            return url;
        } catch (Exception ex) {
            log.error("录像异常" + ex.getMessage());
            return "";
        }
    }
 
    @Override
    public String recordToMinio(CameraCmd cmd) {
        try {
            String cameraId = cmd.getCameraId();
            Integer channelNum = cmd.getChannelNum();
            String path = FileUtils.createFile("D:/recordTemp/" + cameraId + ".mp4");
            boolean enable = cmd.isEnable();
            if (!GlobalVariable.loginMap.containsKey(cameraId)) {
                return "";
            }
            Integer userId = GlobalVariable.loginMap.get(cameraId);
            //强制I帧结构体对象
            HCNetSDK.NET_DVR_I_FRAME netDvrIFrame = new HCNetSDK.NET_DVR_I_FRAME();   //新建结构体对象
            netDvrIFrame.read();
            netDvrIFrame.dwChannel = channelNum;//因为上文代码中设置了通道号,按照上文中的设置
            netDvrIFrame.byStreamType = 0;
            netDvrIFrame.dwSize = netDvrIFrame.size();
            netDvrIFrame.write();
            if (!hCNetSDK.NET_DVR_RemoteControl(userId, 3402, netDvrIFrame.getPointer(), netDvrIFrame.dwSize)) {
                log.error("强制I帧 错误码为:  " + hCNetSDK.NET_DVR_GetLastError());
            }
            //预览参数
            NET_DVR_PREVIEWINFO previewinfo = new NET_DVR_PREVIEWINFO();
            previewinfo.read();
            previewinfo.lChannel = channelNum;
            previewinfo.dwStreamType = 0;//码流类型:0-主码流,1-子码流,2-三码流,3-虚拟码流,以此类推
            previewinfo.dwLinkMode = 0;//连接方式:0-TCP方式,1-UDP方式,2-多播方式,3-RTP方式,4-RTP/RTSP,5-RTP/HTTP,6-HRUDP(可靠传输),7-RTSP/HTTPS,8-NPQ
            previewinfo.hPlayWnd = null;//播放窗口的句柄,为NULL表示不解码显示。
            previewinfo.bBlocked = 0;//0- 非阻塞取流,1-阻塞取流
            previewinfo.byNPQMode = 0;//NPQ模式:0-直连模式,1-过流媒体模式
            previewinfo.write();
            if (enable) {
                if (GlobalVariable.previewMap.containsKey(cameraId)) {
                    Integer lRealHandle = GlobalVariable.previewMap.get(cameraId);
                    hCNetSDK.NET_DVR_StopRealPlay(lRealHandle);
                    GlobalVariable.previewMap.remove(cameraId);
                    log.debug("停止当前录像");
                }
                int lRealHandle = hCNetSDK.NET_DVR_RealPlay_V40(userId, previewinfo, null, null);
                if (lRealHandle == -1) {
                    log.error("取流失败" + hCNetSDK.NET_DVR_GetLastError());
                    return "";
                }
                log.debug("取流成功");
                GlobalVariable.threadMap.put(cameraId, Thread.currentThread().getName());
                GlobalVariable.previewMap.put(cameraId, lRealHandle);
                if (!hCNetSDK.NET_DVR_SaveRealData_V30(GlobalVariable.previewMap.get(cameraId), 2, path)) {
                    log.error("保存视频文件到临时文件夹失败 错误码为:  " + hCNetSDK.NET_DVR_GetLastError());
                    return "";
                }
                log.debug("录像开始");
            } else {
                if (GlobalVariable.previewMap.containsKey(cameraId)) {
                    Integer lRealHandle = GlobalVariable.previewMap.get(cameraId);
                    hCNetSDK.NET_DVR_StopRealPlay(lRealHandle);
                    GlobalVariable.previewMap.remove(cameraId);
                }
                log.debug("录像停止");
                //存入minio
                String BucketName = cmd.getRecordBucketName();
                String ObjectName = cmd.getRecordObjectName();
                String ContentType = "video/MP4";
                FileInputStream stream = new FileInputStream(path);
                String simpleUUID = IdUtils.simpleUUID();
                String time = new SimpleDateFormat("yyyyMMdd").format(new Date());
                String recordName = cameraId + "/" + time + "/" + ObjectName +"_"+ simpleUUID + ".mp4";
                boolean b = MinioUtils.uploadObject(BucketName, recordName, stream, stream.available(), ContentType);
                if (b) {
                    String url = MinioClientSingleton.domainUrl + "/" + BucketName + "/" + recordName;
                    log.debug("上传文件成功!" + url);
                    return url;
                }
            }
            return "";
        } catch (Exception ex) {
            log.error("录像异常" + ex.getMessage());
            return "";
        }
    }
}