Skip to content

Commit

Permalink
Make default localization pose optional for area
Browse files Browse the repository at this point in the history
  • Loading branch information
oysand committed Oct 9, 2023
1 parent ecdb613 commit acf43db
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 21 deletions.
2 changes: 1 addition & 1 deletion backend/api.test/EventHandlers/TestMissionEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public TestMissionEventHandler(DatabaseFixture fixture)
Boundary = new Boundary(),
TransformationMatrices = new TransformationMatrices()
},
DefaultLocalizationPose = new Pose(),
DefaultLocalizationPose = null,
SafePositions = new List<SafePosition>()
};

Expand Down
2 changes: 1 addition & 1 deletion backend/api.test/Services/MissionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public async Task Create()
Plant = testPlant,
Name = "testArea",
MapMetadata = new MapMetadata() { MapName = "testMap" },
DefaultLocalizationPose = new Pose(),
DefaultLocalizationPose = null,
SafePositions = new List<SafePosition>()
},
InstallationCode = "testInstallation",
Expand Down
60 changes: 59 additions & 1 deletion backend/api/Controllers/AreaController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Api.Controllers
public class AreaController : ControllerBase
{
private readonly IAreaService _areaService;
private readonly IDefaultLocalizationPoseService _defaultLocalizationPoseService;

private readonly IMissionDefinitionService _missionDefinitionService;

Expand All @@ -23,12 +24,14 @@ public AreaController(
ILogger<AreaController> logger,
IMapService mapService,
IAreaService areaService,
IDefaultLocalizationPoseService defaultLocalizationPoseService,
IMissionDefinitionService missionDefinitionService
)
{
_logger = logger;
_mapService = mapService;
_areaService = areaService;
_defaultLocalizationPoseService = defaultLocalizationPoseService;
_missionDefinitionService = missionDefinitionService;
}

Expand Down Expand Up @@ -128,6 +131,54 @@ [FromBody] Pose safePosition
}
}

/// <summary>
/// Updates default localization pose
/// </summary>
/// <remarks>
/// <para> This query updates the default localization pose for a deck </para>
/// </remarks>
[HttpPut]
[Authorize(Roles = Role.Admin)]
[Route("{areaId}/update-default-localization-pose")]
[ProducesResponseType(typeof(Deck), StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<Deck>> UpdateDefaultLocalizationPose([FromRoute] string areaId, [FromBody] Pose newDefaultLocalizationPose)
{
_logger.LogInformation("Updating default localization pose on area '{areaId}'", areaId);
try
{
var area = await _areaService.ReadById(areaId);
if (area is null)
{
_logger.LogInformation("A area with id '{areaId}' does not exist", areaId);
return NotFound("Area does not exists");
}

if (area.DefaultLocalizationPose != null)
{
area.DefaultLocalizationPose.Pose = newDefaultLocalizationPose;
_ = await _defaultLocalizationPoseService.Update(area.DefaultLocalizationPose);
}
else
{
area.DefaultLocalizationPose = new DefaultLocalizationPose(newDefaultLocalizationPose);
area = await _areaService.Update(area);
}


return Ok(new AreaResponse(area));
}
catch (Exception e)
{
_logger.LogError(e, "Error while updating the default localization pose");
throw;
}
}


/// <summary>
/// Deletes the area with the specified id from the database.
/// </summary>
Expand Down Expand Up @@ -277,10 +328,17 @@ public async Task<ActionResult<MapMetadata>> GetMapMetadata([FromRoute] string i
return StatusCode(StatusCodes.Status500InternalServerError, errorMessage);
}

if (area.DefaultLocalizationPose is null)
{
string errorMessage = $"Area with id '{area.Id}' does not have a default localization pose";
_logger.LogInformation("{ErrorMessage}", errorMessage);
return NotFound(errorMessage);
}

MapMetadata? mapMetadata;
var positions = new List<Position>
{
area.DefaultLocalizationPose.Position
area.DefaultLocalizationPose.Pose.Position
};
try
{
Expand Down
2 changes: 1 addition & 1 deletion backend/api/Controllers/Models/AreaResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class AreaResponse

public MapMetadata MapMetadata { get; set; }

public Pose DefaultLocalizationPose { get; set; }
public DefaultLocalizationPose? DefaultLocalizationPose { get; set; }

public IList<SafePosition> SafePositions { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion backend/api/Controllers/Models/CreateAreaQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ public struct CreateAreaQuery
public string DeckName { get; set; }
public string AreaName { get; set; }

public Pose DefaultLocalizationPose { get; set; }
public Pose? DefaultLocalizationPose { get; set; }
}
}
5 changes: 0 additions & 5 deletions backend/api/Database/Context/FlotillaDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<Robot>().OwnsOne(r => r.Pose).OwnsOne(p => p.Orientation);
modelBuilder.Entity<Robot>().OwnsOne(r => r.Pose).OwnsOne(p => p.Position);
modelBuilder.Entity<Robot>().OwnsMany(r => r.VideoStreams);
modelBuilder.Entity<Area>().OwnsOne(a => a.DefaultLocalizationPose, poseBuilder =>
{
poseBuilder.OwnsOne(pose => pose.Position);
poseBuilder.OwnsOne(pose => pose.Orientation);
});
modelBuilder.Entity<Area>().HasOne(a => a.Deck).WithMany();
modelBuilder.Entity<Area>().HasOne(a => a.Installation).WithMany();
modelBuilder.Entity<Area>().HasOne(a => a.Plant).WithMany();
Expand Down
12 changes: 6 additions & 6 deletions backend/api/Database/Context/InitDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private static List<Area> GetAreas()
Installation = decks[0].Plant!.Installation,
Name = "AP320",
MapMetadata = new MapMetadata(),
DefaultLocalizationPose = new Pose { },
DefaultLocalizationPose = null,
SafePositions = new List<SafePosition>()
};

Expand All @@ -124,7 +124,7 @@ private static List<Area> GetAreas()
Installation = decks[0].Plant!.Installation,
Name = "AP330",
MapMetadata = new MapMetadata(),
DefaultLocalizationPose = new Pose { },
DefaultLocalizationPose = null,
SafePositions = new List<SafePosition>()
};

Expand All @@ -136,7 +136,7 @@ private static List<Area> GetAreas()
Installation = decks[0].Plant!.Installation,
Name = "testArea",
MapMetadata = new MapMetadata(),
DefaultLocalizationPose = new Pose { },
DefaultLocalizationPose = null,
SafePositions = new List<SafePosition>()
};

Expand All @@ -148,7 +148,7 @@ private static List<Area> GetAreas()
Installation = decks[1].Plant.Installation,
Name = "testArea2",
MapMetadata = new MapMetadata(),
DefaultLocalizationPose = new Pose { },
DefaultLocalizationPose = null,
SafePositions = new List<SafePosition>()
};

Expand All @@ -160,7 +160,7 @@ private static List<Area> GetAreas()
Installation = decks[2].Plant.Installation,
Name = "testArea3",
MapMetadata = new MapMetadata(),
DefaultLocalizationPose = new Pose { },
DefaultLocalizationPose = null,
SafePositions = new List<SafePosition>()
};

Expand All @@ -172,7 +172,7 @@ private static List<Area> GetAreas()
Installation = decks[3].Plant.Installation,
Name = "testArea4",
MapMetadata = new MapMetadata(),
DefaultLocalizationPose = new Pose { },
DefaultLocalizationPose = null,
SafePositions = new List<SafePosition>()
};

Expand Down
4 changes: 2 additions & 2 deletions backend/api/Database/Models/Area.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public class Area
[Required]
public MapMetadata MapMetadata { get; set; }

[Required]
public Pose DefaultLocalizationPose { get; set; }

public DefaultLocalizationPose? DefaultLocalizationPose { get; set; }

public IList<SafePosition> SafePositions { get; set; }
}
Expand Down
14 changes: 11 additions & 3 deletions backend/api/Services/AreaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,16 @@ public class AreaService : IAreaService
private readonly IInstallationService _installationService;
private readonly IPlantService _plantService;
private readonly IDeckService _deckService;
private readonly IDefaultLocalizationPoseService _defaultLocalizationPoseService;

public AreaService(
FlotillaDbContext context, IInstallationService installationService, IPlantService plantService, IDeckService deckService)
FlotillaDbContext context, IInstallationService installationService, IPlantService plantService, IDeckService deckService, IDefaultLocalizationPoseService defaultLocalizationPoseService)
{
_context = context;
_installationService = installationService;
_plantService = plantService;
_deckService = deckService;
_defaultLocalizationPoseService = defaultLocalizationPoseService;
}

public async Task<IEnumerable<Area>> ReadAll()
Expand All @@ -62,7 +64,7 @@ public async Task<IEnumerable<Area>> ReadAll()
private IQueryable<Area> GetAreas()
{
return _context.Areas.Include(a => a.SafePositions)
.Include(a => a.Deck).Include(d => d.Plant).Include(i => i.Installation);
.Include(a => a.Deck).Include(d => d.Plant).Include(i => i.Installation).Include(d => d.DefaultLocalizationPose);
}

public async Task<Area?> ReadById(string id)
Expand Down Expand Up @@ -142,10 +144,16 @@ public async Task<Area> Create(CreateAreaQuery newAreaQuery, List<Pose> position
throw new AreaExistsException($"Area with name {newAreaQuery.AreaName} already exists");
}

DefaultLocalizationPose? defaultLocalizationPose = null;
if (newAreaQuery.DefaultLocalizationPose != null)
{
defaultLocalizationPose = await _defaultLocalizationPoseService.Create(new DefaultLocalizationPose(newAreaQuery.DefaultLocalizationPose));
}

var newArea = new Area
{
Name = newAreaQuery.AreaName,
DefaultLocalizationPose = newAreaQuery.DefaultLocalizationPose,
DefaultLocalizationPose = defaultLocalizationPose,
SafePositions = safePositions,
MapMetadata = new MapMetadata(),
Deck = deck!,
Expand Down

0 comments on commit acf43db

Please sign in to comment.