From cde6ad77663a80d78d77568428a6287b53347716 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期四, 19 三月 2026 17:19:55 +0800
Subject: [PATCH] feat: 新增API路由缓存预热并完善机器人消息日志
---
Code/WCS/WIDESEAWCS_S7Simulator/WIDESEAWCS_S7Simulator.Server/Controllers/RobotClientsController.cs | 126 ++++++++++++++++++++++++++++++++++++++++++
1 files changed, 126 insertions(+), 0 deletions(-)
diff --git a/Code/WCS/WIDESEAWCS_S7Simulator/WIDESEAWCS_S7Simulator.Server/Controllers/RobotClientsController.cs b/Code/WCS/WIDESEAWCS_S7Simulator/WIDESEAWCS_S7Simulator.Server/Controllers/RobotClientsController.cs
new file mode 100644
index 0000000..8192d03
--- /dev/null
+++ b/Code/WCS/WIDESEAWCS_S7Simulator/WIDESEAWCS_S7Simulator.Server/Controllers/RobotClientsController.cs
@@ -0,0 +1,126 @@
+锘縰sing 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 = "娓呯┖鎺ユ敹娑堟伅澶辫触" });
+ }
+ }
+}
--
Gitblit v1.9.3