89 lines
2.9 KiB
C#
89 lines
2.9 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using RobotNet10.RobotApp.Client.Shared.SLAM;
|
|
using RobotNet10.RobotApp.Shared;
|
|
using RobotNet10.RobotApp.SLAM;
|
|
|
|
namespace RobotNet10.RobotApp.Controllers;
|
|
|
|
/// <summary>
|
|
/// API Controller for map management operations
|
|
/// </summary>
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
[Authorize]
|
|
public class MapsController(ISLAMService slamService, ILogger<MapsController> logger) : ControllerBase
|
|
{
|
|
/// <summary>
|
|
/// Get map info by name
|
|
/// </summary>
|
|
/// <param name="mapName">Name of the map</param>
|
|
/// <returns>MapInfoDto if found, NotFound otherwise</returns>
|
|
[HttpGet("{mapName}")]
|
|
public ActionResult<MapInfoDto> GetMapInfo(string mapName)
|
|
{
|
|
try
|
|
{
|
|
var mapInfo = slamService.GetMapInfo(mapName);
|
|
if (mapInfo == null)
|
|
{
|
|
logger.LogWarning("MapsController: Map not found: {MapName}", mapName);
|
|
return NotFound($"Map '{mapName}' not found");
|
|
}
|
|
|
|
return Ok(MapMapInfoToDto(mapInfo));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogError(ex, "MapsController: Failed to get map info: {MapName}", mapName);
|
|
return StatusCode(500, "Failed to get map info");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get map image by name
|
|
/// </summary>
|
|
/// <param name="mapName">Name of the map</param>
|
|
/// <returns>PNG image file</returns>
|
|
[HttpGet("{mapName}/image")]
|
|
public IActionResult GetMapImage(string mapName)
|
|
{
|
|
try
|
|
{
|
|
var imagePath = slamService.GetMapImagePath(mapName);
|
|
if (imagePath == null)
|
|
{
|
|
logger.LogWarning("MapsController: Map image not found: {MapName}", mapName);
|
|
return NotFound($"Map image for '{mapName}' not found");
|
|
}
|
|
|
|
var contentType = imagePath.EndsWith(".png", StringComparison.OrdinalIgnoreCase)
|
|
? "image/png"
|
|
: "image/jpeg";
|
|
|
|
var fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read);
|
|
return File(fileStream, contentType);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogError(ex, "MapsController: Failed to get map image: {MapName}", mapName);
|
|
return StatusCode(500, "Failed to get map image");
|
|
}
|
|
}
|
|
|
|
private static MapInfoDto MapMapInfoToDto(MapInfo mapInfo)
|
|
{
|
|
return new MapInfoDto
|
|
{
|
|
Name = mapInfo.Name,
|
|
CreatedDate = mapInfo.CreatedDate,
|
|
Resolution = mapInfo.Resolution,
|
|
Width = mapInfo.Size.Width,
|
|
Height = mapInfo.Size.Height,
|
|
TrajectoryNodeCount = mapInfo.TrajectoryNodeCount,
|
|
OriginX = mapInfo.Origin.Position.X,
|
|
OriginY = mapInfo.Origin.Position.Y,
|
|
};
|
|
}
|
|
}
|