MdpIngestDatasetController.Edge.cs 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using Admin.NET.Plugin.AiDOP.DataPlatform.Core;
  2. using Admin.NET.Plugin.AiDOP.DataPlatform.Core.Edge;
  3. using Microsoft.AspNetCore.Http;
  4. using Microsoft.Extensions.DependencyInjection;
  5. namespace Admin.NET.Plugin.AiDOP.DataPlatform.UnifiedIngress;
  6. public sealed partial class MdpIngestDatasetController
  7. {
  8. [HttpGet("{id:long}/edge")]
  9. public Task<IActionResult> ListEdgeDevices(long id, long? factoryId) =>
  10. EdgeAction(id, factoryId, TrialPermission, async (service, scope) =>
  11. Ok(new { devices = await service.ListAsync(scope.TenantId, scope.FactoryId, id), canApprove = await Can(PublishPermission) }));
  12. [HttpPost("{id:long}/edge"), RequestSizeLimit(4096)]
  13. public Task<IActionResult> ApproveEdgeDevice(long id, long? factoryId, MdpEdgeApproveInput input) =>
  14. EdgeAction(id, factoryId, PublishPermission, async (service, scope) =>
  15. Ok(await service.ApproveAsync(scope.TenantId, scope.FactoryId, id, input.SourceId, input.Name, user.UserId)));
  16. [HttpPut("{id:long}/edge/{device:guid}/task"), RequestSizeLimit(98304)]
  17. public Task<IActionResult> ConfigureEdgeDevice(long id, Guid device, long? factoryId, MdpEdgeTaskInput input) =>
  18. EdgeAction(id, factoryId, PublishPermission, async (service, scope) =>
  19. {
  20. await service.ConfigureAsync(scope.TenantId, scope.FactoryId, id, device, user.UserId, input);
  21. return NoContent();
  22. });
  23. [HttpPost("{id:long}/edge/{device:guid}/handoff"), RequestSizeLimit(4096)]
  24. public Task<IActionResult> HandoffEdgeDevice(long id, Guid device, long? factoryId, MdpEdgeHandoffInput input) =>
  25. EdgeAction(id, factoryId, PublishPermission, async (service, scope) =>
  26. {
  27. await service.HandoffAsync(scope.TenantId, scope.FactoryId, id, device, user.UserId, input);
  28. return NoContent();
  29. });
  30. [HttpPost("{id:long}/edge/{device:guid}/recovery/preview"), RequestSizeLimit(4096)]
  31. public Task<IActionResult> PreviewEdgePage(long id, Guid device, long? factoryId, MdpEdgePagePreviewInput input) =>
  32. EdgeAction(id, factoryId, TrialPermission, async (service, scope) =>
  33. Ok(await service.PreviewPageAsync(scope.TenantId, scope.FactoryId, id, device, input)));
  34. [HttpPost("{id:long}/edge/{device:guid}/recovery"), RequestSizeLimit(4096)]
  35. public Task<IActionResult> RecoverEdgePage(long id, Guid device, long? factoryId, MdpEdgePageApproval input, CancellationToken token) =>
  36. EdgeAction(id, factoryId, PublishPermission, async (service, scope) =>
  37. {
  38. await service.RecoverPageAsync(scope.TenantId, scope.FactoryId, id, device, user.UserId, input, token);
  39. return NoContent();
  40. });
  41. [HttpPost("{id:long}/edge/{device:guid}/revoke")]
  42. public Task<IActionResult> RevokeEdgeDevice(long id, Guid device, long? factoryId) =>
  43. EdgeAction(id, factoryId, PublishPermission, async (service, scope) =>
  44. {
  45. await service.RevokeAsync(scope.TenantId, scope.FactoryId, id, device, user.UserId);
  46. return NoContent();
  47. });
  48. private Task<IActionResult> EdgeAction(long id, long? factoryId, string permission, Func<MdpEdgeDeviceService, DatasetScope, Task<IActionResult>> action)
  49. => WithScope(factoryId, async scope =>
  50. {
  51. if (!await Can(permission)) return Forbid();
  52. if (!await Datasets(scope).AnyAsync(row => row.Id == id)) return NotFound();
  53. Response.Headers.CacheControl = "no-store";
  54. try { return await action(HttpContext.RequestServices.GetRequiredService<MdpEdgeDeviceService>(), scope); }
  55. catch (Exception error) when (error is EdgeContractException or MdpIngestHttpException or IngestContractException
  56. or System.Data.Common.DbException or SqlSugarException)
  57. {
  58. return MdpEdgeDeviceController.EdgeFailure(error);
  59. }
  60. catch (Exception error) when (error is System.Text.Json.JsonException or ArgumentException or MdpApiPullException)
  61. {
  62. return BadRequest(new { code = "EDGE_TASK_INVALID" });
  63. }
  64. });
  65. }
  66. public sealed record MdpEdgeApproveInput(long SourceId, string Name);