zhanghonglin
2026-03-21 beb0b5d9e1bab1ace04b5860b5ca308b6f913d4f
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
264
265
266
267
268
269
270
271
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autofac.Core;
using HslCommunication.WebSocket;
using Microsoft.AspNetCore.Components;
using Quartz;
using SqlSugar;
using WIDESEA_Model.Models.Basic;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core.BaseRepository;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_DTO.MES;
using WIDESEAWCS_DTO.Stock;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models.System;
using WIDESEAWCS_Model.Models.TaskInfo;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.DTO;
using WIDESEAWCS_Tasks.Command;
using WIDESEAWCS_Tasks.DBname;
using WIDESEAWCS_Tasks.DBName;
using static Dm.net.buffer.ByteArrayBuffer;
 
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public class ConveyorLineJob : JobBase, IJob
    {
 
        private readonly IRepository<Dt_StationManger> _stationMangerRepository;
        private readonly IRepository<Dt_Task> _TaskRepository;
        private readonly IRepository<Dt_FillingOrder> _FillingOrderRepository;
        private readonly ITaskService _ITaskService;
        private static Stock Stock = new Stock();
        private static bool k = true;
        private static int i = 0;
 
        //判断是否把出库信号发给WMS
        private static bool m = true;
        private static bool n = true;
 
        //存储信号
        private readonly Commands comm = new Commands();
 
        public ConveyorLineJob(WebSocketServer webSocketServer, IRepository<Dt_StationManger> stationMangerRepository, IRepository<Dt_Task> TaskRepository, IRepository<Dt_FillingOrder> FillingOrderRepository, ITaskService ITaskService)
        {
            _stationMangerRepository = stationMangerRepository;
            _TaskRepository = TaskRepository;
            _ITaskService = ITaskService;
            _FillingOrderRepository = FillingOrderRepository;
        }
 
        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);
                //具体设备
                foreach (var item in stationMangers.Where(x => deviceStations.Contains(x.StationCode)))
                {
                    i++;
                    //读取信号
                    DeviceProDTO? deviceProRead = device.DeviceProDTOs.Where(x => x.DeviceChildCode == item.StationCode).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
                    //DeviceProDTO? deviceProWrite = device.DeviceProDTOs.Where(x => x.DeviceChildCode == item.StationCode ).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
                    if (deviceProRead != null)
                    {
                        ConveyorLineCommand conveyorLineInfoRead = device.Communicator.ReadCustomer<ConveyorLineCommand>(deviceProRead.DeviceProAddress);
                        //空桶入库任务
                        if (item.StationName == "空桶入")
                        {
                            comm.R_StatusA((short)conveyorLineInfoRead.R_State);
                            comm.R_PickA((short)conveyorLineInfoRead.R_Pick);
                            if (conveyorLineInfoRead.R_Pick == 1 && conveyorLineInfoRead.R_State == 1)
                            {
                                try
                                {
                                    //判断任务是否重复
                                    Dt_Task task = _TaskRepository.QueryFirst(x => x.TaskType == (int)TaskTypeEnum.InEmpty);
                                    if (task == null)
                                    {
                                        //WMS生成任务
                                        string address = AppSettings.Get("WMSApiAddress");
                                        if (!string.IsNullOrEmpty(address))
                                        {
                                            HttpHelper.Post($"{address}/api/Task/addInTask/?location={item.location}", "");
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    //写入日志
                                    WriteInfo(device.DeviceCode, $"{ex.Message}");
                                }
                            }
                        }
                        //满桶入库任务
                        if (item.StationName == "满桶入")
                        {
                            comm.R_StatusC((short)conveyorLineInfoRead.R_State);
                            comm.R_PickC((short)conveyorLineInfoRead.R_Pick);
                            if (conveyorLineInfoRead.R_Pick == 1 && conveyorLineInfoRead.R_State == 1)
                            {
                                try
                                {
                                    //清空物料编号
                                    if (k)
                                    {
                                        Stock.MaterialCode1 = "";
                                        Stock.MaterialCode2 = "";
                                        Stock.MaterialCode3 = "";
                                        Stock.MaterialCode4 = "";
                                        k = false;
                                    }
                                    //存储物料编号
                                    Stock.MaterialCode1 = conveyorLineInfoRead.R_NumberA;
                                    Stock.MaterialCode2 = conveyorLineInfoRead.R_NumberB;
                                    Stock.MaterialCode3 = conveyorLineInfoRead.R_NumberC;
                                    Stock.MaterialCode4 = conveyorLineInfoRead.R_NumberD;
 
                                    //判断任务是否重复
                                    Dt_Task task = _TaskRepository.QueryFirst(x => x.TaskType == (int)TaskTypeEnum.Infull);
                                    if (task == null)
                                    {
                                        Stock.location = item.location;
                                        //WMS生成任务
                                        string address = AppSettings.Get("WMSApiAddress");
                                        if (!string.IsNullOrEmpty(address))
                                        {
                                            HttpHelper.Post($"{address}/api/Task/addOutTask/", Stock.Serialize());
 
                                            //发送收到编号信号
                                            device.SetValue(ConveyorLineName.W_ReceivedD, 1, item.StationCode);
                                            k = true;
 
                                            //向MES发送报工单
                                            if (Stock.MaterialCode1 != null && !Stock.MaterialCode1.Equals(""))
                                            {
                                                MESReport(Stock.MaterialCode1);
                                            }
                                            if (Stock.MaterialCode2 != null && !Stock.MaterialCode2.Equals(""))
                                            {
                                                MESReport(Stock.MaterialCode2);
                                            }
                                            if (Stock.MaterialCode3 != null && !Stock.MaterialCode3.Equals(""))
                                            {
                                                MESReport(Stock.MaterialCode3);
                                            }
                                            if (Stock.MaterialCode4 != null && !Stock.MaterialCode4.Equals(""))
                                            {
                                                MESReport(Stock.MaterialCode4);
                                            }
                                        }
 
                                    }
                                }
                                catch (Exception ex)
                                {
                                    //写入日志
                                    WriteInfo(device.DeviceCode, $"{ex.Message}");
                                }
                            }
                        }
                        //空桶出
                        if (item.StationName == "空桶出")
                        {
                            comm.R_StatusB((short)conveyorLineInfoRead.R_State);
                            comm.R_PickB((short)conveyorLineInfoRead.R_Pick);
                            if (conveyorLineInfoRead.R_Pick == 1 && conveyorLineInfoRead.R_State == 1)
                            {
                                try
                                {
                                    //判断任务是否重复
                                    Dt_Task task = _TaskRepository.QueryFirst(x => x.TaskType == (int)TaskTypeEnum.OutEmpty);
                                    if (task == null)
                                    {
                                        //WMS生成任务
                                        string address = AppSettings.Get("WMSApiAddress");
                                        if (!string.IsNullOrEmpty(address))
                                        {
                                            HttpHelper.Post($"{address}/api/Task/addOutEmptyTask/?location={item.location}", "");
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    //写入日志
                                    WriteInfo(device.DeviceCode, $"{ex.Message}");
                                }
                            }
                        }
                        //满桶出
                        if (item.StationName == "满桶出")
                        {
                            comm.R_StatusD((short)conveyorLineInfoRead.R_State);
                            comm.R_PickD((short)conveyorLineInfoRead.R_Pick);
                            //告诉WMS可以出库
                            if (conveyorLineInfoRead.R_Pick == 1 && conveyorLineInfoRead.R_State == 1)
                            {
                                if (m)
                                {
                                    //WMS生成任务
                                    string address = AppSettings.Get("WMSApiAddress");
                                    if (!string.IsNullOrEmpty(address))
                                    {
                                        HttpHelper.Post($"{address}/api/Task/WCSSignal/?k=1", "");
                                    }
                                    m = false;
                                    n = true;
                                }
                            }
                            //告诉WMS不可以出库
                            else
                            {
                                if (n)
                                {
                                    //WMS生成任务
                                    string address = AppSettings.Get("WMSApiAddress");
                                    if (!string.IsNullOrEmpty(address))
                                    {
                                        HttpHelper.Post($"{address}/api/Task/WCSSignal/?k=0", "");
                                    }
                                    n = false;
                                    m = true;
                                }
                            }
                        }
                    }
                }
                i = 0;
            }
            return Task.CompletedTask;
        }
 
        //MES报工
        public void MESReport(string MaterialCode)
        {
            try
            {
                Reports reports = new Reports();
 
                //查询二维码
                Dt_FillingOrder FillingOrder = _FillingOrderRepository.QueryFirst(x => x.BarCode == MaterialCode);
 
                reports.report.Qty = FillingOrder.BarNum;
                reports.report.DispatchID = FillingOrder.WorkID + "";
                reports.barcodeSN[0].Barcode = FillingOrder.BarCode;
                reports.barcodeSN[0].BatchNum = int.Parse(FillingOrder.batchNum);
                reports.barcodeSN[0].Id = FillingOrder.BarCodeID;
                reports.barcodeSN[0].MaterialId = FillingOrder.ArticleNumID;
                reports.barcodeSN[0].Quantity = (int)FillingOrder.BarNum;
                reports.barcodeSN[0].DispatchId = FillingOrder.WorkID;
 
                string mes = AppSettings.Get("WMSApiAddress");
                if (!string.IsNullOrEmpty(mes))
                {
                    HttpHelper.Post($"{mes}/Task/SaveReportRaw/", reports.Serialize());
                }
            }
            catch (Exception ex) {
                WriteInfo( "MES报工错误", $"{ex.Message}");
            }
        }
    }
}