This commit is contained in:
Đăng Nguyễn
2025-10-30 13:34:44 +07:00
parent 643a34a4b4
commit aa2146e383
43 changed files with 3637 additions and 96 deletions

View File

@@ -1,16 +1,956 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using RobotApp.Common.Shares;
using RobotApp.Common.Shares.Dtos;
using RobotApp.Data;
using RobotApp.Services.Robot;
namespace RobotApp.Controllers;
[Route("api/[controller]")]
[ApiController]
[Authorize]
public class RobotConfigsController(Services.Logger<ImagesController> Logger) : ControllerBase
public class RobotConfigsController(Services.Logger<RobotConfigsController> Logger, ApplicationDbContext AppDb, RobotConfiguration RobotConfiguration) : ControllerBase
{
[HttpGet]
[Route("plc")]
public void GetPLCConfig()
public async Task<MessageResult<RobotPlcConfigDto[]>> GetAllPLCConfigs()
{
try
{
var configs = await AppDb.RobotPlcConfigs
.OrderByDescending(c => c.UpdatedAt)
.ToListAsync();
if (configs is null || configs.Count == 0) return new(false, "PLC configuration not found.");
return new(true)
{
Data = [.. configs.Select(config => new RobotPlcConfigDto
{
Id = config.Id,
ConfigName = config.ConfigName,
Description = config.Description,
PLCAddress = config.PLCAddress,
PLCPort = config.PLCPort,
PLCUnitId = config.PLCUnitId,
CreatedAt = config.CreatedAt,
UpdatedAt = config.UpdatedAt,
IsActive = config.IsActive,
})]
};
}
catch (Exception ex)
{
Logger.Error($"Error in Get PLC Configs: {ex.Message}");
return new(false, "An error occurred while retrieving the PLC configuration.");
}
}
[HttpPut]
[Route("plc/{id:guid}")]
public async Task<MessageResult<RobotPlcConfigDto>> UpdatePLCConfig(Guid id, [FromBody] UpdateRobotPlcConfigDto updateDto)
{
try
{
var config = await AppDb.RobotPlcConfigs.FindAsync(id);
if (config is null) return new(false, "PLC configuration not found.");
if (string.IsNullOrEmpty(updateDto.ConfigName)) return new(false, "ConfigName cannot be null or empty.");
if (await AppDb.RobotPlcConfigs.AnyAsync(cf => cf.Id != id && cf.ConfigName == updateDto.ConfigName))
return new(false, "A PLC configuration with the same name already exists.");
config.ConfigName = updateDto.ConfigName ?? config.ConfigName;
config.Description = updateDto.Description ?? config.Description;
config.PLCAddress = updateDto.PLCAddress ?? config.PLCAddress;
config.PLCPort = updateDto.PLCPort;
config.PLCUnitId = (byte)updateDto.PLCUnitId;
config.UpdatedAt = DateTime.Now;
await AppDb.SaveChangesAsync();
if (config.IsActive) await RobotConfiguration.LoadRobotPlcConfigAsync();
return new(true, "PLC configuration updated successfully.")
{
Data = new RobotPlcConfigDto
{
Id = config.Id,
ConfigName = config.ConfigName,
Description = config.Description,
PLCAddress = config.PLCAddress,
PLCPort = config.PLCPort,
PLCUnitId = config.PLCUnitId,
CreatedAt = config.CreatedAt,
UpdatedAt = config.UpdatedAt,
IsActive = config.IsActive,
}
};
}
catch (Exception ex)
{
Logger.Error($"Error in Update PLC Config: {ex.Message}");
return new(false, "An error occurred while updating the PLC configuration.");
}
}
[HttpPost]
[Route("plc")]
public async Task<MessageResult<RobotPlcConfigDto>> CreatePLCConfig([FromBody] CreateRobotPlcConfigDto createDto)
{
try
{
if (string.IsNullOrEmpty(createDto.ConfigName)) return new(false, "ConfigName cannot be null or empty.");
if (await AppDb.RobotPlcConfigs.AnyAsync(cf => cf.ConfigName == createDto.ConfigName))
return new(false, "A PLC configuration with the same name already exists.");
var config = new RobotPlcConfig
{
ConfigName = createDto.ConfigName,
Description = createDto.Description,
PLCAddress = createDto.PLCAddress,
PLCPort = createDto.PLCPort,
PLCUnitId = (byte)createDto.PLCUnitId,
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now,
IsActive = false
};
AppDb.RobotPlcConfigs.Add(config);
await AppDb.SaveChangesAsync();
return new(true, "PLC configuration created successfully.")
{
Data = new RobotPlcConfigDto
{
Id = config.Id,
ConfigName = config.ConfigName,
Description = config.Description,
PLCAddress = config.PLCAddress,
PLCPort = config.PLCPort,
PLCUnitId = config.PLCUnitId,
CreatedAt = config.CreatedAt,
UpdatedAt = config.UpdatedAt,
IsActive = config.IsActive,
}
};
}
catch (Exception ex)
{
Logger.Error($"Error in Create PLC Config: {ex.Message}");
return new(false, "An error occurred while creating the PLC configuration.");
}
}
[HttpDelete]
[Route("plc/{id:guid}")]
public async Task<MessageResult> DeletePLCConfig(Guid id)
{
try
{
var config = await AppDb.RobotPlcConfigs.FindAsync(id);
if (config is null) return new(false, "PLC configuration not found.");
if (config.IsActive) return new(false, "Cannot delete an active PLC configuration.");
AppDb.RobotPlcConfigs.Remove(config);
await AppDb.SaveChangesAsync();
return new(true, "Configuration deleted successfully.");
}
catch (Exception ex)
{
Logger.Error($"Error in Delete PLC Config: {ex.Message}");
return new(false, "An error occurred while deleting the PLC configuration.");
}
}
[HttpPut]
[Route("plc/active/{id:guid}")]
public async Task<MessageResult> ActivePLCConfig(Guid id)
{
try
{
var config = await AppDb.RobotPlcConfigs.FindAsync(id);
if (config is null) return new(false, "PLC configuration not found.");
await AppDb.RobotPlcConfigs.ExecuteUpdateAsync(cf => cf.SetProperty(c => c.IsActive, false));
config.IsActive = true;
config.UpdatedAt = DateTime.Now;
await AppDb.SaveChangesAsync();
await RobotConfiguration.LoadRobotPlcConfigAsync();
return new(true, $"PLC configuration {config.ConfigName} activated successfully.");
}
catch (Exception ex)
{
Logger.Error($"Error in Update Active PLC Config: {ex.Message}");
return new(false, "An error occurred while updating the active PLC configuration.");
}
}
[HttpGet]
[Route("vda5050")]
public async Task<MessageResult<RobotVDA5050ConfigDto[]>> GetAllVDA5050Configs()
{
try
{
var configs = await AppDb.RobotVDA5050Configs
.OrderByDescending(c => c.UpdatedAt)
.ToListAsync();
if (configs is null || configs.Count == 0) return new(false, "VDA5050 configuration not found.");
var configDtos = configs.Select(config => new RobotVDA5050ConfigDto
{
Id = config.Id,
SerialNumber = config.SerialNumber,
VDA5050HostServer = config.VDA5050HostServer,
VDA5050Port = config.VDA5050Port,
VDA5050UserName = config.VDA5050UserName,
VDA5050Password = config.VDA5050Password,
VDA5050Manufacturer = config.VDA5050Manufacturer,
VDA5050Version = config.VDA5050Version,
VDA5050PublishRepeat = config.VDA5050PublishRepeat,
VDA5050EnablePassword = config.VDA5050EnablePassword,
VDA5050EnableTls = config.VDA5050EnableTls,
CreatedAt = config.CreatedAt,
UpdatedAt = config.UpdatedAt,
IsActive = config.IsActive,
ConfigName = config.ConfigName,
Description = config.Description
}).ToArray();
return new(true) { Data = configDtos };
}
catch (Exception ex)
{
Logger.Error($"Error in Get All VDA5050 Configs: {ex.Message}");
return new(false, "An error occurred while retrieving VDA5050 configurations.");
}
}
[HttpPut]
[Route("vda5050/{id:guid}")]
public async Task<MessageResult<RobotVDA5050ConfigDto>> UpdateVDA5050Config(Guid id, [FromBody] UpdateRobotVDA5050ConfigDto updateDto)
{
try
{
var config = await AppDb.RobotVDA5050Configs.FindAsync(id);
if (config is null) return new(false, "VDA5050 configuration not found.");
if (string.IsNullOrEmpty(updateDto.ConfigName)) return new(false, "ConfigName cannot be null or empty.");
if (await AppDb.RobotVDA5050Configs.AnyAsync(cf => cf.Id != id && cf.ConfigName == updateDto.ConfigName))
return new(false, "A VDA5050 configuration with the same name already exists.");
config.SerialNumber = updateDto.SerialNumber ?? config.SerialNumber;
config.VDA5050HostServer = updateDto.VDA5050HostServer ?? config.VDA5050HostServer;
config.VDA5050Port = updateDto.VDA5050Port;
config.VDA5050UserName = updateDto.VDA5050UserName ?? config.VDA5050UserName;
config.VDA5050Password = updateDto.VDA5050Password ?? config.VDA5050Password;
config.VDA5050Manufacturer = updateDto.VDA5050Manufacturer ?? config.VDA5050Manufacturer;
config.VDA5050Version = updateDto.VDA5050Version ?? config.VDA5050Version;
config.VDA5050PublishRepeat = updateDto.VDA5050PublishRepeat;
config.VDA5050EnablePassword = updateDto.VDA5050EnablePassword;
config.VDA5050EnableTls = updateDto.VDA5050EnableTls;
config.ConfigName = updateDto.ConfigName ?? config.ConfigName;
config.Description = updateDto.Description ?? config.Description;
config.UpdatedAt = DateTime.Now;
await AppDb.SaveChangesAsync();
if (config.IsActive) await RobotConfiguration.LoadVDA5050ConfigAsync();
return new(true, "VDA5050 configuration updated successfully.")
{
Data = new RobotVDA5050ConfigDto
{
Id = config.Id,
SerialNumber = config.SerialNumber,
VDA5050HostServer = config.VDA5050HostServer,
VDA5050Port = config.VDA5050Port,
VDA5050UserName = config.VDA5050UserName,
VDA5050Password = config.VDA5050Password,
VDA5050Manufacturer = config.VDA5050Manufacturer,
VDA5050Version = config.VDA5050Version,
VDA5050PublishRepeat = config.VDA5050PublishRepeat,
VDA5050EnablePassword = config.VDA5050EnablePassword,
VDA5050EnableTls = config.VDA5050EnableTls,
CreatedAt = config.CreatedAt,
UpdatedAt = config.UpdatedAt,
IsActive = config.IsActive,
ConfigName = config.ConfigName,
Description = config.Description
}
};
}
catch (Exception ex)
{
Logger.Error($"Error in Update VDA5050 Config: {ex.Message}");
return new(false, "An error occurred while updating the VDA5050 configuration.");
}
}
[HttpPost]
[Route("vda5050")]
public async Task<MessageResult<RobotVDA5050ConfigDto>> CreateVDA5050Config([FromBody] CreateRobotVDA5050ConfigDto createDto)
{
try
{
if (string.IsNullOrEmpty(createDto.ConfigName)) return new(false, "ConfigName cannot be null or empty.");
if (await AppDb.RobotVDA5050Configs.AnyAsync(cf => cf.ConfigName == createDto.ConfigName))
return new(false, "A VDA5050 configuration with the same name already exists.");
var config = new RobotVDA5050Config
{
ConfigName = createDto.ConfigName,
Description = createDto.Description,
SerialNumber = createDto.SerialNumber,
VDA5050HostServer = createDto.VDA5050HostServer,
VDA5050Port = createDto.VDA5050Port,
VDA5050UserName = createDto.VDA5050UserName,
VDA5050Password = createDto.VDA5050Password,
VDA5050Manufacturer = createDto.VDA5050Manufacturer,
VDA5050Version = createDto.VDA5050Version,
VDA5050PublishRepeat = createDto.VDA5050PublishRepeat,
VDA5050EnablePassword = createDto.VDA5050EnablePassword,
VDA5050EnableTls = createDto.VDA5050EnableTls,
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now,
IsActive = false
};
AppDb.RobotVDA5050Configs.Add(config);
await AppDb.SaveChangesAsync();
return new(true, "VDA5050 configuration created successfully.")
{
Data = new RobotVDA5050ConfigDto
{
Id = config.Id,
ConfigName = config.ConfigName,
Description = config.Description,
SerialNumber = config.SerialNumber,
VDA5050HostServer = config.VDA5050HostServer,
VDA5050Port = config.VDA5050Port,
VDA5050UserName = config.VDA5050UserName,
VDA5050Password = config.VDA5050Password,
VDA5050Manufacturer = config.VDA5050Manufacturer,
VDA5050Version = config.VDA5050Version,
VDA5050PublishRepeat = config.VDA5050PublishRepeat,
VDA5050EnablePassword = config.VDA5050EnablePassword,
VDA5050EnableTls = config.VDA5050EnableTls,
CreatedAt = config.CreatedAt,
UpdatedAt = config.UpdatedAt,
IsActive = config.IsActive,
}
};
}
catch (Exception ex)
{
Logger.Error($"Error in Create VDA5050 Config: {ex.Message}");
return new(false, "An error occurred while creating the VDA5050 configuration.");
}
}
[HttpDelete]
[Route("vda5050/{id:guid}")]
public async Task<MessageResult> DeleteVDA5050Config(Guid id)
{
try
{
var config = await AppDb.RobotVDA5050Configs.FindAsync(id);
if (config is null) return new(false, "VDA5050 configuration not found.");
if (config.IsActive) return new(false, "Cannot delete an active VDA5050 configuration.");
AppDb.RobotVDA5050Configs.Remove(config);
await AppDb.SaveChangesAsync();
return new(true, "Configuration deleted successfully.");
}
catch (Exception ex)
{
Logger.Error($"Error in Delete VDA5050 Config: {ex.Message}");
return new(false, "An error occurred while deleting the VDA5050 configuration.");
}
}
[HttpPut]
[Route("vda5050/active/{id:guid}")]
public async Task<MessageResult> ActiveVDA5050Config(Guid id)
{
try
{
var config = await AppDb.RobotVDA5050Configs.FindAsync(id);
if (config is null) return new(false, "VDA5050 configuration not found.");
await AppDb.RobotVDA5050Configs.ExecuteUpdateAsync(cf => cf.SetProperty(c => c.IsActive, false));
config.IsActive = true;
config.UpdatedAt = DateTime.Now;
await AppDb.SaveChangesAsync();
await RobotConfiguration.LoadVDA5050ConfigAsync();
return new(true, $"VDA5050 configuration {config.ConfigName} activated successfully.");
}
catch (Exception ex)
{
Logger.Error($"Error in Update Active VDA5050 Config: {ex.Message}");
return new(false, "An error occurred while updating the active VDA5050 configuration.");
}
}
[HttpGet]
[Route("robot")]
public async Task<MessageResult<RobotConfigDto[]>> GetAllRobotConfigs()
{
try
{
var configs = await AppDb.RobotConfigs
.OrderByDescending(c => c.UpdatedAt)
.ToListAsync();
if (configs is null || configs.Count == 0) return new(false, "Robot configuration not found.");
var configDtos = configs.Select(config => new RobotConfigDto
{
Id = config.Id,
NavigationType = config.NavigationType,
RadiusWheel = config.RadiusWheel,
Width = config.Width,
Length = config.Length,
Height = config.Height,
CreatedAt = config.CreatedAt,
UpdatedAt = config.UpdatedAt,
IsActive = config.IsActive,
ConfigName = config.ConfigName,
Description = config.Description
}).ToArray();
return new(true) { Data = configDtos };
}
catch (Exception ex)
{
Logger.Error($"Error in Get All Robot Configs: {ex.Message}");
return new(false, "An error occurred while retrieving Robot configurations.");
}
}
[HttpPut]
[Route("robot/{id:guid}")]
public async Task<MessageResult<RobotConfigDto>> UpdateRobotConfig(Guid id, [FromBody] UpdateRobotConfigDto updateDto)
{
try
{
var config = await AppDb.RobotConfigs.FindAsync(id);
if (config is null) return new(false, "Robot configuration not found.");
if (string.IsNullOrEmpty(updateDto.ConfigName)) return new(false, "ConfigName cannot be null or empty.");
if (await AppDb.RobotConfigs.AnyAsync(cf => cf.Id != id && cf.ConfigName == updateDto.ConfigName))
return new(false, "A Robot configuration with the same name already exists.");
config.NavigationType = updateDto.NavigationType;
config.RadiusWheel = updateDto.RadiusWheel;
config.Width = updateDto.Width;
config.Length = updateDto.Length;
config.Height = updateDto.Height;
config.ConfigName = updateDto.ConfigName ?? config.ConfigName;
config.Description = updateDto.Description ?? config.Description;
config.UpdatedAt = DateTime.Now;
await AppDb.SaveChangesAsync();
if (config.IsActive) await RobotConfiguration.LoadRobotConfigAsync();
return new(true, "Robot configuration updated successfully.")
{
Data = new RobotConfigDto
{
Id = config.Id,
NavigationType = config.NavigationType,
RadiusWheel = config.RadiusWheel,
Width = config.Width,
Length = config.Length,
Height = config.Height,
CreatedAt = config.CreatedAt,
UpdatedAt = config.UpdatedAt,
IsActive = config.IsActive,
ConfigName = config.ConfigName,
Description = config.Description
}
};
}
catch (Exception ex)
{
Logger.Error($"Error in Update Robot Config: {ex.Message}");
return new(false, "An error occurred while updating the Robot configuration.");
}
}
[HttpPost]
[Route("robot")]
public async Task<MessageResult<RobotConfigDto>> CreateRobotConfig([FromBody] CreateRobotConfigDto createDto)
{
try
{
if (string.IsNullOrEmpty(createDto.ConfigName)) return new(false, "ConfigName cannot be null or empty.");
if (await AppDb.RobotConfigs.AnyAsync(cf => cf.ConfigName == createDto.ConfigName))
return new(false, "A Robot configuration with the same name already exists.");
var config = new RobotConfig
{
ConfigName = createDto.ConfigName,
Description = createDto.Description,
NavigationType = createDto.NavigationType,
RadiusWheel = createDto.RadiusWheel,
Width = createDto.Width,
Length = createDto.Length,
Height = createDto.Height,
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now,
IsActive = false
};
AppDb.RobotConfigs.Add(config);
await AppDb.SaveChangesAsync();
return new(true, "Robot configuration created successfully.")
{
Data = new RobotConfigDto
{
Id = config.Id,
ConfigName = config.ConfigName,
Description = config.Description,
NavigationType = config.NavigationType,
RadiusWheel = config.RadiusWheel,
Width = config.Width,
Length = config.Length,
Height = config.Height,
CreatedAt = config.CreatedAt,
UpdatedAt = config.UpdatedAt,
IsActive = config.IsActive,
}
};
}
catch (Exception ex)
{
Logger.Error($"Error in Create Robot Config: {ex.Message}");
return new(false, "An error occurred while creating the Robot configuration.");
}
}
[HttpDelete]
[Route("robot/{id:guid}")]
public async Task<MessageResult> DeleteRobotConfig(Guid id)
{
try
{
var config = await AppDb.RobotConfigs.FindAsync(id);
if (config is null) return new(false, "Robot configuration not found.");
if (config.IsActive) return new(false, "Cannot delete an active Robot configuration.");
AppDb.RobotConfigs.Remove(config);
await AppDb.SaveChangesAsync();
return new(true, "Configuration deleted successfully.");
}
catch (Exception ex)
{
Logger.Error($"Error in Delete Robot Config: {ex.Message}");
return new(false, "An error occurred while deleting the Robot configuration.");
}
}
[HttpPut]
[Route("robot/active/{id:guid}")]
public async Task<MessageResult> ActiveRobotConfig(Guid id)
{
try
{
var config = await AppDb.RobotConfigs.FindAsync(id);
if (config is null) return new(false, "Robot configuration not found.");
await AppDb.RobotConfigs.ExecuteUpdateAsync(cf => cf.SetProperty(c => c.IsActive, false));
config.IsActive = true;
config.UpdatedAt = DateTime.Now;
await AppDb.SaveChangesAsync();
await RobotConfiguration.LoadRobotConfigAsync();
return new(true, $"Robot configuration {config.ConfigName} activated successfully.");
}
catch (Exception ex)
{
Logger.Error($"Error in Update Active Robot Config: {ex.Message}");
return new(false, "An error occurred while updating the active Robot configuration.");
}
}
[HttpGet]
[Route("simulation")]
public async Task<MessageResult<RobotSimulationConfigDto[]>> GetAllSimulationConfigs()
{
try
{
var configs = await AppDb.RobotSimulationConfigs
.OrderByDescending(c => c.UpdatedAt)
.ToListAsync();
if (configs is null || configs.Count == 0) return new(false, "Simulation configuration not found.");
var configDtos = configs.Select(config => new RobotSimulationConfigDto
{
Id = config.Id,
EnableSimulation = config.EnableSimulation,
SimulationMaxVelocity = config.SimulationMaxVelocity,
SimulationMaxAngularVelocity = config.SimulationMaxAngularVelocity,
SimulationAcceleration = config.SimulationAcceleration,
SimulationDeceleration = config.SimulationDeceleration,
CreatedAt = config.CreatedAt,
UpdatedAt = config.UpdatedAt,
IsActive = config.IsActive,
ConfigName = config.ConfigName,
Description = config.Description
}).ToArray();
return new(true) { Data = configDtos };
}
catch (Exception ex)
{
Logger.Error($"Error in Get All Simulation Configs: {ex.Message}");
return new(false, "An error occurred while retrieving Simulation configurations.");
}
}
[HttpPut]
[Route("simulation/{id:guid}")]
public async Task<MessageResult<RobotSimulationConfigDto>> UpdateSimulationConfig(Guid id, [FromBody] UpdateRobotSimulationConfigDto updateDto)
{
try
{
var config = await AppDb.RobotSimulationConfigs.FindAsync(id);
if (config is null) return new(false, "Simulation configuration not found.");
if (string.IsNullOrEmpty(updateDto.ConfigName)) return new(false, "ConfigName cannot be null or empty.");
if (await AppDb.RobotSimulationConfigs.AnyAsync(cf => cf.Id != id && cf.ConfigName == updateDto.ConfigName))
return new(false, "A Simulation configuration with the same name already exists.");
config.EnableSimulation = updateDto.EnableSimulation;
config.SimulationMaxVelocity = updateDto.SimulationMaxVelocity;
config.SimulationMaxAngularVelocity = updateDto.SimulationMaxAngularVelocity;
config.SimulationAcceleration = updateDto.SimulationAcceleration;
config.SimulationDeceleration = updateDto.SimulationDeceleration;
config.ConfigName = updateDto.ConfigName ?? config.ConfigName;
config.Description = updateDto.Description ?? config.Description;
config.UpdatedAt = DateTime.Now;
await AppDb.SaveChangesAsync();
if (config.IsActive) await RobotConfiguration.LoadRobotSimulationConfigAsync();
return new(true, "Simulation configuration updated successfully.")
{
Data = new RobotSimulationConfigDto
{
Id = config.Id,
EnableSimulation = config.EnableSimulation,
SimulationMaxVelocity = config.SimulationMaxVelocity,
SimulationMaxAngularVelocity = config.SimulationMaxAngularVelocity,
SimulationAcceleration = config.SimulationAcceleration,
SimulationDeceleration = config.SimulationDeceleration,
CreatedAt = config.CreatedAt,
UpdatedAt = config.UpdatedAt,
IsActive = config.IsActive,
ConfigName = config.ConfigName,
Description = config.Description
}
};
}
catch (Exception ex)
{
Logger.Error($"Error in Update Simulation Config: {ex.Message}");
return new(false, "An error occurred while updating the Simulation configuration.");
}
}
[HttpPost]
[Route("simulation")]
public async Task<MessageResult<RobotSimulationConfigDto>> CreateRobotSimulationConfig([FromBody] CreateRobotSimulationConfigDto createDto)
{
try
{
if (string.IsNullOrEmpty(createDto.ConfigName)) return new(false, "ConfigName cannot be null or empty.");
if (await AppDb.RobotSimulationConfigs.AnyAsync(cf => cf.ConfigName == createDto.ConfigName))
return new(false, "A Simulation configuration with the same name already exists.");
var config = new RobotSimulationConfig
{
ConfigName = createDto.ConfigName,
Description = createDto.Description,
EnableSimulation = createDto.EnableSimulation,
SimulationMaxVelocity = createDto.SimulationMaxVelocity,
SimulationMaxAngularVelocity = createDto.SimulationMaxAngularVelocity,
SimulationAcceleration = createDto.SimulationAcceleration,
SimulationDeceleration = createDto.SimulationDeceleration,
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now,
IsActive = false
};
AppDb.RobotSimulationConfigs.Add(config);
await AppDb.SaveChangesAsync();
return new(true, "Simulation configuration created successfully.")
{
Data = new RobotSimulationConfigDto
{
Id = config.Id,
ConfigName = config.ConfigName,
Description = config.Description,
EnableSimulation = config.EnableSimulation,
SimulationMaxVelocity = config.SimulationMaxVelocity,
SimulationMaxAngularVelocity = config.SimulationMaxAngularVelocity,
SimulationAcceleration = config.SimulationAcceleration,
SimulationDeceleration = config.SimulationDeceleration,
CreatedAt = config.CreatedAt,
UpdatedAt = config.UpdatedAt,
IsActive = config.IsActive,
}
};
}
catch (Exception ex)
{
Logger.Error($"Error in Create Simulation Config: {ex.Message}");
return new(false, "An error occurred while creating the Simulation configuration.");
}
}
[HttpDelete]
[Route("simulation/{id:guid}")]
public async Task<MessageResult> DeleteRobotSimulationConfig(Guid id)
{
try
{
var config = await AppDb.RobotSimulationConfigs.FindAsync(id);
if (config is null) return new(false, "Simulation configuration not found.");
if (config.IsActive) return new(false, "Cannot delete an active Simulation configuration.");
AppDb.RobotSimulationConfigs.Remove(config);
await AppDb.SaveChangesAsync();
return new(true, "Configuration deleted successfully.");
}
catch (Exception ex)
{
Logger.Error($"Error in Delete Simulation Config: {ex.Message}");
return new(false, "An error occurred while deleting the Simulation configuration.");
}
}
[HttpPut]
[Route("simulation/active/{id:guid}")]
public async Task<MessageResult> ActiveSimulationConfig(Guid id)
{
try
{
var config = await AppDb.RobotSimulationConfigs.FindAsync(id);
if (config is null) return new(false, "Simulation configuration not found.");
await AppDb.RobotSimulationConfigs.ExecuteUpdateAsync(cf => cf.SetProperty(c => c.IsActive, false));
config.IsActive = true;
config.UpdatedAt = DateTime.Now;
await AppDb.SaveChangesAsync();
await RobotConfiguration.LoadRobotSimulationConfigAsync();
return new(true, $"Simulation configuration {config.ConfigName} activated successfully.");
}
catch (Exception ex)
{
Logger.Error($"Error in Update Active Simulation Config: {ex.Message}");
return new(false, "An error occurred while updating the active Simulation configuration.");
}
}
[HttpGet]
[Route("safety")]
public async Task<MessageResult<RobotSafetyConfigDto[]>> GetAllSafetyConfigs()
{
try
{
var configs = await AppDb.RobotSafetyConfigs
.OrderByDescending(c => c.UpdatedAt)
.ToListAsync();
if (configs is null || configs.Count == 0) return new(false, "Safety configuration not found.");
var configDtos = configs.Select(config => new RobotSafetyConfigDto
{
Id = config.Id,
SafetySpeedVerySlow = config.SafetySpeedVerySlow,
SafetySpeedSlow = config.SafetySpeedSlow,
SafetySpeedNormal = config.SafetySpeedNormal,
SafetySpeedMedium = config.SafetySpeedMedium,
SafetySpeedOptimal = config.SafetySpeedOptimal,
SafetySpeedFast = config.SafetySpeedFast,
SafetySpeedVeryFast = config.SafetySpeedVeryFast,
CreatedAt = config.CreatedAt,
UpdatedAt = config.UpdatedAt,
IsActive = config.IsActive,
ConfigName = config.ConfigName,
Description = config.Description
}).ToArray();
return new(true) { Data = configDtos };
}
catch (Exception ex)
{
Logger.Error($"Error in Get All Safety Configs: {ex.Message}");
return new(false, "An error occurred while retrieving Safety configurations.");
}
}
[HttpPut]
[Route("safety/{id:guid}")]
public async Task<MessageResult<RobotSafetyConfigDto>> UpdateSafetyConfig(Guid id, [FromBody] UpdateRobotSafetyConfigDto updateDto)
{
try
{
var config = await AppDb.RobotSafetyConfigs.FindAsync(id);
if (config is null) return new(false, "Safety configuration not found.");
if (string.IsNullOrEmpty(updateDto.ConfigName)) return new(false, "ConfigName cannot be null or empty.");
if (await AppDb.RobotSafetyConfigs.AnyAsync(cf => cf.Id != id && cf.ConfigName == updateDto.ConfigName))
return new(false, "A Safety configuration with the same name already exists.");
config.SafetySpeedVerySlow = updateDto.SafetySpeedVerySlow;
config.SafetySpeedSlow = updateDto.SafetySpeedSlow;
config.SafetySpeedNormal = updateDto.SafetySpeedNormal;
config.SafetySpeedMedium = updateDto.SafetySpeedMedium;
config.SafetySpeedOptimal = updateDto.SafetySpeedOptimal;
config.SafetySpeedFast = updateDto.SafetySpeedFast;
config.SafetySpeedVeryFast = updateDto.SafetySpeedVeryFast;
config.ConfigName = updateDto.ConfigName ?? config.ConfigName;
config.Description = updateDto.Description ?? config.Description;
config.UpdatedAt = DateTime.Now;
await AppDb.SaveChangesAsync();
if (config.IsActive) await RobotConfiguration.LoadRobotSafetyConfigAsync();
return new(true, "Safety configuration updated successfully.")
{
Data = new RobotSafetyConfigDto
{
Id = config.Id,
SafetySpeedVerySlow = config.SafetySpeedVerySlow,
SafetySpeedSlow = config.SafetySpeedSlow,
SafetySpeedNormal = config.SafetySpeedNormal,
SafetySpeedMedium = config.SafetySpeedMedium,
SafetySpeedOptimal = config.SafetySpeedOptimal,
SafetySpeedFast = config.SafetySpeedFast,
SafetySpeedVeryFast = config.SafetySpeedVeryFast,
CreatedAt = config.CreatedAt,
UpdatedAt = config.UpdatedAt,
IsActive = config.IsActive,
ConfigName = config.ConfigName,
Description = config.Description
}
};
}
catch (Exception ex)
{
Logger.Error($"Error in Update Safety Config: {ex.Message}");
return new(false, "An error occurred while updating the Safety configuration.");
}
}
[HttpPost]
[Route("safety")]
public async Task<MessageResult<RobotSafetyConfigDto>> CreateRobotSafetyConfig([FromBody] CreateRobotSafetyConfigDto createDto)
{
try
{
if (string.IsNullOrEmpty(createDto.ConfigName)) return new(false, "ConfigName cannot be null or empty.");
if (await AppDb.RobotSafetyConfigs.AnyAsync(cf => cf.ConfigName == createDto.ConfigName))
return new(false, "A Safety configuration with the same name already exists.");
var config = new RobotSafetyConfig
{
ConfigName = createDto.ConfigName,
Description = createDto.Description,
SafetySpeedVerySlow = createDto.SafetySpeedVerySlow,
SafetySpeedSlow = createDto.SafetySpeedSlow,
SafetySpeedNormal = createDto.SafetySpeedNormal,
SafetySpeedMedium = createDto.SafetySpeedMedium,
SafetySpeedOptimal = createDto.SafetySpeedOptimal,
SafetySpeedFast = createDto.SafetySpeedFast,
SafetySpeedVeryFast = createDto.SafetySpeedVeryFast,
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now,
IsActive = false
};
AppDb.RobotSafetyConfigs.Add(config);
await AppDb.SaveChangesAsync();
return new(true, "Safety configuration created successfully.")
{
Data = new RobotSafetyConfigDto
{
Id = config.Id,
ConfigName = config.ConfigName,
Description = config.Description,
SafetySpeedVerySlow = config.SafetySpeedVerySlow,
SafetySpeedSlow = config.SafetySpeedSlow,
SafetySpeedNormal = config.SafetySpeedNormal,
SafetySpeedMedium = config.SafetySpeedMedium,
SafetySpeedOptimal = config.SafetySpeedOptimal,
SafetySpeedFast = config.SafetySpeedFast,
SafetySpeedVeryFast = config.SafetySpeedVeryFast,
CreatedAt = config.CreatedAt,
UpdatedAt = config.UpdatedAt,
IsActive = config.IsActive,
}
};
}
catch (Exception ex)
{
Logger.Error($"Error in Create Safety Config: {ex.Message}");
return new(false, "An error occurred while creating the Safety configuration.");
}
}
[HttpDelete]
[Route("safety/{id:guid}")]
public async Task<MessageResult> DeleteRobotSafetyConfig(Guid id)
{
try
{
var config = await AppDb.RobotSafetyConfigs.FindAsync(id);
if (config is null) return new(false, "Safety configuration not found.");
if (config.IsActive) return new(false, "Cannot delete an active Safety configuration.");
AppDb.RobotSafetyConfigs.Remove(config);
await AppDb.SaveChangesAsync();
return new(true, "Configuration deleted successfully.");
}
catch (Exception ex)
{
Logger.Error($"Error in Delete Safety Config: {ex.Message}");
return new(false, "An error occurred while deleting the Safety configuration.");
}
}
[HttpPut]
[Route("safety/active/{id:guid}")]
public async Task<MessageResult> ActiveSafetyConfig(Guid id)
{
try
{
var config = await AppDb.RobotSafetyConfigs.FindAsync(id);
if (config is null) return new(false, "Safety configuration not found.");
await AppDb.RobotSafetyConfigs.ExecuteUpdateAsync(cf => cf.SetProperty(c => c.IsActive, false));
config.IsActive = true;
config.UpdatedAt = DateTime.Now;
await AppDb.SaveChangesAsync();
await RobotConfiguration.LoadRobotSafetyConfigAsync();
return new(true, $"Safety configuration {config.ConfigName} activated successfully.");
}
catch (Exception ex)
{
Logger.Error($"Error in Update Active Safety Config: {ex.Message}");
return new(false, "An error occurred while updating the active Safety configuration.");
}
}
}