222
liusuyi
2025-02-18 6e33e3f0ee952d768026fda82dc0000204e21c72
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
package com.ard.tianyu.service.impl;
 
import com.ard.common.utils.ByteUtils;
import com.ard.tianyu.config.TyConfiguration;
import com.ard.tianyu.http.TyClient;
import com.ard.tianyu.model.cache.TyTcpServerCache;
import com.ard.tianyu.model.cache.UavSignalInfoCache;
import com.ard.tianyu.model.cache.UavStateCache;
import com.ard.tianyu.model.cache.UavTelemetryDataCache;
import com.ard.tianyu.model.dto.BoxHistoryDTO;
import com.ard.tianyu.model.dto.BoxPhotosDTO;
import com.ard.tianyu.model.dto.BoxSettingsDTO;
import com.ard.tianyu.model.dto.camera.*;
import com.ard.tianyu.model.dto.control.AbsAngleControlDTO;
import com.ard.tianyu.model.dto.control.AngleControlDTO;
import com.ard.tianyu.model.dto.control.SpeedControlDTO;
import com.ard.tianyu.model.dto.converter.realdata.UavSignalInfoConverter;
import com.ard.tianyu.model.dto.converter.realdata.UavStateConverter;
import com.ard.tianyu.model.dto.converter.realdata.UavTelemetryDataConverter;
import com.ard.tianyu.model.dto.converter.wayline.PlanLineDataConverter;
import com.ard.tianyu.model.dto.manual.*;
import com.ard.tianyu.model.dto.settings.ReturnHeightSettingDTO;
import com.ard.tianyu.model.dto.settings.ReturnPointSettingDTO;
import com.ard.tianyu.model.dto.settings.VideoStreamSettingDTO;
import com.ard.tianyu.model.dto.signal.SignalInfo;
import com.ard.tianyu.model.dto.speaker.*;
import com.ard.tianyu.model.dto.state.UavState;
import com.ard.tianyu.model.dto.telemetry.TelemetryData;
import com.ard.tianyu.model.dto.wayline.*;
import com.ard.tianyu.model.receiver.*;
import com.ard.tianyu.netty.command.*;
import com.ard.tianyu.netty.TyNettyTcpClient;
import com.ard.tianyu.netty.command.camera.*;
import com.ard.tianyu.netty.command.control.AbsAngleControlCmd;
import com.ard.tianyu.netty.command.control.AngleControlCmd;
import com.ard.tianyu.netty.command.control.SpeedControlCmd;
import com.ard.tianyu.netty.command.manual.*;
import com.ard.tianyu.netty.command.notice.BoxPowerStatusReplyCmd;
import com.ard.tianyu.netty.command.notice.FlyWayEndReplyCmd;
import com.ard.tianyu.netty.command.notice.UavReadyReplyCmd;
import com.ard.tianyu.netty.command.real.SignalInfoCmd;
import com.ard.tianyu.netty.command.real.UavStateCmd;
import com.ard.tianyu.netty.command.real.UavTelemetryDataCmd;
import com.ard.tianyu.netty.command.register.*;
import com.ard.tianyu.netty.command.settings.*;
import com.ard.tianyu.netty.command.speaker.*;
import com.ard.tianyu.netty.command.wayline.*;
import com.ard.tianyu.netty.handler.ErrorCodeEnum;
import com.ard.tianyu.netty.protobuf.TelemetryDataPB;
import com.ard.tianyu.service.ITyService;
import com.ard.tianyu.websocket.utils.WebSocketUtil;
import com.ard.vtdu.domain.ArdVtdu;
import com.ard.vtdu.service.IMediaService;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
 
/**
 * TyServiceImpl
 *
 * @author liusuyi
 * @date 2024年10月22日8:37
 **/
@Slf4j
@Service
public class TyServiceImpl implements ITyService {
 
    @Resource
    private TyClient tyClient;
 
    @Resource
    private TyConfiguration tyConfiguration;
 
    @Resource
    private IMediaService mediaService;
 
    /**
     * 获取天宇token信息
     *
     * @return AccessTokenReceiver
     */
    @Override
    public AccessTokenReceiver getAccessToken() {
        return tyConfiguration.getAccessToken();
    }
 
    /**
     * 获取云盒列表
     *
     * @return List<BoxInfoReceiver>
     */
    @Override
    public List<BoxInfoReceiver> getBoxList() {
        //请求云盒列表
        try {
            TyHttpCommonReceiver<List<BoxInfoReceiver>> boxListReveiver = tyClient.getBoxList();
            return boxListReveiver.getContent();
        } catch (Exception e) {
            log.error("获取云盒列表失败:{}", e.getMessage());
            return new ArrayList<>();
        }
    }
 
    /**
     * 获取直播地址
     *
     * @return BoxStreamUrlReceiver
     */
    @Override
    public BoxStreamUrlReceiver getBoxLiveUrl(String boxSn) {
 
        TyHttpCommonReceiver<BoxStreamUrlReceiver> boxLiveUrl = tyClient.getBoxLiveUrl(boxSn);
        return boxLiveUrl.getContent();
    }
 
    /**
     * 获取飞行历史
     *
     * @return List<BoxHistoryReceiver>
     */
    @Override
    public List<BoxHistoryReceiver> getBoxHistory(BoxHistoryDTO boxHistoryDTO) {
        TyHttpCommonReceiver<List<BoxHistoryReceiver>> boxHistory = tyClient.getBoxHistory(boxHistoryDTO);
        return boxHistory.getContent();
    }
 
    /**
     * 获取盒子图片
     *
     * @return List<BoxPhotosReceiver>
     */
    @Override
    public List<BoxPhotosReceiver> getBoxPhotos(BoxPhotosDTO boxPhotosDTO) {
        TyHttpCommonReceiver<List<BoxPhotosReceiver>> boxPhotos = tyClient.getBoxPhotos(boxPhotosDTO);
        return boxPhotos.getContent();
    }
 
    /**
     * 修改云盒设置
     */
    @Override
    public Boolean updateBoxSettings(BoxSettingsDTO boxSettingsDTO) {
        TyHttpCommonReceiver<?> result = tyClient.updateBoxSettings(boxSettingsDTO);
        return result.getState() == 1;
    }
 
    /**
     * 设置返航高度
     */
    @Override
    public void updateReturnHeight(ReturnHeightSettingDTO returnHeightSettingDTO) {
        ReturnHeightSettingCmd returnHeightSettingCmd = new ReturnHeightSettingCmd(returnHeightSettingDTO);
        this.sendMessage(tyConfiguration.getIpPort(), returnHeightSettingCmd);
    }
 
    /**
     * 设置返航点
     */
    @Override
    public void updateReturnPoint(ReturnPointSettingDTO returnPointSettingDTO) {
        ReturnPointSettingCmd returnPointSettingCmd = new ReturnPointSettingCmd(returnPointSettingDTO);
        this.sendMessage(tyConfiguration.getIpPort(), returnPointSettingCmd);
    }
 
    /**
     * 设置视频码流
     */
    @Override
    public void updateVideoStream(VideoStreamSettingDTO videoStreamSettingDTO) {
        VideoStreamSettingCmd videoStreamSettingCmd = new VideoStreamSettingCmd(videoStreamSettingDTO);
        this.sendMessage(tyConfiguration.getIpPort(), videoStreamSettingCmd);
    }
 
    /**
     * 注册天宇平台
     */
    @Override
    public void registerMessage() {
        try {
            AccessTokenReceiver tyToken = tyConfiguration.getAccessToken();
            RegisterCmd registerMessage = new RegisterCmd(tyToken.getCompanyId(), tyToken.getAccessToken().getBytes());
            this.sendMessage(tyConfiguration.getIpPort(), registerMessage);
        } catch (Exception e) {
            log.error("注册天宇平台失败:{}", e.getMessage());
        }
    }
 
    /**
     * 云台控制基于角度
     */
    @Override
    public void angleControl(AngleControlDTO angleControlDto) {
        AngleControlCmd controlAngleCommand = new AngleControlCmd(angleControlDto);
        this.sendMessage(tyConfiguration.getIpPort(), controlAngleCommand);
    }
 
    /**
     * 云台绝对值控制(基于角度)
     */
    @Override
    public void absAngleControl(AbsAngleControlDTO absAngleControlDto) {
        AbsAngleControlCmd absAngleControlCommand = new AbsAngleControlCmd(absAngleControlDto);
        this.sendMessage(tyConfiguration.getIpPort(), absAngleControlCommand);
    }
 
    /**
     * 云台转动(基于速度)
     */
    @Override
    public void speedControl(SpeedControlDTO speedControlDto) {
        SpeedControlCmd speedControlCommand = new SpeedControlCmd(speedControlDto);
        this.sendMessage(tyConfiguration.getIpPort(), speedControlCommand);
    }
 
    /**
     * 设置相机模式
     */
    @Override
    public void updateCameraMode(CameraModeDTO cameraModeDTO) {
        CameraModeCmd cameraModeCommand = new CameraModeCmd(cameraModeDTO);
        this.sendMessage(tyConfiguration.getIpPort(), cameraModeCommand);
    }
 
    /**
     * 拍照
     */
    @Override
    public void capturePic(CapturePicDTO capturePicDto) {
        updateCameraMode(CameraModeDTO.builder().boxSn(capturePicDto.getBoxSn()).actionParam((byte) 0x01).build());
        CapturePicCmd capturePicCommand = new CapturePicCmd(capturePicDto);
        this.sendMessage(tyConfiguration.getIpPort(), capturePicCommand);
    }
 
    /**
     * 录像
     */
    @Override
    public void record(RecordDTO recordDto) {
        BaseCmd baseCmd;
        if (recordDto.getIsRecord()) {
            updateCameraMode(CameraModeDTO.builder().boxSn(recordDto.getBoxSn()).actionParam((byte) 0x02).build());
            baseCmd = new RecordStartCmd(recordDto);
        } else {
            baseCmd = new RecordStopCmd(recordDto);
        }
        this.sendMessage(tyConfiguration.getIpPort(), baseCmd);
    }
 
    /**
     * 变倍
     */
    @Override
    public void zoom(ZoomDTO zoomDTO) {
        ZoomCmd zoomCommand = new ZoomCmd(zoomDTO);
        this.sendMessage(tyConfiguration.getIpPort(), zoomCommand);
    }
 
    /**
     * 变倍到指定倍数
     */
    @Override
    public void zoomToLevel(ZoomToLevelDTO zoomToLevelDTO) {
        ZoomToLevelCmd zoomToLevelCommand = new ZoomToLevelCmd(zoomToLevelDTO);
        this.sendMessage(tyConfiguration.getIpPort(), zoomToLevelCommand);
    }
 
    /**
     * 切换摄像头
     */
    @Override
    public void switchCamera(SwitchCameraDTO switchCameraDTO) {
        SwitchCameraCmd switchCameraCmd = new SwitchCameraCmd(switchCameraDTO);
        this.sendMessage(tyConfiguration.getIpPort(), switchCameraCmd);
    }
 
    /**
     * 切换视频源
     */
    @Override
    public void switchSource(SwitchSourceDTO switchSourceDTO) {
        SwitchSourceCmd switchSourceCmd = new SwitchSourceCmd(switchSourceDTO);
        this.sendMessage(tyConfiguration.getIpPort(), switchSourceCmd);
    }
 
    /**
     * 打开或关闭照片回传
     */
    @Override
    public void photoReturnToggle(PhotoReturnToggleDTO photoReturnToggleDTO) {
        PhotoReturnToggleCmd photoReturnToggleCmd = new PhotoReturnToggleCmd(photoReturnToggleDTO);
        this.sendMessage(tyConfiguration.getIpPort(), photoReturnToggleCmd);
    }
 
    /**
     * 切换无人机控制权
     */
    @Override
    public void switchUavControl(SwitchUavControlDTO switchUavControlDTO) {
        SwitchUavControlCmd switchUavControlCmd = new SwitchUavControlCmd(switchUavControlDTO);
        this.sendMessage(tyConfiguration.getIpPort(), switchUavControlCmd);
    }
 
    /**
     * 起飞
     */
    @Override
    public void takeOff(TakeOffDTO takeOffDTO) {
        TakeOffCmd takeOffCmd = new TakeOffCmd(takeOffDTO);
        this.sendMessage(tyConfiguration.getIpPort(), takeOffCmd);
    }
 
    /**
     * 返航
     */
    @Override
    public void returnHome(ReturnHomeDTO returnHomeDTO) {
        ReturnHomeCmd returnHomeCmd = new ReturnHomeCmd(returnHomeDTO);
        this.sendMessage(tyConfiguration.getIpPort(), returnHomeCmd);
    }
 
    /**
     * 取消返航
     */
    @Override
    public void returnHomeCancel(ReturnHomeCancelDTO returnHomeCancelDTO) {
        ReturnHomeCancelCmd returnHomeCancelCmd = new ReturnHomeCancelCmd(returnHomeCancelDTO);
        this.sendMessage(tyConfiguration.getIpPort(), returnHomeCancelCmd);
    }
 
    /**
     * 降落
     */
    @Override
    public void land(LandDTO landDTO) {
        LandCmd landCmd = new LandCmd(landDTO);
        this.sendMessage(tyConfiguration.getIpPort(), landCmd);
    }
 
    /**
     * 取消降落
     */
    @Override
    public void landCancel(LandCancelDTO landCancelDTO) {
        LandCancelCmd landCancelCmd = new LandCancelCmd(landCancelDTO);
        this.sendMessage(tyConfiguration.getIpPort(), landCancelCmd);
    }
 
    /**
     * 强制降落
     */
    @Override
    public void landForced(LandForcedDTO landForcedDTO) {
        LandForcedCmd landForcedCmd = new LandForcedCmd(landForcedDTO);
        this.sendMessage(tyConfiguration.getIpPort(), landForcedCmd);
    }
 
    /**
     * 方向控制
     */
    @Override
    public void flyControl(FlyControlDTO flyControlDTO) {
        FlyControlCmd flyControlCmd = new FlyControlCmd(flyControlDTO);
        this.sendMessage(tyConfiguration.getIpPort(), flyControlCmd);
    }
 
    /**
     * 指点飞行控制
     */
    @Override
    public void flyPointControl(FlyPointControlDTO flyPointControlDTO) {
        FlyPointControlCmd flyPointControlCmd = new FlyPointControlCmd(flyPointControlDTO);
        this.sendMessage(tyConfiguration.getIpPort(), flyPointControlCmd);
    }
 
    /**
     * 停止指点飞行控制
     */
    @Override
    public void flyPointControlCancel(FlyPointControlCancelDTO flyPointControlCancelDTO) {
        FlyPointControlCancelCmd flyPointControlCancelCmd = new FlyPointControlCancelCmd(flyPointControlCancelDTO);
        this.sendMessage(tyConfiguration.getIpPort(), flyPointControlCancelCmd);
    }
 
    /**
     * 紧急制动
     */
    @Override
    public void emeragencyBrake(EmeragencyBrakeDTO emeragencyBrakeDTO) {
        EmergencyBrakeCmd emergencyBrakeCmd = new EmergencyBrakeCmd(emeragencyBrakeDTO);
        this.sendMessage(tyConfiguration.getIpPort(), emergencyBrakeCmd);
    }
 
    /**
     * 获取无人机状态
     */
    @Override
    public UavState getUavState(String boxSn) {
        return UavStateCache.getUavState(boxSn);
    }
 
    /**
     * 获取无人机遥测数据
     */
    @Override
    public TelemetryData getUavTelemetryData(String boxSn) {
        return UavTelemetryDataCache.getUavTelemetryData(boxSn);
    }
 
    /**
     * 获取无人机遥测数据
     */
    @Override
    public SignalInfo getSignalInfo(String boxSn) {
        return UavSignalInfoCache.getUavSignalInfo(boxSn);
    }
 
    /**
     * 喊话模式切换
     */
    @Override
    public void speakModeSwitch(SpeakModeSwitchDTO speakModeSwitchDTO) {
        SpeakModeSwitchCmd speakModeSwitchCmd = new SpeakModeSwitchCmd(speakModeSwitchDTO);
        this.sendMessage(tyConfiguration.getIpPort(), speakModeSwitchCmd);
    }
 
    /**
     * 文字喊话
     */
    @Override
    public void textSpeak(TextSpeakDTO textSpeakDTO) {
        TextSpeakCmd textSpeakCmd = new TextSpeakCmd(textSpeakDTO);
        this.sendMessage(tyConfiguration.getIpPort(), textSpeakCmd);
    }
 
    /**
     * 实时喊话
     */
    @Override
    public void realSpeak(RealSpeakDTO realSpeakDTO) {
        RealSpeakCmd realSpeakCmd = new RealSpeakCmd(realSpeakDTO);
        this.sendMessage(tyConfiguration.getIpPort(), realSpeakCmd);
    }
 
    /**
     * 录音喊话
     */
    @Override
    public void recordSpeak(RecordSpeakDTO recordSpeakDTO) {
        RecordSpeakCmd recordSpeakCmd = new RecordSpeakCmd(recordSpeakDTO);
        this.sendMessage(tyConfiguration.getIpPort(), recordSpeakCmd);
    }
 
    /**
     * 设置音量
     */
    @Override
    public void volumeSetting(VolumeSettingDTO volumeSettingDTO) {
        VolumeSettingCmd volumeSettingCmd = new VolumeSettingCmd(volumeSettingDTO);
        this.sendMessage(tyConfiguration.getIpPort(), volumeSettingCmd);
    }
 
    /**
     * 设置循环播放
     */
    @Override
    public void loopSetting(LoopSettingDTO loopSettingDTO) {
        LoopSettingCmd loopSettingCmd = new LoopSettingCmd(loopSettingDTO);
        this.sendMessage(tyConfiguration.getIpPort(), loopSettingCmd);
    }
 
    /**
     * 停止喊话
     */
    @Override
    public void stopSpeak(StopSpeakDTO stopSpeakDTO) {
        StopSpeakCmd stopSpeakCmd = new StopSpeakCmd(stopSpeakDTO);
        this.sendMessage(tyConfiguration.getIpPort(), stopSpeakCmd);
    }
 
    /**
     * 航线上传
     */
    @Override
    public void wayLineUpload(WayLineUploadDTO wayLineUploadDTO) {
        wayLineUploadDTO.setWayLineData(this.CreatePlanLineData());//设置航线数据
        WayLineUploadCmd wayLineUploadCmd = new WayLineUploadCmd(wayLineUploadDTO);
        this.sendMessage(tyConfiguration.getIpPort(), wayLineUploadCmd);
    }
 
    //创建航线
    private PlanLineData CreatePlanLineData() {
        TelemetryDataPB.PointData pointData1 = TelemetryDataPB.PointData.newBuilder()
                .setLng(124.12345).setLat(45.12345).setHeight(80).setSpeed(10)
                .setFlightPathMode(1)//航线飞行模式:1=直线飞行,2=协调转弯,3=曲线飞行(停),4=曲线飞行(不停)
                .setSafeGohomeFlag(0)//是否为安全返航点 0=不是,1=是
                .build();
        TelemetryDataPB.PointData pointData2 = TelemetryDataPB.PointData.newBuilder()
                .setLng(124.22222).setLat(45.22222).setHeight(80).setSpeed(10)
                .setFlightPathMode(1)//航线飞行模式:1=直线飞行,2=协调转弯,3=曲线飞行(停),4=曲线飞行(不停)
                .setSafeGohomeFlag(0)//是否为安全返航点 0=不是,1=是
                .build();
        TelemetryDataPB.PointData pointData3 = TelemetryDataPB.PointData.newBuilder()
                .setLng(124.33333).setLat(45.33333).setHeight(80).setSpeed(10)
                .setFlightPathMode(1)//航线飞行模式:1=直线飞行,2=协调转弯,3=曲线飞行(停),4=曲线飞行(不停)
                .setSafeGohomeFlag(0)//是否为安全返航点 0=不是,1=是
                .build();
 
        //创建航线
        TelemetryDataPB.PlanLineData PlanLineDataPB = TelemetryDataPB.PlanLineData.newBuilder()
                .setFinishedAction(2)//结束航线后动作:1=返航,2=原地悬停,3=原地降落,4=原地绕飞
                .setMaxSpeed(10)//最大飞行速度
                .setAutoSpeed(10)//自动飞行速度
                .addPoints(pointData1)//航线点1
                .addPoints(pointData2)//航线点2
                .addPoints(pointData3)//航线点3
                .setLoseAction(1)//网络失联后动作:0=返回HOME点,1=继续航线,默认0
                .setHomeHeight(6)//返航高度:(单位:米,范围20~1500)
                .setIsSaveEnergyMode(0)////是否开启节能模式:0=不是,1=是(说明:节能模式下,飞机沿最短直线距离由起飞点到航线第一个航点,非升高到第一个航点高度再飞往第一个航点)
                .build();
 
        return PlanLineDataConverter.convertProtoToEntity(PlanLineDataPB);
    }
 
    /**
     * 航线开始
     */
    @Override
    public void wayLineBegin(WayLineBeginDTO wayLineBeginDTO) {
        WayLineBeginCmd wayLineBeginCmd = new WayLineBeginCmd(wayLineBeginDTO);
        this.sendMessage(tyConfiguration.getIpPort(), wayLineBeginCmd);
    }
 
    /**
     * 航线暂停
     */
    @Override
    public void wayLinePause(WayLinePauseDTO wayLinePauseDTO) {
        WayLinePauseCmd wayLinePauseCmd = new WayLinePauseCmd(wayLinePauseDTO);
        this.sendMessage(tyConfiguration.getIpPort(), wayLinePauseCmd);
    }
 
    /**
     * 航线恢复
     */
    @Override
    public void wayLineResume(WayLineResumeDTO wayLineResumeDTO) {
        WayLineResumeCmd wayLineResumeCmd = new WayLineResumeCmd(wayLineResumeDTO);
        this.sendMessage(tyConfiguration.getIpPort(), wayLineResumeCmd);
    }
 
    /**
     * 航线结束
     */
    @Override
    public void wayLineEnd(WayLineEndDTO wayLineEndDTO) {
        WayLineEndCmd wayLineEndCmd = new WayLineEndCmd(wayLineEndDTO);
        this.sendMessage(tyConfiguration.getIpPort(), wayLineEndCmd);
    }
 
    /**
     * 点播
     */
    @Override
    public String play(String boxSn) {
        BoxStreamUrlReceiver boxLiveUrl = getBoxLiveUrl(boxSn);
        if(boxLiveUrl == null||boxLiveUrl.getRtsp() == null) {
            return "";
        }
        ArdVtdu ardVtdu = ArdVtdu.builder().name(boxSn).rtspSource(boxLiveUrl.getRtsp()).build();
        ardVtdu = mediaService.addPath(ardVtdu);
        return ardVtdu.getWebrtcUrl();
    }
 
    /**
     * 处理天宇服务器返回的消息
     */
    @Override
    public void processReplyMessage(String key, BaseCmd tyCommand) {
        byte cmdCode = tyCommand.getCmdCode();
        switch (cmdCode) {
            case 0x01:
                // 处理心跳消息
                RegisterReplyCmd registerReplyCommand = (RegisterReplyCmd) tyCommand;
                logPrint("注册消息", registerReplyCommand.getSuccess());
                if (registerReplyCommand.getSuccess() != 1) {
                    // 注册失败,重新注册
                    log.info("注册失败,重新注册");
                    registerMessage();
                }
                break;
            case 0x02:
                // 处理注册消息
                HeartbeatReplyCmd heartbeatReplyCmd = (HeartbeatReplyCmd) tyCommand;
                logPrint("心跳消息", heartbeatReplyCmd.getTimestamp());
                break;
            case 0x03:
                // 处理订阅消息
                break;
            case 0x04:
                // 处理取消订阅消息
                break;
            case 0x0A:
                // 处理云盒开关机通知消息
                BoxPowerStatusReplyCmd boxPowerStatusReplyCmd = (BoxPowerStatusReplyCmd) tyCommand;
                logPrint("云盒开关机通知消息", boxPowerStatusReplyCmd.getStatus());
                break;
            case (byte) 0xA8:
                // 处理无人机状态消息
                UavStateCmd uavStateCmd = (UavStateCmd) tyCommand;
                UavStateCache.putUavState(uavStateCmd.getUavState());
                logPrint("无人机状态", uavStateCmd.getUavState().getBoxSn());
                WebSocketUtil.sendMessageAll(uavStateCmd.getUavState());
                break;
            case (byte) 0xA9:
                // 处理无人机遥测消息
                UavTelemetryDataCmd uavTelemetryDataCmd = (UavTelemetryDataCmd) tyCommand;
                UavTelemetryDataCache.putUavTelemetryData(uavTelemetryDataCmd.getTelemetryData());
                logPrint("无人机遥测", uavTelemetryDataCmd.getTelemetryData().getBoxSn());
                WebSocketUtil.sendMessageAll(uavTelemetryDataCmd.getTelemetryData());
                break;
 
            case (byte) 0x0C:
                // 处理无人机信道质量数据
                SignalInfoCmd signalInfoCmd = (SignalInfoCmd) tyCommand;
                UavSignalInfoCache.putUavSignalInfoCache(signalInfoCmd.getSignalInfo());
                logPrint("无人机信道质量", signalInfoCmd.getSignalInfo().getBoxSn());
                break;
            case (byte) 0xD1:
                // 处理无人机
                if (tyCommand instanceof UavInitReplyCmd) {
                    UavInitReplyCmd uavInitReplyCmd = (UavInitReplyCmd) tyCommand;
                    UavInitInfoCmd uavInitCommand = new UavInitInfoCmd(uavInitReplyCmd.getBoxSn());
                    this.sendMessage(tyConfiguration.getIpPort(), uavInitCommand);
                    logPrint("无人机初始化详情", uavInitCommand.getBoxSn());
                }
                if (tyCommand instanceof UavReadyReplyCmd) {
                    UavReadyReplyCmd uavReadyReplyCmd = (UavReadyReplyCmd) tyCommand;
                    logPrint("无人机准备完成通知", uavReadyReplyCmd.getBatteryPower());
                }
                if (tyCommand instanceof CameraModeReplyCmd) {
                    CameraModeReplyCmd cameraModeReplyCommand = (CameraModeReplyCmd) tyCommand;
                    logPrint("无人机设置相机模式", cameraModeReplyCommand.getResult(), cameraModeReplyCommand.getErrorCode());
                }
                if (tyCommand instanceof FlyWayEndReplyCmd) {
                    FlyWayEndReplyCmd flyWayEndReplyCmd = (FlyWayEndReplyCmd) tyCommand;
                    logPrint("无人机飞行结束", flyWayEndReplyCmd.getResult(), flyWayEndReplyCmd.getErrorCode());
                }
                if (tyCommand instanceof CapturePicReplyCmd) {
                    CapturePicReplyCmd capturePicReplyCommand = (CapturePicReplyCmd) tyCommand;
                    logPrint("无人机拍照", capturePicReplyCommand.getResult(), capturePicReplyCommand.getErrorCode());
                }
                if (tyCommand instanceof RecordStartReplyCmd) {
                    RecordStartReplyCmd recordStartReplyCommand = (RecordStartReplyCmd) tyCommand;
                    logPrint("无人机开始录像", recordStartReplyCommand.getResult(), recordStartReplyCommand.getErrorCode());
                }
                if (tyCommand instanceof RecordStopReplyCmd) {
                    RecordStopReplyCmd recordStopReplyCommand = (RecordStopReplyCmd) tyCommand;
                    logPrint("无人机停止录像", recordStopReplyCommand.getResult(), recordStopReplyCommand.getErrorCode());
                }
                //起飞回复
                if (tyCommand instanceof TakeOffReplyCmd) {
                    TakeOffReplyCmd takeOffReplyCommand = (TakeOffReplyCmd) tyCommand;
                    logPrint("无人机起飞", takeOffReplyCommand.getResult(), takeOffReplyCommand.getErrorCode());
                }
                //返航回复
                if (tyCommand instanceof ReturnHomeReplyCmd) {
                    ReturnHomeReplyCmd returnHomeReplyCmd = (ReturnHomeReplyCmd) tyCommand;
                    logPrint("无人机返航", returnHomeReplyCmd.getResult(), returnHomeReplyCmd.getErrorCode());
                }
                //取消返航回复
                if (tyCommand instanceof ReturnHomeCancelReplyCmd) {
                    ReturnHomeCancelReplyCmd returnHomeCancelReplyCmd = (ReturnHomeCancelReplyCmd) tyCommand;
                    logPrint("取消无人机返航", returnHomeCancelReplyCmd.getResult(), returnHomeCancelReplyCmd.getErrorCode());
                }
                //降落回复
                if (tyCommand instanceof LandReplyCmd) {
                    LandReplyCmd landReplyCmd = (LandReplyCmd) tyCommand;
                    logPrint("降落", landReplyCmd.getResult(), landReplyCmd.getErrorCode());
                }
                //取消降落回复
                if (tyCommand instanceof LandCancelReplyCmd) {
                    LandCancelReplyCmd landCancelReplyCmd = (LandCancelReplyCmd) tyCommand;
                    logPrint("取消降落", landCancelReplyCmd.getResult(), landCancelReplyCmd.getErrorCode());
                }
                //强制降落回复
                if (tyCommand instanceof LandForcedReplyCmd) {
                    LandForcedReplyCmd landForcedReplyCmd = (LandForcedReplyCmd) tyCommand;
                    logPrint("强制降落", landForcedReplyCmd.getResult(), landForcedReplyCmd.getErrorCode());
                }
                //到达指点飞行位置回复
                if (tyCommand instanceof PointControlReachedReplyCmd) {
                    PointControlReachedReplyCmd pointControlReachedReplyCmd = (PointControlReachedReplyCmd) tyCommand;
                    logPrint("到达指点飞行位置", pointControlReachedReplyCmd.getResult(), pointControlReachedReplyCmd.getErrorCode());
                }
                //紧急制动回复
                if (tyCommand instanceof EmergencyBrakeReplyCmd) {
                    EmergencyBrakeReplyCmd emergencyBrakeReplyCmd = (EmergencyBrakeReplyCmd) tyCommand;
                    logPrint("紧急制动", emergencyBrakeReplyCmd.getResult(), emergencyBrakeReplyCmd.getErrorCode());
                }
                //上传航线回复
                if (tyCommand instanceof WayLineUploadReplyCmd) {
                    WayLineUploadReplyCmd wayLineUploadReplyCmd = (WayLineUploadReplyCmd) tyCommand;
                    logPrint("上传航线", wayLineUploadReplyCmd.getResult(), wayLineUploadReplyCmd.getErrorCode());
                }
                //开始航线回复
                if (tyCommand instanceof WayLineBeginReplyCmd) {
                    WayLineBeginReplyCmd wayLineBeginReplyCmd = (WayLineBeginReplyCmd) tyCommand;
                    logPrint("开始航线", wayLineBeginReplyCmd.getResult(), wayLineBeginReplyCmd.getErrorCode());
                }
                //暂停航线回复
                if (tyCommand instanceof WayLinePauseReplyCmd) {
                    WayLinePauseReplyCmd wayLinePauseReplyCmd = (WayLinePauseReplyCmd) tyCommand;
                    logPrint("暂停航线", wayLinePauseReplyCmd.getResult(), wayLinePauseReplyCmd.getErrorCode());
                }
                //恢复航线回复
                if (tyCommand instanceof WayLineResumeReplyCmd) {
                    WayLineResumeReplyCmd wayLineResumeReplyCmd = (WayLineResumeReplyCmd) tyCommand;
                    logPrint("恢复航线", wayLineResumeReplyCmd.getResult(), wayLineResumeReplyCmd.getErrorCode());
                }
                //结束航线回复
                if (tyCommand instanceof WayLineEndReplyCmd) {
                    WayLineEndReplyCmd wayLineEndReplyCmd = (WayLineEndReplyCmd) tyCommand;
                    logPrint("结束航线", wayLineEndReplyCmd.getResult(), wayLineEndReplyCmd.getErrorCode());
                }
                //设置返航高度回复
                if (tyCommand instanceof ReturnHeightSettingReplyCmd) {
                    ReturnHeightSettingReplyCmd returnHeightSettingReplyCmd = (ReturnHeightSettingReplyCmd) tyCommand;
                    logPrint("设置返航高度", returnHeightSettingReplyCmd.getResult(), returnHeightSettingReplyCmd.getErrorCode());
                }
                //设置返航点回复
                if (tyCommand instanceof ReturnPointSettingReplyCmd) {
                    ReturnPointSettingReplyCmd returnPointSettingReplyCmd = (ReturnPointSettingReplyCmd) tyCommand;
                    logPrint("设置返航点", returnPointSettingReplyCmd.getResult(), returnPointSettingReplyCmd.getErrorCode());
                }
                break;
            case (byte) 0xD2:
                //设置视频码流回复
                if (tyCommand instanceof VideoStreamSettingReplyCmd) {
                    VideoStreamSettingReplyCmd videoStreamSettingReplyCmd = (VideoStreamSettingReplyCmd) tyCommand;
                    logPrint("设置视频码流", videoStreamSettingReplyCmd.getResult(), videoStreamSettingReplyCmd.getErrorCode());
                }
                break;
            default:
                // 处理其他消息
                log.warn("收到其他消息-->{}", tyCommand.getCmdCode());
        }
    }
 
    //公共日志打印
    private void logPrint(String message, Object obj) {
        log.debug("收到{}回复{}", message, obj);
    }
 
    //公共日志打印
    private void logPrint(String message, byte result, Integer errorCode) {
        log.info("收到{}回复:{} 错误码:{}【{}】",
                message,
                ByteUtils.byteToHex(result),
                errorCode,
                ErrorCodeEnum.getDescByCode(errorCode));
    }
 
    /**
     * 定时3秒发送心跳指令
     */
    @Scheduled(fixedRate = 3000)
    public void sendHeartBeat() {
        if (!TyTcpServerCache.getTcpServerCache(tyConfiguration.getTcpserver() + ":" + tyConfiguration.getTcpport())) {
            return;
        }
        long timestamp = System.currentTimeMillis();
        log.debug("定时向天宇发送⼼跳,时间戳{}", timestamp);
        HeartbeatCmd heartbeatMessage = new HeartbeatCmd(timestamp);
        this.sendMessage(tyConfiguration.getIpPort(), heartbeatMessage);
    }
 
    /**
     * 发送消息
     */
    private void sendMessage(String key, BaseCmd msg) {
        try {
            ChannelFuture future = TyNettyTcpClient.channels.get(key).getChannelFuture();
            Channel channel = future.channel();
            if (channel.isActive()) {
                InetSocketAddress ipSocket = (InetSocketAddress) channel.remoteAddress();
                String host = ipSocket.getHostString();
                int port = ipSocket.getPort();
                channel.writeAndFlush(msg); // 发送编码后的数据
                log.debug("向天宇服务器【{}:{}】发送数据,命令码:{}", host, port, String.format("0x%02X", msg.getCmdCode() & 0xFF));
            } else {
                log.error("与天宇服务器链接断开,无法发送数据!");
            }
        } catch (Exception e) {
            log.error("发送消息异常:{}", e.getMessage());
        }
    }
 
}