| | |
| | | using Microsoft.AspNetCore.Mvc; |
| | | using WIDESEAWCS_S7Simulator.Application.Protocol; |
| | | using WIDESEAWCS_S7Simulator.Core.Entities; |
| | | using WIDESEAWCS_S7Simulator.Core.Interfaces; |
| | | |
| | |
| | | public class SimulatorInstancesController : ControllerBase |
| | | { |
| | | private readonly ISimulatorInstanceManager _instanceManager; |
| | | private readonly IProtocolTemplateService _protocolTemplateService; |
| | | private readonly ILogger<SimulatorInstancesController> _logger; |
| | | |
| | | public SimulatorInstancesController( |
| | | ISimulatorInstanceManager instanceManager, |
| | | IProtocolTemplateService protocolTemplateService, |
| | | ILogger<SimulatorInstancesController> logger) |
| | | { |
| | | _instanceManager = instanceManager ?? throw new ArgumentNullException(nameof(instanceManager)); |
| | | _protocolTemplateService = protocolTemplateService ?? throw new ArgumentNullException(nameof(protocolTemplateService)); |
| | | _logger = logger ?? throw new ArgumentNullException(nameof(logger)); |
| | | } |
| | | |
| | |
| | | if (config.Port <= 0 || config.Port > 65535) |
| | | { |
| | | return BadRequest(new { error = "Port must be between 1 and 65535" }); |
| | | } |
| | | |
| | | NormalizeMemoryConfig(config); |
| | | |
| | | if (string.IsNullOrWhiteSpace(config.ProtocolTemplateId)) |
| | | { |
| | | return BadRequest(new { error = "Protocol template is required" }); |
| | | } |
| | | |
| | | if (!await _protocolTemplateService.ExistsAsync(config.ProtocolTemplateId)) |
| | | { |
| | | return BadRequest(new { error = $"Protocol template '{config.ProtocolTemplateId}' not found" }); |
| | | } |
| | | |
| | | var instance = await _instanceManager.CreateInstanceAsync(config); |
| | |
| | | if (config.Port <= 0 || config.Port > 65535) |
| | | { |
| | | return BadRequest(new { error = "Port must be between 1 and 65535" }); |
| | | } |
| | | |
| | | NormalizeMemoryConfig(config); |
| | | |
| | | if (string.IsNullOrWhiteSpace(config.ProtocolTemplateId)) |
| | | { |
| | | return BadRequest(new { error = "Protocol template is required" }); |
| | | } |
| | | |
| | | if (!await _protocolTemplateService.ExistsAsync(config.ProtocolTemplateId)) |
| | | { |
| | | return BadRequest(new { error = $"Protocol template '{config.ProtocolTemplateId}' not found" }); |
| | | } |
| | | |
| | | // Delete existing instance and recreate with new config |
| | |
| | | return StatusCode(StatusCodes.Status500InternalServerError, new { error = "Failed to stop instances" }); |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 规范化并校验内存配置,优先使用显式 DB 块列表。 |
| | | /// </summary> |
| | | private static void NormalizeMemoryConfig(InstanceConfig config) |
| | | { |
| | | if (config.MemoryConfig == null) |
| | | { |
| | | config.MemoryConfig = new MemoryRegionConfig(); |
| | | } |
| | | |
| | | var dbBlockNumbers = (config.MemoryConfig.DBBlockNumbers ?? new List<int>()) |
| | | .Where(x => x > 0) |
| | | .Distinct() |
| | | .OrderBy(x => x) |
| | | .ToList(); |
| | | |
| | | if (dbBlockNumbers.Count == 0) |
| | | { |
| | | var count = config.MemoryConfig.DBBlockCount > 0 ? config.MemoryConfig.DBBlockCount : 1; |
| | | dbBlockNumbers = Enumerable.Range(1, count).ToList(); |
| | | } |
| | | |
| | | config.MemoryConfig.DBBlockNumbers = dbBlockNumbers; |
| | | config.MemoryConfig.DBBlockCount = dbBlockNumbers.Count; |
| | | config.MemoryConfig.DBBlockSize = config.MemoryConfig.DBBlockSize > 0 ? config.MemoryConfig.DBBlockSize : 1024; |
| | | } |
| | | } |
| | | } |