194 lines
7.8 KiB
C#
194 lines
7.8 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using RobotApp.Common.Shares.Enums;
|
|
using RobotApp.Interfaces;
|
|
using RobotApp.Services.Robot.Simulation;
|
|
|
|
namespace RobotApp.Services.Robot;
|
|
|
|
public class RobotConfiguration(IServiceProvider ServiceProvider, Logger<RobotConfiguration> Logger) : BackgroundService
|
|
{
|
|
public bool IsReady { get; private set; } = false;
|
|
public string SerialNumber { get; private set; } = "";
|
|
public double RadiusWheel { get; private set; } = 0.1;
|
|
public double Width { get; private set; } = 0.606;
|
|
public double Length { get; private set; } = 1.106;
|
|
public double Height { get; private set; } = 0.5;
|
|
public NavigationType NavigationType { get; private set; } = NavigationType.Differential;
|
|
public VDA5050.VDA5050Setting VDA5050Setting { get; private set; } = new();
|
|
public string PLCAddress { get; private set; } = "127.0.0.1";
|
|
public int PLCPort { get; private set; } = 502;
|
|
public byte PLCUnitId { get; private set; } = 1;
|
|
public bool IsSimulation { get; private set; } = true;
|
|
public SimulationModel SimulationModel { get; private set; } = new();
|
|
public Dictionary<SafetySpeed, double> SafetySpeedMap { get; private set; } = new()
|
|
{
|
|
{ SafetySpeed.Very_Slow, 0.15},
|
|
{ SafetySpeed.Slow, 0.3},
|
|
{ SafetySpeed.Normal, 0.6},
|
|
{ SafetySpeed.Medium, 0.9},
|
|
{ SafetySpeed.Optimal, 1.2},
|
|
{ SafetySpeed.Fast, 1.5},
|
|
{ SafetySpeed.Very_Fast, 1.9},
|
|
};
|
|
|
|
public async Task LoadVDA5050ConfigAsync()
|
|
{
|
|
try
|
|
{
|
|
using var scope = ServiceProvider.CreateAsyncScope();
|
|
var appDb = scope.ServiceProvider.GetRequiredService<Data.ApplicationDbContext>();
|
|
var config = await appDb.RobotVDA5050Configs.FirstOrDefaultAsync(c => c.IsActive);
|
|
if (config is not null)
|
|
{
|
|
VDA5050Setting.HostServer = config.VDA5050HostServer;
|
|
VDA5050Setting.Port = config.VDA5050Port;
|
|
VDA5050Setting.UserName = config.VDA5050UserName;
|
|
VDA5050Setting.Password = config.VDA5050Password;
|
|
VDA5050Setting.Manufacturer = config.VDA5050Manufacturer;
|
|
VDA5050Setting.Version = config.VDA5050Version;
|
|
VDA5050Setting.TopicPrefix = config.VDA5050TopicPrefix;
|
|
VDA5050Setting.PublishRepeat = config.VDA5050PublishRepeat;
|
|
VDA5050Setting.EnablePassword = config.VDA5050EnablePassword;
|
|
VDA5050Setting.EnableTls = config.VDA5050EnableTls;
|
|
VDA5050Setting.CAFile = config.VDA5050CA;
|
|
VDA5050Setting.CerFile = config.VDA5050Cer;
|
|
VDA5050Setting.KeyFile = config.VDA5050Key;
|
|
SerialNumber = config.SerialNumber;
|
|
if (IsReady)
|
|
{
|
|
var robotConnection = scope.ServiceProvider.GetRequiredService<RobotConnection>();
|
|
await robotConnection.StopConnection();
|
|
_ = Task.Run(async () => await robotConnection.StartConnection(CancellationToken.None));
|
|
}
|
|
}
|
|
else throw new Exception("Chưa có cấu hình VDA5050.");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.Warning($"Có lỗi xảy ra khi tải cấu hình VDA5050: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
public async Task LoadRobotConfigAsync()
|
|
{
|
|
try
|
|
{
|
|
using var scope = ServiceProvider.CreateAsyncScope();
|
|
var appDb = scope.ServiceProvider.GetRequiredService<Data.ApplicationDbContext>();
|
|
var config = await appDb.RobotConfigs.FirstOrDefaultAsync(c => c.IsActive);
|
|
if (config is not null)
|
|
{
|
|
Width = config.Width;
|
|
Length = config.Length;
|
|
Height = config.Height;
|
|
RadiusWheel = config.RadiusWheel;
|
|
NavigationType = config.NavigationType;
|
|
}
|
|
else throw new Exception("Chưa có cấu hình robot.");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.Warning($"Có lỗi xảy ra khi tải cấu hình robot: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
public async Task LoadRobotSafetyConfigAsync()
|
|
{
|
|
try
|
|
{
|
|
using var scope = ServiceProvider.CreateAsyncScope();
|
|
var appDb = scope.ServiceProvider.GetRequiredService<Data.ApplicationDbContext>();
|
|
var config = await appDb.RobotSafetyConfigs.FirstOrDefaultAsync(c => c.IsActive);
|
|
if (config is not null)
|
|
{
|
|
SafetySpeedMap[SafetySpeed.Very_Slow] = config.SafetySpeedVerySlow;
|
|
SafetySpeedMap[SafetySpeed.Slow] = config.SafetySpeedSlow;
|
|
SafetySpeedMap[SafetySpeed.Normal] = config.SafetySpeedNormal;
|
|
SafetySpeedMap[SafetySpeed.Medium] = config.SafetySpeedMedium;
|
|
SafetySpeedMap[SafetySpeed.Optimal] = config.SafetySpeedOptimal;
|
|
SafetySpeedMap[SafetySpeed.Fast] = config.SafetySpeedFast;
|
|
SafetySpeedMap[SafetySpeed.Very_Fast] = config.SafetySpeedVeryFast;
|
|
}
|
|
else throw new Exception("Chưa có cấu hình safety robot.");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.Warning($"Có lỗi xảy ra khi tải cấu hình safety robot: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
public async Task LoadRobotSimulationConfigAsync()
|
|
{
|
|
try
|
|
{
|
|
using var scope = ServiceProvider.CreateAsyncScope();
|
|
var appDb = scope.ServiceProvider.GetRequiredService<Data.ApplicationDbContext>();
|
|
var config = await appDb.RobotSimulationConfigs.FirstOrDefaultAsync(c => c.IsActive);
|
|
if (config is not null)
|
|
{
|
|
IsSimulation = config.EnableSimulation;
|
|
SimulationModel.MaxVelocity = config.SimulationMaxVelocity;
|
|
SimulationModel.MaxAngularVelocity = config.SimulationMaxAngularVelocity;
|
|
SimulationModel.Acceleration = config.SimulationAcceleration;
|
|
SimulationModel.Deceleration = config.SimulationDeceleration;
|
|
}
|
|
else throw new Exception("Chưa có cấu hình simulation robot.");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.Warning($"Có lỗi xảy ra khi tải cấu hình simulation robot: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
public async Task LoadRobotPlcConfigAsync()
|
|
{
|
|
try
|
|
{
|
|
using var scope = ServiceProvider.CreateAsyncScope();
|
|
var appDb = scope.ServiceProvider.GetRequiredService<Data.ApplicationDbContext>();
|
|
var config = await appDb.RobotPlcConfigs.FirstOrDefaultAsync(c => c.IsActive);
|
|
if (config is not null)
|
|
{
|
|
PLCAddress = config.PLCAddress;
|
|
PLCPort = config.PLCPort;
|
|
PLCUnitId = config.PLCUnitId;
|
|
}
|
|
else throw new Exception("Chưa có cấu hình plc robot.");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.Warning($"Có lỗi xảy ra khi tải cấu hình plc robot: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
public async Task LoadConfig()
|
|
{
|
|
IsReady = false;
|
|
try
|
|
{
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.Error($"Lỗi tải lại cấu hình Robot: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
|
{
|
|
try
|
|
{
|
|
await LoadVDA5050ConfigAsync();
|
|
await LoadRobotConfigAsync();
|
|
await LoadRobotSimulationConfigAsync();
|
|
await LoadRobotSafetyConfigAsync();
|
|
await LoadRobotPlcConfigAsync();
|
|
IsReady = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.Error($"Lỗi khởi tạo RobotConfiguration: {ex.Message}");
|
|
}
|
|
}
|
|
}
|