1
huangxiaoqiang
2025-07-02 9b139ce92aa09f466c01111dd66af9347a34d865
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
{
    /// <summary>
    /// AGV操作接口
    /// </summary>
    [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;
        }
        /// <summary>
        /// 请求入库任务
        /// </summary>
        /// <param name="aGVDTO"></param>
        /// <returns></returns>
        [HttpGet, HttpPost, Route("RequestInTask"), AllowAnonymous]
        public WebResponseContent RequestInTask([FromBody] AGVDTO aGVDTO)
        {
            return _service.RequestInTask(aGVDTO);
        }
 
        /// <summary>
        /// AGV状态
        /// </summary>
        /// <param name="Status"></param>
        /// <returns></returns>
        [HttpGet, HttpPost, Route("ReceiveAGVRuntimeStatus"), AllowAnonymous]
        public WebResponseContent ReceiveAGVRuntimeStatus([FromBody] AGVStatus Status)
        {
            return _service.ReceiveAGVRuntimeStatus(Status);
        }
 
        /// <summary>
        /// AGV任务开始或结束
        /// </summary>
        /// <param name="aGVDTO"></param>
        /// <returns></returns>
        [HttpGet, HttpPost, Route("AGVStartOrEndJob"), AllowAnonymous]
        public WebResponseContent AGVStartOrEndJob([FromBody] AGVDTO aGVDTO)
        {
            return _service.AGVStartOrEndJob(aGVDTO);
        }
 
        /// <summary>
        /// 设备状态上报
        /// </summary>
        /// <param name="aGVDTO"></param>
        /// <returns></returns>
        [HttpGet, HttpPost, Route("DeviceErrorResponse"), AllowAnonymous]
        public WebResponseContent DeviceErrorResponse([FromBody] AGVDTO aGVDTO)
        {
            return _service.DeviceErrorResponse(aGVDTO);
        }
 
        /// <summary>
        /// 设备异常上报
        /// </summary>
        /// <param name="DTO"></param>
        /// <returns></returns>
        [HttpGet, HttpPost, Route("DeviceWarning"), AllowAnonymous]
        [TypeFilter(typeof(ThrottleFilter), Arguments = new object[] { 10 })] // 10秒节流
        public async Task<WebResponseContent> DeviceWarning([FromBody] AGVDTO DTO)
        {
            WebResponseContent responseContent = new WebResponseContent();
            try
            {
                // 使用真正的锁结构
                if (Islock)
                {
                    Islock = false;
                    try
                    {
                        return await _service.DeviceWarning(DTO);
                    }
                    finally
                    {
                        Islock = true;
                    }
                }
                else
                {
                    return responseContent.Error("请求过于频繁,请稍后再试");
                }
            }
            catch (Exception ex)
            {
                // 不需要在这里释放锁,因为TryEnter成功后会在finally中释放
                return responseContent.Error($"设备异常上报失败: {ex.Message}");
            }
        }
    }
}