1
HuBingJie
2025-11-13 d58196721475e968769d708d9c14f60dd8d5671f
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
using Microsoft.AspNetCore.Routing;
using Quartz;
using SqlSugar;
using System.Threading.Tasks;
using WIDESEA_Comm.Http;
using WIDESEAWCS_Common;
using WIDESEAWCS_Common.Helper;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.BaseRepository;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.ConveyorLine.Enum;
using WIDESEAWCS_QuartzJob.DTO;
using WIDESEAWCS_QuartzJob.Models;
using WIDESEAWCS_QuartzJob.Repository;
using WIDESEAWCS_QuartzJob.Service;
using WIDESEAWCS_Tasks.ConveyorLineJob;
using ICacheService = WIDESEAWCS_Core.Caches.ICacheService;
 
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public class CommonConveyorLineJob : JobBase, IJob
    {
        private readonly ICacheService _cacheService;
        private readonly ITaskService _taskService;
        private readonly ITaskExecuteDetailService _taskExecuteDetailService;
        private readonly IRouterRepository _routerRepository;
        private readonly IRouterService _routerService;
        private readonly IRepository<Dt_Task> _taskRepository;
        private readonly IRepository<Dt_StationManger> _stationMangerRepository;
 
 
        public CommonConveyorLineJob(ICacheService cacheService, ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, IRepository<Dt_StationManger> stationMangerRepository, IRepository<Dt_Task> taskRepository, IRouterRepository routerRepository, IRouterService routerService)
        {
            _cacheService = cacheService;
            _taskService = taskService;
            _taskExecuteDetailService = taskExecuteDetailService;
            _routerRepository = routerRepository;
            _routerService = routerService;
            _stationMangerRepository = stationMangerRepository;
            _taskRepository = taskRepository;
        }
 
        public Task Execute(IJobExecutionContext context)
        {
            bool flag = context.JobDetail.JobDataMap.TryGetValue("JobParams", out object? value);
            if (flag && value != null)
            {
                OtherDevice device = (OtherDevice)value;
                List<string> deviceStations = device.DeviceProDTOs.Select(x => x.DeviceChildCode).Distinct().ToList();//获取设备下的站台
                List<Dt_StationManger> stationMangers = _stationMangerRepository.QueryData(x => x.StationDeviceCode == device.DeviceCode); //获取设备下的站台
                try
                {
                    foreach (var item in stationMangers.Where(x => deviceStations.Contains(x.StationCode))) //循环设备下的站台
                    {
                        DeviceProDTO? deviceProRead = device.DeviceProDTOs.Where(x => x.DeviceChildCode == item.StationCode && x.DeviceProParamType == nameof(R_ConveyorLineDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault(); //输送线读取的全部协议
 
                        DeviceProDTO? deviceProWrite = device.DeviceProDTOs.Where(x => x.DeviceChildCode == item.StationCode && x.DeviceProParamType == nameof(W_ConveyorLineDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault(); //输送线写入的全部协议
 
                        if (deviceProRead != null && deviceProWrite != null) //读取写入的协议判断是否为null
                        {
                            R_ConveyorLineInfo conveyorLineInfoRead = device.Communicator.ReadCustomer<R_ConveyorLineInfo>(deviceProRead.DeviceProAddress); //读取协议
 
                            W_ConveyorLineInfo conveyorLineInfoWrite = device.Communicator.ReadCustomer<W_ConveyorLineInfo>(deviceProWrite.DeviceProAddress); //写入协议
                            if (conveyorLineInfoRead == null || conveyorLineInfoWrite == null)
                            {
                                continue;
                            }
 
                            R_ConveyorLineStatus conveyorLineStatus = conveyorLineInfoRead.Status.ByteToBoolObject<R_ConveyorLineStatus>(); //获取设备状态
 
                            ConveyorLineSignal conveyorLineSignalRead = conveyorLineInfoRead.Signal.ByteToBoolObject<ConveyorLineSignal>(); //获取设备信号
                            bool ACK = device.GetValue<W_ConveyorLineDB, bool>(W_ConveyorLineDB.ACK, item.StationCode); //获取ACK确认信息
 
                            bool STB = device.GetValue<W_ConveyorLineDB, bool>(W_ConveyorLineDB.STB, item.StationCode); //获取STB请求信息
                            //ConveyorLineSignal conveyorLineSignalWrite = conveyorLineInfoWrite.Signal.ByteToBoolObject<ConveyorLineSignal>();
                            //出库站台
                            if (item.StationType == StationTypeEnum.StationType_OnlyOutbound.ObjToInt())  //2007交互口进入
                            {
                                // 出库
                                if (conveyorLineSignalRead.STB && !conveyorLineSignalRead.ACK && conveyorLineStatus.Online && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && !STB && !ACK)//2楼出库
                                {
                                    if (!string.IsNullOrEmpty(conveyorLineInfoRead.Barcode))
                                    {
 
                                        Dt_Task dt_Ta = _taskService.GetOutTaskInfo2(conveyorLineInfoRead.Barcode);
                                        if (dt_Ta != null)
                                        {
                                            device.SetValue(W_ConveyorLineDB.TaskNo, dt_Ta.TaskNum, item.StationCode);
                                            device.SetValue(W_ConveyorLineDB.ACK, true, item.StationCode);
 
 
                                            //删除任务
                                            _taskRepository.DeleteData(dt_Ta);
                                        }
                                    }
                                }
                                else if (!conveyorLineSignalRead.STB && !conveyorLineSignalRead.ACK && conveyorLineStatus.Online && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && !STB && ACK)
                                {
                                    device.SetValue(W_ConveyorLineDB.ACK, false, item.StationCode);
                                }
                            }
                            //出入库站台
                            else if (item.StationType == StationTypeEnum.StationType_InboundAndOutbound.ObjToInt())
                            {
                                //输送线完成修改成输送线完成状态待堆垛机执行任务
                                if (conveyorLineSignalRead.STB && conveyorLineStatus.Online && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && !ACK)
                                {
                                    if (!string.IsNullOrEmpty(conveyorLineInfoRead.Barcode))
                                    {
                                        WebResponseContent contentweb = _taskService.UpdateTaskIninfo(conveyorLineInfoRead.Barcode,item.StationCode); //根据条码跟设备编号去查询并修改任务
                                        if (contentweb.Status)
                                        {
                                            device.SetValue(W_ConveyorLineDB.ACK, true, item.StationCode);
                                            device.SetValue(W_ConveyorLineDB.TaskNo,1, item.StationCode);
                                        }
                                    }
                                }
                                else if (!conveyorLineSignalRead.STB && conveyorLineStatus.Online && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && ACK)
                                {
                                    device.SetValue(W_ConveyorLineDB.ACK, false, item.StationCode);
                                }
                                /*else if (!conveyorLineSignalRead.STB && !conveyorLineSignalRead.ACK && conveyorLineStatus.Online && conveyorLineStatus.Free && !conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && !STB && !ACK && conveyorLineInfoRead.TaskNo == 0)//出库
                                {
                                    Dt_Task task = _taskRepository.QueryFirst(x => x.CurrentAddress == item.StationCode && x.TaskState == TaskStatusEnum.New.ObjToInt() && x.DeviceCode == item.StationDeviceCode && _taskService.TaskOutboundTypes.Contains(x.TaskType));
                                    if (task != null )
                                    {
                                        Dt_Router routers = _routerRepository.QueryFirst(x => x.StartPosi == item.StationCode);
                                        if (routers != null)
                                        {
                                            deviceProRead = device.DeviceProDTOs.Where(x => x.DeviceChildCode == routers.NextPosi && x.DeviceProParamType == nameof(R_ConveyorLineDB)).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
                                            if (deviceProRead != null)
                                            {
                                                R_ConveyorLineInfo conveyorLineInfoRead2 = device.Communicator.ReadCustomer<R_ConveyorLineInfo>(deviceProRead.DeviceProAddress);
 
                                                ConveyorLineSignal conveyorLineSignalRead2 = conveyorLineInfoRead2.Signal.ByteToBoolObject<ConveyorLineSignal>();
 
                                                R_ConveyorLineStatus conveyorLineStatus2 = conveyorLineInfoRead2.Status.ByteToBoolObject<R_ConveyorLineStatus>();
 
                                                if (!conveyorLineSignalRead2.STB && !conveyorLineSignalRead2.ACK && conveyorLineStatus.Online && conveyorLineStatus2.Free && !conveyorLineStatus2.Goods && !conveyorLineStatus2.Alarm && !STB && !ACK)//判断出库口是否空闲
                                                {
                                                    Dt_StationManger? stationManger = stationMangers.FirstOrDefault(x => x.StationCode == item.StationCode);
                                                    if (stationManger == null)
                                                    {
                                                        WriteError(item.StationName, $"未找到对应站台信息,设备编号:{item.StationCode},任务号:{task.TaskNum}");
                                                        continue;
                                                    }
                                                    _taskService.UpdateTask(task, TaskStatusEnum.SC_Execute, nextAddress: routers.NextPosi, targetAddress: routers.NextPosi);
                                                }
                                            }
                                        }
                                    }
                                }*/
                                else if (!conveyorLineSignalRead.STB && !conveyorLineSignalRead.ACK && conveyorLineStatus.Online && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && !STB && !ACK)//其他楼层出库
                                {
                                    
                                    Dt_Task dt_Ta = _taskService.GetOutTaskInfo(item.StationCode);  //获取出库任务
 
                                    if (dt_Ta != null)
                                    {
                                        device.SetValue(W_ConveyorLineDB.TaskNo, dt_Ta.TaskNum, item.StationCode);
                                        device.SetValue(W_ConveyorLineDB.EndPos, dt_Ta.OutboundPlatform, item.StationCode);
                                        device.SetValue(W_ConveyorLineDB.STB, true, item.StationCode);
                                        //堆垛机任务完成下一地址改成输送线下一地址,设备编号改成输送线
                                        _taskService.UpdateTaskStatusToNext(dt_Ta.TaskNum);
 
                                        if (dt_Ta.TargetAddress !="2021" && dt_Ta.TargetAddress != "2020")
                                        {
                                            //删除任务
                                            _taskRepository.DeleteData(dt_Ta);
                                        }
                                        
                                    }
 
                                }
 
                                else if (!conveyorLineSignalRead.STB && conveyorLineSignalRead.ACK && conveyorLineStatus.Online && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && STB && !ACK)
                                {
                                    device.SetValue(W_ConveyorLineDB.STB, false, item.StationCode);
                                }//清楚确认信号
 
                            }
                            //入库站台
                            else if (item.StationType == StationTypeEnum.StationType_OnlyInbound.ObjToInt()) //2014交互口
                            {
                                if (conveyorLineSignalRead.STB && conveyorLineStatus.Online && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && !ACK)
                                {
                                    if (conveyorLineInfoRead.TaskNo == 0 && !string.IsNullOrEmpty(conveyorLineInfoRead.Barcode))//采购入库
                                    {
                                        Dt_Task dt_Ta = _taskService.GetTaskIninfo(conveyorLineInfoRead.Barcode); //通过条码去查询任务
                                        if(dt_Ta != null)
                                        {
                                            device.SetValue(W_ConveyorLineDB.EndPos, dt_Ta.SourceAddress, item.StationCode);
                                            device.SetValue(W_ConveyorLineDB.TaskNo, dt_Ta.TaskNum, item.StationCode);
                                            device.SetValue(W_ConveyorLineDB.ACK, true, item.StationCode);
                                        }
                                       
                                    }
 
 
                                }
                                if (!conveyorLineSignalRead.STB && conveyorLineStatus.Online && conveyorLineStatus.Goods && !conveyorLineStatus.Alarm && ACK)
                                {
                                    device.SetValue(W_ConveyorLineDB.ACK, false, item.StationCode);
                                }
                            }
                            }
                        }
 
                    }
        
                
                catch (Exception ex)
                {
                }
 
 
 
            }
            return Task.CompletedTask;
        }
    }
}