Skip to content

Commit

Permalink
Let user configure custom zoom for tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
andchiind committed Dec 6, 2024
1 parent 6dd8eda commit 85a70d8
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 4 deletions.
53 changes: 53 additions & 0 deletions backend/api/Controllers/EchoController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using Api.Controllers.Models;
using Api.Services;
using Api.Services.MissionLoaders;
using Api.Services.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Api.Controllers
{
[ApiController]
[Route("echo")]
public class EchoController(
ILogger<EchoController> logger,
IEchoService echoService
) : ControllerBase
{
/// <summary>
/// Updates the Flotilla metadata for an Echo tag
/// </summary>
/// <remarks>
/// <para> This query updates the Flotilla metadata for an Echo tag </para>
/// </remarks>
[HttpPost("{tagId}/tag-zoom")]
[Authorize(Roles = Role.Admin)]
[ProducesResponseType(typeof(EchoTagInspectionMetadata), StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<EchoTagInspectionMetadata>> Create([FromRoute] string tagId, [FromBody] IsarZoomDescription zoom)
{
logger.LogInformation($"Updating zoom value for tag with ID {tagId}");

var newMetadata = new EchoTagInspectionMetadata
{
TagId = tagId,
ZoomDescription = zoom
};

try
{
var metadata = await echoService.CreateOrUpdateEchoTagInspectionMetadata(newMetadata);

return metadata;
}
catch (Exception e)
{
logger.LogError(e, "Error while creating or updating Echo tag inspection metadata");
throw;
}
}
}
}
5 changes: 5 additions & 0 deletions backend/api/Controllers/Models/CustomMissionQuery.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Api.Database.Models;
using Api.Services.Models;
namespace Api.Controllers.Models
{
public struct CustomInspectionQuery
Expand All @@ -22,6 +23,8 @@ public struct CustomTaskQuery

public Pose RobotPose { get; set; }

public IsarZoomDescription? IsarZoomDescription { get; set; }

public CustomInspectionQuery? Inspection { get; set; }
}

Expand All @@ -44,5 +47,7 @@ public struct CustomMissionQuery
public string? Comment { get; set; }

public List<CustomTaskQuery> Tasks { get; set; }

public IsarZoomDescription? IsarZoomDescription { get; set; }
}
}
2 changes: 2 additions & 0 deletions backend/api/Database/Context/FlotillaDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Api.Database.Models;
using Api.Services.MissionLoaders;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
Expand Down Expand Up @@ -26,6 +27,7 @@ public FlotillaDbContext(DbContextOptions options) : base(options)
public DbSet<DefaultLocalizationPose> DefaultLocalizationPoses => Set<DefaultLocalizationPose>();
public DbSet<AccessRole> AccessRoles => Set<AccessRole>();
public DbSet<UserInfo> UserInfos => Set<UserInfo>();
public DbSet<EchoTagInspectionMetadata> EchoTagInspectionMetadata => Set<EchoTagInspectionMetadata>();

// Timeseries:
public DbSet<RobotPressureTimeseries> RobotPressureTimeseries => Set<RobotPressureTimeseries>();
Expand Down
15 changes: 15 additions & 0 deletions backend/api/Database/Models/EchoTagInspectionMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#nullable disable
using System.ComponentModel.DataAnnotations;
using Api.Database.Models;
using Api.Services.Models;

namespace Api.Services.MissionLoaders
{
public class EchoTagInspectionMetadata
{
[Key]
public string TagId { get; set; }

public IsarZoomDescription? ZoomDescription { get; set; }

Check warning on line 13 in backend/api/Database/Models/EchoTagInspectionMetadata.cs

View workflow job for this annotation

GitHub Actions / build_backend

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 13 in backend/api/Database/Models/EchoTagInspectionMetadata.cs

View workflow job for this annotation

GitHub Actions / build_backend

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check failure on line 13 in backend/api/Database/Models/EchoTagInspectionMetadata.cs

View workflow job for this annotation

GitHub Actions / test_backend

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check failure on line 13 in backend/api/Database/Models/EchoTagInspectionMetadata.cs

View workflow job for this annotation

GitHub Actions / test_backend

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
}
}
1 change: 1 addition & 0 deletions backend/api/Database/Models/MissionDefinition.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Api.Services.Models;

#pragma warning disable CS8618
namespace Api.Database.Models
Expand Down
6 changes: 6 additions & 0 deletions backend/api/Database/Models/MissionTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public MissionTask(
Uri? tagLink,
string? tagId,
int? poseId,
IsarZoomDescription? zoomDescription = null,
TaskStatus status = TaskStatus.NotStarted,
MissionTaskType type = MissionTaskType.Inspection)
{
Expand All @@ -35,6 +36,7 @@ public MissionTask(
TaskOrder = taskOrder;
Status = status;
Type = type;
IsarZoomDescription = zoomDescription;
if (inspection != null) Inspection = new Inspection(inspection);
}

Expand All @@ -45,6 +47,7 @@ public MissionTask(CustomTaskQuery taskQuery)
RobotPose = taskQuery.RobotPose;
TaskOrder = taskQuery.TaskOrder;
Status = TaskStatus.NotStarted;
IsarZoomDescription = taskQuery.IsarZoomDescription;
if (taskQuery.Inspection is not null)
{
Inspection = new Inspection((CustomInspectionQuery)taskQuery.Inspection);
Expand Down Expand Up @@ -98,6 +101,7 @@ public MissionTask(MissionTask copy, TaskStatus? status = null)
RobotPose = new Pose(copy.RobotPose);
PoseId = copy.PoseId;
Status = status ?? copy.Status;
IsarZoomDescription = copy.IsarZoomDescription;
if (copy.Inspection is not null)
{
Inspection = new Inspection(copy.Inspection, InspectionStatus.NotStarted);
Expand Down Expand Up @@ -156,6 +160,8 @@ or TaskStatus.Failed

public DateTime? EndTime { get; private set; }

public IsarZoomDescription? IsarZoomDescription { get; set; }

public Inspection? Inspection { get; set; }

public void UpdateWithIsarInfo(IsarTask isarTask)
Expand Down
33 changes: 30 additions & 3 deletions backend/api/Services/EchoService.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System.Globalization;
using System.Text.Json;
using Api.Controllers.Models;
using Api.Database.Context;
using Api.Database.Models;
using Api.Services.MissionLoaders;
using Api.Services.Models;
using Api.Utilities;
using Microsoft.EntityFrameworkCore;
using Microsoft.Identity.Abstractions;
namespace Api.Services
{
Expand All @@ -13,10 +16,11 @@ public interface IEchoService
public Task<MissionDefinition?> GetMissionById(string sourceMissionId);
public Task<List<MissionTask>> GetTasksForMission(string missionSourceId);
public Task<List<PlantInfo>> GetPlantInfos();
public Task<EchoTagInspectionMetadata> CreateOrUpdateEchoTagInspectionMetadata(EchoTagInspectionMetadata metadata);
}

public class EchoService(
ILogger<EchoService> logger, IDownstreamApi echoApi, ISourceService sourceService, IStidService stidService) : IEchoService
ILogger<EchoService> logger, IDownstreamApi echoApi, ISourceService sourceService, IStidService stidService, FlotillaDbContext context) : IEchoService
{

public const string ServiceName = "EchoApi";
Expand Down Expand Up @@ -93,7 +97,7 @@ private async Task<EchoMission> GetEchoMission(string echoMissionId)
public async Task<List<MissionTask>> GetTasksForMission(string missionSourceId)
{
var echoMission = await GetEchoMission(missionSourceId);
var missionTasks = echoMission.Tags.SelectMany(t => MissionTasksFromEchoTag(t)).ToList();
var missionTasks = echoMission.Tags.Select(t => MissionTasksFromEchoTag(t)).SelectMany(task => task.Result).ToList();
return missionTasks;
}

Expand Down Expand Up @@ -269,7 +273,7 @@ List<EchoPlantInfoResponse> echoPlantInfoResponse
return echoPlantInfos;
}

public IList<MissionTask> MissionTasksFromEchoTag(EchoTag echoTag)
public async Task<IList<MissionTask>> MissionTasksFromEchoTag(EchoTag echoTag)
{
var inspections = echoTag.Inspections
.Select(inspection => new Inspection(
Expand All @@ -292,12 +296,35 @@ public IList<MissionTask> MissionTasksFromEchoTag(EchoTag echoTag)
robotPose: echoTag.Pose,
poseId: echoTag.PoseId,
taskOrder: echoTag.PlanOrder,
zoomDescription: await FindInspectionZoom(echoTag),
status: Database.Models.TaskStatus.NotStarted,
type: MissionTaskType.Inspection
));
}

return missionTasks;
}

public async Task<EchoTagInspectionMetadata> CreateOrUpdateEchoTagInspectionMetadata(EchoTagInspectionMetadata metadata)
{
var existingMetadata = await context.EchoTagInspectionMetadata.Where(e => e.TagId == metadata.TagId).FirstOrDefaultAsync();
if (existingMetadata == null)
{
await context.EchoTagInspectionMetadata.AddAsync(metadata);
}
else
{
existingMetadata.ZoomDescription = metadata.ZoomDescription;
context.EchoTagInspectionMetadata.Update(existingMetadata);
}

await context.SaveChangesAsync();
return metadata;
}

private async Task<IsarZoomDescription?> FindInspectionZoom(EchoTag echoTag)
{
return (await context.EchoTagInspectionMetadata.Where((e) => e.TagId == echoTag.TagId).FirstOrDefaultAsync())?.ZoomDescription;
}
}
}
27 changes: 26 additions & 1 deletion backend/api/Services/Models/IsarMissionDefinition.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Globalization;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Text.Json.Serialization;
using Api.Database.Models;
using Microsoft.EntityFrameworkCore;
namespace Api.Services.Models
{
/// <summary>
Expand Down Expand Up @@ -61,12 +63,17 @@ public struct IsarTaskDefinition
[JsonPropertyName("inspection")]
public IsarInspectionDefinition? Inspection { get; set; }

[JsonPropertyName("zoom")]
public IsarZoomDescription? Zoom { get; set; }

public IsarTaskDefinition(MissionTask missionTask, MissionRun missionRun)
{
Id = missionTask.IsarTaskId;
Type = MissionTask.ConvertMissionTaskTypeToIsarTaskType(missionTask.Type);
Pose = new IsarPose(missionTask.RobotPose);
Tag = missionTask.TagId;
Zoom = missionTask.IsarZoomDescription;

if (missionTask.Inspection != null) Inspection = new IsarInspectionDefinition(missionTask.Inspection, missionRun);
}
}
Expand Down Expand Up @@ -162,4 +169,22 @@ public readonly struct IsarPose(Pose pose)
[JsonPropertyName("frame_name")]
public string FrameName { get; } = "asset";
}

[Owned]
public class IsarZoomDescription
{
public IsarZoomDescription(double width, double height)
{
Width = width;
Height = height;
}

[Required]
[JsonPropertyName("width")]
public double Width { get; set; }

[Required]
[JsonPropertyName("height")]
public double Height { get; set; }
}
}

0 comments on commit 85a70d8

Please sign in to comment.