This commit is contained in:
Đăng Nguyễn
2025-10-31 15:03:37 +07:00
parent aa2146e383
commit aea55d52f1
16 changed files with 1907 additions and 222 deletions

View File

@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using RobotApp.Common.Shares;
@@ -49,18 +50,13 @@ public class RobotConfigsController(Services.Logger<RobotConfigsController> Logg
[HttpPut]
[Route("plc/{id:guid}")]
public async Task<MessageResult<RobotPlcConfigDto>> UpdatePLCConfig(Guid id, [FromBody] UpdateRobotPlcConfigDto updateDto)
public async Task<MessageResult> 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;
@@ -70,21 +66,7 @@ public class RobotConfigsController(Services.Logger<RobotConfigsController> Logg
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,
}
};
return new(true, "PLC configuration updated successfully.");
}
catch (Exception ex)
{
@@ -150,6 +132,7 @@ public class RobotConfigsController(Services.Logger<RobotConfigsController> Logg
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.");
if (config.ConfigName == "Default") return new(false, "Cannot delete the default PLC configuration.");
AppDb.RobotPlcConfigs.Remove(config);
await AppDb.SaveChangesAsync();
@@ -211,6 +194,9 @@ public class RobotConfigsController(Services.Logger<RobotConfigsController> Logg
VDA5050PublishRepeat = config.VDA5050PublishRepeat,
VDA5050EnablePassword = config.VDA5050EnablePassword,
VDA5050EnableTls = config.VDA5050EnableTls,
VDA5050CA = config.VDA5050CA,
VDA5050Cer = config.VDA5050Cer,
VDA5050_Key = config.VDA5050_Key,
CreatedAt = config.CreatedAt,
UpdatedAt = config.UpdatedAt,
IsActive = config.IsActive,
@@ -229,17 +215,13 @@ public class RobotConfigsController(Services.Logger<RobotConfigsController> Logg
[HttpPut]
[Route("vda5050/{id:guid}")]
public async Task<MessageResult<RobotVDA5050ConfigDto>> UpdateVDA5050Config(Guid id, [FromBody] UpdateRobotVDA5050ConfigDto updateDto)
public async Task<MessageResult> 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;
@@ -250,35 +232,16 @@ public class RobotConfigsController(Services.Logger<RobotConfigsController> Logg
config.VDA5050PublishRepeat = updateDto.VDA5050PublishRepeat;
config.VDA5050EnablePassword = updateDto.VDA5050EnablePassword;
config.VDA5050EnableTls = updateDto.VDA5050EnableTls;
config.ConfigName = updateDto.ConfigName ?? config.ConfigName;
config.VDA5050CA = updateDto.VDA5050CA;
config.VDA5050Cer = updateDto.VDA5050Cer;
config.VDA5050_Key = updateDto.VDA5050_Key;
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
}
};
return new(true, "VDA5050 configuration updated successfully.");
}
catch (Exception ex)
{
@@ -311,6 +274,9 @@ public class RobotConfigsController(Services.Logger<RobotConfigsController> Logg
VDA5050PublishRepeat = createDto.VDA5050PublishRepeat,
VDA5050EnablePassword = createDto.VDA5050EnablePassword,
VDA5050EnableTls = createDto.VDA5050EnableTls,
VDA5050CA = createDto.VDA5050CA,
VDA5050Cer = createDto.VDA5050Cer,
VDA5050_Key = createDto.VDA5050_Key,
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now,
IsActive = false
@@ -336,6 +302,9 @@ public class RobotConfigsController(Services.Logger<RobotConfigsController> Logg
VDA5050PublishRepeat = config.VDA5050PublishRepeat,
VDA5050EnablePassword = config.VDA5050EnablePassword,
VDA5050EnableTls = config.VDA5050EnableTls,
VDA5050CA = config.VDA5050CA,
VDA5050Cer = config.VDA5050Cer,
VDA5050_Key = config.VDA5050_Key,
CreatedAt = config.CreatedAt,
UpdatedAt = config.UpdatedAt,
IsActive = config.IsActive,
@@ -358,6 +327,7 @@ public class RobotConfigsController(Services.Logger<RobotConfigsController> Logg
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.");
if (config.ConfigName == "Default") return new(false, "Cannot delete the default PLC configuration.");
AppDb.RobotVDA5050Configs.Remove(config);
await AppDb.SaveChangesAsync();
@@ -432,46 +402,25 @@ public class RobotConfigsController(Services.Logger<RobotConfigsController> Logg
[HttpPut]
[Route("robot/{id:guid}")]
public async Task<MessageResult<RobotConfigDto>> UpdateRobotConfig(Guid id, [FromBody] UpdateRobotConfigDto updateDto)
public async Task<MessageResult> 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
}
};
return new(true, "Robot configuration updated successfully.");
}
catch (Exception ex)
{
@@ -541,6 +490,7 @@ public class RobotConfigsController(Services.Logger<RobotConfigsController> Logg
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.");
if (config.ConfigName == "Default") return new(false, "Cannot delete the default PLC configuration.");
AppDb.RobotConfigs.Remove(config);
await AppDb.SaveChangesAsync();
@@ -615,46 +565,25 @@ public class RobotConfigsController(Services.Logger<RobotConfigsController> Logg
[HttpPut]
[Route("simulation/{id:guid}")]
public async Task<MessageResult<RobotSimulationConfigDto>> UpdateSimulationConfig(Guid id, [FromBody] UpdateRobotSimulationConfigDto updateDto)
public async Task<MessageResult> 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
}
};
return new(true, "Simulation configuration updated successfully.");
}
catch (Exception ex)
{
@@ -724,6 +653,7 @@ public class RobotConfigsController(Services.Logger<RobotConfigsController> Logg
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.");
if (config.ConfigName == "Default") return new(false, "Cannot delete the default PLC configuration.");
AppDb.RobotSimulationConfigs.Remove(config);
await AppDb.SaveChangesAsync();
@@ -799,17 +729,13 @@ public class RobotConfigsController(Services.Logger<RobotConfigsController> Logg
[HttpPut]
[Route("safety/{id:guid}")]
public async Task<MessageResult<RobotSafetyConfigDto>> UpdateSafetyConfig(Guid id, [FromBody] UpdateRobotSafetyConfigDto updateDto)
public async Task<MessageResult> 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;
@@ -817,32 +743,13 @@ public class RobotConfigsController(Services.Logger<RobotConfigsController> Logg
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
}
};
return new(true, "Safety configuration updated successfully.");
}
catch (Exception ex)
{
@@ -916,6 +823,7 @@ public class RobotConfigsController(Services.Logger<RobotConfigsController> Logg
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.");
if (config.ConfigName == "Default") return new(false, "Cannot delete the default PLC configuration.");
AppDb.RobotSafetyConfigs.Remove(config);
await AppDb.SaveChangesAsync();