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); } /// /// 获取指定客户端的统计配置 /// /// 客户端IP地址 /// 统计配置 [HttpGet("statistics/{ipAddress}")] public ActionResult GetClientStatisticsConfig(string ipAddress) { try { var config = _webSocketManager.GetClientStatisticsConfig(ipAddress); return Ok(new { success = true, data = config }); } catch (Exception ex) { _logger.LogError(ex, "获取客户端统计配置时出错: {IpAddress}", ipAddress); return BadRequest(new { success = false, message = ex.Message }); } } /// /// 设置指定客户端的统计配置 /// /// 客户端IP地址 /// 统计配置 /// 操作结果 [HttpPost("statistics/{ipAddress}")] public ActionResult SetClientStatisticsConfig(string ipAddress, [FromBody] StatisticsConfig config) { try { config.IpAddress = ipAddress; // 确保IP地址正确 _webSocketManager.SetClientStatisticsConfig(config); return Ok(new { success = true, message = "客户端统计配置已更新" }); } catch (Exception ex) { _logger.LogError(ex, "设置客户端统计配置时出错: {IpAddress}", ipAddress); return BadRequest(new { success = false, message = ex.Message }); } } /// /// 更新客户端的Samples参数 /// /// 客户端IP地址 /// 是否启用Samples /// 操作结果 [HttpPost("statistics/{ipAddress}/samples")] public ActionResult UpdateClientSamples(string ipAddress, [FromBody] bool enableSamples) { try { var config = _webSocketManager.GetClientStatisticsConfig(ipAddress); if (config == null) { // 创建新配置 config = new StatisticsConfig { IpAddress = ipAddress, EnableSamples = enableSamples, EnableRf = false, IsEnabled = true }; } else { config.EnableSamples = enableSamples; } _webSocketManager.SetClientStatisticsConfig(config); return Ok(new { success = true, message = "Samples参数已更新" }); } catch (Exception ex) { _logger.LogError(ex, "更新客户端Samples参数时出错: {IpAddress}", ipAddress); return BadRequest(new { success = false, message = ex.Message }); } } /// /// 更新客户端的RF参数 /// /// 客户端IP地址 /// 是否启用RF /// 操作结果 [HttpPost("statistics/{ipAddress}/rf")] public ActionResult UpdateClientRf(string ipAddress, [FromBody] bool enableRf) { try { var config = _webSocketManager.GetClientStatisticsConfig(ipAddress); if (config == null) { // 创建新配置 config = new StatisticsConfig { IpAddress = ipAddress, EnableSamples = false, EnableRf = enableRf, IsEnabled = true }; } else { config.EnableRf = enableRf; } _webSocketManager.SetClientStatisticsConfig(config); return Ok(new { success = true, message = "RF参数已更新" }); } catch (Exception ex) { _logger.LogError(ex, "更新客户端RF参数时出错: {IpAddress}", ipAddress); return BadRequest(new { success = false, message = ex.Message }); } } } }