Initial commit
This commit is contained in:
@@ -0,0 +1,245 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using RobotNet10.NavigationTune.Shared.Interfaces;
|
||||
using RobotNet10.NavigationTune.Shared.Models;
|
||||
|
||||
namespace RobotNet10.NavigationTune.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// REST API controller for parameter sets management
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class ParameterSetsController(
|
||||
IParameterManager parameterManager,
|
||||
ILogger<ParameterSetsController> logger) : ControllerBase
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Get all parameter sets
|
||||
/// </summary>
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<List<NavigationParameterSet>>> GetAll()
|
||||
{
|
||||
try
|
||||
{
|
||||
var parameterSets = await parameterManager.GetAllAsync();
|
||||
return Ok(parameterSets);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Error getting all parameter sets");
|
||||
return StatusCode(500, new { error = "Failed to retrieve parameter sets" });
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get parameter set by ID
|
||||
/// </summary>
|
||||
[HttpGet("{id:guid}")]
|
||||
public async Task<ActionResult<NavigationParameterSet>> GetById(Guid id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var parameterSet = await parameterManager.GetByIdAsync(id);
|
||||
if (parameterSet == null)
|
||||
return NotFound(new { error = $"Parameter set with ID {id} not found" });
|
||||
|
||||
return Ok(parameterSet);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Error getting parameter set {Id}", id);
|
||||
return StatusCode(500, new { error = "Failed to retrieve parameter set" });
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get parameter set by name
|
||||
/// </summary>
|
||||
[HttpGet("name/{name}")]
|
||||
public async Task<ActionResult<NavigationParameterSet>> GetByName(string name)
|
||||
{
|
||||
try
|
||||
{
|
||||
var parameterSet = await parameterManager.GetByNameAsync(name);
|
||||
if (parameterSet == null)
|
||||
return NotFound(new { error = $"Parameter set with name '{name}' not found" });
|
||||
|
||||
return Ok(parameterSet);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Error getting parameter set {Name}", name);
|
||||
return StatusCode(500, new { error = "Failed to retrieve parameter set" });
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create new parameter set
|
||||
/// </summary>
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<NavigationParameterSet>> Create([FromBody] NavigationParameterSet parameterSet)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Validate
|
||||
if(await parameterManager.GetByNameAsync(parameterSet.Name) is not null) return StatusCode(500, new { error = "Paramter name is existed" });
|
||||
var validation = parameterManager.Validate(parameterSet);
|
||||
if (!validation.IsValid)
|
||||
{
|
||||
return BadRequest(new
|
||||
{
|
||||
error = "Validation failed",
|
||||
errors = validation.Errors,
|
||||
warnings = validation.Warnings
|
||||
});
|
||||
}
|
||||
|
||||
var id = await parameterManager.SaveAsync(parameterSet);
|
||||
var created = await parameterManager.GetByIdAsync(id);
|
||||
return CreatedAtAction(nameof(GetById), new { id }, created);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Error creating parameter set");
|
||||
return StatusCode(500, new { error = "Failed to create parameter set" });
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update existing parameter set
|
||||
/// </summary>
|
||||
[HttpPut("{id}")]
|
||||
public async Task<ActionResult> Update(Guid id, [FromBody] NavigationParameterSet parameterSet)
|
||||
{
|
||||
try
|
||||
{
|
||||
var existing = await parameterManager.GetByIdAsync(id);
|
||||
if (existing == null)
|
||||
return NotFound(new { error = $"Parameter set with ID {id} not found" });
|
||||
|
||||
// Validate
|
||||
var validation = parameterManager.Validate(parameterSet);
|
||||
if (!validation.IsValid)
|
||||
{
|
||||
return BadRequest(new
|
||||
{
|
||||
error = "Validation failed",
|
||||
errors = validation.Errors,
|
||||
warnings = validation.Warnings
|
||||
});
|
||||
}
|
||||
existing.ControllerType = parameterSet.ControllerType;
|
||||
existing.PurePursuitConfig = parameterSet.PurePursuitConfig;
|
||||
existing.StanleyConfig = parameterSet.StanleyConfig ?? new StanleyConfig();
|
||||
existing.EstimatorConfig = parameterSet.EstimatorConfig;
|
||||
existing.NavigationConfig = parameterSet.NavigationConfig;
|
||||
existing.MovePidConfig = parameterSet.MovePidConfig;
|
||||
existing.RotatePidConfig = parameterSet.RotatePidConfig;
|
||||
existing.SignalConfig = parameterSet.SignalConfig;
|
||||
existing.MotorDynamicsConfig = parameterSet.MotorDynamicsConfig;
|
||||
await parameterManager.UpdateAsync(existing);
|
||||
return NoContent();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Error updating parameter set {Id}", id);
|
||||
return StatusCode(500, new { error = "Failed to update parameter set" });
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Delete parameter set
|
||||
/// </summary>
|
||||
[HttpDelete("{id}")]
|
||||
public async Task<ActionResult> Delete(Guid id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var existing = await parameterManager.GetByIdAsync(id);
|
||||
if (existing == null)
|
||||
return NotFound(new { error = $"Parameter set with ID {id} not found" });
|
||||
|
||||
await parameterManager.DeleteAsync(id);
|
||||
return NoContent();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Error deleting parameter set {Id}", id);
|
||||
return StatusCode(500, new { error = "Failed to delete parameter set" });
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validate parameter set
|
||||
/// </summary>
|
||||
[HttpPost("validate")]
|
||||
public ActionResult<ValidationResult> Validate([FromBody] NavigationParameterSet parameterSet)
|
||||
{
|
||||
try
|
||||
{
|
||||
var validation = parameterManager.Validate(parameterSet);
|
||||
return Ok(validation);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Error validating parameter set");
|
||||
return StatusCode(500, new { error = "Failed to validate parameter set" });
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get default preset
|
||||
/// </summary>
|
||||
[HttpGet("presets/default")]
|
||||
public ActionResult<NavigationParameterSet> GetDefaultPreset()
|
||||
{
|
||||
try
|
||||
{
|
||||
var preset = parameterManager.GetDefaultPreset();
|
||||
return Ok(preset);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Error getting default preset");
|
||||
return StatusCode(500, new { error = "Failed to get default preset" });
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get aggressive preset
|
||||
/// </summary>
|
||||
[HttpGet("presets/aggressive")]
|
||||
public ActionResult<NavigationParameterSet> GetAggressivePreset()
|
||||
{
|
||||
try
|
||||
{
|
||||
var preset = parameterManager.GetAggressivePreset();
|
||||
return Ok(preset);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Error getting aggressive preset");
|
||||
return StatusCode(500, new { error = "Failed to get aggressive preset" });
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get smooth preset
|
||||
/// </summary>
|
||||
[HttpGet("presets/smooth")]
|
||||
public ActionResult<NavigationParameterSet> GetSmoothPreset()
|
||||
{
|
||||
try
|
||||
{
|
||||
var preset = parameterManager.GetSmoothPreset();
|
||||
return Ok(preset);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Error getting smooth preset");
|
||||
return StatusCode(500, new { error = "Failed to get smooth preset" });
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user