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.
173 lines
6.2 KiB
173 lines
6.2 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 ClientController : ControllerBase
|
|
{
|
|
private readonly WebSocketManagerService _webSocketManager;
|
|
private readonly ILogger<ClientController> _logger;
|
|
|
|
public ClientController(WebSocketManagerService webSocketManager, ILogger<ClientController> logger)
|
|
{
|
|
_webSocketManager = webSocketManager;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取所有客户端状态
|
|
/// </summary>
|
|
/// <returns>客户端状态列表</returns>
|
|
[HttpGet("states")]
|
|
public ActionResult<Dictionary<string, ClientState>> GetClientStates()
|
|
{
|
|
_logger.LogInformation("获取所有客户端状态");
|
|
var states = _webSocketManager.GetAllClientStates();
|
|
return Ok(states);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 启动客户端
|
|
/// </summary>
|
|
/// <param name="clientName">客户端名称</param>
|
|
/// <returns>操作结果</returns>
|
|
[HttpPost("{clientName}/start")]
|
|
public ActionResult StartClient(string clientName)
|
|
{
|
|
_logger.LogInformation($"API请求: 启动客户端 {clientName}");
|
|
var success = _webSocketManager.StartClient(clientName);
|
|
if (success)
|
|
{
|
|
_logger.LogInformation($"客户端 {clientName} 启动成功");
|
|
return Ok(new { message = $"客户端 '{clientName}' 已启动" });
|
|
}
|
|
else
|
|
{
|
|
_logger.LogWarning($"客户端 {clientName} 启动失败");
|
|
return BadRequest($"启动客户端 '{clientName}' 失败");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 停止客户端
|
|
/// </summary>
|
|
/// <param name="clientName">客户端名称</param>
|
|
/// <returns>操作结果</returns>
|
|
[HttpPost("{clientName}/stop")]
|
|
public ActionResult StopClient(string clientName)
|
|
{
|
|
var success = _webSocketManager.StopClient(clientName);
|
|
if (success)
|
|
return Ok(new { message = $"客户端 '{clientName}' 已停止" });
|
|
else
|
|
return BadRequest($"停止客户端 '{clientName}' 失败");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 播放/暂停客户端
|
|
/// </summary>
|
|
/// <param name="clientName">客户端名称</param>
|
|
/// <returns>操作结果</returns>
|
|
[HttpPost("{clientName}/playpause")]
|
|
public ActionResult PlayPauseClient(string clientName)
|
|
{
|
|
var success = _webSocketManager.PlayPauseClient(clientName);
|
|
if (success)
|
|
return Ok(new { message = $"客户端 '{clientName}' 播放/暂停状态已切换" });
|
|
else
|
|
return BadRequest($"切换客户端 '{clientName}' 播放/暂停状态失败");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 重置客户端日志
|
|
/// </summary>
|
|
/// <param name="clientName">客户端名称</param>
|
|
/// <returns>操作结果</returns>
|
|
[HttpPost("{clientName}/reset-logs")]
|
|
public ActionResult ResetClientLogs(string clientName)
|
|
{
|
|
var success = _webSocketManager.ResetClientLogs(clientName);
|
|
if (success)
|
|
return Ok(new { message = $"客户端 '{clientName}' 日志已重置" });
|
|
else
|
|
return BadRequest($"重置客户端 '{clientName}' 日志失败");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取客户端日志
|
|
/// </summary>
|
|
/// <param name="clientName">客户端名称</param>
|
|
/// <param name="limit">日志数量限制</param>
|
|
/// <returns>日志列表</returns>
|
|
[HttpGet("{clientName}/logs")]
|
|
public ActionResult<List<LTELog>?> GetClientLogs(string clientName, [FromQuery] int limit = 100)
|
|
{
|
|
var logs = _webSocketManager.GetClientLogs(clientName);
|
|
if (logs == null)
|
|
return NotFound($"客户端 '{clientName}' 不存在或未连接");
|
|
|
|
// 限制返回的日志数量
|
|
var limitedLogs = logs.TakeLast(limit).ToList();
|
|
return Ok(limitedLogs);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置客户端日志配置
|
|
/// </summary>
|
|
/// <param name="clientName">客户端名称</param>
|
|
/// <param name="request">请求体</param>
|
|
[HttpPost("{clientName}/logs-config")]
|
|
public ActionResult SetClientLogsConfig(string clientName, [FromBody] ClientLogsConfig request)
|
|
{
|
|
var success = _webSocketManager.SetClientLogsConfig(clientName, request);
|
|
if (success)
|
|
{
|
|
return Ok(new { message = "日志配置已更新" });
|
|
}
|
|
else
|
|
{
|
|
return NotFound(new { message = $"客户端 '{clientName}' 未找到或更新失败" });
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取连接统计信息
|
|
/// </summary>
|
|
/// <returns>统计信息</returns>
|
|
[HttpGet("statistics")]
|
|
public ActionResult<ConnectionStatistics> GetStatistics()
|
|
{
|
|
var stats = _webSocketManager.GetConnectionStatistics();
|
|
return Ok(stats);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 启动所有已配置的客户端
|
|
/// </summary>
|
|
/// <returns>操作结果</returns>
|
|
[HttpPost("start-all")]
|
|
public ActionResult StartAllClients()
|
|
{
|
|
_webSocketManager.StartAllConfiguredClients();
|
|
return Ok(new { message = "所有已配置的客户端已启动" });
|
|
}
|
|
|
|
/// <summary>
|
|
/// 停止所有客户端
|
|
/// </summary>
|
|
/// <returns>操作结果</returns>
|
|
[HttpPost("stop-all")]
|
|
public ActionResult StopAllClients()
|
|
{
|
|
_webSocketManager.StopAllClients();
|
|
return Ok(new { message = "所有客户端已停止" });
|
|
}
|
|
}
|
|
}
|