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.
 
 
 

153 lines
5.6 KiB

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 ConfigController : ControllerBase
{
private readonly WebSocketManagerService _webSocketManager;
private readonly ILogger<ConfigController> _logger;
public ConfigController(WebSocketManagerService webSocketManager, ILogger<ConfigController> logger)
{
_webSocketManager = webSocketManager;
_logger = logger;
}
/// <summary>
/// 获取所有客户端配置
/// </summary>
/// <returns>客户端配置列表</returns>
[HttpGet]
public ActionResult<List<ClientConfig>> GetAllConfigs()
{
var configs = _webSocketManager.GetAllClientConfigs();
return Ok(configs);
}
/// <summary>
/// 获取指定客户端的统计配置
/// </summary>
/// <param name="ipAddress">客户端IP地址</param>
/// <returns>统计配置</returns>
[HttpGet("statistics/{ipAddress}")]
public ActionResult<StatisticsConfig?> 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 });
}
}
/// <summary>
/// 设置指定客户端的统计配置
/// </summary>
/// <param name="ipAddress">客户端IP地址</param>
/// <param name="config">统计配置</param>
/// <returns>操作结果</returns>
[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 });
}
}
/// <summary>
/// 更新客户端的Samples参数
/// </summary>
/// <param name="ipAddress">客户端IP地址</param>
/// <param name="enableSamples">是否启用Samples</param>
/// <returns>操作结果</returns>
[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 });
}
}
/// <summary>
/// 更新客户端的RF参数
/// </summary>
/// <param name="ipAddress">客户端IP地址</param>
/// <param name="enableRf">是否启用RF</param>
/// <returns>操作结果</returns>
[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 });
}
}
}
}