using Microsoft.AspNetCore.Mvc; using NewLife.Net; using System.Threading.Tasks; using WIDESEA_DTO.AGV; using WIDESEA_IStoragIntegrationServices; using WIDESEA_StorageSocketServices; namespace WIDESEA_WMSServer.Controllers { /// /// AGV操作接口 /// [Route("api/[controller]")] [ApiController] public class AGVController : Controller { private readonly IHttpContextAccessor _httpContextAccessor; private readonly IAGVService _service; public static bool Islock = true; // 锁,防止重复请求 public AGVController(IAGVService service, IHttpContextAccessor httpContextAccessor) { _httpContextAccessor = httpContextAccessor; _service = service; } /// /// 请求入库任务 /// /// /// [HttpGet, HttpPost, Route("RequestInTask"), AllowAnonymous] public WebResponseContent RequestInTask([FromBody] AGVDTO aGVDTO) { return _service.RequestInTask(aGVDTO); } /// /// AGV状态 /// /// /// [HttpGet, HttpPost, Route("ReceiveAGVRuntimeStatus"), AllowAnonymous] public WebResponseContent ReceiveAGVRuntimeStatus([FromBody] AGVStatus Status) { return _service.ReceiveAGVRuntimeStatus(Status); } /// /// AGV任务开始或结束 /// /// /// [HttpGet, HttpPost, Route("AGVStartOrEndJob"), AllowAnonymous] public WebResponseContent AGVStartOrEndJob([FromBody] AGVDTO aGVDTO) { return _service.AGVStartOrEndJob(aGVDTO); } /// /// 设备状态上报 /// /// /// [HttpGet, HttpPost, Route("DeviceErrorResponse"), AllowAnonymous] public WebResponseContent DeviceErrorResponse([FromBody] AGVDTO aGVDTO) { return _service.DeviceErrorResponse(aGVDTO); } /// /// 设备异常上报 /// /// /// [HttpGet, HttpPost, Route("DeviceWarning"), AllowAnonymous] [TypeFilter(typeof(ThrottleFilter), Arguments = new object[] { 10 })] // 10秒节流 public async Task DeviceWarning([FromBody] AGVDTO DTO) { WebResponseContent responseContent = new WebResponseContent(); try { // 使用真正的锁结构 if (Islock) { Islock = false; try { return await Task.Run(()=> _service.DeviceWarning(DTO)); } finally { Islock = true; } } else { return responseContent.Error("请求过于频繁,请稍后再试"); } } catch (Exception ex) { // 不需要在这里释放锁,因为TryEnter成功后会在finally中释放 return responseContent.Error($"设备异常上报失败: {ex.Message}"); } } } }