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.
262 lines
9.5 KiB
262 lines
9.5 KiB
using Microsoft.AspNetCore.Mvc;
|
|
using LTEMvcApp.Models;
|
|
using LTEMvcApp.Services;
|
|
using Newtonsoft.Json.Linq;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace LTEMvcApp.Controllers
|
|
{
|
|
/// <summary>
|
|
/// WebSocket控制器 - 提供LTE客户端WebSocket管理API
|
|
/// </summary>
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class WebSocketController : ControllerBase
|
|
{
|
|
private readonly WebSocketManagerService _webSocketManager;
|
|
private readonly ILogger<WebSocketController> _logger;
|
|
|
|
public WebSocketController(WebSocketManagerService webSocketManager, ILogger<WebSocketController> logger)
|
|
{
|
|
_webSocketManager = webSocketManager;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取所有客户端状态
|
|
/// </summary>
|
|
/// <returns>客户端状态列表</returns>
|
|
[HttpGet("clients")]
|
|
public ActionResult<Dictionary<string, ClientState>> GetClientStates()
|
|
{
|
|
_logger.LogInformation("获取所有客户端状态");
|
|
var states = _webSocketManager.GetAllClientStates();
|
|
return Ok(states);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取客户端配置
|
|
/// </summary>
|
|
/// <param name="clientName">客户端名称</param>
|
|
/// <returns>客户端配置</returns>
|
|
[HttpGet("clients/{clientName}/config")]
|
|
public ActionResult<ClientConfig?> GetClientConfig(string clientName)
|
|
{
|
|
var config = _webSocketManager.GetClientConfig(clientName);
|
|
if (config == null)
|
|
return NotFound($"客户端 '{clientName}' 不存在");
|
|
|
|
return Ok(config);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取所有客户端配置
|
|
/// </summary>
|
|
/// <returns>客户端配置列表</returns>
|
|
[HttpGet("configs")]
|
|
public ActionResult<List<ClientConfig>> GetAllConfigs()
|
|
{
|
|
var configs = _webSocketManager.GetAllClientConfigs();
|
|
return Ok(configs);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 添加客户端配置
|
|
/// </summary>
|
|
/// <param name="config">客户端配置</param>
|
|
/// <returns>操作结果</returns>
|
|
[HttpPost("configs")]
|
|
public ActionResult AddClientConfig([FromBody] ClientConfig config)
|
|
{
|
|
if (string.IsNullOrEmpty(config.Name))
|
|
return BadRequest("客户端名称不能为空");
|
|
|
|
var success = _webSocketManager.AddClientConfig(config);
|
|
if (success)
|
|
return Ok(new { message = $"客户端 '{config.Name}' 配置已添加" });
|
|
else
|
|
return BadRequest("添加客户端配置失败");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 启动客户端
|
|
/// </summary>
|
|
/// <param name="clientName">客户端名称</param>
|
|
/// <returns>操作结果</returns>
|
|
[HttpPost("clients/{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("clients/{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("clients/{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("clients/{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("clients/{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>
|
|
/// <returns>操作结果</returns>
|
|
[HttpPost("clients/{clientName}/logs-config")]
|
|
public ActionResult SetClientLogsConfig(string clientName, [FromBody] LogsConfigRequest request)
|
|
{
|
|
var success = _webSocketManager.SetClientLogsConfig(clientName, request.Config, request.Save);
|
|
if (success)
|
|
return Ok(new { message = $"客户端 '{clientName}' 日志配置已更新" });
|
|
else
|
|
return BadRequest($"更新客户端 '{clientName}' 日志配置失败");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发送消息到客户端
|
|
/// </summary>
|
|
/// <param name="clientName">客户端名称</param>
|
|
/// <param name="message">消息内容</param>
|
|
/// <returns>操作结果</returns>
|
|
[HttpPost("clients/{clientName}/send-message")]
|
|
public ActionResult SendMessage(string clientName, [FromBody] JObject message)
|
|
{
|
|
var messageId = _webSocketManager.SendMessageToClient(clientName, message);
|
|
if (messageId >= 0)
|
|
return Ok(new { messageId, message = $"消息已发送到客户端 '{clientName}'" });
|
|
else
|
|
return BadRequest($"发送消息到客户端 '{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 = "所有客户端已停止" });
|
|
}
|
|
|
|
/// <summary>
|
|
/// 移除客户端配置
|
|
/// </summary>
|
|
/// <param name="clientName">客户端名称</param>
|
|
/// <returns>操作结果</returns>
|
|
[HttpDelete("configs/{clientName}")]
|
|
public ActionResult RemoveClientConfig(string clientName)
|
|
{
|
|
var success = _webSocketManager.RemoveClientConfig(clientName);
|
|
if (success)
|
|
return Ok(new { message = $"客户端 '{clientName}' 配置已移除" });
|
|
else
|
|
return BadRequest($"移除客户端 '{clientName}' 配置失败");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 日志配置请求
|
|
/// </summary>
|
|
public class LogsConfigRequest
|
|
{
|
|
/// <summary>
|
|
/// 日志配置
|
|
/// </summary>
|
|
public Dictionary<string, object> Config { get; set; } = new();
|
|
|
|
/// <summary>
|
|
/// 是否保存配置
|
|
/// </summary>
|
|
public bool Save { get; set; } = false;
|
|
}
|
|
}
|