huanghongfeng
2025-01-16 77864cf547c716d4dca35e735636013dbc4dc033
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
using Autofac.Core;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.AspNetCore.Hosting;
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
using Quartz;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common.Log;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Enums;
using WIDESEAWCS_ISystemServices;
using WIDESEAWCS_ITaskInfoRepository;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_Model.Models.System;
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_Tasks.ConveyorLineJob;
using WIDESEAWCS_Tasks.StackerCraneJob;
 
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public class SpeStackerCraneJob : IJob
    {
        private readonly ITaskService _taskService;
        private readonly ITaskExecuteDetailService _taskExecuteDetailService;
        private readonly ITaskRepository _taskRepository;
        private readonly IRouterService _routerService;
        private readonly IPlatformStationService _PlatformStationService;
 
 
 
        public SpeStackerCraneJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService, IPlatformStationService platformStation)
        {
            _taskService = taskService;
            _taskExecuteDetailService = taskExecuteDetailService;
            _taskRepository = taskRepository;
            _routerService = routerService;
            _PlatformStationService = platformStation;
        }
 
        public Task Execute(IJobExecutionContext context)
        {
            try
            {
                SpeStackerCrane speStackerCrane = (SpeStackerCrane)context.JobDetail.JobDataMap.Get("JobParams");
                if (speStackerCrane != null)
                {
                    
                    ConveyorRgvCommand command = RgvCommand(speStackerCrane);
                    if (command.Online && !command.Alarm) //判断是否联机与是否报警
                    {
                        if (command.STB && command.TaskNo !=0 && command.TaskFinish && !command.Free) //判断申请,任务不为0,搬货完成,繁忙状态
                        {
                            
                            //写入完成流程
                            CommonStackerCrane_StackerCraneTaskCompletedEventHandler(speStackerCrane, command.TaskNo);
                        }
                        else
                        {
                            if(command.TaskNo == 0 && command.Free)
                            {
                                Dt_Task? task = GetTask(speStackerCrane);
                                if(task != null)
                                {
                                    ConveyorRgvDB conveyorRgv = RgvCommandDB(speStackerCrane);
                                    if (conveyorRgv != null)
                                    {
                                        bool sendFlag = SendCommand(conveyorRgv, speStackerCrane, task);
                                        if (sendFlag)
                                        {
                                            _taskService.UpdateTaskStatusToNext(task.TaskNum);
                                        }
                                    }
                                }
                                else
                                {
                                    wcsWriteLog(speStackerCrane.DeviceCode, "错误信息", $"未找到需要下发的任务");
                                }
                                
                            }
                        }
                    }
                }
 
 
            }
            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(SpeStackerCrane speStackerCrane,int TaskNum)
        {
            wcsWriteLog(speStackerCrane.DeviceCode,"任务完成",$"读取到{speStackerCrane.DeviceCode}号RGV任务完成,任务号:{TaskNum}");
            DeviceProDTO? deviceProDTO6 = speStackerCrane.DeviceProDTOs.Find(x=> x.DeviceProParamName == "ACK" && x.DeviceProParamType == "DeviceCommand");
            
            if (_taskService.StackCraneTaskCompleted(TaskNum).Status)
            {
                if (speStackerCrane.Communicator.Write(deviceProDTO6.DeviceProAddress, (bool)true))
                {
                    wcsWriteLog(speStackerCrane.DeviceCode, "正常信息", $"写入{speStackerCrane.DeviceCode}任务完成交互信号成功,地址:{deviceProDTO6.DeviceProAddress}");
                    DeviceProDTO? deviceProDTO7 = speStackerCrane.DeviceProDTOs.Find(x => x.DeviceProParamName == "STB" && x.DeviceProParamType == "ReadDeviceCommand");
                    if (!speStackerCrane.Communicator.Read<bool>(deviceProDTO7.DeviceProAddress))
                    {
                        speStackerCrane.Communicator.Write(deviceProDTO6.DeviceProAddress, (bool)false);
                        wcsWriteLog(speStackerCrane.DeviceCode, "正常信息", $"清除{speStackerCrane.DeviceCode}任务完成交互信号成功,地址:{deviceProDTO6.DeviceProAddress}");
                    }
                    else
                    {
                        wcsWriteLog(speStackerCrane.DeviceCode, "错误信息", $"未读取到{speStackerCrane.DeviceCode}反馈信息,地址:{deviceProDTO7.DeviceProAddress}");
                    }
                }
                else
                {
                    wcsWriteLog(speStackerCrane.DeviceCode, "错误信息", $"写入{speStackerCrane.DeviceCode}任务完成交互信号失败,{deviceProDTO6.DeviceProAddress}");
                }
            }
            else
            {
                wcsWriteLog(speStackerCrane.DeviceCode, "错误信息", $"任务号:{TaskNum},任务更新失败");
            }
                
        }
 
        /// <summary>
        /// 获取任务
        /// </summary>
        /// <param name="commonStackerCrane">堆垛机对象</param>
        /// <returns></returns>
        private Dt_Task? GetTask(SpeStackerCrane speStackerCrane)
        {
            Dt_Task task;
            List<string> platformsList = _PlatformStationService.GetPlatform3(speStackerCrane.DeviceCode);
            task = _taskService.QueryRgvCraneTask(platformsList);
            return task;
        }
 
 
 
 
        public ConveyorRgvCommand RgvCommand(SpeStackerCrane conveyorRgv)
        {
            ConveyorRgvCommand conveyorRgvCommand = new ConveyorRgvCommand();
            List<DeviceProDTO> deviceProDTO6 = conveyorRgv.DeviceProDTOs
                .Where(x => x.DeviceChildCode == conveyorRgv.DeviceCode
                            && DBLine.RgvDBstaticdata.Contains(x.DeviceProParamName)
                            && x.DeviceProParamType == "ReadDeviceCommand")
                .ToList();
            foreach (var item in deviceProDTO6)
            {
                var paramName = item.DeviceProParamName;
                var propertyInfo = typeof(ConveyorRgvCommand).GetProperty(paramName);
 
                if (propertyInfo != null)
                {
                    if (item.DeviceDataType == "bool")
                    {
                        var value = conveyorRgv.Communicator.Read<bool>(item.DeviceProAddress);
                        propertyInfo.SetValue(conveyorRgvCommand, value);
                    }
                    else if (item.DeviceDataType == "int")
                    {
                        var value = conveyorRgv.Communicator.Read<int>(item.DeviceProAddress);
                        propertyInfo.SetValue(conveyorRgvCommand, value);
                    }
                }
            }
            return conveyorRgvCommand;
        }
 
        public ConveyorRgvDB RgvCommandDB(SpeStackerCrane conveyorRgv)
        {
            ConveyorRgvDB rgvDB = new ConveyorRgvDB();
            List<DeviceProDTO> deviceProDTO6 = conveyorRgv.DeviceProDTOs
                .Where(x => x.DeviceChildCode == conveyorRgv.DeviceCode
                            && DBLine.RgvDBdata.Contains(x.DeviceProParamName)
                            && x.DeviceProParamType == "DeviceCommand")
                .ToList();
            foreach (var item in deviceProDTO6)
            {
                var paramName = item.DeviceProParamName;
                var propertyInfo = typeof(ConveyorRgvDB).GetProperty(paramName);
 
                if (propertyInfo != null)
                {
                    propertyInfo.SetValue(rgvDB, item.DeviceProAddress);
                }
            }
            return rgvDB;
        }
 
        public bool SendCommand(ConveyorRgvDB conveyorRgv, SpeStackerCrane speStackerCrane, [NotNull] Dt_Task task)
        {
            try
            {
                speStackerCrane.Communicator.Write(conveyorRgv.TaskNo, (int)task.TaskNum);
                speStackerCrane.Communicator.Write(conveyorRgv.StartPos, (short)Convert.ToInt16(task.CurrentAddress));
                speStackerCrane.Communicator.Write(conveyorRgv.EndPos, (short)Convert.ToInt16(task.NextAddress));
                speStackerCrane.Communicator.Write(conveyorRgv.WorkType, (short)2);
                speStackerCrane.Communicator.Write(conveyorRgv.GoodsType, (short)1);
 
                if (speStackerCrane.Communicator.Write(conveyorRgv.STB, (bool)true))
                {
                    DeviceProDTO? deviceProDTO6 = speStackerCrane.DeviceProDTOs.Find(x => x.DeviceChildCode == speStackerCrane.DeviceCode
                            && x.DeviceProParamName == "ACK"
                            && x.DeviceProParamType == "ReadDeviceCommand");
                    if (speStackerCrane.Communicator.Read<bool>(deviceProDTO6.DeviceProAddress))
                    {
                        /*speStackerCrane.Communicator.Write(conveyorRgv.TaskNo, (int)0);
                        speStackerCrane.Communicator.Write(conveyorRgv.WorkType, (int)0);
                        speStackerCrane.Communicator.Write(conveyorRgv.WorkType, (int)0);
                        speStackerCrane.Communicator.Write(conveyorRgv.StartPos, (int)0);
                        speStackerCrane.Communicator.Write(conveyorRgv.EndPos, (int)0);*/
                        speStackerCrane.Communicator.Write(conveyorRgv.STB, (bool)false);
                        wcsWriteLog(speStackerCrane.DeviceCode, "正常信息", $"读取到反馈信息,并清除STB,false,地址{deviceProDTO6.DeviceProAddress}");
                        return true;
                    }
                    else
                    {
                        wcsWriteLog(speStackerCrane.DeviceCode, "错误信息", $"未读取到反馈信息ACK,地址{deviceProDTO6.DeviceProAddress}");
                    }
                }
                else
                {
                    wcsWriteLog(speStackerCrane.DeviceCode, "错误信息", $"下发RGV任务失败,写入STB交互失败,地址{conveyorRgv.STB}");
                }
                return false;
            }
            catch (Exception ex)
            {
                wcsWriteLog(speStackerCrane.DeviceCode, "错误信息", $"下发RGV任务失败,错误信息:{ex.Message}");
                return false;
            }
        }
 
 
        public void wcsWriteLog(string SCLLinStack, string Logtype, string Magessadd)
        {
            WriteLog.Write_Log("RGV", SCLLinStack + "号RGV", Logtype, new { 信息 = Magessadd });
        }
    }
}