using System.Text; using Infrastructure; using Infrastructure.CloudSdk.wayline; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using OpenAuth.App.ServiceApp; using OpenAuth.App.ServiceApp.AirLine.Request; using OpenAuth.App.ServiceApp.Request; using OpenAuth.App.ServiceApp.Response; using OpenAuth.Repository.Domain; namespace OpenAuth.WebApi.Controllers.ServiceControllers { /// /// 后台管理模块 /// [Route("api/[controller]/[action]")] [ApiController] public class ManageController : ControllerBase { private readonly ManageApp _app; private readonly MqttClientManager _mqttClientManager; public ManageController(ManageApp app, MqttClientManager mqttClientManager) { _app = app; _mqttClientManager = mqttClientManager; } #region 机场管理 /// /// 获取机场列表 /// /// /// /// sn /// 类型 /// 项目 /// [HttpGet] public async Task>>> GetDataList(int page, int limit, string sn, string type, string workspaceid) { var result = new Response>>(); try { result = await _app.GetPageList(page, limit, sn, type, workspaceid); } catch (Exception ex) { result.Code = 500; result.Message = ex.Message; } return result; } /// /// 编辑机场 /// /// [HttpPost] public async Task> EditDronePort(LasaDronePort info) { return await _app.EditDronePort(info); } /// /// 修改机场固件信息 /// /// 机场id /// 机场版本 /// [HttpPost] public async Task> EditDronePortFirmware(string id, string firmwareVersion) { return await _app.EditDronePortFirmware(id, firmwareVersion); } /// /// 删除机场 /// /// [HttpPost] public async Task> DeleteDronePort(string id) { return await _app.DeleteDronePort(id); } #endregion #region 无人机管理 /// /// 获取无人机列表 /// /// /// /// sn /// 类型 /// 项目 /// [HttpGet] public async Task>>> GetUavPageList(int page, int limit, string sn, string type, string workspaceid) { var result = new Response>>(); try { result = await _app.GetUavPageList(page, limit, sn, type, workspaceid); } catch (Exception ex) { result.Code = 500; result.Message = ex.Message; } return result; } /// /// 编辑无人机 /// /// [HttpPost] public async Task> EditUav(LasaUav info) { return await _app.EditUav(info); } /// /// 修改无人机固件信息 /// /// 无人机id /// 机场版本 /// [HttpPost] public async Task> EditUavFirmware(string id, string firmwareVersion) { return await _app.EditUavFirmware(id, firmwareVersion); } /// /// 删除无人机 /// /// [HttpPost] public async Task> DeleteUav(string id) { return await _app.DeleteUav(id); } /// /// 根据机场sn获取无人机列表 /// /// /// /// /// [HttpGet] public async Task>>> GetUavPageByDocksn(int page, int limit, string sn) { var result = new Response>>(); try { result = await _app.GetUavPageByDocksn(page, limit, sn); } catch (Exception ex) { result.Code = 500; result.Message = ex.Message; } return result; } /// /// 导出无人机信息 /// /// [HttpGet] public async Task ExportDevice() { var content = await _app.GetUavSn(); byte[] fileBytes = Encoding.UTF8.GetBytes(content); string fileName = "DeviceSNList_" + DateTime.Now.ToString("yyyyMMdd") + ".txt"; return File(fileBytes, "text/plain", fileName); } #endregion #region 任务管理 /// /// 获取任务列表 /// /// /// /// /// [HttpGet] public async Task>>> GetTaskPageList(int page, int limit, string key, int? status) { var result = new Response>>(); try { result = await _app.GetTaskPageList(page, limit, key, status); } catch (Exception ex) { result.Code = 500; result.Message = ex.Message; } return result; } /// /// 添加任务 /// /// [HttpPost] public async Task> AddTask(LasaTask info) { return await _app.AddTask(info); } /// /// 编辑任务,不再允许编辑任务 /// /// [HttpPost] [Obsolete] public async Task> EditTask(LasaTask info) { return await _app.EditTask(info); } /// /// 删除任务 /// /// [HttpPost] public async Task> DeleteTask(string id) { return await _app.DeleteTask(id); } /// /// 根据flightId查询指令拍摄图片 /// /// /// /// public async Task> GetTaskPicList(string flightId,long timestamp) { return await _app.GetTaskPicList(flightId,timestamp); } #endregion #region 航线管理 // todo 创建文件夹 // todo 删除文件夹 // todo 文件夹列表 [HttpPost] public async Task> CreateAirLineFolder(FolderCreateReq req) { return await _app.CreateAirLineFolder(req.FolderName, req.ParentId); } /// /// 删除文件夹及文件夹航线 /// /// /// [HttpPost] public async Task> DeleteAirLineFolder(string folderId) { return await _app.DeleteAirLineFolder(folderId); } [HttpGet] public async Task>> ListAirLineFolder() { return await _app.ListAirLineFolder(); } /// /// 获取航线列表 /// /// /// /// /// [HttpGet] public async Task>>> GetAirLineList([FromQuery] AirLineListRequestPage req) { var result = new Response>>(); try { result = await _app.GetAirLinePageList(req); } catch (Exception ex) { result.Code = 500; result.Message = ex.Message; } return result; } /// /// 航线详情 /// /// /// [HttpGet] public async Task> GetAirLine(string airLineId) { return await _app.GetAirLine(airLineId); } /// /// 添加航线 /// /// [HttpPost] public async Task> AddAirLine(LasaAirLine info) { return await _app.AddAirLine(info); } /// /// 编辑航线 /// /// [HttpPost] public async Task> EditAirLine(LasaAirLine info) { return await _app.EditAirLine(info); } /// /// 删除航线 /// /// [HttpPost] public async Task> DeleteAirLine(string id) { return await _app.DeleteAirLine(id); } /// /// 上传航线文件 /// /// kmz文件 /// 文件夹 /// [HttpPost("upload")] [AllowAnonymous] public async Task UploadXmlFile(IFormFile xmlFile,string folder) { if (xmlFile == null || xmlFile.Length == 0) return BadRequest("文件为空"); var path = await _app.UploadFile(xmlFile,folder); return Ok(new { message = "上传成功", path }); } /*/// /// 更新航线文件 /// /// /// /// [HttpPost("uploadwpmlfile")] [AllowAnonymous] public async Task UploadWpmlFile(IFormFile xmlFile, string id) { if (xmlFile == null || xmlFile.Length == 0) return BadRequest("文件为空"); string baseFolder = "Waylines/" + (string.IsNullOrEmpty(id) ? Guid.NewGuid().ToString() : id); var uploadsFolder = Path.Combine(Directory.GetCurrentDirectory(), baseFolder); if (!Directory.Exists(uploadsFolder)) Directory.CreateDirectory(uploadsFolder); // todo 这里只有一个文件,是否得改 var filePath = Path.Combine(uploadsFolder, "waylines.wpml"); using (var stream = new FileStream(filePath, FileMode.Create)) { await xmlFile.CopyToAsync(stream); } return Ok(new { message = "上传成功", path = filePath }); }*/ #endregion #region 项目管理 /// /// 获取项目列表 /// /// 是否是已加入的项目(0全部,1加入,2未加入) /// 项目名称筛选 /// 状态(0全部,1进行中,2已归档) /// 排序 /// [HttpGet] public async Task>> GetWorkspaceList(int isjoin, string key, int state, string order = "\"CreateTime\" desc") { var result = new Response>(); try { result = await _app.GetWorkspaceList(isjoin, key, state, order); } catch (Exception ex) { result.Code = 500; result.Message = ex.Message; } return result; } /// /// 添加项目 /// /// [HttpPost] public async Task> AddWorkspace(WorkSpace info) { return await _app.AddWorkspace(info); } /// /// 编辑项目 /// /// [HttpPost] public async Task> EditWorkspace(WorkSpace info) { return await _app.EditWorkspace(info); } /// /// 删除项目 /// /// [HttpPost] public async Task> DeleteWorkspace(string id) { return await _app.DeleteWorkspace(id); } /// /// 归档项目 /// /// [HttpPost] public async Task> CompleteWorkspace(string id) { return await _app.CompleteWorkspace(id); } /// /// 获取机场列表,添加项目使用 /// /// /// [HttpGet] public async Task>> GetUavList() { var result = new Response>(); try { result = await _app.GetUavList(); } catch (Exception ex) { result.Code = 500; result.Message = ex.Message; } return result; } /// /// 根据项目id获取项目信息,编辑项目使用 /// /// 项目id /// [HttpGet] public async Task> GetWorkSpaceById(string id) { var result = new Response(); try { result = await _app.GetWorkSpaceById(id); } catch (Exception ex) { result.Code = 500; result.Message = ex.Message; } return result; } #endregion // 航线任务在云端的 共享查看、下发执行、取消以及进度上报等功能。 /// /// 解除挂起(执行任务) todo 需不需要加时间限制判断? /// /// [HttpPost] [AllowAnonymous] // todo 根据项目设置的起飞条件设置是否起飞 public async Task ExecuteFlyTask(string taskId) { await _app.ExecuteFlyTask(taskId); } /// /// 挂起任务 /// /// [HttpPost] public async Task PendingFlyTask(string taskId) { // todo 先只针对立即任务实现 await _app.PendingFlyTask(taskId); } /// /// 任务图片列表查询 /// /// /// [HttpGet] [AllowAnonymous] public async Task>> GetTAskImageList(string taskId) { return await _app.GetTAskImageList(taskId); } /// /// 开启机场直播(测试使用) /// /// /// [HttpPost] [AllowAnonymous] public async Task> StartDronePortLive(string streamUrl) { return await _app.StartDronePortLive(streamUrl); } /// /// 测试使用 /// /// /// [HttpPost] [AllowAnonymous] public async Task> StopDronePortLive(string streamUrl) { return await _app.StopDronePortLive(streamUrl); } [HttpPost] [AllowAnonymous] public async Task TestExecuteFlyTask(string flightid) { return await _app.TestExecuteFlyTask(flightid); } /// /// 测试 /// [HttpGet] [AllowAnonymous] public async Task Test() { await _mqttClientManager.SubscribeAsync("thing/product/8UUXN5400A079H/osd", async (args) => { var payload = args.ApplicationMessage.Payload; var message = Encoding.UTF8.GetString(payload); Console.WriteLine($"收到主题 [{args.ApplicationMessage.Topic}] 的消息: {message}"); await Task.CompletedTask; // 可选:实际逻辑中可执行更多异步操作 }); } /// /// 测试序列化,是否符合要求 /// /// [HttpGet] [AllowAnonymous] public string TestJsonRequest() { var request = new TopicServicesRequest() .SetTid("tid") .SetBid("bid") .SetTimestamp(DateTime.Now.Ticks) .SetData(new { name = "张三" }); return JsonConvert.SerializeObject(request); } [HttpGet] [AllowAnonymous] public string TestGetDevJson() { var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile( $"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development"}.json", optional: true) .AddEnvironmentVariables(); // 构建配置 var configuration = builder.Build(); // 读取连接字符串 var dir = configuration["WpmlDir"]; return dir; } #region 添加地图作业区域 /// /// 添加地图作业区域 /// /// /// [HttpPost] public async Task> AddWorkArea([FromBody] LasaShpData model) { var res = new Response(); try { return await _app.AddWorkArea(model); } catch (Exception ex) { res.Code = 500; res.Message = ex.InnerException?.Message ?? ex.Message; } return res; } /// /// 更新地图作业区域 /// /// /// [HttpPost] public async Task> UpdateWorkArea([FromBody] LasaShpData model) { var res = new Response(); try { return await _app.UpdateWorkArea(model); } catch (Exception ex) { res.Code = 500; res.Message = ex.InnerException?.Message ?? ex.Message; } return res; } /// /// 删除地图作业区域 /// /// 地图作业区域id /// [HttpPost] public async Task> DeleteWorkArea(string id) { return await _app.DeleteWorkArea(id); } /// /// 获取地图作业区域列表 /// /// 项目id /// 项目状态(0已启用,1已禁用,null全部) /// 区域类型(作业区,限飞区等) /// [HttpGet] public async Task>> GetWorkAreaList(string workspaceid, int? state, string type) { var result = new Response>(); try { result = await _app.GetWorkAreaList(workspaceid, state, type); } catch (Exception ex) { result.Code = 500; result.Message = ex.Message; } return result; } #endregion #region 添加地图标注 /// /// 添加地图标注 /// /// /// [HttpPost] public async Task> AddAnnotation([FromBody] LasaAnnotation model) { var res = new Response(); try { return await _app.AddAnnotation(model); } catch (Exception ex) { res.Code = 500; res.Message = ex.InnerException?.Message ?? ex.Message; } return res; } /// /// 更新地图标注 /// /// /// [HttpPost] public async Task> UpdateAnnotation([FromBody] LasaAnnotation model) { var res = new Response(); try { return await _app.UpdateAnnotation(model); } catch (Exception ex) { res.Code = 500; res.Message = ex.InnerException?.Message ?? ex.Message; } return res; } /// /// 删除地图标注 /// /// 地图标注id /// [HttpPost] public async Task> DeleteAnnotation(string id) { return await _app.DeleteAnnotation(id); } /// /// 获取地图标注列表 /// /// 项目id /// 项目状态(0已启用,1已禁用,null全部) /// 区域类型(0点,1线,2多边行,3圈) /// [HttpGet] public async Task>> GetAnnotationList(string workspaceid, int? state, int? type) { var result = new Response>(); try { result = await _app.GetAnnotationList(workspaceid, state, type); } catch (Exception ex) { result.Code = 500; result.Message = ex.Message; } return result; } #endregion #region 基本信息 /// /// 获取基本信息 /// /// [HttpGet] [AllowAnonymous] public async Task> GetBasicInfo() { return await _app.GetBasicInfo(); } #endregion } }