package com.ard.work.service.impl; import com.ard.common.utils.ByteUtils; import com.ard.work.aspect.annotation.CheckControlRight; import com.ard.work.aspect.annotation.CheckTakeoffAltitude; import com.ard.work.component.forest.TyClient; import com.ard.work.component.netty.TcpClientUtil; import com.ard.work.component.netty.command.BaseCmd; import com.ard.work.component.netty.command.camera.*; import com.ard.work.component.netty.command.control.AbsAngleControlCmd; import com.ard.work.component.netty.command.control.AngleControlCmd; import com.ard.work.component.netty.command.control.SpeedControlCmd; import com.ard.work.component.netty.command.manual.*; import com.ard.work.component.netty.command.notice.BoxPowerStatusReplyCmd; import com.ard.work.component.netty.command.notice.FlyWayEndReplyCmd; import com.ard.work.component.netty.command.notice.UavReadyReplyCmd; import com.ard.work.component.netty.command.real.SignalInfoCmd; import com.ard.work.component.netty.command.real.UavStateCmd; import com.ard.work.component.netty.command.real.UavTelemetryDataCmd; import com.ard.work.component.netty.command.register.*; import com.ard.work.component.netty.command.settings.*; import com.ard.work.component.netty.command.speaker.*; import com.ard.work.component.netty.command.wayline.*; import com.ard.work.component.netty.handler.ErrorCodeEnum; import com.ard.work.component.netty.protobuf.TelemetryDataPB; import com.ard.work.domain.cache.UavSignalInfoCache; import com.ard.work.domain.cache.UavStateCache; import com.ard.work.domain.cache.UavTelemetryDataCache; import com.ard.work.domain.dto.BoxHistoryDTO; import com.ard.work.domain.dto.BoxPhotosDTO; import com.ard.work.domain.dto.BoxSettingsDTO; import com.ard.work.domain.dto.camera.*; import com.ard.work.domain.dto.control.AbsAngleControlDTO; import com.ard.work.domain.dto.control.AngleControlDTO; import com.ard.work.domain.dto.control.SpeedControlDTO; import com.ard.work.domain.dto.converter.wayline.PlanLineDataConverter; import com.ard.work.domain.dto.manual.*; import com.ard.work.domain.dto.settings.ReturnHeightSettingDTO; import com.ard.work.domain.dto.settings.ReturnPointSettingDTO; import com.ard.work.domain.dto.settings.VideoStreamSettingDTO; import com.ard.work.domain.dto.signal.SignalInfo; import com.ard.work.domain.dto.speaker.*; import com.ard.work.domain.dto.state.UavState; import com.ard.work.domain.dto.telemetry.TelemetryData; import com.ard.work.domain.dto.wayline.*; import com.ard.work.domain.entity.ArdDroneInfo; import com.ard.work.domain.entity.ArdServerInfo; import com.ard.work.domain.message.UavControlType; import com.ard.work.domain.receiver.*; import com.ard.work.service.IArdDroneInfoService; import com.ard.work.service.IArdServerInfoService; import com.ard.work.service.ITyService; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; /** * TyServiceImpl * * @author liusuyi * @date 2024年10月22日8:37 **/ @Slf4j @Service public class TyServiceImpl implements ITyService { @Resource private IArdServerInfoService ardServerService; @Resource private IArdDroneInfoService ardDroneService; @Resource private TyClient tyClient; /** * 获取边缘服务器token信息 * * @return AccessTokenReceiver */ @Override public AccessTokenReceiver getAccessToken(String serverId) { return ardServerService.getAccessToken(serverId); } /** * 获取全部云盒列表 * * @return List */ @Override public List getBoxList() { List ardServerInfos = ardServerService.selectArdServerInfoList(new ArdServerInfo()); return getBoxList(ardServerInfos); } /** * 获取指定服务器云盒列表 * * @return List */ @Override public List getBoxList(String serverId) { TyHttpCommonReceiver> boxList = tyClient.getBoxList(serverId); return boxList == null ? null : boxList.getContent(); } /** * 获取直播地址 * * @return BoxStreamUrlReceiver */ @Override public BoxStreamUrlReceiver getBoxLiveUrl(String boxSn) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(boxSn); if (ardDroneInfo == null) { log.warn("未找到对应的飞行器信息,boxSn:{}", boxSn); return null; } TyHttpCommonReceiver boxLiveUrl = tyClient.getBoxLiveUrl(ardDroneInfo.getServerId(), boxSn); return boxLiveUrl == null ? null : boxLiveUrl.getContent(); } /** * 获取飞行历史 * * @return List */ @Override public List getBoxHistory(BoxHistoryDTO boxHistoryDTO) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(boxHistoryDTO.getBoxSn()); TyHttpCommonReceiver> boxHistory = tyClient.getBoxHistory(ardDroneInfo.getServerId(), boxHistoryDTO); return boxHistory == null ? null : boxHistory.getContent(); } /** * 获取盒子图片 * * @return List */ @Override public List getBoxPhotos(BoxPhotosDTO boxPhotosDTO) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(boxPhotosDTO.getBoxSn()); TyHttpCommonReceiver> boxPhotos = tyClient.getBoxPhotos(ardDroneInfo.getServerId(), boxPhotosDTO); return boxPhotos == null ? null : boxPhotos.getContent(); } /** * 修改云盒设置 */ @Override public Boolean updateBoxSettings(BoxSettingsDTO boxSettingsDTO) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(boxSettingsDTO.getBoxSn()); TyHttpCommonReceiver result = tyClient.updateBoxSettings(ardDroneInfo.getServerId(), boxSettingsDTO); return result.getState() == 1; } /** * 设置返航高度 */ @Override public Boolean updateReturnHeight(ReturnHeightSettingDTO returnHeightSettingDTO) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(returnHeightSettingDTO.getBoxSn()); ReturnHeightSettingCmd returnHeightSettingCmd = new ReturnHeightSettingCmd(returnHeightSettingDTO); this.sendMessage(ardDroneInfo.getServerId(), returnHeightSettingCmd); return true; } /** * 设置返航点 */ @Override public Boolean updateReturnPoint(ReturnPointSettingDTO returnPointSettingDTO) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(returnPointSettingDTO.getBoxSn()); ReturnPointSettingCmd returnPointSettingCmd = new ReturnPointSettingCmd(returnPointSettingDTO); this.sendMessage(ardDroneInfo.getServerId(), returnPointSettingCmd); return true; } /** * 设置视频码流 */ @Override public Boolean updateVideoStream(VideoStreamSettingDTO videoStreamSettingDTO) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(videoStreamSettingDTO.getBoxSn()); VideoStreamSettingCmd videoStreamSettingCmd = new VideoStreamSettingCmd(videoStreamSettingDTO); this.sendMessage(ardDroneInfo.getServerId(), videoStreamSettingCmd); return true; } /** * 注册边缘服务器平台 */ @Override public Boolean registerMessage(String serverId) { try { AccessTokenReceiver tyToken = ardServerService.getAccessToken(serverId); RegisterCmd registerMessage = new RegisterCmd(tyToken.getCompanyId(), tyToken.getAccessToken().getBytes()); this.sendMessage(serverId, registerMessage); } catch (Exception e) { log.error("注册边缘服务器平台失败:{}", e.getMessage()); } return true; } /** * 云台控制基于角度 */ @CheckControlRight(UavControlType.PAYLOAD) @Override public Boolean angleControl(AngleControlDTO angleControlDto) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(angleControlDto.getBoxSn()); AngleControlCmd controlAngleCommand = new AngleControlCmd(angleControlDto); this.sendMessage(ardDroneInfo.getServerId(), controlAngleCommand); return true; } /** * 云台绝对值控制(基于角度) */ @CheckControlRight(UavControlType.PAYLOAD) @Override public Boolean absAngleControl(AbsAngleControlDTO absAngleControlDto) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(absAngleControlDto.getBoxSn()); AbsAngleControlCmd absAngleControlCommand = new AbsAngleControlCmd(absAngleControlDto); this.sendMessage(ardDroneInfo.getServerId(), absAngleControlCommand); return true; } /** * 云台转动(基于速度) */ @CheckControlRight(UavControlType.PAYLOAD) @Override public Boolean speedControl(SpeedControlDTO speedControlDto) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(speedControlDto.getBoxSn()); SpeedControlCmd speedControlCommand = new SpeedControlCmd(speedControlDto); this.sendMessage(ardDroneInfo.getServerId(), speedControlCommand); return true; } /** * 设置相机模式 */ @Override public Boolean updateCameraMode(CameraModeDTO cameraModeDTO) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(cameraModeDTO.getBoxSn()); CameraModeCmd cameraModeCommand = new CameraModeCmd(cameraModeDTO); this.sendMessage(ardDroneInfo.getServerId(), cameraModeCommand); return true; } /** * 拍照 */ @Override public Boolean capturePic(CapturePicDTO capturePicDto) { //设置相机模式为拍照模式 updateCameraMode(CameraModeDTO.builder().boxSn(capturePicDto.getBoxSn()).actionParam((byte) 0x01).build()); //设置拍照回传 photoReturnToggle(PhotoReturnToggleDTO.builder().boxSn(capturePicDto.getBoxSn()).actionParam((byte) 0x01).build()); ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(capturePicDto.getBoxSn()); CapturePicCmd capturePicCommand = new CapturePicCmd(capturePicDto); this.sendMessage(ardDroneInfo.getServerId(), capturePicCommand); return true; } /** * 录像 */ @Override public Boolean 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); } ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(recordDto.getBoxSn()); this.sendMessage(ardDroneInfo.getServerId(), baseCmd); return true; } /** * 变倍 */ @CheckControlRight(UavControlType.PAYLOAD) @Override public Boolean zoom(ZoomDTO zoomDTO) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(zoomDTO.getBoxSn()); ZoomCmd zoomCommand = new ZoomCmd(zoomDTO); this.sendMessage(ardDroneInfo.getServerId(), zoomCommand); return true; } /** * 变倍到指定倍数 */ @CheckControlRight(UavControlType.PAYLOAD) @Override public Boolean zoomToLevel(ZoomToLevelDTO zoomToLevelDTO) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(zoomToLevelDTO.getBoxSn()); ZoomToLevelCmd zoomToLevelCommand = new ZoomToLevelCmd(zoomToLevelDTO); this.sendMessage(ardDroneInfo.getServerId(), zoomToLevelCommand); return true; } /** * 切换摄像头 */ @Override public Boolean switchCamera(SwitchCameraDTO switchCameraDTO) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(switchCameraDTO.getBoxSn()); SwitchCameraCmd switchCameraCmd = new SwitchCameraCmd(switchCameraDTO); this.sendMessage(ardDroneInfo.getServerId(), switchCameraCmd); return true; } /** * 切换视频源 */ @Override public Boolean switchSource(SwitchSourceDTO switchSourceDTO) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(switchSourceDTO.getBoxSn()); SwitchSourceCmd switchSourceCmd = new SwitchSourceCmd(switchSourceDTO); this.sendMessage(ardDroneInfo.getServerId(), switchSourceCmd); return true; } /** * 打开或关闭照片回传 */ @Override public Boolean photoReturnToggle(PhotoReturnToggleDTO photoReturnToggleDTO) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(photoReturnToggleDTO.getBoxSn()); PhotoReturnToggleCmd photoReturnToggleCmd = new PhotoReturnToggleCmd(photoReturnToggleDTO); this.sendMessage(ardDroneInfo.getServerId(), photoReturnToggleCmd); return true; } /** * 切换无人机控制权 */ @Override public Boolean switchUavControl(SwitchUavControlDTO switchUavControlDTO) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(switchUavControlDTO.getBoxSn()); SwitchUavControlCmd switchUavControlCmd = new SwitchUavControlCmd(switchUavControlDTO); this.sendMessage(ardDroneInfo.getServerId(), switchUavControlCmd); return true; } /** * 起飞 */ @CheckControlRight(UavControlType.FLY) @Override public Boolean takeOff(TakeOffDTO takeOffDTO) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(takeOffDTO.getBoxSn()); TakeOffCmd takeOffCmd = new TakeOffCmd(takeOffDTO); this.sendMessage(ardDroneInfo.getServerId(), takeOffCmd); return true; } /** * 返航 */ @CheckControlRight(UavControlType.FLY) @Override public Boolean returnHome(ReturnHomeDTO returnHomeDTO) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(returnHomeDTO.getBoxSn()); ReturnHomeCmd returnHomeCmd = new ReturnHomeCmd(returnHomeDTO); this.sendMessage(ardDroneInfo.getServerId(), returnHomeCmd); return true; } /** * 取消返航 */ @CheckControlRight(UavControlType.FLY) @Override public Boolean returnHomeCancel(ReturnHomeCancelDTO returnHomeCancelDTO) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(returnHomeCancelDTO.getBoxSn()); ReturnHomeCancelCmd returnHomeCancelCmd = new ReturnHomeCancelCmd(returnHomeCancelDTO); this.sendMessage(ardDroneInfo.getServerId(), returnHomeCancelCmd); return true; } /** * 降落 */ @CheckControlRight(UavControlType.FLY) @Override public Boolean land(LandDTO landDTO) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(landDTO.getBoxSn()); LandCmd landCmd = new LandCmd(landDTO); this.sendMessage(ardDroneInfo.getServerId(), landCmd); return true; } /** * 取消降落 */ @CheckControlRight(UavControlType.FLY) @Override public Boolean landCancel(LandCancelDTO landCancelDTO) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(landCancelDTO.getBoxSn()); LandCancelCmd landCancelCmd = new LandCancelCmd(landCancelDTO); this.sendMessage(ardDroneInfo.getServerId(), landCancelCmd); return true; } /** * 强制降落 */ @CheckControlRight(UavControlType.FLY) @Override public Boolean landForced(LandForcedDTO landForcedDTO) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(landForcedDTO.getBoxSn()); LandForcedCmd landForcedCmd = new LandForcedCmd(landForcedDTO); this.sendMessage(ardDroneInfo.getServerId(), landForcedCmd); return true; } /** * 方向控制 */ @CheckControlRight(UavControlType.FLY) @CheckTakeoffAltitude(minAltitude = 30.0) @Override public Boolean flyControl(FlyControlDTO flyControlDTO) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(flyControlDTO.getBoxSn()); FlyControlCmd flyControlCmd = new FlyControlCmd(flyControlDTO); this.sendMessage(ardDroneInfo.getServerId(), flyControlCmd); return true; } /** * 指点飞行控制 */ @CheckControlRight(UavControlType.FLY) @CheckTakeoffAltitude(minAltitude = 30.0) @Override public Boolean flyPointControl(FlyPointControlDTO flyPointControlDTO) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(flyPointControlDTO.getBoxSn()); FlyPointControlCmd flyPointControlCmd = new FlyPointControlCmd(flyPointControlDTO); this.sendMessage(ardDroneInfo.getServerId(), flyPointControlCmd); return true; } /** * 停止指点飞行控制 */ @CheckControlRight(UavControlType.FLY) @Override public Boolean flyPointControlCancel(FlyPointControlCancelDTO flyPointControlCancelDTO) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(flyPointControlCancelDTO.getBoxSn()); FlyPointControlCancelCmd flyPointControlCancelCmd = new FlyPointControlCancelCmd(flyPointControlCancelDTO); this.sendMessage(ardDroneInfo.getServerId(), flyPointControlCancelCmd); return true; } /** * 紧急制动 */ @CheckControlRight(UavControlType.FLY) @Override public Boolean emeragencyBrake(EmeragencyBrakeDTO emeragencyBrakeDTO) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(emeragencyBrakeDTO.getBoxSn()); EmergencyBrakeCmd emergencyBrakeCmd = new EmergencyBrakeCmd(emeragencyBrakeDTO); this.sendMessage(ardDroneInfo.getServerId(), emergencyBrakeCmd); return true; } /** * 获取无人机状态 */ @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) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(speakModeSwitchDTO.getBoxSn()); SpeakModeSwitchCmd speakModeSwitchCmd = new SpeakModeSwitchCmd(speakModeSwitchDTO); this.sendMessage(ardDroneInfo.getServerId(), speakModeSwitchCmd); } /** * 文字喊话 */ @Override public void textSpeak(TextSpeakDTO textSpeakDTO) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(textSpeakDTO.getBoxSn()); TextSpeakCmd textSpeakCmd = new TextSpeakCmd(textSpeakDTO); this.sendMessage(ardDroneInfo.getServerId(), textSpeakCmd); } /** * 实时喊话 */ @Override public void realSpeak(RealSpeakDTO realSpeakDTO) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(realSpeakDTO.getBoxSn()); RealSpeakCmd realSpeakCmd = new RealSpeakCmd(realSpeakDTO); this.sendMessage(ardDroneInfo.getServerId(), realSpeakCmd); } /** * 录音喊话 */ @Override public void recordSpeak(RecordSpeakDTO recordSpeakDTO) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(recordSpeakDTO.getBoxSn()); RecordSpeakCmd recordSpeakCmd = new RecordSpeakCmd(recordSpeakDTO); this.sendMessage(ardDroneInfo.getServerId(), recordSpeakCmd); } /** * 设置音量 */ @Override public void volumeSetting(VolumeSettingDTO volumeSettingDTO) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(volumeSettingDTO.getBoxSn()); VolumeSettingCmd volumeSettingCmd = new VolumeSettingCmd(volumeSettingDTO); this.sendMessage(ardDroneInfo.getServerId(), volumeSettingCmd); } /** * 设置循环播放 */ @Override public void loopSetting(LoopSettingDTO loopSettingDTO) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(loopSettingDTO.getBoxSn()); LoopSettingCmd loopSettingCmd = new LoopSettingCmd(loopSettingDTO); this.sendMessage(ardDroneInfo.getServerId(), loopSettingCmd); } /** * 停止喊话 */ @Override public void stopSpeak(StopSpeakDTO stopSpeakDTO) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(stopSpeakDTO.getBoxSn()); StopSpeakCmd stopSpeakCmd = new StopSpeakCmd(stopSpeakDTO); this.sendMessage(ardDroneInfo.getServerId(), stopSpeakCmd); } /** * 航线上传 */ @Override public void wayLineUpload(WayLineUploadDTO wayLineUploadDTO) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(wayLineUploadDTO.getBoxSn()); wayLineUploadDTO.setWayLineData(this.CreatePlanLineData());//设置航线数据 WayLineUploadCmd wayLineUploadCmd = new WayLineUploadCmd(wayLineUploadDTO); this.sendMessage(ardDroneInfo.getServerId(), 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); } /** * 航线开始 */ @CheckControlRight(UavControlType.FLY) @Override public void wayLineBegin(WayLineBeginDTO wayLineBeginDTO) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(wayLineBeginDTO.getBoxSn()); WayLineBeginCmd wayLineBeginCmd = new WayLineBeginCmd(wayLineBeginDTO); this.sendMessage(ardDroneInfo.getServerId(), wayLineBeginCmd); } /** * 航线暂停 */ @CheckControlRight(UavControlType.FLY) @Override public void wayLinePause(WayLinePauseDTO wayLinePauseDTO) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(wayLinePauseDTO.getBoxSn()); WayLinePauseCmd wayLinePauseCmd = new WayLinePauseCmd(wayLinePauseDTO); this.sendMessage(ardDroneInfo.getServerId(), wayLinePauseCmd); } /** * 航线恢复 */ @CheckControlRight(UavControlType.FLY) @Override public void wayLineResume(WayLineResumeDTO wayLineResumeDTO) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(wayLineResumeDTO.getBoxSn()); WayLineResumeCmd wayLineResumeCmd = new WayLineResumeCmd(wayLineResumeDTO); this.sendMessage(ardDroneInfo.getServerId(), wayLineResumeCmd); } /** * 航线结束 */ @CheckControlRight(UavControlType.FLY) @Override public void wayLineEnd(WayLineEndDTO wayLineEndDTO) { ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(wayLineEndDTO.getBoxSn()); WayLineEndCmd wayLineEndCmd = new WayLineEndCmd(wayLineEndDTO); this.sendMessage(ardDroneInfo.getServerId(), wayLineEndCmd); } /** * 处理边缘服务器返回的消息 */ @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(key); } break; case 0x02: // 处理注册消息 HeartbeatReplyCmd heartbeatReplyCmd = (HeartbeatReplyCmd) tyCommand; logPrint("心跳消息", heartbeatReplyCmd.getTimestamp()); break; case 0x03: // 处理订阅消息 break; case 0x04: // 处理取消订阅消息 break; case 0x09: // 处理图片上传完成通知 PicUploadCompletedReplyCmd picUploadCompletedReplyCmd = (PicUploadCompletedReplyCmd) tyCommand; logPrint("图片上传完成", picUploadCompletedReplyCmd.getPicUrl()); 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(key, 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()); } } /** * 根据[服务器id]获取边缘服务器盒子列表 */ @Override public List getBoxSnToList(String id) { try { List remoteBoxList = tyClient.getBoxList(id).getContent(); List existingDrones = getDroneLs(id); return existingDrones.isEmpty() ? remoteBoxList : filterBoxInfoReceivers(remoteBoxList, existingDrones); } catch (Exception e) { log.error("获取云盒列表失败, 服务器id: {}", id, e); return Collections.emptyList(); // 返回不可变空列表,更安全 } } /** * 根据[服务器id]查询无人机盒子列表 */ private List getDroneLs(String serverId) { ArdDroneInfo query = new ArdDroneInfo(); query.setServerId(serverId); return ardDroneService.selectArdDroneList(query); } /** * 过滤掉在ardDroneInfos中已存在的boxSn */ public List filterBoxInfoReceivers( List boxInfoReceivers, List ardDroneInfos) { Set existingBoxSns = ardDroneInfos.stream() .map(ArdDroneInfo::getBoxSn) .filter(Objects::nonNull) .collect(Collectors.toSet()); return boxInfoReceivers.stream() .filter(box -> box.getBoxSn() != null && !existingBoxSns.contains(box.getBoxSn())) .collect(Collectors.toList()); } private List getBoxList(List ls) { return ls.stream() .filter(ardServerInfo -> ardServerInfo.getServerStatus().equals("1")) .flatMap(ardServerInfo -> { try { List boxes = tyClient.getBoxList(ardServerInfo.getId()).getContent(); return Optional.ofNullable(boxes).orElse(Collections.emptyList()).stream(); } catch (Exception e) { log.error("获取云盒列表失败: {}", ardServerInfo.getId(), e); return Stream.empty(); } }) .collect(Collectors.toList()); } //公共日志打印 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)); } /** * 发送消息 */ private void sendMessage(String key, BaseCmd msg) { TcpClientUtil.sendMessage(key, msg); } }