wangxinhui
10 天以前 6861434f1445d1685b67a24897890c34f8c54f85
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
using Quartz;
using System.Collections.Concurrent;
using System.Net.Sockets;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_ITaskInfoRepository;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.Service;
using WIDESEAWCS_Tasks.SocketServer;
 
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public class StackerCraneJob : IJob
    {
        private readonly TcpSocketServer _TcpSocket;
        private static readonly ConcurrentDictionary<string, StackerSocketState> _socketStates = new();
        private static int _eventSubscribedFlag;
        private readonly ITaskService _taskService;
        private readonly ITaskExecuteDetailService _taskExecuteDetailService;
        private readonly ITaskRepository _taskRepository;
        private readonly IRouterService _routerService;
 
        public StackerCraneJob(TcpSocketServer TcpSocket, ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService)
        {
            _TcpSocket = TcpSocket;
            _taskService = taskService;
            _taskExecuteDetailService = taskExecuteDetailService;
            _taskRepository = taskRepository;
            _routerService = routerService;
        }
 
        public async Task Execute(IJobExecutionContext context)
        {
            bool flag = context.JobDetail.JobDataMap.TryGetValue("JobParams", out object? value);
            StackerCraneDevice Crane = (StackerCraneDevice?)value ?? new StackerCraneDevice();
            if (!flag || Crane.IsNullOrEmpty())
            {
                return;
            }
 
            string ipAddress = Crane.IPAddress;
 
            // 获取或创建状态
            StackerSocketState state = _socketStates.GetOrAdd(ipAddress, _ => new StackerSocketState
            {
                IPAddress = ipAddress,
                StackerCrane = Crane
            });
 
            // 更新设备信息
            state.StackerCrane = Crane;
 
            // 检查是否有该客户端连接
            var clientIds = _TcpSocket.GetClientIds();
            if (!clientIds.Contains(ipAddress))
            {
                return;
            }
 
            // 订阅一次 message 事件(全局一次)
            if (Interlocked.CompareExchange(ref _eventSubscribedFlag, 1, 0) == 0)
            {
                _TcpSocket.MessageReceived += _TcpSocket_MessageReceived;
                _TcpSocket.RobotReceived += _TcpSocket_RobotReceived;
            }
 
            if (!state.IsEventSubscribed)
            {
                _TcpSocket._clients.TryGetValue(ipAddress, out TcpClient client);
                Task clientTask = _TcpSocket.HandleClientAsync(client, Crane.IPAddress, _TcpSocket._cts.Token, state);
                state.IsEventSubscribed = true;
            }
            // 获取当前需下发任务并缓存到状态中
            Dt_Task? task = GetTask(Crane);
            if (task != null && state.CurrentTask == null && state.StackerRunMode== && state.StackerTaskNum == 0)
            {
                SendStackerTask(task, state);
            }
            return;
        }
 
        /// <summary>
        ///  事件:客户端断开连接时触发
        /// </summary>
        /// <param name="clientId"></param>
        /// <returns></returns>
        private Task<string?> _TcpSocket_RobotReceived(string clientId)
        {
            _socketStates.TryRemove(clientId, out _);
            return Task.FromResult<string?>(null);
        }
 
        /// <summary>
        /// 事件:收到消息时触发
        /// </summary>
        /// <param name="message"></param>
        /// <param name="isJson"></param>
        /// <param name="client"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        private async Task<string?> _TcpSocket_MessageReceived(string message, bool isJson, TcpClient client, StackerSocketState state)
        {
            string messageLower = message.ToUpperInvariant();
 
            //区分堆垛机发送命令
            try
            {
                
            }
            catch 
            {
                
            }
 
            return null;
        }
        /// <summary>
        /// 堆垛机任务下发
        /// </summary>
        private bool SendStackerTask(Dt_Task task, StackerSocketState state)
        {
            string message = "<";
            //获取对起始和终点位置进行解析
            message += ">";
            return _TcpSocket.SendToDeviceAsync(state.IPAddress, message).Result;
        }
 
        private Dt_Task? GetTask(StackerCraneDevice stackerCrane, TaskTypeGroup? taskTypeGroup = null)
        {
            return _taskService.QueryStackerTask(stackerCrane.DeviceCode,taskTypeGroup);
        }
    }
 
    public class StackerSocketState
    {
        public string IPAddress { get; set; } = string.Empty;
 
        /// <summary>
        /// 是否已订阅消息事件
        /// </summary>
        public bool IsEventSubscribed { get; set; }
 
        /// <summary>
        /// 堆垛机当前任务号
        /// </summary>
        public int? StackerTaskNum { get; set; }
 
        /// <summary>
        /// 堆垛机运行模式<br/>
        /// 1:自动模式<br/>
        /// 2:停止模式<br/>
        /// 3:手动模式或离线模式<br/>
        /// </summary>
        public int? StackerRunMode { get; set; }
 
        /// <summary>
        /// 堆垛机故障状态<br/>
        /// 正常<br/>
        /// 故障<br/>
        /// </summary>
        public int? StackerError { get; set; }
 
        /// <summary>
        /// 堆垛机设备信息
        /// </summary>
        public StackerCraneDevice? StackerCrane { get; set; }
 
        /// <summary>
        /// 取货位置
        /// </summary>
        public string? PickPosition { get; set; }
 
        /// <summary>
        /// 放货位置
        /// </summary>
        public string? PutPosition{ get; set; }
 
        /// <summary>
        /// 当前任务
        /// </summary>
        public Dt_Task? CurrentTask { get; set; }
    }
    public enum StackerModeEnum
    {
        /// <summary>
        /// 自动模式
        /// </summary>
        Automatic = 1,
        /// <summary>
        /// 停止模式
        /// </summary>
        Unkonw = 2,
        /// <summary>
        /// 手动模式
        /// </summary>
        Manual = 3
    }
    public enum StackerErrorEnum
    {
        /// <summary>
        /// 正常
        /// </summary>
        Normal = 1,
        /// <summary>
        /// 故障
        /// </summary>
        Fault = 2
    }
}