You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
6.4 KiB

1 month ago
using Microsoft.AspNetCore.Mvc;
using LTEMvcApp.Models;
using LTEMvcApp.Services;
using Microsoft.Extensions.Logging;
namespace LTEMvcApp.Controllers
{
/// <summary>
/// 测试配置管理控制器 - 负责测试客户端配置管理
/// </summary>
[ApiController]
[Route("api/[controller]")]
public class TestConfigController : ControllerBase
{
private readonly WebSocketManagerService _webSocketManager;
private readonly ILogger<TestConfigController> _logger;
public TestConfigController(WebSocketManagerService webSocketManager, ILogger<TestConfigController> logger)
{
_webSocketManager = webSocketManager;
_logger = logger;
}
/// <summary>
/// 获取默认测试客户端配置
/// </summary>
/// <returns>测试客户端配置</returns>
[HttpGet("default")]
public ActionResult<ClientConfig> GetDefaultTestClientConfig()
{
var testConfig = _webSocketManager.GetDefaultTestClientConfig();
return Ok(testConfig);
}
/// <summary>
/// 设置测试客户端配置
/// </summary>
/// <param name="config">测试客户端配置</param>
/// <returns>操作结果</returns>
[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("更新测试客户端配置失败");
}
/// <summary>
/// 获取所有测试客户端配置
/// </summary>
/// <returns>测试客户端配置列表</returns>
[HttpGet]
public ActionResult<List<ClientConfig>> GetAllTestClientConfigs()
{
var configs = _webSocketManager.GetAllTestClientConfigs();
return Ok(configs);
}
/// <summary>
/// 根据地址获取测试客户端配置
/// </summary>
/// <param name="address">服务器地址</param>
/// <returns>测试客户端配置</returns>
[HttpGet("address/{address}")]
public ActionResult<ClientConfig?> GetTestClientConfigByAddress(string address)
{
var config = _webSocketManager.GetTestClientConfigByAddress(address);
if (config == null)
return NotFound($"测试客户端配置 (地址: {address}) 不存在");
return Ok(config);
}
/// <summary>
/// 删除测试客户端配置
/// </summary>
/// <param name="address">服务器地址</param>
/// <returns>操作结果</returns>
[HttpDelete("address/{address}")]
public ActionResult RemoveTestClientConfig(string address)
{
var success = _webSocketManager.RemoveTestClientConfig(address);
if (success)
return Ok(new { message = $"测试客户端配置 (地址: {address}) 已删除" });
else
return NotFound($"测试客户端配置 (地址: {address}) 不存在");
}
/// <summary>
/// 启动测试客户端
/// </summary>
/// <param name="request">启动请求</param>
/// <returns>操作结果</returns>
[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} 失败");
}
}
/// <summary>
/// 停止测试客户端
/// </summary>
/// <param name="request">停止请求</param>
/// <returns>操作结果</returns>
[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} 失败");
}
}
}
/// <summary>
/// 启动/停止请求
/// </summary>
public class StartStopRequest
{
/// <summary>
/// 服务器地址
/// </summary>
public string? Address { get; set; }
}
}