1
huangxiaoqiang
2025-06-17 3d1f19f7ab5f3adb28a29d5b955dc298aadf6973
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
using Newtonsoft.Json;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Logical;
using Quartz;
using SqlSugar;
using SqlSugar.Extensions;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using WIDESEA_Core.Enums;
using WIDESEAWCS_Core.Caches;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_Core.HttpContextUser;
using WIDESEAWCS_IBasicInfoRepository;
using WIDESEAWCS_ISystemRepository;
using WIDESEAWCS_ITaskInfoRepository;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.DeviceBase;
using WIDESEAWCS_QuartzJob.DTO;
using WIDESEAWCS_QuartzJob.Models;
using WIDESEAWCS_QuartzJob.Service;
using WIDESEAWCS_QuartzJob.StackerCrane;
using WIDESEAWCS_QuartzJob.StackerCrane.Enum;
using WIDESEAWCS_SignalR;
using WIDESEAWCS_Tasks.StackerCraneJob;
using ICacheService = WIDESEAWCS_Core.Caches.ICacheService;
 
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public class CommonStackerCraneJob : IJob
    {
        private readonly ITaskService _taskService;
        private readonly ITaskExecuteDetailService _taskExecuteDetailService;
        private readonly ITaskRepository _taskRepository;
        private readonly IDt_StationManagerRepository _stationManagerRepository;
        private readonly ICacheService _cacheService;
        private readonly INoticeService _noticeService;
 
        public CommonStackerCraneJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository,IDt_StationManagerRepository stationManagerRepository, ICacheService cacheService, INoticeService noticeService)
        {
            _taskService = taskService;
            _taskExecuteDetailService = taskExecuteDetailService;
            _taskRepository = taskRepository;
            _stationManagerRepository = stationManagerRepository;
            _cacheService = cacheService;
            _noticeService = noticeService;
        }
 
        public Task Execute(IJobExecutionContext context)
        {
            try
            {
                CommonStackerCrane commonStackerCrane = (CommonStackerCrane)context.JobDetail.JobDataMap.Get("JobParams");
 
                if (commonStackerCrane != null)
                {
                    if (!commonStackerCrane.IsEventSubscribed)
                    {
                        commonStackerCrane.StackerCraneTaskCompletedEventHandler += CommonStackerCrane_StackerCraneTaskCompletedEventHandler;//订阅任务完成事件
                    }
 
                    if (commonStackerCrane.StackerCraneAutoStatusValue == StackerCraneAutoStatus.Automatic && commonStackerCrane.StackerCraneStatusValue == StackerCraneStatus.Normal)
                    {
                        commonStackerCrane.CheckStackerCraneTaskCompleted();//检测堆垛机任务完成事件
 
                        if (commonStackerCrane.StackerCraneWorkStatusValue == StackerCraneWorkStatus.Idle)
                        {
                            GetTask(commonStackerCrane);
                        }
                    }
 
                    #region 调用事件总线通知前端
 
                    var tokenInfos = _cacheService.Get<List<UserInfo>>("Cache_UserToken");
                    if (tokenInfos != null && tokenInfos.Any())
                    {
                        var userTokenIds = tokenInfos?.Select(x => x.Token_ID).ToList();
                        var userIds = tokenInfos?.Select(x => x.UserId).ToList();
                        object obj = new
                        {
                            commonStackerCrane.StackerCraneStatusDes,
                            commonStackerCrane.StackerCraneAutoStatusDes,
                            commonStackerCrane.StackerCraneWorkStatusDes,
                            commonStackerCrane.DeviceCode,
                            commonStackerCrane.DeviceName,
                            commonStackerCrane.CurrentTaskNum,
                            commonStackerCrane.LastTaskNum,
                            SourceAddress = commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.SourceAddress),
                            TargetAddress = commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.TargetAddress),
                            Command = commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.Command),
                            Electricity = commonStackerCrane.GetValue<StackerCraneDBName, float>(StackerCraneDBName.Electricity),
                        };
                    
                        _noticeService.StackerData(userIds?.FirstOrDefault(), userTokenIds, new { commonStackerCrane.DeviceName, data = obj });
                    }
 
                    #endregion 调用事件总线通知前端
                }
            }
            catch (Exception ex)
            {
                //Console.WriteLine(nameof(CommonStackerCraneJob) + ":" + ex.ToString());
            }
            return Task.CompletedTask;
        }
 
        /// <summary>
        /// 任务完成事件订阅的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommonStackerCrane_StackerCraneTaskCompletedEventHandler(object? sender, StackerCraneTaskCompletedEventArgs e)
        {
           CommonStackerCrane? commonStackerCrane = sender as CommonStackerCrane;
            if (commonStackerCrane != null)
            {
 
                Console.Out.WriteLine("TaskCompleted" + e.TaskNum);
                _taskService.StackCraneTaskCompleted(e.TaskNum);
                commonStackerCrane.SetValue(StackerCraneDBName.TaskCompleted, 1);
 
            }
        }
 
        /// <summary>
        /// 获取任务
        /// </summary>
        /// <param name="commonStackerCrane">堆垛机对象</param>
        /// <returns></returns>
        private void GetTask(CommonStackerCrane commonStackerCrane)
        {
            Dt_Task task;
            task = _taskService.QueryStackerCraneTask(commonStackerCrane.DeviceCode);
            if (task != null)
            {
                StackerCraneTaskCommand? stackerCraneTaskCommand = ConvertToStackerCraneTaskCommand(task);
                if (stackerCraneTaskCommand != null)
                {
                    bool sendFlag = commonStackerCrane.SendCommand(stackerCraneTaskCommand);
                    Thread.Sleep(1000);
                    if (ReadReceived(commonStackerCrane) == 1)
                    {
                        WriteConfirm(commonStackerCrane);
                        _taskService.UpdateTaskStatusToNext(task.TaskNum);
                    }
                }
            }
        }
 
 
        //读取AGV状态
        public int ReadAGVstatus(CommonStackerCrane commonStackerCrane)
        {
            var deviceProDTO = commonStackerCrane.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == commonStackerCrane.DeviceCode && x.DeviceProParamName == "AGVCraneStatus");
            return deviceProDTO != null ? commonStackerCrane.Communicator.Read<int>(deviceProDTO.DeviceProAddress) : 99;
        }
        //读取AGV工作模式
        public int ReadAGVworkingmode(CommonStackerCrane commonStackerCrane)
        {
            var deviceProDTO = commonStackerCrane.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == commonStackerCrane.DeviceCode && x.DeviceProParamName == "AGVWorkingmode");
            return deviceProDTO != null ? commonStackerCrane.Communicator.Read<int>(deviceProDTO.DeviceProAddress) : 99;
        }
 
        //读取任务号
        public int Readtasknumber(CommonStackerCrane commonStackerCrane)
        {
            var deviceProDTO = commonStackerCrane.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == commonStackerCrane.DeviceCode && x.DeviceProParamName == "AGVWorkingmode");
            return deviceProDTO != null ? commonStackerCrane.Communicator.Read<int>(deviceProDTO.DeviceProAddress) : 99;
        }
 
        /// <summary>
        /// 读取AGV收到作业信号
        /// </summary>
        /// <param name="commonStackerCrane"></param>
        /// <returns></returns>
        public int ReadReceived(CommonStackerCrane commonStackerCrane)
        {
            return commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.Received);
        }
        public void WriteConfirm(CommonStackerCrane commonStackerCrane)
        {
            commonStackerCrane.SetValue(StackerCraneDBName.WriteConfirm, 1);
        }
 
        /// <summary>
        /// 调度AGV去封边区域
        /// </summary>
        /// <param name="commonStackerCrane"></param>
        /// <param name="value"></param>
        public void AGVToEdgeBanding(CommonStackerCrane commonStackerCrane)
        {
            commonStackerCrane.SetValue(StackerCraneDBName.AreaWrite, true);
        }
 
        /// <summary>
        /// 调度AGV去砂边区域
        /// </summary>
        /// <param name="commonStackerCrane"></param>
        /// <param name="value"></param>
        public void AGVToSandyEdge(CommonStackerCrane commonStackerCrane)
        {
            DeviceProDTO? devicePro = commonStackerCrane.DeviceProDTOs.Where(x => x.DeviceProParamType == "AreaWrite" && x.DeviceChildCode == commonStackerCrane.DeviceCode && x.DeviceProParamName == "AreaWrite").OrderBy(x => x.DeviceProOffset).FirstOrDefault();
 
            if(devicePro == null)
            {
                ConsoleHelper.WriteColorLine($"【未找到配置调度AGV区域协议】", ConsoleColor.Magenta);
                return;
            }
            var writeBool = commonStackerCrane.Communicator.Read<bool>(devicePro.DeviceProAddress+0.1);
            if (!writeBool)
            {
                commonStackerCrane.Communicator.Write(devicePro.DeviceProAddress + 0.1, true);
            }
        }
        /// <summary>
        /// 任务实体转换成命令Model
        /// </summary>
        /// <param name="task">任务实体</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public StackerCraneTaskCommand? ConvertToStackerCraneTaskCommand([NotNull] Dt_Task task)
        {
            var SourceAddress = _stationManagerRepository.QueryFirst(x => x.stationName == task.SourceAddress);
            var TargetAddress = _stationManagerRepository.QueryFirst(x => x.stationName == task.TargetAddress);
 
            if(SourceAddress == null || TargetAddress == null)
            {
                ConsoleHelper.WriteColorLine($"【任务{JsonConvert.SerializeObject(task)}未找到站台配置】", ConsoleColor.Magenta);
                return null;
            }
            StackerCraneTaskCommand stackerCraneTaskCommand = new StackerCraneTaskCommand();
 
            stackerCraneTaskCommand.TaskNum = task.TaskNum;
            stackerCraneTaskCommand.WorkType = 1;
            stackerCraneTaskCommand.TaskType = 0;
            stackerCraneTaskCommand.StartAddress = Convert.ToInt16(SourceAddress.stationCode);
            stackerCraneTaskCommand.EndAddress = Convert.ToInt16(TargetAddress.stationCode);
 
            return stackerCraneTaskCommand;            
        }
    }
}