using Microsoft.AspNetCore.Mvc;
|
using WIDESEAWCS_S7Simulator.Server.Services;
|
|
namespace WIDESEAWCS_S7Simulator.Server.Controllers;
|
|
/// <summary>
|
/// 机械手客户端管理接口(主动连接模式)。
|
/// </summary>
|
[ApiController]
|
[Route("api/[controller]")]
|
public class RobotClientsController : ControllerBase
|
{
|
private readonly IRobotClientManager _robotClientManager;
|
private readonly ILogger<RobotClientsController> _logger;
|
|
public RobotClientsController(IRobotClientManager robotClientManager, ILogger<RobotClientsController> logger)
|
{
|
_robotClientManager = robotClientManager;
|
_logger = logger;
|
}
|
|
[HttpGet("status")]
|
[ProducesResponseType(typeof(RobotServerCollectionStatusResponse), StatusCodes.Status200OK)]
|
public async Task<ActionResult<RobotServerCollectionStatusResponse>> GetStatus()
|
{
|
var status = await _robotClientManager.GetStatusAsync();
|
return Ok(status);
|
}
|
|
[HttpPost("start")]
|
[ProducesResponseType(typeof(RobotServerCollectionStatusResponse), StatusCodes.Status200OK)]
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
public async Task<ActionResult<RobotServerCollectionStatusResponse>> Start([FromBody] RobotServerStartRequest request)
|
{
|
try
|
{
|
var status = await _robotClientManager.StartAsync(request, HttpContext.RequestAborted);
|
return Ok(status);
|
}
|
catch (ArgumentException ex)
|
{
|
return BadRequest(new { error = ex.Message });
|
}
|
catch (Exception ex)
|
{
|
_logger.LogError(ex, "启动机械手客户端实例失败");
|
return StatusCode(StatusCodes.Status500InternalServerError, new { error = "启动机械手客户端实例失败" });
|
}
|
}
|
|
[HttpPost("stop")]
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
public async Task<ActionResult> Stop([FromQuery] string? serverId = null)
|
{
|
await _robotClientManager.StopAsync(serverId);
|
return Ok(new { message = string.IsNullOrWhiteSpace(serverId) ? "机械手客户端已全部停止" : $"机械手客户端 {serverId} 已停止" });
|
}
|
|
[HttpPost("send")]
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
public async Task<ActionResult> Send([FromBody] RobotServerSendRequest request)
|
{
|
if (string.IsNullOrWhiteSpace(request.Message))
|
{
|
return BadRequest(new { error = "发送内容不能为空" });
|
}
|
|
try
|
{
|
if (string.IsNullOrWhiteSpace(request.ServerId))
|
{
|
return BadRequest(new { error = "ServerId 不能为空" });
|
}
|
|
if (request.ClientId.HasValue)
|
{
|
await _robotClientManager.SendToClientAsync(request.ServerId, request.ClientId.Value, request.Message);
|
}
|
else
|
{
|
await _robotClientManager.SendToAllAsync(request.ServerId, request.Message);
|
}
|
|
return Ok(new { message = "发送成功" });
|
}
|
catch (InvalidOperationException ex)
|
{
|
return BadRequest(new { error = ex.Message });
|
}
|
catch (Exception ex)
|
{
|
_logger.LogError(ex, "发送机械手客户端消息失败");
|
return StatusCode(StatusCodes.Status500InternalServerError, new { error = "发送机械手客户端消息失败" });
|
}
|
}
|
|
/// <summary>
|
/// 清空指定客户端实例的消息日志。
|
/// </summary>
|
[HttpPost("clear-received")]
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
public async Task<ActionResult> ClearReceived([FromQuery] string serverId)
|
{
|
if (string.IsNullOrWhiteSpace(serverId))
|
{
|
return BadRequest(new { error = "ServerId 不能为空" });
|
}
|
|
try
|
{
|
await _robotClientManager.ClearReceivedMessagesAsync(serverId);
|
return Ok(new { message = $"实例 {serverId} 的接收消息已清空" });
|
}
|
catch (InvalidOperationException ex)
|
{
|
return BadRequest(new { error = ex.Message });
|
}
|
catch (Exception ex)
|
{
|
_logger.LogError(ex, "清空接收消息失败");
|
return StatusCode(StatusCodes.Status500InternalServerError, new { error = "清空接收消息失败" });
|
}
|
}
|
}
|