liusuyi
2026-05-18 b5cd784d0cde5b7c82ea78aef4ac0e5a161d8c5d
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
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
package com.ard.work.sdk.hp.service;
 
import com.ard.common.core.constant.CacheConstants;
import com.ard.common.core.domain.R;
import com.ard.common.core.exception.ServiceException;
import com.ard.common.core.exception.device.CameraSDKException;
import com.ard.common.core.utils.StringUtils;
import com.ard.common.core.utils.file.FileMultipartFile;
import com.ard.common.core.utils.file.FileUtils;
import com.ard.common.core.utils.file.MimeTypeUtils;
import com.ard.common.redis.service.RedisService;
import com.ard.system.api.RemoteFileService;
import com.ard.system.api.domain.SysFile;
import com.ard.work.api.domian.*;
import com.ard.work.device.camera.domain.PtzParamDTO;
import com.ard.work.event.HepuLoginEvent;
import com.ard.work.event.LoginEvent;
import com.ard.work.sdk.common.GlobalVariable;
import com.ard.work.sdk.common.SdkErrorCodeEnum;
import com.ard.work.sdk.fjr.netty.FjrNettyClient;
import com.ard.work.sdk.fjr.netty.ThermalCommand;
import com.ard.work.sdk.hik.lib.HCNetSDK;
import com.ard.work.sdk.hik.service.AbstractHikVisionSDK;
import com.ard.work.sdk.hp.cache.CameraPTZCache;
import com.ard.work.sdk.hp.cache.CameraSerialCache;
import com.ard.work.sdk.model.PtzScope;
import com.ard.work.utils.ArdTool;
import com.ard.work.utils.FFmpegUtils;
import com.ard.work.utils.gis.GisUtil;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
 
import static com.ard.work.sdk.dh.lib.ToolKits.getErrorCodePrint;
import static com.ard.work.sdk.hik.lib.HCNetSDK.*;
 
/**
 * @Description: 合浦sdk策略 继承海康抽象类
 * @ClassName: HpSDK
 * @Author: 刘苏义
 * @Date: 2026年03月06日13:05:09
 **/
@Service("hpSDK")
@Slf4j
public class HpSDK extends AbstractHikVisionSDK {
 
    @Resource
    private RedisService redisService;
 
    @Resource
    private ApplicationEventPublisher eventPublisher;
 
    @Value("${sdk.tempDir}")
    private String tempDir;
 
    @Resource
    private RemoteFileService remoteFileService;
 
 
    @Resource
    private FjrNettyClient nettyClient;
 
    //登录
    @Override
    public void login(ArdCamera camera) {
        // --- 步骤 1: 登录主设备---
        long masterLoginId = doSingleCameraLogin(camera);
        if (masterLoginId < 0) {
            int errCode = hCNetSDK.NET_DVR_GetLastError();
            log.warn("设备[{}:{}]登录失败,错误码:{} 原因:{}", camera.getIp(), camera.getPort(), errCode,
                    SdkErrorCodeEnum.getDescByCode(errCode));
            handleLoginFail(camera);
            eventPublisher.publishEvent(new LoginEvent(camera));
            return; // 主设备失败,直接终止,无需尝试子设备
        }
        camera.setLoginId(masterLoginId);
        int serialHandle = initTransparentSerial(camera);
        if (serialHandle < 0) {
            int errorCode = hCNetSDK.NET_DVR_GetLastError();
            log.warn("【{}:{}】建立透明通道失败,错误码:{} 原因:{}", camera.getIp(), camera.getPort(), errorCode,
                    SdkErrorCodeEnum.getDescByCode(errorCode));
            handleLoginFail(camera);
            eventPublisher.publishEvent(new LoginEvent(camera));
            return;// 建立透明串口失败,直接终止,无需尝试子设备
        }
        CameraSerialCache.put(camera.getId(), serialHandle);
        log.info("【{}:{}】建立透明通道成功,serialHandle:{}", camera.getIp(), camera.getPort(), serialHandle);
        // 更新缓存
        redisService.setCacheMapValue(CacheConstants.CAMERA_ONLINE, camera.getId(), masterLoginId);
        log.debug("主设备 [{}] 登录成功,ID: {}", camera.getIp(), masterLoginId);
 
        // --- 步骤 2: 登录机芯节点 ---
        List<ArdCameraHepu> nodes = camera.getHepuNodes();
        List<CompletableFuture<Integer>> futures = new ArrayList<>();
        // 1. 为每个子节点创建并行登录任务
        for (ArdCameraHepu node : nodes) {
            futures.add(CompletableFuture.supplyAsync(() -> {
                try {
                    int nodeLoginId = -1;
                    switch (node.getNodeType()) {
                        //可见光
                        case "1":
                            nodeLoginId = hikNodeLogin(node);
                            break;
                        //热红外
                        case "2":
                            nodeLoginId = fjrNodeLogin(node);
                            break;
                    }
                    if (nodeLoginId > 0) {
                        node.setLoginId((long) nodeLoginId);
                        node.setStatus("1");
                        redisService.setCacheMapValue(CacheConstants.CAMERA_ONLINE, node.getId(), (long) nodeLoginId);
                    } else {
                        node.setLoginId(-1L);
                        node.setStatus("0");
                        // 发布合浦节点登录事件
                        redisService.deleteCacheMapValue(CacheConstants.CAMERA_ONLINE, node.getId());
                    }
                    // 发布合浦节点登录事件
                    eventPublisher.publishEvent(new HepuLoginEvent(node));
                    return nodeLoginId;
                } catch (Exception e) {
                    log.error("子节点 {} 登录异常", node.getRemark(), e);
                    return -1;
                }
            }));
        }
        // 2. 等待完成
        CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
        // 3. 统计成功数 (ID > 0 即为成功)
        long successCount = futures.stream().filter(f -> f.join() > 0).count();
        boolean allSuccess = (successCount == nodes.size());
        // 4. 只有全部成功
        if (allSuccess) {
            handleLoginSuccess(camera);
        } else {
            handleLoginFail(camera);
        }
        eventPublisher.publishEvent(new LoginEvent(camera));
    }
 
    //登录失败处理
    private void handleLoginFail(ArdCamera camera) {
        camera.setChanNum(0);
        camera.setLoginId(-1L);
        camera.setState("0");
        camera.setChannelList(null);
    }
 
    //登录成功处理
    private void handleLoginSuccess(ArdCamera camera) {
        camera.setState("1");
        camera.setStartChan(1);
        camera.setChanNum(2);
 
        // 获取最新通道
        List<ArdChannel> channels = getChannels(camera);
        if (!channels.isEmpty()) {
            camera.setChannelList(channels);
        }
        log.info("设备登录成功 [{}:{}]", camera.getIp(), camera.getPort());
    }
 
    // --- 公共核心登录方法 ---
 
    /**
     * 执行通用的 SDK 登录操作
     *
     * @param ip       设备 IP
     * @param port     端口
     * @param username 用户名
     * @param password 密码
     * @return 登录句柄 (成功 > 0, 失败 <= 0)
     */
    private int doLoginCore(String ip, short port, String username, String password) {
        // 设备信息输出参数
        NET_DVR_DEVICEINFO_V40 deviceInfo = new NET_DVR_DEVICEINFO_V40();
        NET_DVR_USER_LOGIN_INFO loginInfo = new NET_DVR_USER_LOGIN_INFO();
 
        // 1. 初始化字节数组 (防止空指针或长度不足)
        loginInfo.sDeviceAddress = new byte[HCNetSDK.NET_DVR_DEV_ADDRESS_MAX_LEN];
        loginInfo.sUserName = new byte[HCNetSDK.NET_DVR_LOGIN_USERNAME_MAX_LEN];
        loginInfo.sPassword = new byte[HCNetSDK.NET_DVR_LOGIN_PASSWD_MAX_LEN];
 
        // 2. 填充数据 (注意处理字符串长度不超过数组最大长度的情况,虽然通常不会超)
        byte[] ipBytes = ip.getBytes();
        byte[] userBytes = username.getBytes();
        byte[] pwdBytes = password.getBytes();
 
        System.arraycopy(ipBytes, 0, loginInfo.sDeviceAddress, 0, Math.min(ipBytes.length,
                loginInfo.sDeviceAddress.length));
        System.arraycopy(userBytes, 0, loginInfo.sUserName, 0, Math.min(userBytes.length, loginInfo.sUserName.length));
        System.arraycopy(pwdBytes, 0, loginInfo.sPassword, 0, Math.min(pwdBytes.length, loginInfo.sPassword.length));
 
        // 3. 设置其他参数
        loginInfo.wPort = port;
        loginInfo.byVerifyMode = 0; // 默认验证模式
        loginInfo.byLoginMode = 0;  // 默认登录模式
        loginInfo.bUseAsynLogin = false; // 同步登录
 
        // 4. 写入内存 (JNA 必需步骤)
        loginInfo.write();
 
        // 5. 执行登录
        int loginId = hCNetSDK.NET_DVR_Login_V40(loginInfo, deviceInfo);
 
        // 可选:如果登录失败,可以在这里统一记录错误码日志,或者由调用方处理
        if (loginId <= 0) {
            int errCode = hCNetSDK.NET_DVR_GetLastError();
            // 注意:这里不要打太频繁的日志,因为并发登录时失败可能会刷屏,建议由上层捕获后记录
            // log.debug("登录失败 IP:{} Port:{} ErrCode:{}", ip, port, errCode);
        }
 
        return loginId;
    }
 
    // --- 主相机登录 (调用核心方法) ---
    private int doSingleCameraLogin(ArdCamera camera) {
        return doLoginCore(
                camera.getIp(),
                camera.getPort().shortValue(),
                camera.getUsername(),
                camera.getPassword()
        );
    }
 
    // --- 合浦海康节点机芯登录 (调用核心方法) ---
    private int hikNodeLogin(ArdCameraHepu node) {
        return doLoginCore(
                node.getIp(),
                node.getPort().shortValue(),
                node.getUsername(),
                node.getPassword()
        );
    }
 
    // --- 合浦富吉瑞节点机芯登录 (调用核心方法) ---
    private int fjrNodeLogin(ArdCameraHepu node) {
        ArdCamera camera = new ArdCamera(node.getIp(), node.getPort(), node.getUsername(), node.getPassword());
        camera.setId(node.getId());
        boolean success = nettyClient.connect(camera);
        if (!success) {
            log.error("设备 {} 登录失败", camera.getIp());
            return -1;
        }
        return (int) (System.currentTimeMillis() / 1000);
    }
 
    // 注销登录
    @Override
    public void logout(String cameraId) {
        // 1. 注销主设备
        Long loginId = redisService.getCacheMapValue(CacheConstants.CAMERA_ONLINE, cameraId);
        if (loginId != null) {
            Integer lSerialHandle = CameraSerialCache.get(cameraId);
            if (lSerialHandle != null) {
                CameraSerialCache.remove(cameraId);
                if (hCNetSDK.NET_DVR_SerialStop(lSerialHandle)) {
                    log.info("【{}】关闭透明串口成功", cameraId);
                }
            }
            redisService.deleteCacheMapValue(CacheConstants.CAMERA_ONLINE, cameraId);
            hCNetSDK.NET_DVR_Logout(loginId.intValue());
        }
        // 2. 注销合浦节点1
        Long node1 = redisService.getCacheMapValue(CacheConstants.CAMERA_ONLINE, cameraId + "_1");
        if (node1 != null) {
            redisService.deleteCacheMapValue(CacheConstants.CAMERA_ONLINE, cameraId + "_1");
            hCNetSDK.NET_DVR_Logout(node1.intValue());
        }
        // 3. 注销合浦节点2
        Long node2 = redisService.getCacheMapValue(CacheConstants.CAMERA_ONLINE, cameraId + "_2");
        if (node2 != null) {
            redisService.deleteCacheMapValue(CacheConstants.CAMERA_ONLINE, cameraId + "_2");
            // fjrSDK.logout(node2.intValue());
        }
    }
 
    // 初始化透明串口
    private int initTransparentSerial(ArdCamera ardCamera) {
        NET_DVR_SERIALSTART_V40 serialStartV40 = new NET_DVR_SERIALSTART_V40();
        serialStartV40.dwSize = serialStartV40.size(); // 结构体大小
        serialStartV40.dwSerialType = 2; // 串口类型:1-232 2-485
        serialStartV40.bySerialNum = 0;  // 串口号:0表示第一个串口
        serialStartV40.write(); // 写入内存
 
        Pointer lpInBuffer = serialStartV40.getPointer();
        int dwInBufferSize = serialStartV40.size();
        HpSerialDataCallBack serialDataCallback = new HpSerialDataCallBack(ardCamera);
 
        Pointer pUser = null;
 
        return hCNetSDK.NET_DVR_SerialStart_V40(ardCamera.getLoginId().intValue(), lpInBuffer,
                dwInBufferSize,
                serialDataCallback, pUser);
    }
 
 
    // 透明串口发送指令
    private Boolean SerialSend(int m_lSerialHandle, byte[] data) {
        // 1. 定义查询指令
        boolean b = hCNetSDK.NET_DVR_SerialSend(m_lSerialHandle, 1, data, data.length);
        if (!b) {
            int errorCode = hCNetSDK.NET_DVR_GetLastError();
            log.error("透传指令发送失败,错误码:{}", errorCode);
            return false;
        }
        return true;
    }
 
    //透明串口云台控制
    public Boolean serialControl(CameraCmd cmd) {
 
        String cameraId = cmd.getCameraId();
        Integer serialHandle = CameraSerialCache.get(cameraId);
        if (serialHandle == null) {
            log.error("【{}】未找到对应的串口通道", cameraId);
            return false;
        }
 
        byte CMD_HEAD = (byte) 0xFF;
        byte ADDRESS = 0x01;
 
        byte COMMAND1 = 0x00;
        byte COMMAND2 = 0x00;
 
        int speed = cmd.getSpeed() * 10;
        if (speed <= 0) speed = 0x10;
        if (speed > 0x3F) speed = 0x3F;
 
        byte dataH = (byte) speed;
        byte dataL = (byte) speed;
 
        String action = "";
 
        switch (cmd.getCode()) {
 
            case 1:
                COMMAND2 = 0x0C;
                action = "左上";
                break;
 
            case 2:
                COMMAND2 = 0x08;
                action = "上";
                break;
 
            case 3:
                COMMAND2 = 0x0A;
                action = "右上";
                break;
 
            case 4:
                COMMAND2 = 0x04;
                action = "左";
                break;
 
            case 6:
                COMMAND2 = 0x02;
                action = "右";
                break;
 
            case 7:
                COMMAND2 = 0x14;
                action = "左下";
                break;
 
            case 8:
                COMMAND2 = 0x10;
                action = "下";
                break;
 
            case 9:
                COMMAND2 = 0x12;
                action = "右下";
                break;
 
            case 10:
                COMMAND2 = 0x20;
                dataH = 0;
                dataL = 0;
                action = "变倍+";
                break;
 
            case 11:
                COMMAND2 = 0x40;
                dataH = 0;
                dataL = 0;
                action = "变倍-";
                break;
 
            case 12:
                COMMAND1 = 0x01;
                dataH = 0;
                dataL = 0;
                action = "近焦";
                break;
 
            case 13:
                COMMAND1 = (byte) 0x80;
                dataH = 0;
                dataL = 0;
                action = "远焦";
                break;
 
            case 14:
                COMMAND1 = 0x02;
                action = "光圈开";
                break;
 
            case 15:
                COMMAND1 = 0x04;
                action = "光圈关";
                break;
 
            default:
                log.warn("【{}】未知PTZ控制指令: {}", cameraId, cmd.getCode());
                return false;
        }
 
        if (!cmd.isEnable()) {
            COMMAND1 = 0x00;
            COMMAND2 = 0x00;
            dataH = 0;
            dataL = 0;
            action = action + " 停止";
        }
 
        int sum = (ADDRESS & 0xFF)
                + (COMMAND1 & 0xFF)
                + (COMMAND2 & 0xFF)
                + (dataH & 0xFF)
                + (dataL & 0xFF);
 
        byte checkSum = (byte) (sum & 0xFF);
 
        byte[] bytes = new byte[]{
                CMD_HEAD,
                ADDRESS,
                COMMAND1,
                COMMAND2,
                dataH,
                dataL,
                checkSum
        };
 
        // 打印完整指令
        log.info("【{}】PTZ控制 -> 动作:{} 速度:{} 指令:{}",
                cameraId,
                action,
                speed,
                ArdTool.bytesToHex(bytes));
 
        Boolean result = SerialSend(serialHandle, bytes);
 
        if (!result) {
            int errorCode = hCNetSDK.NET_DVR_GetLastError();
            log.warn("【{}】PTZ串口发送失败 错误码:{} 原因:{}",
                    cameraId,
                    errorCode,
                    SdkErrorCodeEnum.getDescByCode(errorCode));
        }
 
        return result;
    }
 
    /**
     * 带速度的云台控制操作
     *
     * @param cmd 相机命令
     * @return true 表示控制命令发送成功
     * @throws CameraSDKException SDK调用失败时抛出
     */
    @Override
    public Boolean pTZControl(CameraCmd cmd) {
        Integer chanNo = cmd.getChanNo();
        // 2. 核心分流逻辑
        // 假设 Code 10-13 是变倍和调焦相关指令
        // 其他指令(1-9)是云台转动
        boolean isRotationCommand = (cmd.getCode() >= 1 && cmd.getCode() <= 9);
        if (isRotationCommand) {
            //云台旋转指令走透明串口协议
            return serialControl(cmd);
        }
        ArdCamera ardCamera = redisService.getCacheMapValue(CacheConstants.CAMERA_LIST, cmd.getCameraId());
        ArdCameraHepu targetNode = ardCamera.getHepuNodes().stream()
                .filter(node -> node.getSortOrder().equals(chanNo))
                .findFirst()
                .orElse(null); // 如果没找到,返回 null
        if (targetNode == null) return false;
        cmd.setCameraId(targetNode.getId());
        switch (chanNo) {
            case 1:
                return hikControl(cmd);
            case 2:
                return fjrControl(cmd);
        }
        return false;
    }
 
    public Boolean hikControl(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer chanNo = cmd.getChanNo();
        Long userId = redisService.getCacheMapValue(CacheConstants.CAMERA_ONLINE, cameraId);
        if (userId == null) {
            throw new ServiceException("设备离线");
        }
        int dwStop = cmd.isEnable() ? 0 : 1;
        int dwPTZCommand;
 
        switch (cmd.getCode()) {
            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;
            default:
                return false; // 或者记录日志
        }
 
        boolean success = hCNetSDK.NET_DVR_PTZControlWithSpeed_Other(
                userId.intValue(), cmd.getChanNo(), dwPTZCommand, dwStop, cmd.getSpeed()
        );
 
        if (!success) {
            int errorCode = hCNetSDK.NET_DVR_GetLastError();
            log.warn("【{}】PTZ控制失败,错误码:{}, 原因:{}", cameraId, errorCode,
                    SdkErrorCodeEnum.getDescByCode(errorCode));
        }
 
        return success;
    }
 
    public Boolean fjrControl(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer speed = cmd.getSpeed();
        // 1. 先判断是否是“停止”动作
        // 如果 isEnable 为 false,说明按钮松开了,必须发送停止指令
        if (!cmd.isEnable()) {
            ThermalCommand stopCmd = ThermalCommand.createFocusStopCmd(cameraId);
            nettyClient.send(cameraId, stopCmd);
            if (cmd.getCode() == 10 || cmd.getCode() == 11) {
                // 2. 延迟 300ms 发送自动调焦
                ThermalCommand autoFocusCmd = ThermalCommand.createAutoFocusCmd(cameraId);
                nettyClient.sendDelay(cameraId, autoFocusCmd, 1000);
            }
            return true;
        }
        // 1. 创建一个统一速度指令
        ThermalCommand uniformSpeedCmd = ThermalCommand.createUniformSpeedCmd(cameraId, speed);
        nettyClient.send(cameraId, uniformSpeedCmd);
        // 2. 如果是“开始”动作 (isEnable == true),再判断方向
        switch (cmd.getCode()) {
            case 10: // 变倍+
                ThermalCommand zoomPlus = ThermalCommand.createZoomCmd(cameraId, true);
                nettyClient.send(cameraId, zoomPlus);
                break;
 
            case 11: // 变倍-
                ThermalCommand zoomMinus = ThermalCommand.createZoomCmd(cameraId, false);
                nettyClient.send(cameraId, zoomMinus);
                break;
 
            case 12: // 调焦-
                // 这里假设你的 createFocusCmd 接受 boolean,true代表+,false代表-
                ThermalCommand cmdMinus = ThermalCommand.createFocusCmd(cameraId, false);
                nettyClient.send(cameraId, cmdMinus);
                break;
 
            case 13: // 调焦+
                ThermalCommand cmdPlus = ThermalCommand.createFocusCmd(cameraId, true);
                nettyClient.send(cameraId, cmdPlus);
                break;
 
            default:
                System.err.println("未知的指令码: " + cmd.getCode());
                return false;
        }
 
        return true;
    }
 
    // 切换透雾
    @Override
    public Boolean controlDefog(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer chanNo = cmd.getChanNo();
        ArdCamera ardCamera = redisService.getCacheMapValue(CacheConstants.CAMERA_LIST, cameraId);
        ArdCameraHepu targetNode = ardCamera.getHepuNodes().stream()
                .filter(node -> node.getSortOrder().equals(chanNo))
                .findFirst()
                .orElse(null); // 如果没找到,返回 null
        if (targetNode == null) return false;
        cmd.setCameraId(targetNode.getId());
        switch (chanNo) {
            case 1:
                return hikControlDefog(cmd);
            case 2:
                throw new ServiceException("设备不支持");
        }
        return false;
    }
 
    private Boolean hikControlDefog(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer chanNo = cmd.getChanNo();
        Long userId = redisService.getCacheMapValue(CacheConstants.CAMERA_ONLINE, cameraId);
        if (userId == null) {
            throw new ServiceException("设备离线");
        }
        boolean enable = cmd.isEnable();
        try {
            // 获取前端参数
            NET_DVR_CAMERAPARAMCFG_EX cameraParam = new NET_DVR_CAMERAPARAMCFG_EX();
            Pointer point = cameraParam.getPointer();
            IntByReference ibrBytesReturned = new IntByReference(0);
            boolean gotParam = hCNetSDK.NET_DVR_GetDVRConfig(userId.intValue(), NET_DVR_GET_CCDPARAMCFG_EX, chanNo,
                    point, cameraParam.size(), ibrBytesReturned);
            if (!gotParam) {
                log.warn("【{}】获取前端参数失败: {}", cameraId, getErrorCodePrint());
                return false;
            }
 
            cameraParam.read();
            log.debug("当前透雾模式: {}", cameraParam.struDefogCfg.byMode);
 
            // 设置透雾配置
            NET_DVR_DEFOGCFG defogCfg = new NET_DVR_DEFOGCFG();
            if (enable) {
                defogCfg.byMode = 2; // 常开模式
                defogCfg.byLevel = 100; // 0-100
            } else {
                defogCfg.byMode = 0; // 不启用
            }
            cameraParam.struDefogCfg = defogCfg;
            cameraParam.write();
 
            boolean success = hCNetSDK.NET_DVR_SetDVRConfig(userId.intValue(), NET_DVR_SET_CCDPARAMCFG_EX,
                    chanNo, point, cameraParam.size());
            if (!success) {
                log.warn("【{}】切换透雾失败: {}", cameraId, getErrorCodePrint());
                return false;
            }
 
            log.debug("【{}】切换透雾成功", cameraId);
            return true;
 
        } catch (Exception ex) {
            log.error("切换透雾异常:{}", ex.getMessage());
            return false;
        }
    }
 
    // 获取透雾状态
    @Override
    public Boolean isEnableDefog(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer chanNo = cmd.getChanNo();
        ArdCamera ardCamera = redisService.getCacheMapValue(CacheConstants.CAMERA_LIST, cameraId);
        ArdCameraHepu targetNode = ardCamera.getHepuNodes().stream()
                .filter(node -> node.getSortOrder().equals(chanNo))
                .findFirst()
                .orElse(null); // 如果没找到,返回 null
        if (targetNode == null) return false;
        cmd.setCameraId(targetNode.getId());
        switch (chanNo) {
            case 1:
                return hikIsEnableDefog(cmd);
            case 2:
                throw new ServiceException("设备不支持");
        }
        return false;
    }
 
    private Boolean hikIsEnableDefog(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer chanNo = cmd.getChanNo();
        Long userId = redisService.getCacheMapValue(CacheConstants.CAMERA_ONLINE, cameraId);
        if (userId == null) {
            throw new ServiceException("设备离线");
        }
        try {
            NET_DVR_CAMERAPARAMCFG_EX strutCameraParam = new NET_DVR_CAMERAPARAMCFG_EX();
            Pointer point = strutCameraParam.getPointer();
            IntByReference ibrBytesReturned = new IntByReference(0);
 
            boolean success = hCNetSDK.NET_DVR_GetDVRConfig(
                    userId.intValue(),
                    NET_DVR_GET_CCDPARAMCFG_EX,
                    chanNo,
                    point,
                    strutCameraParam.size(),
                    ibrBytesReturned
            );
 
            if (!success) {
                int errorCode = hCNetSDK.NET_DVR_GetLastError();
                String errorDesc = SdkErrorCodeEnum.getDescByCode(errorCode);
                log.warn("【{}】获取前端参数失败,错误码:{}, 原因:{}", cameraId, errorCode, errorDesc);
                return false; // SDK 层只返回 true/false
            }
 
            strutCameraParam.read();
            log.debug("【{}】是否开启透雾:{}", cameraId, strutCameraParam.struDefogCfg.byMode);
            return strutCameraParam.struDefogCfg.byMode != 0;
 
        } catch (Exception e) {
            log.error("【{}】查询透雾异常: {}", cameraId, e.getMessage());
            return false;
        }
    }
 
    // 切换红外
    @Override
    public Boolean controlInfrared(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer chanNo = cmd.getChanNo();
        ArdCamera ardCamera = redisService.getCacheMapValue(CacheConstants.CAMERA_LIST, cameraId);
        ArdCameraHepu targetNode = ardCamera.getHepuNodes().stream()
                .filter(node -> node.getSortOrder().equals(chanNo))
                .findFirst()
                .orElse(null); // 如果没找到,返回 null
        if (targetNode == null) return false;
        cmd.setCameraId(targetNode.getId());
        switch (chanNo) {
            case 1:
                return hikControlInfrared(cmd);
            case 2:
                throw new ServiceException("设备不支持");
        }
        return false;
    }
 
    private Boolean hikControlInfrared(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        boolean enable = cmd.isEnable();
        Integer chanNo = cmd.getChanNo();
        Long userId = redisService.getCacheMapValue(CacheConstants.CAMERA_ONLINE, cameraId);
        if (userId == null) {
            throw new ServiceException("设备离线");
        }
 
        try {
            NET_DVR_CAMERAPARAMCFG_EX cameraParamCfg = new NET_DVR_CAMERAPARAMCFG_EX();
            Pointer point = cameraParamCfg.getPointer();
            IntByReference ibrBytesReturned = new IntByReference(0);
 
            // 获取当前参数
            boolean b_GetCameraParam = hCNetSDK.NET_DVR_GetDVRConfig(userId.intValue(), NET_DVR_GET_CCDPARAMCFG,
                    chanNo, point,
                    cameraParamCfg.size(), ibrBytesReturned);
            if (!b_GetCameraParam) {
                log.warn("【{}】获取前端参数失败: {}", cameraId, getErrorCodePrint());
                return false;
            }
 
            cameraParamCfg.read();
            log.debug("当前红外状态:{}", cameraParamCfg.struDayNight.byDayNightFilterType == 1 ? "夜晚" : "白天");
 
            // 设置红外
            NET_DVR_DAYNIGHT dayNight = new NET_DVR_DAYNIGHT();
            dayNight.byDayNightFilterType = (byte) (enable ? 1 : 0); // 夜晚/白天
            dayNight.bySwitchScheduleEnabled = 1;
            dayNight.byDayNightFilterTime = 60;
            cameraParamCfg.struDayNight = dayNight;
            cameraParamCfg.write();
 
            boolean success = hCNetSDK.NET_DVR_SetDVRConfig(userId.intValue(), NET_DVR_SET_CCDPARAMCFG, chanNo, point,
                    cameraParamCfg.size());
            if (!success) {
                log.warn("【{}】切换红外失败: {}", cameraId, getErrorCodePrint());
                return false;
            }
 
            log.info("【{}】切换红外成功", cameraId);
            return true;
 
        } catch (Exception e) {
            log.error("【{}】切换红外异常: {}", cameraId, e.getMessage());
            return false;
        }
    }
 
    // 查询是否开启红外
    @Override
    public Boolean isEnableInfrared(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer chanNo = cmd.getChanNo();
        ArdCamera ardCamera = redisService.getCacheMapValue(CacheConstants.CAMERA_LIST, cameraId);
        ArdCameraHepu targetNode = ardCamera.getHepuNodes().stream()
                .filter(node -> node.getSortOrder().equals(chanNo))
                .findFirst()
                .orElse(null); // 如果没找到,返回 null
        if (targetNode == null) return false;
        cmd.setCameraId(targetNode.getId());
        switch (chanNo) {
            case 1:
                return hikIsEnableInfrared(cmd);
            case 2:
                throw new ServiceException("设备不支持");
        }
        return false;
    }
 
    private Boolean hikIsEnableInfrared(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer chanNo = cmd.getChanNo();
        Long userId = redisService.getCacheMapValue(CacheConstants.CAMERA_ONLINE, cameraId);
        if (userId == null) {
            throw new ServiceException("设备离线");
        }
        try {
            NET_DVR_CAMERAPARAMCFG_EX cameraParamCfg = new NET_DVR_CAMERAPARAMCFG_EX();
            Pointer point = cameraParamCfg.getPointer();
            IntByReference ibrBytesReturned = new IntByReference(0);
 
            boolean success = hCNetSDK.NET_DVR_GetDVRConfig(userId.intValue(), NET_DVR_GET_CCDPARAMCFG, chanNo, point,
                    cameraParamCfg.size(), ibrBytesReturned);
 
            if (!success) {
                int errorCode = hCNetSDK.NET_DVR_GetLastError();
                String errorDesc = SdkErrorCodeEnum.getDescByCode(errorCode);
                log.warn("【{}】获取前端参数失败,错误码:{},原因:{}", cameraId, errorCode, errorDesc);
                return false;
            }
 
            cameraParamCfg.read();
            // 1表示夜晚/红外开启,0表示白天/红外关闭
            return cameraParamCfg.struDayNight.byDayNightFilterType == 1;
        } catch (Exception ex) {
            log.error("【{}】查询红外开关异常", cameraId, ex);
            return false;
        }
    }
 
    //切换聚焦模式
    @Override
    public Boolean controlFocusMode(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer chanNo = cmd.getChanNo();
        ArdCamera ardCamera = redisService.getCacheMapValue(CacheConstants.CAMERA_LIST, cameraId);
        ArdCameraHepu targetNode = ardCamera.getHepuNodes().stream()
                .filter(node -> node.getSortOrder().equals(chanNo))
                .findFirst()
                .orElse(null); // 如果没找到,返回 null
        if (targetNode == null) return false;
        cmd.setCameraId(targetNode.getId());
        switch (chanNo) {
            case 1:
                return hikControlFocusMode(cmd);
            case 2:
                return fjrControlFocusMode(cmd);
        }
        return false;
    }
 
    private Boolean hikControlFocusMode(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer chanNo = cmd.getChanNo();
        boolean enable = cmd.isEnable();
        Long userId = redisService.getCacheMapValue(CacheConstants.CAMERA_ONLINE, cameraId);
        if (userId == null) {
            throw new ServiceException("设备离线");
        }
        NET_DVR_FOCUSMODE_CFG focusModeCfg = new NET_DVR_FOCUSMODE_CFG();
        Pointer point = focusModeCfg.getPointer();
        IntByReference ibrBytesReturned = new IntByReference(0);
 
        // 获取当前聚焦模式
        boolean got = hCNetSDK.NET_DVR_GetDVRConfig(userId.intValue(), NET_DVR_GET_FOCUSMODECFG, chanNo, point,
                focusModeCfg.size(), ibrBytesReturned);
        if (!got) {
            int errorCode = hCNetSDK.NET_DVR_GetLastError();
            String errorDesc = SdkErrorCodeEnum.getDescByCode(errorCode);
            log.warn("获取前端参数失败,错误码:{},原因:{}", errorCode, errorDesc);
            return false;
        }
 
        focusModeCfg.read();
        log.debug("当前聚焦模式:{}", focusModeCfg.byFocusMode);
 
        // 设置聚焦模式
        if (enable) {
            focusModeCfg.byFocusMode = 1;          // 手动聚焦
            focusModeCfg.byAutoFocusMode = 0;
        } else {
            focusModeCfg.byFocusMode = 2;          // 自动聚焦
            focusModeCfg.byAutoFocusMode = 1;
        }
        focusModeCfg.byFocusDefinitionDisplay = 1;
        focusModeCfg.byFocusSpeedLevel = 3;
        focusModeCfg.write();
 
        boolean setOk = hCNetSDK.NET_DVR_SetDVRConfig(userId.intValue(), NET_DVR_SET_FOCUSMODECFG, chanNo, point,
                focusModeCfg.size());
        if (!setOk) {
            int errorCode = hCNetSDK.NET_DVR_GetLastError();
            String errorDesc = SdkErrorCodeEnum.getDescByCode(errorCode);
            log.warn("设置聚焦模式失败,错误码:{},原因:{}", errorCode, errorDesc);
            return false;
        }
 
        log.debug("设置聚焦模式成功");
        return true;
    }
 
    private Boolean fjrControlFocusMode(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer chanNo = cmd.getChanNo();
        boolean enable = cmd.isEnable();
        if (enable) {
            ThermalCommand autoFocusCmd = ThermalCommand.createAutoFocusCmd(cameraId);
            nettyClient.sendDelay(cameraId, autoFocusCmd, 300);
        }
        return true;
    }
 
    //获取聚焦模式
    @Override
    public Boolean getFocusMode(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer chanNo = cmd.getChanNo();
        ArdCamera ardCamera = redisService.getCacheMapValue(CacheConstants.CAMERA_LIST, cameraId);
        ArdCameraHepu targetNode = ardCamera.getHepuNodes().stream()
                .filter(node -> node.getSortOrder().equals(chanNo))
                .findFirst()
                .orElse(null); // 如果没找到,返回 null
        if (targetNode == null) return false;
        cmd.setCameraId(targetNode.getId());
        switch (chanNo) {
            case 1:
                return hikGetFocusMode(cmd);
            case 2:
                throw new ServiceException("设备不支持");
        }
        return false;
    }
 
    private Boolean hikGetFocusMode(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer chanNo = cmd.getChanNo();
        Long userId = redisService.getCacheMapValue(CacheConstants.CAMERA_ONLINE, cameraId);
        if (userId == null) {
            throw new ServiceException("设备离线");
        }
 
        NET_DVR_FOCUSMODE_CFG strutFocusMode = new NET_DVR_FOCUSMODE_CFG();
        Pointer point = strutFocusMode.getPointer();
        IntByReference ibrBytesReturned = new IntByReference(0);
 
        boolean b_GetCameraParam = hCNetSDK.NET_DVR_GetDVRConfig(userId.intValue(), NET_DVR_GET_FOCUSMODECFG, chanNo,
                point,
                strutFocusMode.size(), ibrBytesReturned);
        if (!b_GetCameraParam) {
            int errorCode = hCNetSDK.NET_DVR_GetLastError();
            String errorDesc = SdkErrorCodeEnum.getDescByCode(errorCode);
            String errorMsg = String.format("获取聚焦模式失败,错误码:%s,原因:%s", errorCode, errorDesc);
            log.warn(errorMsg);
            throw new CameraSDKException(errorCode, cameraId, errorMsg);
        }
 
        strutFocusMode.read();
 
        // 判断手动/自动聚焦
        // 0-自动,1-手动,2-半自动
        return strutFocusMode.byFocusMode == 1; // true=手动
    }
 
    //3D定位
    @Override
    public Boolean set3DPosition(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer chanNo = cmd.getChanNo();
        ArdCamera ardCamera = redisService.getCacheMapValue(CacheConstants.CAMERA_LIST, cameraId);
        ArdCameraHepu targetNode = ardCamera.getHepuNodes().stream()
                .filter(node -> node.getSortOrder().equals(chanNo))
                .findFirst()
                .orElse(null); // 如果没找到,返回 null
        if (targetNode == null) return false;
        cmd.setCameraId(targetNode.getId());
        switch (chanNo) {
            case 1:
                return hikSet3DPosition(cmd);
            case 2:
                throw new ServiceException("设备不支持");
        }
        return false;
    }
 
    private Boolean hikSet3DPosition(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer chanNo = cmd.getChanNo();
        PointFrame pointFrame = cmd.getPointFrame();
        Long userId = redisService.getCacheMapValue(CacheConstants.CAMERA_ONLINE, cameraId);
        if (userId == null) {
            throw new ServiceException("设备离线");
        }
        NET_DVR_POINT_FRAME netDvrPointFrame = new NET_DVR_POINT_FRAME();
 
        netDvrPointFrame.xTop = pointFrame.getXTop();
        netDvrPointFrame.yTop = pointFrame.getYTop();
        netDvrPointFrame.xBottom = pointFrame.getXBottom();
        netDvrPointFrame.yBottom = pointFrame.getYBottom();
        netDvrPointFrame.write();
        boolean bool = hCNetSDK.NET_DVR_PTZSelZoomIn_EX(userId.intValue(), chanNo, netDvrPointFrame);
        if (!bool) {
            int errorCode = hCNetSDK.NET_DVR_GetLastError();
            String errorDesc = SdkErrorCodeEnum.getDescByCode(errorCode);
            String errorMsg = String.format("3D定位失败,错误码:%s,原因:%s", errorCode, errorDesc);
            log.warn(errorMsg);
            return false;
        }
        return true;
    }
 
    //获取ptz信息
    @Override
    public PtzDto getPtz(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        PtzDto ptzDto = CameraPTZCache.get(cameraId);
        if (ptzDto == null) {
            ptzDto = new PtzDto(0f, 0f, 0f);
            CameraPTZCache.put(cameraId, ptzDto);
        }
        return ptzDto;
    }
 
    //设置ptz信息
    @Override
    public Boolean setPtz(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        PtzDto ptzDto = cmd.getPtzDto();
        int m_lSerialHandle = CameraSerialCache.get(cameraId);
        float p = ptzDto.getP();
        int dataInt = Math.round(p * 100);
        byte[] dataBytes = intTo2BytesBigEndian(dataInt);
        byte dataH = dataBytes[0]; // DataH(Byte5)
        byte dataL = dataBytes[1]; // DataL(Byte6)
        byte CMD_HEAD = (byte) 0xff; // 指令头(Byte1)
        byte ADDRESS = 0x01; // 设备地址(Byte2)
        byte COMMAND1 = 0x00; // 命令1(Byte3)
        byte COMMAND2 = 0x4B; // 命令2(Byte4)
        // CheckSum = (Address + Command1 + Command2 + DataH + DataL) 的低8位
        int sum = (ADDRESS & 0xFF) + (COMMAND1 & 0xFF) + (COMMAND2 & 0xFF) + (dataH & 0xFF) + (dataL & 0xFF);
        byte checkSum = (byte) (sum & 0xFF); // 取低8位作为校验和
        // 组装指令数组
        byte[] cmdPBytes = new byte[]{CMD_HEAD, ADDRESS, COMMAND1, COMMAND2, dataH, dataL, checkSum}; // 总长度7字节
        log.info("[PTZ报文:] cameraId={},p={}, hex={}", cameraId, ptzDto.getP(),bytesToHex(cmdPBytes));
        Boolean b = SerialSend(m_lSerialHandle, cmdPBytes);
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        if (!b) {
            int errorCode = hCNetSDK.NET_DVR_GetLastError();
            String errorDesc = SdkErrorCodeEnum.getDescByCode(errorCode);
            String errorMsg = String.format("设置PTZ(P)失败,错误码:%s,原因:%s", errorCode, errorDesc);
            log.warn(errorMsg);
            return false;
        }
 
        dataInt = Math.round(ptzDto.getT() * 100);
        dataInt = 36000 - dataInt;
        dataBytes = intTo2BytesBigEndian(dataInt);
        dataH = dataBytes[0];
        dataL = dataBytes[1];
        COMMAND2 = 0x4D;
        // 5. Step3:计算校验和(单行版,规则同水平)
        sum = (ADDRESS & 0xFF) + (COMMAND1 & 0xFF) + (COMMAND2 & 0xFF) + (dataH & 0xFF) + (dataL & 0xFF);
        checkSum = (byte) (sum & 0xFF);
        // 组装指令数组
        cmdPBytes = new byte[]{CMD_HEAD, ADDRESS, COMMAND1, COMMAND2, dataH, dataL, checkSum}; // 总长度7字节
        log.info("[PTZ报文:] cameraId={},t={}, hex={}", cameraId, ptzDto.getT(),bytesToHex(cmdPBytes));
        b = SerialSend(m_lSerialHandle, cmdPBytes);
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        if (!b) {
            int errorCode = hCNetSDK.NET_DVR_GetLastError();
            String errorDesc = SdkErrorCodeEnum.getDescByCode(errorCode);
            String errorMsg = String.format("设置PTZ(T)失败,错误码:%s,原因:%s", errorCode, errorDesc);
            log.warn(errorMsg);
            return false;
        }
 
        int z = ptzDto.getZ().intValue();
        dataInt = convertFrontZoomToDeviceFocal(z);
        dataBytes = intTo2BytesBigEndian(dataInt);
        dataH = dataBytes[0];
        dataL = dataBytes[1];
        COMMAND2 = 0x4F;
        // 5. Step3:计算校验和(单行版,规则同水平)
        sum = (ADDRESS & 0xFF) + (COMMAND1 & 0xFF) + (COMMAND2 & 0xFF) + (dataH & 0xFF) + (dataL & 0xFF);
        checkSum = (byte) (sum & 0xFF);
        // 组装指令数组
        cmdPBytes = new byte[]{CMD_HEAD, ADDRESS, COMMAND1, COMMAND2, dataH, dataL, checkSum}; // 总长度7字节
        log.info("[PTZ报文:] cameraId={},z={}, hex={}", cameraId, ptzDto.getZ(),bytesToHex(cmdPBytes));
        b = SerialSend(m_lSerialHandle, cmdPBytes);
        if (!b) {
            int errorCode = hCNetSDK.NET_DVR_GetLastError();
            String errorDesc = SdkErrorCodeEnum.getDescByCode(errorCode);
            String errorMsg = String.format("设置PTZ(Z)失败,错误码:%s,原因:%s", errorCode, errorDesc);
            log.warn(errorMsg);
            return false;
        }
        return true;
    }
 
    /**
     * 前端1-60的倍率值 转换为 设备可识别的焦距控制值
     *
     * @param frontZoom 前端传递的1~60的整数
     * @return 设备焦距控制值(放大100倍的整数,如3.3mm→330,37mm→3700)
     */
    public int convertFrontZoomToDeviceFocal(int frontZoom) {
        // 1. 定义固定参数(设备物理参数+映射关系)
        final float MIN_FOCAL = 3.3f;    // 设备最小焦距(mm)
        final float MAX_FOCAL = 37.0f;   // 设备最大焦距(mm)
        final int FRONT_MIN = 1;         // 前端最小值
        final int FRONT_MAX = 60;        // 前端最大值
 
        // 2. 校验前端输入范围(避免非法值)
        if (frontZoom < FRONT_MIN || frontZoom > FRONT_MAX) {
            throw new IllegalArgumentException("前端倍率值需在1~60之间,当前值:" + frontZoom);
            // 若不想抛异常,也可自动修正:frontZoom = Math.max(FRONT_MIN, Math.min(FRONT_MAX, frontZoom));
        }
 
        // 3. 核心:线性映射公式(前端值 → 设备焦距mm)
        // 公式:目标值 = 最小值 + (最大值-最小值) × (当前前端值-前端最小值)/(前端最大值-前端最小值)
        float deviceFocal = MIN_FOCAL + (MAX_FOCAL - MIN_FOCAL) * (frontZoom - FRONT_MIN) / (FRONT_MAX - FRONT_MIN);
 
        // 4. 焦距值放大100倍并取整(设备底层常用整数控制,避免浮点精度问题)
        return Math.round(deviceFocal * 100);
    }
 
    /**
     * 整数转2字节数组(大端模式:高字节在前,低字节在后)
     * 示例:9000 → 0x23 0x28(因为9000的十六进制是0x2328)
     *
     * @param value 待转换整数(0~36000)
     * @return 2字节数组([0]=高字节,[1]=低字节)
     */
    private byte[] intTo2BytesBigEndian(int value) {
        byte[] bytes = new byte[2];
        bytes[0] = (byte) (value >> 8 & 0xFF); // 高字节(DataH)
        bytes[1] = (byte) (value & 0xFF);      // 低字节(DataL)
        return bytes;
    }
 
    //设置零方位角
    @Override
    public Boolean setZeroPtz(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        int m_lSerialHandle = CameraSerialCache.get(cameraId);
        byte[] cmdPBytes = new byte[]{(byte) 0xff, 0x01, 0x00, 0x6F, 0x00, 0x00, 0x70};
        boolean bool = SerialSend(m_lSerialHandle, cmdPBytes);
        if (!bool) {
            int errorCode = hCNetSDK.NET_DVR_GetLastError();
            String errorDesc = SdkErrorCodeEnum.getDescByCode(errorCode);
            String errorMsg = String.format("设置零方位角失败,错误码:%s,原因:%s", errorCode, errorDesc);
            log.warn(errorMsg);
            return false;
        }
        return true;
    }
 
    //获取视场角
    @Override
    public PtzParamDTO getGisInfo(CameraCmd cmd) {
        PtzParamDTO ptzParamDTO = new PtzParamDTO();
        String cameraId = cmd.getCameraId();
        PtzDto ptzDto = CameraPTZCache.get(cameraId);
        if (ptzDto == null) {
            ptzParamDTO.setP(0f);
            ptzParamDTO.setT(0f);
            ptzParamDTO.setZ(0f);
        }
        ptzParamDTO.setFHorFieldAngle(1.8f);
        ptzParamDTO.setFVerFieldAngle(1f);
        return ptzParamDTO;
    }
 
    //获取球机PTZ参数取值范围
    @Override
    public PtzScope getPtzScope(CameraCmd cmd) {
        PtzScope ptzScope = new PtzScope();
        ptzScope.setPMax(360f);
        ptzScope.setPMin(0f);
        ptzScope.setTMax(45f);
        ptzScope.setTMin(315f);
        ptzScope.setZMax(60f);
        ptzScope.setZMin(1f);
        return ptzScope;
    }
 
    //引导目标位置
    @Override
    public Boolean guideTargetPosition(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Float baseYaw = cmd.getBaseYaw();
        ArdCamera ardCamera = redisService.getCacheMapValue(CacheConstants.CAMERA_LIST, cameraId);
        if (StringUtils.isNull(ardCamera)) {
            log.warn("未找到相机信息,cameraId:{}", cameraId);
            return false;
        }
        Point cameraPosition = new Point(ardCamera.getLongitude(), ardCamera.getLatitude(), ardCamera.getAltitude());
        cameraPosition.validate(); // 校验目标位置
        Point targetPositions = cmd.getTargetPosition();
        targetPositions.validate(); // 校验目标位置
        PtzDto targetPTZ = GisUtil.getCameraPTZ(cameraPosition, targetPositions, 20, 150);
        //若为车载光电,需要根据修改P值
        if ("4".equals(ardCamera.getType())) {
            targetPTZ.setP((targetPTZ.getP() - baseYaw + 360) % 360);
        }
        // pt校验范围
        cmd.setPtzDto(targetPTZ);
        validatePtzRange(cmd);
        return setPtz(cmd);
    }
 
    //录像开始
    @Override
    public Boolean recordStart(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer chanNo = cmd.getChanNo();
        ArdCamera ardCamera = redisService.getCacheMapValue(CacheConstants.CAMERA_LIST, cameraId);
        ArdCameraHepu targetNode = ardCamera.getHepuNodes().stream()
                .filter(node -> node.getSortOrder().equals(chanNo))
                .findFirst()
                .orElse(null); // 如果没找到,返回 null
        if (targetNode == null) return false;
        cmd.setCameraId(targetNode.getId());
        switch (chanNo) {
            case 1:
                return hikRecordStart(cmd);
            case 2:
                throw new ServiceException("设备不支持");
        }
        return false;
    }
 
    private Boolean hikRecordStart(CameraCmd cmd) {
        try {
            String cameraId = cmd.getCameraId();
            Integer chanNo = cmd.getChanNo();
            String name = cameraId + "_" + chanNo;
            // 本地临时录像地址
            String path = FileUtils.createFile(tempDir + "/record/" + name + ".mp4");
            Long userId = redisService.getCacheMapValue(CacheConstants.CAMERA_ONLINE, cameraId);
            if (userId == null) {
                throw new ServiceException("设备离线");
            }
 
            //强制I帧结构体对象
            NET_DVR_I_FRAME netDvrIFrame = new NET_DVR_I_FRAME();   //新建结构体对象
            netDvrIFrame.read();
            netDvrIFrame.dwChannel = chanNo;//因为上文代码中设置了通道号,按照上文中的设置
            netDvrIFrame.byStreamType = 0;
            netDvrIFrame.dwSize = netDvrIFrame.size();
            netDvrIFrame.write();
            if (!hCNetSDK.NET_DVR_RemoteControl(userId.intValue(), 3402, netDvrIFrame.getPointer(),
                    netDvrIFrame.dwSize)) {
                int code = hCNetSDK.NET_DVR_GetLastError();
                log.error("设置强制I帧错误:{}", SdkErrorCodeEnum.getDescByCode(code) + "(" + code + ")");
            }
            //预览参数
            NET_DVR_PREVIEWINFO previewInfo = new NET_DVR_PREVIEWINFO();
            previewInfo.read();
            previewInfo.lChannel = chanNo;
            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 (GlobalVariable.previewMap.containsKey(name)) {
                Integer lRealHandle = (Integer) GlobalVariable.previewMap.get(name);
                hCNetSDK.NET_DVR_StopRealPlay(lRealHandle);
                GlobalVariable.previewMap.remove(name);
                log.debug("停止当前录像");
            }
            int lRealHandle = hCNetSDK.NET_DVR_RealPlay_V40(userId.intValue(), previewInfo, null, null);
            if (lRealHandle == -1) {
                log.error("取流失败:{}", hCNetSDK.NET_DVR_GetLastError());
                return false;
            }
            log.debug("取流成功");
            GlobalVariable.threadMap.put(cameraId, Thread.currentThread().getName());
            GlobalVariable.previewMap.put(name, lRealHandle);
            if (!hCNetSDK.NET_DVR_SaveRealData_V30((int) GlobalVariable.previewMap.get(name), 2, path)) {
                log.error("保存视频文件到临时文件夹失败 错误码为:{}", hCNetSDK.NET_DVR_GetLastError());
                return false;
            }
            log.debug("录像开始");
            return true;
        } catch (Exception ex) {
            log.error("开始录像异常:{}", ex.getMessage());
            return false;
        }
    }
 
    //录像停止
    @Override
    public String recordStop(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer chanNo = cmd.getChanNo();
        ArdCamera ardCamera = redisService.getCacheMapValue(CacheConstants.CAMERA_LIST, cameraId);
        ArdCameraHepu targetNode = ardCamera.getHepuNodes().stream()
                .filter(node -> node.getSortOrder().equals(chanNo))
                .findFirst()
                .orElse(null); // 如果没找到,返回 null
        if (targetNode == null) return "";
        cmd.setCameraId(targetNode.getId());
        switch (chanNo) {
            case 1:
                return hikRecordStop(cmd);
            case 2:
                throw new ServiceException("设备不支持");
        }
        return "";
    }
 
    private String hikRecordStop(CameraCmd cmd) {
        String url = "";
        File videoFile = null;
        File transcodeFile = null;
 
        try {
            String cameraId = cmd.getCameraId();
            Integer chanNo = cmd.getChanNo();
            String name = cameraId + "_" + chanNo;
 
            // 本地临时录像地址
            String path = FileUtils.createFile(tempDir + "/record/" + name + ".mp4");
 
            Long userId = redisService.getCacheMapValue(CacheConstants.CAMERA_ONLINE, cameraId);
            if (userId == null) {
                throw new ServiceException("设备离线");
            }
 
            // 停止录像
            if (GlobalVariable.previewMap.containsKey(name)) {
                Integer lRealHandle = (Integer) GlobalVariable.previewMap.get(name);
                hCNetSDK.NET_DVR_StopRealPlay(lRealHandle);
                GlobalVariable.previewMap.remove(name);
            }
            log.debug("录像停止");
 
            // 检查原录像文件是否存在
            videoFile = new File(path);
            if (!videoFile.exists() || videoFile.length() == 0) {
                log.error("录像文件不存在或为空: {}", path);
                return "";
            }
 
            // ffmpeg转码
            StopWatch watchStop = new StopWatch();
            watchStop.start();
            String newpath = FileUtils.createFile(tempDir + "/record/" + name + "_c.mp4");
            transcodeFile = new File(newpath);
            FFmpegUtils.transcodeToMP4(path, newpath);
            watchStop.stop();
            log.warn("转码耗时:{}", watchStop.getTotalTimeMillis());
 
            // 检查转码后的文件是否存在
            if (!transcodeFile.exists() || transcodeFile.length() == 0) {
                log.error("转码文件不存在或为空: {}", newpath);
                return "";
            }
 
            // 存入minio - 使用 FileMultipartFile
            String bucketName = "record";
            String objectName = cameraId + "_" + chanNo + ".mp4";
            String type = cmd.getType() == null ? "record" : cmd.getType();
 
            MultipartFile multipartFile = new FileMultipartFile(transcodeFile, objectName, MimeTypeUtils.VIDEO_MP4);
            R<SysFile> sysFileR = remoteFileService.upload(multipartFile, bucketName, type);
 
            if (sysFileR.getCode() == R.SUCCESS) {
                url = sysFileR.getData().getUrl();
                log.info("上传文件成功:{}", url);
            }
 
            return url;
 
        } catch (Exception ex) {
            log.error("录像异常:{}", ex.getMessage());
            return "";
        } finally {
            // 清理临时文件
            try {
                if (videoFile != null && videoFile.exists()) {
                    videoFile.delete();
                    log.debug("删除原录像文件: {}", videoFile.getPath());
                }
                if (transcodeFile != null && transcodeFile.exists()) {
                    transcodeFile.delete();
                    log.debug("删除转码文件: {}", transcodeFile.getPath());
                }
            } catch (Exception e) {
                log.warn("删除临时文件失败", e);
            }
        }
    }
 
    //获取通道
    @Override
    public List<ArdChannel> getChannels(ArdCamera camera) {
        List<ArdChannel> channelList = new ArrayList<>();
        // 获取所有子节点
        List<ArdCameraHepu> nodes = camera.getHepuNodes();
        if (nodes == null || nodes.isEmpty()) {
            log.warn("设备 {} 无子节点,无法获取通道", camera.getName());
            return channelList;
        }
        log.debug("开始聚合子设备通道信息,共 {} 个子节点", nodes.size());
        // 1. 遍历每一个子节点
        for (ArdCameraHepu node : nodes) {
            ArdChannel channel = new ArdChannel();
            if ("1".equals(node.getNodeType())) {
                Long nodeLoginId = redisService.getCacheMapValue(CacheConstants.CAMERA_ONLINE, node.getId());
                if (nodeLoginId == null) {
                    log.warn("子节点 {} 未登录或 LoginId 无效,跳过其通道获取", node.getRemark());
                    continue;
                }
                int chanNo = 1; // 子设备内部的物理通道号 (通常从 1 开始)
                NET_DVR_PICCFG_V40 strPicCfg = new NET_DVR_PICCFG_V40();
                int structSize = strPicCfg.size();
                IntByReference pInt = new IntByReference(0);
                // 复用结构体
                strPicCfg.dwSize = structSize;
                strPicCfg.write();
                // 调用 SDK 获取配置 (使用子节点的 loginId 和 物理通道号)
                boolean success = hCNetSDK.NET_DVR_GetDVRConfig(
                        nodeLoginId.intValue(),
                        HCNetSDK.NET_DVR_GET_PICCFG_V40,
                        chanNo,
                        strPicCfg.getPointer(),
                        structSize,
                        pInt
                );
 
                if (!success) {
                    int errorCode = hCNetSDK.NET_DVR_GetLastError();
                    if (errorCode != 0) {
                        continue;
                    }
                }
 
                // 读取结果
                strPicCfg.read();
 
                // 解析通道名称
                String channelName;
                try {
                    channelName = new String(strPicCfg.sChanName, "GBK").trim();
                } catch (Exception e) {
                    channelName = "通道" + chanNo;
                }
                // 如果名字为空,给个默认名
                if (channelName.isEmpty() || channelName.contains("\u0000")) {
                    channelName = "通道" + chanNo;
                }
                // 构建父设备视角的通道对象
                channel.setDeviceId(camera.getId());       // 归属父设备 ID
                channel.setName(channelName);              // 通道名称
                channel.setChanNo(chanNo);                 // 【关键】使用全局递增的通道号
                // 获取视频编码
                CameraCmd cmd = new CameraCmd(node.getId(), chanNo);
                cmd.setLoginId(nodeLoginId);
                // 假设 getVideoCompressionCfg 能根据 cmd 里的信息去查
                VideoCompressionCfg videoCfg = getVideoCompressionCfg(cmd);
                if (videoCfg != null) {
                    channel.setVideoCode(videoCfg.getVideoEncType());
                }
                String rtspUrl = buildRtspUrlForType1(node, chanNo);
                channel.setLiveAddress(rtspUrl);
            } else if ("2".equals(node.getNodeType())) {
 
                channel.setDeviceId(camera.getId());       // 归属父设备 ID
                channel.setName("热红外");                  // 写死通道名称
                channel.setChanNo(2);                      // 写死通道号2
                channel.setVideoCode("h264");              // 写死通道编码格式
                // 【新增】构建 rtspUrl - 类型2
                String rtspUrl = buildRtspUrlForType2(node);
                channel.setLiveAddress(rtspUrl);
            }
            channelList.add(channel);
        }
        log.debug("聚合完成,父设备 {} 共获取到 {} 个逻辑通道", camera.getName(), channelList.size());
        return channelList;
    }
    // 构建类型1的 RTSP URL
    private String buildRtspUrlForType1(ArdCameraHepu node, int chanNo) {
        return String.format("rtsp://%s:%s@%s:%d/h264/ch%d/main/av_stream",
                node.getUsername(),
                node.getPassword(),
                node.getIp(),
                node.getRtspPort(),
                chanNo
        );
    }
 
    // 构建类型2的 RTSP URL
    private String buildRtspUrlForType2(ArdCameraHepu node) {
        return String.format("rtsp://%s:%d/live/video",
                node.getIp(),
                node.getRtspPort()
        );
    }
    private String bytesToHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(String.format("%02X ", b));
        }
        return sb.toString().trim();
    }
 
}