liusuyi
2026-05-30 f4f4fc53260eb67483dce406a963628273786a61
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
package com.ard.work.sdk.zlxd.service;
 
import com.ard.common.core.constant.CacheConstants;
import com.ard.common.core.constant.SecurityConstants;
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.MimeTypeUtils;
import com.ard.common.redis.service.RedisService;
import com.ard.system.api.RemoteFileService;
import com.ard.system.api.domain.SysFile;
import com.ard.vtdu.api.RemoteArdVtduService;
import com.ard.vtdu.api.domian.ArdVtdu;
import com.ard.work.api.domian.*;
import com.ard.work.device.camera.domain.PtzParamDTO;
import com.ard.work.device.camera.service.IArdCameraPtzScopeService;
import com.ard.work.event.LoginEvent;
import com.ard.work.sdk.dh.common.Base64;
import com.ard.work.sdk.model.DeviceCFG;
import com.ard.work.sdk.model.PtzScope;
import com.ard.work.sdk.service.CameraSDK;
import com.ard.work.sdk.zlxd.cache.RecordSessionCache;
import com.ard.work.sdk.zlxd.netty.message.parameters.CameraParameters;
import com.ard.work.sdk.zlxd.netty.message.request.*;
import com.ard.work.sdk.zlxd.netty.message.response.*;
import com.ard.work.sdk.zlxd.netty.tcp.NettyTcpClient;
import com.ard.work.sdk.zlxd.utils.PTZ3DLocationConverter;
import com.ard.work.sdk.zlxd.utils.XmlUtils;
import com.ard.work.utils.gis.GisUtil;
import com.galaxy.sdk.RecordSdk;
import com.galaxy.sdk.SDKConfig;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
 
/**
 * 中林信达sdk策略
 *
 * @author 刘苏义
 * @since 2023-11-07
 */
@Service("zlxdSDK")
@Slf4j
public class ZlxdSDK implements CameraSDK {
 
    @Value("${sdk.tempDir}")
    private String tempDir;
 
    private RecordSdk recordSdk;
    @Resource
    private RemoteFileService remoteFileService;
    @Resource
    private RemoteArdVtduService remoteArdVtduService;
    @Resource
    private NettyTcpClient nettyTcpClient;
    @Resource
    private ApplicationEventPublisher eventPublisher;
    @Resource
    private RedisService redisService;
    @Resource
    private IArdCameraPtzScopeService ardCameraPtzScopeService;
    @PostConstruct
    private void initSdk() {
        recordSdk = RecordSdk.getInstance();
        SDKConfig config = new SDKConfig();
        String saveDir = tempDir + File.separator + "record";
        config.setStoragePath(saveDir);
        config.setEnableAutoRetry(true);
        config.setMaxRetryCount(3);
        config.setMaxStorageSize(5L * 1024 * 1024 * 1024); //最大存储5G
        config.setPerFileMaxSize(1L * 1024 * 1024 * 1024);
        int ret = recordSdk.init(config);
        if (ret < 0) {
            System.out.println("SDK初始化失败");
        }
        System.out.println("SDK初始化成功(预热已完成)");
        System.out.println("SDK实例: " + recordSdk + ", 类加载器: " + recordSdk.getClass().getClassLoader());
    }
 
    private final AtomicInteger SEQ = new AtomicInteger(1);
 
    // 安全的序列号生成
    private synchronized int getSafeSeq() {
        int seq = SEQ.getAndIncrement();
        // 检测并重置回绕
        if (seq >= Integer.MAX_VALUE - 1000) {
            SEQ.set(1);
            log.warn("序列号即将达到上限,已重置");
        }
        return seq;
    }
 
    /**
     * TCP+XML设备登录
     *
     * @param camera 设备信息
     * @return 登录ID,失败返回 -1
     * @throws Exception 网络或解析异常由上层处理
     */
    @Override
    public void login(ArdCamera camera) {
        String ip = camera.getIp();
        Integer port = camera.getPort();
 
        // 建立TCP连接
        boolean connected = nettyTcpClient.connectCamera(camera);
        if (!connected) {
            log.error("TCP连接建立失败: {}:{}", ip, port);
            camera.setState("0");
            camera.setChanNum(0);
            camera.setLoginId(-1L);
            eventPublisher.publishEvent(new LoginEvent(camera));
            return;
        }
        log.debug("TCP连接建立成功,准备发送登录请求");
 
        // 1️⃣ 生成序列号
        int preLoginSeq = getSafeSeq();
 
        // 2️⃣ 预登录获取盐
        String preLoginResp = nettyTcpClient.sendXmlAndWait(camera.getId(), new PreLoginRequest(preLoginSeq));
        PreLoginResponse preRes = XmlUtils.fromXml(preLoginResp, PreLoginResponse.class);
 
        if (preRes.getResult().getCode() != 0) {
            camera.setLoginId(-1L);
            camera.setState("0");
            Integer errorCode = preRes.getResult().getCode();
            String errorDesc = ErrorCodeEnum.getDescByCode(errorCode);
            log.warn("设备[{}:{}]预登录失败,错误码:{},原因:{}", ip, port, errorCode, errorDesc);
            return;
        }
 
        // 3️⃣ 登录
        int loginSeq = getSafeSeq();
        String loginResp = nettyTcpClient.sendXmlAndWait(camera.getId(),
                new LoginRequest(camera, preRes.getParameters().getSessionID(),
                        preRes.getParameters().getChallenge(), loginSeq));
        LoginResponse res = XmlUtils.fromXml(loginResp, LoginResponse.class);
        if (res.getResult().getCode() != 0) {
            // 登录失败处理
            handleLoginFail(camera, res);
        } else {
            // 登录成功处理
            handleLoginSuccess(camera, res);
        }
        // 登录成功
        log.debug("TCP+XML设备登录成功 [{}:{}]", ip, port);
        // 发布登录事件
        eventPublisher.publishEvent(new LoginEvent(camera));
    }
 
    /**
     * 登录失败处理
     */
    private void handleLoginFail(ArdCamera camera, LoginResponse res) {
        camera.setChanNum(0);
        camera.setLoginId(-1L);
        camera.setState("0");
        camera.setChannelList(null);
        Integer errorCode = res.getResult().getCode();
        String errorDesc = ErrorCodeEnum.getDescByCode(errorCode);
        log.warn("设备[{}:{}]登录失败,错误码:{},原因:{}", camera.getIp(), camera.getPort(), errorCode, errorDesc);
    }
 
    /**
     * 登录成功处理
     */
    private void handleLoginSuccess(ArdCamera camera, LoginResponse res) {
        camera.setState("1");
        camera.setChanNum(2);
        Long loginId = System.currentTimeMillis(); // 可以用时间戳或其他唯一ID
        camera.setLoginId(loginId);
        String sessionId = res.getHeader().getSessionId();
        SessionCache.put(camera.getId(), sessionId);
        // 获取最新通道
        List<ArdChannel> channels = getChannels(camera);
        if (!channels.isEmpty()) {
            camera.setChannelList(channels);
        }
        log.info("设备登录成功 [{}:{}]", camera.getIp(), camera.getPort());
    }
 
    @Override
    public void logout(String cameraId) {
        nettyTcpClient.disconnectCamera(cameraId);
    }
 
    // 建议按 cameraId 维度保存失败次数
    private final ConcurrentHashMap<String, AtomicInteger> offlineFailCountMap = new ConcurrentHashMap<>();
    private static final int MAX_FAIL_COUNT = 2;
 
    @Override
    public Boolean isOnLine(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        AtomicInteger failCount = offlineFailCountMap.computeIfAbsent(cameraId, k -> new AtomicInteger(0));
        try {
            int seq = getSafeSeq();
            String sessionId = SessionCache.get(cameraId);
            GetDeviceStateRequest request = new GetDeviceStateRequest("", seq, sessionId);
            String response = nettyTcpClient.sendXmlAndWait(cameraId, request);
            GetDeviceStateResponse res = XmlUtils.fromXml(response, GetDeviceStateResponse.class);
            if (res != null && res.getResult() != null && res.getResult().getCode() == 0) {
                // ✅ 成功:清零失败计数
                failCount.set(0);
                return true;
            }
        } catch (Exception e) {
            log.error("设备在线状态查询异常:{}", e.getMessage());
            int count = failCount.incrementAndGet();
            log.warn("设备 [{}] 在线检测异常,第 {} 次", cameraId, count);
            // ⭐ 关键:3 次以内,仍认为在线
            if (count <= MAX_FAIL_COUNT) {
                return true;
            }
        }
        return false;
    }
 
    @Override
    public Boolean pTZControl(CameraCmd cameraCmd) {
        String cameraId = cameraCmd.getCameraId();
        Integer chanNo = cameraCmd.getChanNo();
        boolean enable = cameraCmd.isEnable();
        Integer speed = cameraCmd.getSpeed();
        Integer code = cameraCmd.getCode();
 
        try {
            int seq = getSafeSeq();
            String sessionId = SessionCache.get(cameraId);
            if (sessionId == null) {
                log.warn("【{}】未找到有效会话,PTZ控制失败", cameraId);
                return false;
            }
 
            String cmd = "";
            String param = "";
 
            // 1️⃣ 指令映射
            switch (code) {
                case 1:
                    cmd = "TUPL";
                    break;
                case 2:
                    cmd = "TU";
                    break;
                case 3:
                    cmd = "TUPR";
                    break;
                case 4:
                    cmd = "PL";
                    break;
                case 5:
                    break;
                case 6:
                    cmd = "PR";
                    break;
                case 7:
                    cmd = "TDPL";
                    break;
                case 8:
                    cmd = "TD";
                    break;
                case 9:
                    cmd = "TDPR";
                    break;
                case 10:
                    cmd = "ZIN";
                    break;
                case 11:
                    cmd = "ZOUT";
                    break;
                case 12:
                    cmd = "FN";
                    break;
                case 13:
                    cmd = "FR";
                    break;
                case 14:
                    cmd = "IO";
                    break;
                case 15:
                    cmd = "IC";
                    break;
                case 16:
                    cmd = "WP_ON";
                    break;
                default:
                    cmd = "STOP";
                    break;
            }
 
            // 2️⃣ 关闭动作处理
            if (!enable) {
                param = cmd;
                cmd = "STOP";
            }
 
            // 3️⃣ 速度映射
            int bizSpeed = ((speed - 1) * 31) + 1;
 
            log.debug("【{}】发送PTZ指令: {} 参数:{} 速度:{}", cameraId, cmd, param, bizSpeed);
 
            ControlPTZRequest request = new ControlPTZRequest(chanNo, cmd, param, bizSpeed, seq, sessionId);
 
            // 4️⃣ 发送请求并解析响应
            String response = nettyTcpClient.sendXmlAndWait(cameraId, request);
            ControlPTZResponse res = XmlUtils.fromXml(response, ControlPTZResponse.class);
 
            if (res != null && res.getResult() != null && res.getResult().getCode() == 0) {
                log.debug("【{}】PTZ指令发送成功: {} 参数:{} 速度:{}", cameraId, cmd, param, bizSpeed);
                return true;
            } else {
                Integer errorCode = (res != null && res.getResult() != null) ? res.getResult().getCode() : null;
                String errorDesc = ErrorCodeEnum.getDescByCode(errorCode);
                log.warn("【{}】PTZ控制失败,错误码:{} 原因:{}", cameraId, errorCode, errorDesc);
                return false;
            }
        } catch (Exception e) {
            log.error("【{}】PTZ控制异常:{}", cameraId, e.getMessage());
            return false;
        }
    }
 
 
    /**
     * 设置聚焦值
     *
     * @param cmd 相机命令
     * @return true 成功,false 失败
     */
    @Override
    public Boolean setFocusPos(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        int chanNo = cmd.getChanNo();
        Integer focus = cmd.getDwFocusPos();
 
        try {
            int seq = getSafeSeq();
            String sessionId = SessionCache.get(cameraId);
 
            // 1️⃣ 获取当前云台信息
            GetPtzInfoRequest getPtzInfoRequest = new GetPtzInfoRequest(chanNo, seq, sessionId);
            String getPtzInfoXml = nettyTcpClient.sendXmlAndWait(cameraId, getPtzInfoRequest);
            GetPtzInfoResponse getPtzInfoRes = XmlUtils.fromXml(getPtzInfoXml, GetPtzInfoResponse.class);
 
            if (getPtzInfoRes == null || getPtzInfoRes.getResult() == null || getPtzInfoRes.getResult().getCode() != 0) {
                log.warn("【{}】获取当前云台信息失败", cameraId);
                return false;
            }
 
            Float pan = getPtzInfoRes.getParameters().getPanAngle();
            Float tilt = getPtzInfoRes.getParameters().getTiltAngle();
            Float zoom = getPtzInfoRes.getParameters().getZoom();
 
            // 2️⃣ 设置绝对位置,包括聚焦值
            SetAbsLocationZoomRequest request = new SetAbsLocationZoomRequest(chanNo, pan, tilt, zoom, focus, seq,
                    sessionId);
            String response = nettyTcpClient.sendXmlAndWait(cameraId, request);
            GetPtzInfoResponse res = XmlUtils.fromXml(response, GetPtzInfoResponse.class);
 
            if (res != null && res.getResult() != null && res.getResult().getCode() == 0) {
                return true;
            } else {
                Integer errorCode = res != null && res.getResult() != null ? res.getResult().getCode() : null;
                String errorDesc = ErrorCodeEnum.getDescByCode(errorCode);
                log.warn("【{}】设置聚焦值失败,错误码:{},原因:{}", cameraId, errorCode, errorDesc);
                return false;
            }
 
        } catch (Exception e) {
            log.error("【{}】设置聚焦值异常: {}", cameraId, e.getMessage(), e);
            return false;
        }
    }
 
 
    @Override
    public Integer getFocusPos(CameraCmd cmd) {
        try {
            String cameraId = cmd.getCameraId();
            int chanNo = cmd.getChanNo();
            int seq = getSafeSeq();
            String sessionId = SessionCache.get(cameraId);
 
            GetPtzInfoRequest request = new GetPtzInfoRequest(chanNo, seq, sessionId);
            String response = nettyTcpClient.sendXmlAndWait(cameraId, request);
            GetPtzInfoResponse res = XmlUtils.fromXml(response, GetPtzInfoResponse.class);
 
            if (res != null && res.getResult() != null && res.getResult().getCode() == 0) {
                Float focus = res.getParameters().getFocus();
                return focus != null ? focus.intValue() : null;
            }
 
        } catch (Exception e) {
            log.error("获取聚焦值异常: {}", e.getMessage());
        }
        return null;
    }
 
 
    @Override
    public Boolean setPreset(CameraCmd cmd) {
        throw new CameraSDKException(
                CameraSDKException.ErrorCode.CMD_NOT_SUPPORT.getCode(),
                cmd.getCameraId(),
                "设置预置点功能暂不支持"
        );
    }
 
    @Override
    public Boolean gotoPreset(CameraCmd cmd) {
        throw new CameraSDKException(
                CameraSDKException.ErrorCode.CMD_NOT_SUPPORT.getCode(),
                cmd.getCameraId(),
                "调用预置位功能暂不支持"
        );
    }
 
    @Override
    public VideoCompressionCfg getVideoCompressionCfg(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer chanNo = cmd.getChanNo();
        int seq = getSafeSeq();
        String sessionId = SessionCache.get(cameraId);
        GetPuImageEncodeParaInfoRequest request = new GetPuImageEncodeParaInfoRequest(chanNo, seq, sessionId);
        String response = nettyTcpClient.sendXmlAndWait(cameraId, request);
        GetPuImageEncodeParaInfoResponse res = XmlUtils.fromXml(response, GetPuImageEncodeParaInfoResponse.class);
        if (res.getParameters() != null && !res.getParameters().getImageEncodeParas().isEmpty()) {
            String videoBitrate = res.getParameters().getImageEncodeParas().get(0).getBitRate().toString();
            String resolution = res.getParameters().getImageEncodeParas().get(0).getImageSize();
            String videoEncType = res.getParameters().getImageEncodeParas().get(0).getEncodeMode();
            String nFrameRate = res.getParameters().getImageEncodeParas().get(0).getFrameRate().toString();
            VideoCompressionCfg videoCompressionCfg = new VideoCompressionCfg();
            videoCompressionCfg.setVideoBitrate(videoBitrate);//比特率
            videoCompressionCfg.setResolution(resolution);//分辨率
            videoCompressionCfg.setVideoEncType(videoEncType);//编码
            videoCompressionCfg.setFrameRate(nFrameRate);//帧率
            return videoCompressionCfg;
        }
        return null;
    }
 
    @Override
    public Boolean controlDefog(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer chanNo = cmd.getChanNo();
        int seq = getSafeSeq();
        String sessionId = SessionCache.get(cameraId);
 
        // 光学除雾0: 关闭; 1: 黑白; 2: 彩色; 3: 自适应; 默认值:0.
        Integer enable = cmd.isEnable() ? 1 : 0;
 
        // 获取前端设备图像参数
        GetGeneralCameraParaRequest getRequest = new GetGeneralCameraParaRequest(chanNo, seq, sessionId);
        String response = nettyTcpClient.sendXmlAndWait(cameraId, getRequest);
        GetGeneralCameraParaResponse getRes = XmlUtils.fromXml(response, GetGeneralCameraParaResponse.class);
 
        if (getRes == null || getRes.getResult() == null || getRes.getResult().getCode() != 0) {
            int errorCode = getRes != null && getRes.getResult() != null ? getRes.getResult().getCode() : -1;
            String errorDesc = ErrorCodeEnum.getDescByCode(errorCode);
            log.warn("获取前端设备图像参数失败,错误码:{},原因:{}", errorCode, errorDesc);
            return false;
        }
 
        // 修改聚焦模式
        CameraParameters parameters = getRes.getParameters();
        parameters.setOpticalDefog(enable);
 
        // 发送设置请求
        seq = getSafeSeq();
        SetGeneralCameraParaRequest setRequest = new SetGeneralCameraParaRequest(parameters, seq, sessionId);
        response = nettyTcpClient.sendXmlAndWait(cameraId, setRequest);
        SetGeneralCameraParaResponse setRes = XmlUtils.fromXml(response, SetGeneralCameraParaResponse.class);
 
        if (setRes == null || setRes.getResult() == null || setRes.getResult().getCode() != 0) {
            int errorCode = setRes != null && setRes.getResult() != null ? setRes.getResult().getCode() : -1;
            String errorDesc = ErrorCodeEnum.getDescByCode(errorCode);
            log.warn("设置透雾失败,错误码:{},原因:{}", errorCode, errorDesc);
            return false;
        }
 
        log.debug("设置透雾成功");
        return true;
    }
 
    @Override
    public Boolean isEnableDefog(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer chanNo = cmd.getChanNo();
        int seq = getSafeSeq();
        String sessionId = SessionCache.get(cameraId);
 
        // 获取前端设备参数
        GetGeneralCameraParaRequest request = new GetGeneralCameraParaRequest(chanNo, seq, sessionId);
        String response = nettyTcpClient.sendXmlAndWait(cameraId, request);
        GetGeneralCameraParaResponse res = XmlUtils.fromXml(response, GetGeneralCameraParaResponse.class);
 
        if (res == null || res.getResult() == null || res.getResult().getCode() != 0) {
            int errorCode = res != null && res.getResult() != null ? res.getResult().getCode() : -1;
            String errorDesc = ErrorCodeEnum.getDescByCode(errorCode);
            String errorMsg = String.format("获取聚焦模式失败,错误码:%s,原因:%s", errorCode, errorDesc);
            log.warn(errorMsg);
            return null; // 或 false,根据你的业务逻辑
        }
 
        // 0-自动;1-单次;2-手动
        int mode = res.getParameters().getOpticalDefog();
 
        //光学除雾0: 关闭; 1: 黑白; 2: 彩色; 3: 自适应; 默认值:0.
        return mode != 0;
    }
 
    @Override
    public Boolean controlInfrared(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer chanNo = cmd.getChanNo();
        int seq = getSafeSeq();
        String sessionId = SessionCache.get(cameraId);
 
        // 1: 自动; 2: 黑白; 3: 彩色; 4: 报警触发; 5: 定时;
        Integer enable = cmd.isEnable() ? 2 : 3;
 
        // 获取前端设备图像参数
        GetGeneralCameraParaRequest getRequest = new GetGeneralCameraParaRequest(chanNo, seq, sessionId);
        String response = nettyTcpClient.sendXmlAndWait(cameraId, getRequest);
        GetGeneralCameraParaResponse getRes = XmlUtils.fromXml(response, GetGeneralCameraParaResponse.class);
 
        if (getRes == null || getRes.getResult() == null || getRes.getResult().getCode() != 0) {
            int errorCode = getRes != null && getRes.getResult() != null ? getRes.getResult().getCode() : -1;
            String errorDesc = ErrorCodeEnum.getDescByCode(errorCode);
            log.warn("获取前端设备图像参数失败,错误码:{},原因:{}", errorCode, errorDesc);
            return false;
        }
 
        // 修改日夜模式
        CameraParameters parameters = getRes.getParameters();
        parameters.setDayNightMode(enable);
 
        // 发送设置请求
        seq = getSafeSeq();
        SetGeneralCameraParaRequest setRequest = new SetGeneralCameraParaRequest(parameters, seq, sessionId);
        response = nettyTcpClient.sendXmlAndWait(cameraId, setRequest);
        SetGeneralCameraParaResponse setRes = XmlUtils.fromXml(response, SetGeneralCameraParaResponse.class);
 
        if (setRes == null || setRes.getResult() == null || setRes.getResult().getCode() != 0) {
            int errorCode = setRes != null && setRes.getResult() != null ? setRes.getResult().getCode() : -1;
            String errorDesc = ErrorCodeEnum.getDescByCode(errorCode);
            log.warn("设置透雾失败,错误码:{},原因:{}", errorCode, errorDesc);
            return false;
        }
 
        log.debug("设置透雾成功");
        return true;
    }
 
 
    @Override
    public Boolean isEnableInfrared(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer chanNo = cmd.getChanNo();
        int seq = getSafeSeq();
        String sessionId = SessionCache.get(cameraId);
 
        // 获取前端设备参数
        GetGeneralCameraParaRequest request = new GetGeneralCameraParaRequest(chanNo, seq, sessionId);
        String response = nettyTcpClient.sendXmlAndWait(cameraId, request);
        GetGeneralCameraParaResponse res = XmlUtils.fromXml(response, GetGeneralCameraParaResponse.class);
 
        if (res == null || res.getResult() == null || res.getResult().getCode() != 0) {
            int errorCode = res != null && res.getResult() != null ? res.getResult().getCode() : -1;
            String errorDesc = ErrorCodeEnum.getDescByCode(errorCode);
            String errorMsg = String.format("获取聚焦模式失败,错误码:%s,原因:%s", errorCode, errorDesc);
            log.warn(errorMsg);
            return null; // 或 false,根据你的业务逻辑
        }
 
        // 日夜切换, 2: 黑白; 3: 彩色;
        int mode = res.getParameters().getDayNightMode();
 
        //日夜切换, 1: 自动; 2: 黑白; 3: 彩色; 4: 报警触发; 5: 定时;
        return mode != 3;
    }
 
 
    @Override
    public Boolean controlFocusMode(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer chanNo = cmd.getChanNo();
        int seq = getSafeSeq();
        String sessionId = SessionCache.get(cameraId);
 
        // 聚焦模式:0-自动;1-单次;2-手动
        Integer enable = cmd.isEnable() ? 2 : 0;
 
        // 获取前端设备图像参数
        GetGeneralCameraParaRequest getRequest = new GetGeneralCameraParaRequest(chanNo, seq, sessionId);
        String response = nettyTcpClient.sendXmlAndWait(cameraId, getRequest);
        GetGeneralCameraParaResponse getRes = XmlUtils.fromXml(response, GetGeneralCameraParaResponse.class);
 
        if (getRes == null || getRes.getResult() == null || getRes.getResult().getCode() != 0) {
            int errorCode = getRes != null && getRes.getResult() != null ? getRes.getResult().getCode() : -1;
            String errorDesc = ErrorCodeEnum.getDescByCode(errorCode);
            log.warn("获取前端设备图像参数失败,错误码:{},原因:{}", errorCode, errorDesc);
            return false;
        }
 
        // 修改聚焦模式
        CameraParameters parameters = getRes.getParameters();
        parameters.setFocusMode(enable);
 
        // 发送设置请求
        seq = getSafeSeq();
        SetGeneralCameraParaRequest setRequest = new SetGeneralCameraParaRequest(parameters, seq, sessionId);
        response = nettyTcpClient.sendXmlAndWait(cameraId, setRequest);
        SetGeneralCameraParaResponse setRes = XmlUtils.fromXml(response, SetGeneralCameraParaResponse.class);
 
        if (setRes == null || setRes.getResult() == null || setRes.getResult().getCode() != 0) {
            int errorCode = setRes != null && setRes.getResult() != null ? setRes.getResult().getCode() : -1;
            String errorDesc = ErrorCodeEnum.getDescByCode(errorCode);
            log.warn("设置聚焦模式失败,错误码:{},原因:{}", errorCode, errorDesc);
            return false;
        }
 
        log.debug("设置聚焦模式成功");
        return true;
    }
 
 
    @Override
    public Boolean getFocusMode(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer chanNo = cmd.getChanNo();
        int seq = getSafeSeq();
        String sessionId = SessionCache.get(cameraId);
 
        // 获取前端设备参数
        GetGeneralCameraParaRequest request = new GetGeneralCameraParaRequest(chanNo, seq, sessionId);
        String response = nettyTcpClient.sendXmlAndWait(cameraId, request);
        GetGeneralCameraParaResponse res = XmlUtils.fromXml(response, GetGeneralCameraParaResponse.class);
 
        if (res == null || res.getResult() == null || res.getResult().getCode() != 0) {
            int errorCode = res != null && res.getResult() != null ? res.getResult().getCode() : -1;
            String errorDesc = ErrorCodeEnum.getDescByCode(errorCode);
            String errorMsg = String.format("获取聚焦模式失败,错误码:%s,原因:%s", errorCode, errorDesc);
            log.warn(errorMsg);
            return null; // 或 false,根据你的业务逻辑
        }
 
        // 0-自动;1-单次;2-手动
        int mode = res.getParameters().getFocusMode();
 
        // 这里用 Boolean 返回:true=手动,false=自动
        return mode == 2;
    }
 
 
    @Override
    public Boolean controlPTHeater(CameraCmd cmd) {
        throw new CameraSDKException(
                CameraSDKException.ErrorCode.CMD_NOT_SUPPORT.getCode(),
                cmd.getCameraId(),
                "功能暂不支持"
        );
    }
 
    @Override
    public Boolean controlCameraDeicing(CameraCmd cmd) {
        throw new CameraSDKException(
                CameraSDKException.ErrorCode.CMD_NOT_SUPPORT.getCode(),
                cmd.getCameraId(),
                "功能暂不支持"
        );
    }
 
    @Override
    public Boolean getPTZLockInfo(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer chanNo = cmd.getChanNo();
        int seq = getSafeSeq();
        String sessionId = SessionCache.get(cameraId);
 
        // 获取前端设备参数
        GetPuLockRequest request = new GetPuLockRequest(seq, sessionId);
        String response = nettyTcpClient.sendXmlAndWait(cameraId, request);
        GetPuLockResponse res = XmlUtils.fromXml(response, GetPuLockResponse.class);
 
        if (res == null || res.getResult() == null || res.getResult().getCode() != 0) {
            int errorCode = res != null && res.getResult() != null ? res.getResult().getCode() : -1;
            String errorDesc = ErrorCodeEnum.getDescByCode(errorCode);
            String errorMsg = String.format("获取聚焦模式失败,错误码:%s,原因:%s", errorCode, errorDesc);
            log.warn(errorMsg);
            return null; // 或 false,根据你的业务逻辑
        }
 
        //0:未锁定;1:锁定
        int mode = res.getParameters().getPuLock();
 
        // 这里用 Boolean 返回:true=锁定,false=未锁定
        return mode == 1;
    }
 
    @Override
    public Boolean setPTZLockInfo(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer chanNo = cmd.getChanNo();
        int seq = getSafeSeq();
        String sessionId = SessionCache.get(cameraId);
 
        // 0: 解锁; 1: 锁定;
        Integer enable = cmd.isEnable() ? 1 : 0;
 
        // 设置云台锁定
        SetPuLockRequest request = new SetPuLockRequest(enable, seq, sessionId);
        String response = nettyTcpClient.sendXmlAndWait(cameraId, request);
        SetPuLockResponse getRes = XmlUtils.fromXml(response, SetPuLockResponse.class);
        // 没返回 → 失败
        if (getRes == null || getRes.getResult() == null) {
            log.warn("云台锁定:设备未返回结果");
            return false;
        }
        int code = getRes.getResult().getCode();
        if (code == 0) {
            return true;
        } else {
            int errorCode = getRes != null && getRes.getResult() != null ? getRes.getResult().getCode() : -1;
            String errorDesc = ErrorCodeEnum.getDescByCode(errorCode);
            log.warn("设置云台锁定参数失败,错误码:{},原因:{}", errorCode, errorDesc);
            return false;
        }
    }
 
    @Override
    public byte[] localCapture(CameraCmd cmd) {
        throw new CameraSDKException(
                CameraSDKException.ErrorCode.CMD_NOT_SUPPORT.getCode(),
                cmd.getCameraId(),
                "功能暂不支持"
        );
    }
 
    @Override
    public String picCutCate(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer chanNo = cmd.getChanNo();
        String type = cmd.getType() == null ? "other" : cmd.getType();
        String bucketName = cmd.getBucketName() == null ? "pic" : cmd.getBucketName();
        int seq = getSafeSeq();
        String sessionId = SessionCache.get(cameraId);
 
        // 获取前端设备图像参数
        GetSnapPicSyncRequest getRequest = new GetSnapPicSyncRequest(chanNo, seq, sessionId);
        String response = nettyTcpClient.sendXmlAndWait(cameraId, getRequest);
        GetSnapPicSyncResponse getRes = XmlUtils.fromXml(response, GetSnapPicSyncResponse.class);
 
        if (getRes == null || getRes.getResult() == null || getRes.getResult().getCode() != 0) {
            int errorCode = getRes != null && getRes.getResult() != null ? getRes.getResult().getCode() : -1;
            String errorDesc = ErrorCodeEnum.getDescByCode(errorCode);
            log.warn("抓图失败,错误码:{},原因:{}", errorCode, errorDesc);
            return "";
        }
 
        String base64Str = getRes.getParameters().getPicBuf();
        if (base64Str == null || base64Str.isEmpty()) {
            log.warn("Base64图片数据为空");
            return "";
        }
 
        // 处理 Base64 前缀(如果有 data:image/jpeg;base64, 这样的前缀)
        String pureBase64 = base64Str;
        if (base64Str.contains(",")) {
            pureBase64 = base64Str.split(",")[1];
        }
 
        // 存储到minio
        String objectName = cameraId + "_" + chanNo + ".jpeg";
        String url = "";
        File tempFile = null;
 
        try {
            // Base64 解码
            byte[] imageBytes = Base64.getDecoder().decode(pureBase64);
 
            // 创建临时文件
            tempFile = File.createTempFile("snap_", ".jpeg");
            java.nio.file.Files.write(tempFile.toPath(), imageBytes);
 
            // 使用 FileMultipartFile 转换
            MultipartFile multipartFile = new FileMultipartFile(tempFile, objectName, MimeTypeUtils.IMAGE_JPEG);
 
            R<SysFile> sysFileR = remoteFileService.upload(multipartFile, bucketName, type);
            if (sysFileR.getCode() == R.SUCCESS) {
                url = sysFileR.getData().getUrl();
                log.debug("上传文件成功:{}", url);
            }
 
        } catch (Exception e) {
            log.error("上传失败", e);
        } finally {
            // 删除临时文件
            if (tempFile != null && tempFile.exists()) {
                tempFile.delete();
            }
        }
 
        return url;
    }
 
    @Override
    public String picCutCate(CameraCmd cmd, String bucketName, String objectName) {
        return "";
    }
 
    @Override
    public PtzDto getPtz(CameraCmd cmd) {
        try {
            String cameraId = cmd.getCameraId();
            Integer chanNo = cmd.getChanNo();
            int seq = getSafeSeq();
            String sessionId = SessionCache.get(cameraId);
            GetPtzInfoRequest request = new GetPtzInfoRequest(chanNo, seq, sessionId);
            String response = nettyTcpClient.sendXmlAndWait(cameraId, request);
            GetPtzInfoResponse res = XmlUtils.fromXml(response, GetPtzInfoResponse.class);
            // 合并空值判断
            if (res == null || res.getResult() == null) {
                String reason = res == null ? "接口返回结果为空" : "接口返回无错误信息";
                return null;
            }
            Integer errorCode = res.getResult().getCode();
            if (errorCode != 0) {
                // 2. 通过ErrorCodeEnum获取错误描述
                String errorDesc = ErrorCodeEnum.getDescByCode(errorCode);
                // 3. 拼接错误信息,返回包含错误码和描述的提示
                String errorMsg = String.format("获取ptz失败,错误码:%s,原因:%s", errorCode, errorDesc);
                return null;
            }
            Float panAngle = res.getParameters().getPanAngle();
            Float P = new BigDecimal(panAngle.toString()).setScale(2, RoundingMode.HALF_UP).floatValue();
            Float tiltAngle = res.getParameters().getTiltAngle();
            Float T = tiltAngle < 0 ? tiltAngle + 360 : tiltAngle;
            T = new BigDecimal(T.toString()).setScale(2, RoundingMode.HALF_UP).floatValue();
            float zoomRatio = res.getParameters().getZoomRatio();
            return new PtzDto(P, T, zoomRatio);
        } catch (Exception ex) {
            log.error("获取ptz异常:{}", ex.getMessage());
            return null;
        }
    }
 
    @Override
    public PtzScope getPtzScope(CameraCmd cmd) {
        PtzScope ptzScope = new PtzScope();
        ptzScope.setPMax(360f);
        ptzScope.setPMin(0f);
        ptzScope.setTMax(90f);
        ptzScope.setTMin(270f);
        ptzScope.setZMax(40f);
        ptzScope.setZMin(0f);
        return ptzScope;
    }
 
    @Override
    public Boolean setPtz(CameraCmd cmd) {
        try {
            String cameraId = cmd.getCameraId();
            Integer chanNo = cmd.getChanNo();
            PtzDto ptzDto = cmd.getPtzDto();
            int seq = getSafeSeq();
            String sessionId = SessionCache.get(cameraId);
            //保留小数点2位
            Float P = new BigDecimal(ptzDto.getP().toString()).setScale(2, RoundingMode.HALF_UP).floatValue();
            Float T = ptzDto.getT() > 300 ? ptzDto.getT() - 360 : ptzDto.getT();
            T = new BigDecimal(T.toString()).setScale(2, RoundingMode.HALF_UP).floatValue();
            Float Z = ptzDto.getZ();
            SetAbsLocationZoomRequest request = new SetAbsLocationZoomRequest(chanNo, P, T, Z, seq, sessionId);
            String response = nettyTcpClient.sendXmlAndWait(cameraId, request);
            GetPtzInfoResponse res = XmlUtils.fromXml(response, GetPtzInfoResponse.class);
            // 合并空值判断
            if (res == null || res.getResult() == null) {
                String reason = res == null ? "接口返回结果为空" : "接口返回无错误信息";
                log.warn(reason);
                return false;
            }
            Integer errorCode = res.getResult().getCode();
            if (errorCode != 0) {
                // 2. 通过ErrorCodeEnum获取错误描述
                String errorDesc = ErrorCodeEnum.getDescByCode(errorCode);
                // 3. 拼接错误信息,返回包含错误码和描述的提示
                String errorMsg = String.format("设置ptz失败,错误码:%s,原因:%s", errorCode, errorDesc);
                log.warn(errorMsg);
                return false;
            }
            return true;
        } catch (Exception ex) {
            log.error("设置ptz异常:{}", ex.getMessage());
            return false;
        }
    }
 
    @Override
    public Boolean setZeroPtz(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer chanNo = cmd.getChanNo();
        int seq = getSafeSeq();
        String sessionId = SessionCache.get(cameraId);
        // 设置零方位角
        SetLocationParaPtRequest request = new SetLocationParaPtRequest(seq, sessionId);
        String response = nettyTcpClient.sendXmlAndWait(cameraId, request);
        SetLocationParaPtResponse getRes = XmlUtils.fromXml(response, SetLocationParaPtResponse.class);
        // 没返回 → 失败
        if (getRes == null || getRes.getResult() == null) {
            return false;
        }
        int code = getRes.getResult().getCode();
        if (code == 0) {
            return true;
        } else {
            int errorCode = getRes != null && getRes.getResult() != null ? getRes.getResult().getCode() : -1;
            String errorDesc = ErrorCodeEnum.getDescByCode(errorCode);
            log.warn("设置零方位角失败,错误码:{},原因:{}", errorCode, errorDesc);
            return false;
        }
    }
 
    @Override
    public String record(CameraCmd cmd) {
        return "";
    }
 
    @Override
    public Boolean guideTargetPosition(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        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 cameraPTZ = GisUtil.getCameraPTZ(cameraPosition, targetPositions, 20, 150);
        cmd.setPtzDto(cameraPTZ);
        validatePtzRange(cmd);
        setPtz(cmd);
        return true;
    }
 
    /**
     * 校验 PTZ 参数是否在设备支持的范围内
     *
     * @param cmd 相机命令,需包含 PTZ 参数
     * @throws CameraSDKException 校验失败时抛出异常
     */
    protected void validatePtzRange(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
 
        // 从数据库获取 PTZ 范围
        PtzScope ptzScope = ardCameraPtzScopeService.getPtzScopeByCameraId(cameraId);
        if (ptzScope == null) {
            log.warn("获取 PTZ 范围失败: " + cameraId);
            throw new CameraSDKException(CameraSDKException.ErrorCode.OVER_GUIDANCE_RANGE.getCode(),
                    cameraId, "获取 PTZ 范围失败,请检查设备配置");
        }
 
        PtzDto ptzDto = cmd.getPtzDto();
        if (ptzDto == null) {
            log.warn("PTZ 控制参数为空: " + cameraId);
            throw new CameraSDKException(CameraSDKException.ErrorCode.CMD_NOT_SUPPORT.getCode(),
                    cameraId,
                    "PTZ 控制参数为空");
        }
 
        float p = ptzDto.getP();
        float t = ptzDto.getT();
        float z = ptzDto.getZ() < 1 ? 1 : ptzDto.getZ();  // 修正 z 小于 1 的情况
 
        // 水平角度校验
        if (p < ptzScope.getPMin() || p > ptzScope.getPMax()) {
            log.warn("水平角度超出范围:" + p + ",允许范围:" + ptzScope.getPMin() + " ~ " + ptzScope.getPMax());
            throw new CameraSDKException(CameraSDKException.ErrorCode.OVER_GUIDANCE_RANGE.getCode(),
                    cameraId,
                    "水平角度超出范围:" + ptzScope.getPMin() + " ~ " + ptzScope.getPMax());
        }
 
        // 俯仰角度校验
        if (t <= 90) {
            if (t > ptzScope.getTMax()) {
                log.warn("俯仰角度超出向上范围:" + t + ",最大允许:" + ptzScope.getTMax());
                throw new CameraSDKException(CameraSDKException.ErrorCode.OVER_GUIDANCE_RANGE.getCode(),
                        cameraId,
                        "俯仰角度超出向上范围:" + ptzScope.getTMax());
            }
        } else if (t >= 270 && t <= 360) {
            if (t < ptzScope.getTMin()) {
                log.warn("俯仰角度超出向下范围:" + t + ",最小允许:" + ptzScope.getTMin());
                throw new CameraSDKException(CameraSDKException.ErrorCode.OVER_GUIDANCE_RANGE.getCode(),
                        cameraId,
                        "俯仰角度超出向下范围:" + ptzScope.getTMin());
            }
        } else {
            log.warn("俯仰角度非法:" + t);
            throw new CameraSDKException(CameraSDKException.ErrorCode.BEYOND_VISIBILITY_RANGE.getCode(),
                    cameraId,
                    "俯仰角度非法:" + t);
        }
 
        // 变倍校验
        if (z > ptzScope.getZMax()) {
            log.warn("变倍超出范围:" + z + ",最大允许:" + ptzScope.getZMax());
            throw new CameraSDKException(CameraSDKException.ErrorCode.OVER_GUIDANCE_RANGE.getCode(),
                    cameraId,
                    "变倍超出范围:" + ptzScope.getZMax());
        }
    }
    @Override
    public Boolean recordStart(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer chanNo = cmd.getChanNo();
        String name = cameraId + "_" + chanNo;
 
        try {
            // 1. 获取流媒体信息
            R<ArdVtdu> info = remoteArdVtduService.getInfo(name, SecurityConstants.INNER);
            if (info.getCode() != R.SUCCESS || info.getData() == null) {
                log.warn("获取摄像头信息失败: {}, code: {}", name, info.getCode());
                return false;
            }
 
            ArdVtdu ardVtdu = info.getData();
            String rtspUrl = ardVtdu.getRtspUrl();
            if (rtspUrl == null || rtspUrl.isEmpty()) {
                log.error("RTSP地址为空: {}", name);
                return false;
            }
 
            // 2. 开始录像
            String sessionId = recordSdk.startRecording(cameraId, rtspUrl, null);
 
            if (sessionId == null || sessionId.isEmpty()) {
                log.error("录像SDK返回SessionId为空,开始录像失败: {}", name);
                return false;
            }
 
            // 3. 缓存 SessionId
            RecordSessionCache.put(name, sessionId);
            log.info("录像开始成功: {}, sessionId: {}", name, sessionId);
            return true;
 
        } catch (Exception ex) {
            log.error("开始录像发生未知异常: {}", name, ex);
            return false;
        }
    }
 
    @Override
    public String recordStop(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer chanNo = cmd.getChanNo();
        String name = cameraId + "_" + chanNo;
        String localFilePath = null;
 
        try {
            // 1. 获取并移除 SessionId (原子操作,防止并发问题)
            String sessionId = RecordSessionCache.remove(name);
            if (sessionId == null) {
                log.warn("未找到正在进行的录像会话,可能已停止或未开始: {}", name);
                return "";
            }
 
            // 2. 停止录像并获取本地文件路径
            List<String> filePaths = recordSdk.stopRecording(sessionId);
            if (filePaths == null || filePaths.isEmpty()) {
                log.error("录像停止失败,未获取到文件路径: {}, sessionId: {}", name, sessionId);
                return "";
            }
 
            localFilePath = filePaths.get(0);
            log.info("录像停止成功,本地文件: {}", localFilePath);
 
            // 3. 校验文件是否存在
            File file = new File(localFilePath);
            if (!file.exists()) {
                log.error("录像文件不存在,无法上传: {}", localFilePath);
                return "";
            }
 
            // 4. 使用 FileMultipartFile 转换(不用 MultipartFileUtil)
            String objectName = cameraId + "_" + chanNo + ".mp4";
            String type = cmd.getType() == null ? "record" : cmd.getType();
 
            MultipartFile multipartFile = new FileMultipartFile(file, objectName, MimeTypeUtils.VIDEO_MP4);
 
            R<SysFile> sysFileR = remoteFileService.upload(multipartFile, "record", type);
 
            if (sysFileR.getCode() == R.SUCCESS && sysFileR.getData() != null) {
                String url = sysFileR.getData().getUrl();
                log.info("录像上传MinIO成功: {}", url);
                return url;
            } else {
                log.error("录像上传MinIO失败: {}, msg: {}", name, sysFileR.getMsg());
                return "";
            }
 
        } catch (Exception ex) {
            log.error("停止录像过程发生异常: {}", name, ex);
            return "";
        } finally {
            // 5. 【重要】清理本地临时文件
            if (localFilePath != null) {
                try {
                    java.nio.file.Files.deleteIfExists(java.nio.file.Paths.get(localFilePath));
                    log.debug("本地临时录像文件已删除: {}", localFilePath);
                } catch (Exception e) {
                    log.warn("删除本地临时文件失败: {}", localFilePath, e);
                }
            }
        }
    }
 
    @Override
    public PtzParamDTO getGisInfo(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer chanNo = cmd.getChanNo();
        try {
            int seq = getSafeSeq();
            String sessionId = SessionCache.get(cameraId);
            GetPtzInfoRequest request = new GetPtzInfoRequest(chanNo, seq, sessionId);
            String response = nettyTcpClient.sendXmlAndWait(cameraId, request);
            GetPtzInfoResponse res = XmlUtils.fromXml(response, GetPtzInfoResponse.class);
            // 合并空值判断
            if (res == null || res.getResult() == null) {
                String reason = res == null ? "接口返回结果为空" : "接口返回无错误信息";
                return new PtzParamDTO();
            }
            Integer errorCode = res.getResult().getCode();
            if (errorCode != 0) {
                // 2. 通过ErrorCodeEnum获取错误描述
                String errorDesc = ErrorCodeEnum.getDescByCode(errorCode);
                // 3. 拼接错误信息,返回包含错误码和描述的提示
                String errorMsg = String.format("获取ptz失败,错误码:%s,原因:%s", errorCode, errorDesc);
                return new PtzParamDTO();
            }
            Float panAngle = res.getParameters().getPanAngle();
            Float tiltAngle = res.getParameters().getTiltAngle();
            Float zoomRatio = res.getParameters().getZoomRatio();
            Float zoomAngle = res.getParameters().getZoom();//水平视场角
            Float tZoomAngle = res.getParameters().getTZoom();//垂直视场角
 
            PtzParamDTO ptzDTO = new PtzParamDTO();
            ptzDTO.setP(panAngle);
            ptzDTO.setT(tiltAngle);
            ptzDTO.setZ(zoomRatio);
            ptzDTO.setFHorFieldAngle(zoomAngle);
            ptzDTO.setFVerFieldAngle(tZoomAngle);
            return ptzDTO;
        } catch (Exception ex) {
            log.error("设备【{}】获取ptz异常:{}", cameraId, ex.getMessage());
            return new PtzParamDTO();
        }
    }
 
    @Override
    public Double correctPitch(CameraCmd cmd) {
        return 0.0;
    }
 
    @Override
    public List<ArdChannel> getChannels(ArdCamera camera) {
        if (camera == null || camera.getChanNum() <= 0) {
            return new ArrayList<>();
        }
        String cameraId = camera.getId();
        List<ArdChannel> channels = new ArrayList<>();
        for (int i = 1; i <= 2; i++) {
            ArdChannel channel = new ArdChannel();
            CameraCmd cameraCmd = new CameraCmd(cameraId, i);
            // 设置通道基本属性
            channel.setDeviceId(cameraId);
            String channelName = getChannelName(cameraCmd);
            channel.setName(channelName);
            channel.setChanNo(i);  // 通道号从1开始
            VideoCompressionCfg videoCompressionCfg = getVideoCompressionCfg(cameraCmd);
            if (videoCompressionCfg != null) {
                channel.setVideoCode(videoCompressionCfg.getVideoEncType());
            }
            // 获取rtsp地址
            String rtspUrl = String.format("rtsp://%s:%s@%s:%d/00010%d",
                    camera.getUsername(),
                    camera.getPassword(),
                    camera.getIp(),
                    camera.getRtspPort(),
                    channel.getChanNo() - 1
            );
            channel.setLiveAddress(rtspUrl);
            channels.add(channel);
        }
 
        return channels;
    }
 
    private String getChannelName(CameraCmd cmd) {
        String cameraId = cmd.getCameraId();
        Integer chanNo = cmd.getChanNo();
        String sessionId = SessionCache.get(cameraId);
        int seq = getSafeSeq();
        GetPuImageTextParaRequest request = new GetPuImageTextParaRequest(chanNo, seq, sessionId);
        String response = nettyTcpClient.sendXmlAndWait(cameraId, request);
        GetPuImageTextParaResponse res = XmlUtils.fromXml(response, GetPuImageTextParaResponse.class);
        if (res.getParameters() != null && !res.getParameters().getBitmapText().isEmpty()) {
            String bitmapText = res.getParameters().getBitmapText();
            return new String(Base64.getDecoder().decode(bitmapText), Charset.forName("GBK"));
        }
        return "通道" + chanNo;
    }
 
 
    @Override
    public Boolean set3DPosition(CameraCmd cmd) {
        try {
            String cameraId = cmd.getCameraId();
            Integer chanNo = cmd.getChanNo();
            PointFrame pointFrame = cmd.getPointFrame();
            PTZ3DLocationConverter.LocationParam locationParam = PTZ3DLocationConverter.convertPrecise(pointFrame);
            int seq = getSafeSeq();
            String sessionId = SessionCache.get(cameraId);
            SetPTZ3DLocationRequest request = new SetPTZ3DLocationRequest(chanNo, locationParam, seq, sessionId);
            String response = nettyTcpClient.sendXmlAndWait(cameraId, request);
            SetPTZ3DLocationResponse res = XmlUtils.fromXml(response, SetPTZ3DLocationResponse.class);
            // 合并空值判断
            if (res == null || res.getResult() == null) {
                String reason = res == null ? "接口返回结果为空" : "接口返回无错误信息";
                log.warn(reason);
                return false;
            }
            Integer errorCode = res.getResult().getCode();
            if (errorCode != 0) {
                // 2. 通过ErrorCodeEnum获取错误描述
                String errorDesc = ErrorCodeEnum.getDescByCode(errorCode);
                // 3. 拼接错误信息,返回包含错误码和描述的提示
                String errorMsg = String.format("获取ptz失败,错误码:%s,原因:%s", errorCode, errorDesc);
                log.warn(errorMsg);
                return false;
            }
            return true;
        } catch (Exception e) {
            log.error("3D定位异常:{}", e.getMessage());
            return false;
        }
    }
 
    @Override
    public DeviceCFG getSystemInfo(CameraCmd cmd) {
        try {
            String cameraId = cmd.getCameraId();
            int seq = getSafeSeq();
            String sessionId = SessionCache.get(cameraId);
            GetDeviceInfoRequest request = new GetDeviceInfoRequest(cameraId, seq, sessionId);
            String response = nettyTcpClient.sendXmlAndWait(cameraId, request);
            GetDeviceInfoResponse res = XmlUtils.fromXml(response, GetDeviceInfoResponse.class);
            // 合并空值判断
            if (res == null || res.getResult() == null) {
                String reason = res == null ? "接口返回结果为空" : "接口返回无错误信息";
                return null;
            }
            Integer errorCode = res.getResult().getCode();
            if (errorCode != 0) {
                // 2. 通过ErrorCodeEnum获取错误描述
                String errorDesc = ErrorCodeEnum.getDescByCode(errorCode);
                // 3. 拼接错误信息,返回包含错误码和描述的提示
                String errorMsg = String.format("获取设备信息失败,错误码:%s,原因:%s", errorCode, errorDesc);
                return null;
            }
            DeviceCFG deviceCFG = new DeviceCFG();
            deviceCFG.setSDVRName(res.getParameters().getDeviceName());
            deviceCFG.setDwSoftwareVersion(res.getParameters().getFirmwareVersion());
            return deviceCFG;
        } catch (Exception ex) {
            log.error("获取设备信息异常:{}", ex.getMessage());
            return null;
        }
    }
    @Override
    public List<Map<String, Object>> getCameraVideoLable(Long userId, CameraCmd cmd)throws ServiceException {
        return null;
    }
 
}