using Microsoft.AspNetCore.Mvc; using LTEMvcApp.Models; using LTEMvcApp.Services; using Microsoft.Extensions.Logging; namespace LTEMvcApp.Controllers { /// /// 测试配置管理控制器 - 负责测试客户端配置管理 /// [ApiController] [Route("api/[controller]")] public class TestConfigController : ControllerBase { private readonly WebSocketManagerService _webSocketManager; private readonly ILogger _logger; public TestConfigController(WebSocketManagerService webSocketManager, ILogger logger) { _webSocketManager = webSocketManager; _logger = logger; } /// /// 获取默认测试客户端配置 /// /// 测试客户端配置 [HttpGet("default")] public ActionResult GetDefaultTestClientConfig() { var testConfig = _webSocketManager.GetDefaultTestClientConfig(); return Ok(testConfig); } /// /// 设置测试客户端配置 /// /// 测试客户端配置 /// 操作结果 [HttpPost("default")] public ActionResult SetTestClientConfig([FromBody] ClientConfig config) { if (string.IsNullOrEmpty(config.Name)) return BadRequest("客户端名称不能为空"); var success = _webSocketManager.SetTestClientConfig(config); if (success) return Ok(new { message = "测试客户端配置已更新" }); else return BadRequest("更新测试客户端配置失败"); } /// /// 获取所有测试客户端配置 /// /// 测试客户端配置列表 [HttpGet] public ActionResult> GetAllTestClientConfigs() { var configs = _webSocketManager.GetAllTestClientConfigs(); return Ok(configs); } /// /// 根据地址获取测试客户端配置 /// /// 服务器地址 /// 测试客户端配置 [HttpGet("address/{address}")] public ActionResult GetTestClientConfigByAddress(string address) { var config = _webSocketManager.GetTestClientConfigByAddress(address); if (config == null) return NotFound($"测试客户端配置 (地址: {address}) 不存在"); return Ok(config); } /// /// 删除测试客户端配置 /// /// 服务器地址 /// 操作结果 [HttpDelete("address/{address}")] public ActionResult RemoveTestClientConfig(string address) { var success = _webSocketManager.RemoveTestClientConfig(address); if (success) return Ok(new { message = $"测试客户端配置 (地址: {address}) 已删除" }); else return NotFound($"测试客户端配置 (地址: {address}) 不存在"); } /// /// 启动测试客户端 /// /// 启动请求 /// 操作结果 [HttpPost("start")] public ActionResult StartTestClient([FromBody] StartStopRequest? request = null) { string? address = request?.Address; if (string.IsNullOrEmpty(address)) { // 使用默认配置启动 var success = _webSocketManager.StartTestClient(); if (success) return Ok(new { message = "测试客户端已启动" }); else return BadRequest("启动测试客户端失败"); } else { // 根据地址查找配置并启动 var config = _webSocketManager.GetTestClientConfigByAddress(address); if (config == null) return NotFound($"未找到地址为 {address} 的测试客户端配置"); var success = _webSocketManager.StartClient(config.Name); if (success) return Ok(new { message = $"测试客户端 {config.Name} 已启动" }); else return BadRequest($"启动测试客户端 {config.Name} 失败"); } } /// /// 停止测试客户端 /// /// 停止请求 /// 操作结果 [HttpPost("stop")] public ActionResult StopTestClient([FromBody] StartStopRequest? request = null) { string? address = request?.Address; if (string.IsNullOrEmpty(address)) { // 使用默认配置停止 _logger.LogInformation("API 请求: 停止测试客户端"); var success = _webSocketManager.StopTestClient(); if (success) return Ok(new { message = "测试客户端停止成功" }); else return BadRequest("停止测试客户端失败"); } else { // 根据地址查找配置并停止 var config = _webSocketManager.GetTestClientConfigByAddress(address); if (config == null) return NotFound($"未找到地址为 {address} 的测试客户端配置"); _logger.LogInformation($"API 请求: 停止测试客户端 {config.Name}"); var success = _webSocketManager.StopClient(config.Name); if (success) return Ok(new { message = $"测试客户端 {config.Name} 停止成功" }); else return BadRequest($"停止测试客户端 {config.Name} 失败"); } } } /// /// 启动/停止请求 /// public class StartStopRequest { /// /// 服务器地址 /// public string? Address { get; set; } } }