wangxinhui
2026-03-26 ad05ef2341fe19a4220f7ada1987636f9ec4a1a9
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using RYB_PTL_API;
using System.Collections.Generic;
using System.Linq;
using WIDESEAWCS_DTO;
using WIDESEAWCS_DTO.TaskInfo;
using WIDESEAWCS_ITaskInfoService;
 
namespace WIDESEAWCS_Server.Controllers
{
    [Route("PTL")]
    [ApiController]
    public class PTLAPIController : ControllerBase
    {
        private readonly ITaskService _taskService;
        private bool _eventSubscribed = false;
 
        public PTLAPIController(ITaskService taskService)
        {
            _taskService = taskService;
            SubscribeEvent();
        }
 
        /// <summary>
        /// 订阅PTL事件
        /// </summary>
        private void SubscribeEvent()
        {
            if (!_eventSubscribed)
            {
                RYB_PTL.UserResultAvailable += new RYB_PTL.UserResultAvailableEventHandler(RYB_PTL_UserResultAvailable);
                _eventSubscribed = true;
            }
        }
        
        /// <summary>
        /// 取消订阅PTL事件
        /// </summary>
        private void UnsubscribeEvent()
        {
            if (_eventSubscribed)
            {
                RYB_PTL.UserResultAvailable -= new RYB_PTL.UserResultAvailableEventHandler(RYB_PTL_UserResultAvailable);
                _eventSubscribed = false;
            }
        }
        /// <summary>
        /// PTL回调事件处理(只处理手拍事件)
        /// </summary>
        private void RYB_PTL_UserResultAvailable(RYB_PTL.RtnValueStruct rs)
        {
            try
            {
                if (rs.KeyCode == null)
                {
                    return;
                }
                // 构建回调数据
                var pTLCallBackDTO = new PTLCallBackDTO
                {
                    sIp = rs.Ip,
                    sTagID = rs.Tagid,
                    sValue = rs.Number,
                    sKeyCode = rs.KeyCode,
                    sLocator = rs.Locator
                };
 
                // 根据业务需要构建任务信息
                var taskBackLight = new TaskBackLight()
                {
                    TagNo = "B1", 
                    TagCode = pTLCallBackDTO.sLocator,
                };
 
                // 调用服务处理手拍事件
                _taskService.WMSLightBack(new List<TaskBackLight> { taskBackLight });
                UnsubscribeEvent();
            }
            catch (Exception ex)
            {
                // 记录日志或处理异常
                Console.WriteLine($"处理PTL手拍回调时出错: {ex.Message}");
            }
        }
        /// <summary>
        /// 播种墙下发(一期)
        /// </summary>
        [HttpPost, Route("RYB_PTL_CloseDigit5"), AllowAnonymous]
        public EPLightContent RYB_PTL_CloseDigit5([FromBody] CloseDigit5DTO request)
        {
            var content = new EPLightContent();
            try
            {
                bool isConnected = RYB_PTL.RYB_PTL_DspDigit5(
                    request.sIP,
                    request.sTagID,
                    request.iNum,
                    request.iMode,
                    request.iColorIndex);
            }
            catch (Exception ex)
            {
                return content.Error(ex.Message);
            }
            return content;
        }
        /// <summary>
        /// 回调上传
        /// </summary>
        /// <returns></returns>
        [HttpPost, Route("RYB_PTL_UserResultAvailable"), AllowAnonymous]
        public EPLightContent? RYB_PTL_UserResultAvailable([FromBody] List<PTLCallBackDTO> pTLCallBackDTOs)
        {
            EPLightContent content = new EPLightContent();
            try
            {
                if (pTLCallBackDTOs == null)
                {
                    return content.Error("传入不能为空");
                }
 
                List<TaskBackLight> taskBackLights = pTLCallBackDTOs.Select(x => new TaskBackLight()
                {
                    TagNo = "B1",
                    TagCode = x.sLocator,
                }).ToList();
                content = _taskService.WMSLightBack(taskBackLights);
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
 
        /// <summary>
        /// 播种墙初始化(一期)
        /// </summary>
        /// <returns></returns>
        [HttpPost, Route("RYB_PTL_Connect"), AllowAnonymous]
        public EPLightContent RYB_PTL_Connect([FromBody] ConnectDTO request)
        {
            EPLightContent content = new EPLightContent();
            try
            {
                //RYB_PTL.UserResultAvailable += new RYB_PTL.UserResultAvailableEventHandler(RYB_PTL_UserResultAvailable);
                bool isCfg = RYB_PTL.RYB_PTL_InitialConfiguration(new string[,] { { "11.2.30.252", "0001-0016" },{ "11.2.30.252", "1-16" } });
                bool isConnected = RYB_PTL.RYB_PTL_Connect(request.sIp, request.iPort);
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
 
        /// <summary>
        /// 播种墙结束作业(一期)
        /// </summary>
        /// <returns></returns>
        [HttpPost, Route("RYB_PTL_Disconnect"), AllowAnonymous]
        public EPLightContent RYB_PTL_Disconnect()
        {
            EPLightContent content = new EPLightContent();
            try
            {
                bool isDisconnected = RYB_PTL.RYB_PTL_Disconnect();
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
 
        /// <summary>
        /// 资源释放(标记为不暴露给Swagger)
        /// </summary>
        [ApiExplorerSettings(IgnoreApi = true)]
        public void Dispose()
        {
            UnsubscribeEvent();
        }
    }
}