using Microsoft.AspNetCore.Mvc; using LTEMvcApp.Models; using LTEMvcApp.Services; using Microsoft.Extensions.Logging; namespace LTEMvcApp.Controllers { /// /// 配置管理控制器 - 负责普通客户端配置管理 /// [ApiController] [Route("api/[controller]")] public class ConfigController : ControllerBase { private readonly WebSocketManagerService _webSocketManager; private readonly ILogger _logger; public ConfigController(WebSocketManagerService webSocketManager, ILogger logger) { _webSocketManager = webSocketManager; _logger = logger; } /// /// 获取所有客户端配置 /// /// 客户端配置列表 [HttpGet] public ActionResult> GetAllConfigs() { var configs = _webSocketManager.GetAllClientConfigs(); return Ok(configs); } /// /// 获取指定客户端的统计配置 /// /// 客户端名称 /// 统计配置 [HttpGet("statistics/{clientName}")] public ActionResult GetClientStatisticsConfig(string clientName) { try { var config = _webSocketManager.GetClientStatisticsConfig(clientName); return Json(new { success = true, data = config }); } catch (Exception ex) { _logger.LogError(ex, "获取客户端统计配置时出错: {ClientName}", clientName); return Json(new { success = false, message = ex.Message }); } } /// /// 设置指定客户端的统计配置 /// /// 客户端名称 /// 统计配置 /// 操作结果 [HttpPost("statistics/{clientName}")] public ActionResult SetClientStatisticsConfig(string clientName, [FromBody] StatisticsConfig config) { try { config.ClientName = clientName; // 确保客户端名称正确 _webSocketManager.SetClientStatisticsConfig(config); return Json(new { success = true, message = "客户端统计配置已更新" }); } catch (Exception ex) { _logger.LogError(ex, "设置客户端统计配置时出错: {ClientName}", clientName); return Json(new { success = false, message = ex.Message }); } } /// /// 更新客户端的Samples参数 /// /// 客户端名称 /// 是否启用Samples /// 操作结果 [HttpPost("statistics/{clientName}/samples")] public ActionResult UpdateClientSamples(string clientName, [FromBody] bool enableSamples) { try { var config = _webSocketManager.GetClientStatisticsConfig(clientName); if (config == null) { // 创建新配置 config = new StatisticsConfig { ClientName = clientName, EnableSamples = enableSamples, EnableRf = false, IsEnabled = true }; } else { config.EnableSamples = enableSamples; } _webSocketManager.SetClientStatisticsConfig(config); return Json(new { success = true, message = "Samples参数已更新" }); } catch (Exception ex) { _logger.LogError(ex, "更新客户端Samples参数时出错: {ClientName}", clientName); return Json(new { success = false, message = ex.Message }); } } /// /// 更新客户端的RF参数 /// /// 客户端名称 /// 是否启用RF /// 操作结果 [HttpPost("statistics/{clientName}/rf")] public ActionResult UpdateClientRf(string clientName, [FromBody] bool enableRf) { try { var config = _webSocketManager.GetClientStatisticsConfig(clientName); if (config == null) { // 创建新配置 config = new StatisticsConfig { ClientName = clientName, EnableSamples = false, EnableRf = enableRf, IsEnabled = true }; } else { config.EnableRf = enableRf; } _webSocketManager.SetClientStatisticsConfig(config); return Json(new { success = true, message = "RF参数已更新" }); } catch (Exception ex) { _logger.LogError(ex, "更新客户端RF参数时出错: {ClientName}", clientName); return Json(new { success = false, message = ex.Message }); } } } }