initial commit

This commit is contained in:
robotics_x1
2026-10-12 15:24:08 +07:00
commit 55228b3772
367 changed files with 162893 additions and 0 deletions

View File

@@ -0,0 +1,157 @@
using Microsoft.Extensions.Logging;
namespace Lab505.Sim;
/// <summary>
/// Quản lý tất cả simulation services - có thể start/stop tại runtime
/// </summary>
public interface ISimulationManager
{
bool IsRunning { get; }
Task StartAllAsync();
Task StopAllAsync();
}
public class SimulationManager : ISimulationManager
{
private readonly ILogger<SimulationManager> _logger;
private readonly VdaSimulationHostedService? _vdaServer;
private readonly OpcUaSimulationServer? _opcUaServer;
private readonly ModbusSimulationServer_ASRS? _asrsServer;
private readonly ConveyorModbusServer? _conveyorServer;
private bool _isRunning;
private readonly object _lock = new();
public bool IsRunning
{
get { lock (_lock) return _isRunning; }
}
public SimulationManager(
ILogger<SimulationManager> logger,
VdaSimulationHostedService? vdaServer = null,
OpcUaSimulationServer? opcUaServer = null,
ModbusSimulationServer_ASRS? asrsServer = null,
ConveyorModbusServer? conveyorServer = null)
{
_logger = logger;
_vdaServer = vdaServer;
_opcUaServer = opcUaServer;
_asrsServer = asrsServer;
_conveyorServer = conveyorServer;
}
public async Task StartAllAsync()
{
lock (_lock)
{
if (_isRunning)
{
_logger.LogWarning("Simulation services already running");
return;
}
}
_logger.LogInformation("Starting all simulation services...");
try
{
// Start Modbus servers
if (_asrsServer != null)
{
_asrsServer.Start();
_logger.LogInformation("✅ ASRS Modbus Simulation Server started");
}
if (_conveyorServer != null)
{
_conveyorServer.Start();
_logger.LogInformation("✅ Conveyor Modbus Simulation Server started");
}
// Start OPC UA server
if (_opcUaServer != null)
{
await _opcUaServer.StartAsync();
_logger.LogInformation("✅ OPC UA Simulation Server started");
}
// Start VDA server
if (_vdaServer != null)
{
_vdaServer.Start();
_logger.LogInformation("✅ VDA Mock Server started");
}
lock (_lock) _isRunning = true;
_logger.LogInformation("All simulation services started successfully");
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to start simulation services");
throw;
}
}
public async Task StopAllAsync()
{
lock (_lock)
{
if (!_isRunning)
{
_logger.LogWarning("Simulation services not running");
return;
}
}
_logger.LogInformation("Stopping all simulation services...");
try
{
// Stop VDA server
if (_vdaServer != null)
{
_vdaServer.Stop();
_logger.LogInformation("✅ VDA Mock Server stopped");
}
// Stop OPC UA server
if (_opcUaServer != null)
{
await _opcUaServer.StopAsync();
_logger.LogInformation("✅ OPC UA Simulation Server stopped");
}
// Stop Modbus servers
if (_conveyorServer != null)
{
_conveyorServer.Stop();
_logger.LogInformation("✅ Conveyor Modbus Simulation Server stopped");
}
if (_asrsServer != null)
{
_asrsServer.Stop();
_logger.LogInformation("✅ ASRS Modbus Simulation Server stopped");
}
lock (_lock) _isRunning = false;
_logger.LogInformation("All simulation services stopped successfully");
}
catch (Exception ex)
{
_logger.LogError(ex, "Error stopping simulation services");
lock (_lock) _isRunning = false;
throw;
}
}
}
/// <summary>Stub dùng khi tắt toàn bộ simulation servers.</summary>
public class NullSimulationManager : ISimulationManager
{
public bool IsRunning => false;
public Task StartAllAsync() => Task.CompletedTask;
public Task StopAllAsync() => Task.CompletedTask;
}