qinchulong
2025-03-29 039a4a5433e7f80adc88b491b549e5d9486e4f9a
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
272
273
274
275
276
277
278
279
280
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using WIDESEA_Common;
using WIDESEA_Common.Tools;
using WIDESEA_Core.EFDbContext;
using WIDESEA_Core.Utilities;
using WIDESEA_Entity.DomainModels;
using WIDESEA_Services;
using WIDESEA_Services.IRepositories;
using WIDESEA_Services.Repositories;
using WIDESEA_Services.Services;
 
namespace WIDESEA_WCS.Jobs
{
    public partial class EquipmentExecutor : SchedulerExecuteBase
    {
        private static int _readJRSignalso2 = 0;
 
        /// <summary>
        /// 格林晟卷绕机请求上料
        /// </summary>
        public static void JRSB_UpTask_New()
        {
            if (Interlocked.Exchange(ref _readJRSignalso2, 1) == 0)
            {
                try
                {
                    List<string> JRSBS = new List<string> { "格林晟1号卷绕机", "格林晟2号卷绕机", "格林晟3号卷绕机", "格林晟4号卷绕机", "格林晟5号卷绕机", "格林晟6号卷绕机", "格林晟7号卷绕机", "格林晟8号卷绕机", "格林晟9号卷绕机", "格林晟10号卷绕机" };
                    JRSB_UpTask_Run(JRSBS, "FJ");
                    JRSB_UpTask_Run(JRSBS, "ZJ");
                }
                catch (Exception ex)
                {
                    WriteLog.Info("JRSB_UpTask").Write("JRSB_UpTask" + ex.Message, "JRSB_UpTask");
                }
                finally
                {
                    Interlocked.Exchange(ref _readJRSignalso2, 0);
                }
            }
        }
 
        /// <summary>
        /// 运行方法
        /// </summary>
        /// <param name="JRSBS">卷绕设备集合</param>
        /// <param name="ZFType">正极或负极</param>
        private static void JRSB_UpTask_Run(List<string> JRSBS, string ZFType)
        {
            VOLContext Context = new VOLContext();
            IJROutBindRepository jrRepository = new JROutBindRepository(Context);
            Idt_agvtaskRepository agvRepository = new dt_agvtaskRepository(Context);
 
            //卷绕机任务
            var jrTaskList = agvRepository.Find(v => v.agv_toaddress.StartsWith(ZFType) && v.agv_toaddress.Contains("JR")).OrderBy(t => t.agv_createtime).ToList();
            bool needCallMES = jrTaskList.Count() == 0;
 
            //MES到达信息
            string materiel = needCallMES ? "" : jrTaskList[0].agv_materielid;
            JROutBind bind = jrRepository.Find(f => f.sum > 0 && f.materialtype == materiel && f.barcode.Substring(0, 2)/*正负极标识*/ == ZFType).FirstOrDefault();
            if (!needCallMES)
            {
                WriteLog.Info($"卷绕机上料请求").Write($"{DateTime.Now}--{jrTaskList[0].agv_tasknum},请求上料\n", $"卷绕机上料请求");
            }
            
           
            try
            {
                #region 遍历设备请求
                for (int i = 0; i < JRSBS.Count; i++)
                {
                    string eqName = JRSBS[i];
                    var indexRange = ZFType == "FJ" ? new int[] { 1, 2 } : new int[] { 3, 4 };
                    //1、2负极口。3、4正极口
                    for (int stationIndex = indexRange[0]; stationIndex <= indexRange[1]; stationIndex++)
                    {
                        var plcClient = WCSService.Clients.Find(v => v.PLCName == eqName);
                        if (plcClient != null)
                        {
                            //请求上料
                            var requestIn = bool.Parse(plcClient.ReadValue($"R_GLSJRSB{stationIndex}_UPrequest", eqName).ToString());
                            if (requestIn)
                            {
                                //工单号
                                string batchNo = plcClient.ReadValue(ConveyorLineInfoDBName.R_JRSB_UPbatchNo.ToString(), eqName).ToString();
                                WriteLog.Info($"卷绕机上料请求").Write($"{DateTime.Now}--{eqName},{stationIndex}口,请求上料,工单号:{batchNo}\n", $"卷绕机上料请求");
                                if (string.IsNullOrEmpty(batchNo)) continue;
 
                                string stationNum = (stationIndex + i * 4).ToString().PadLeft(3, '0');
                                //1、2口负极。3、4口正极
                                string stationAddress = stationIndex <= 2 ? $"FJSL-JRSB{stationNum}-B" : $"ZJSL-JRSB{stationNum}-B";
 
                                if (needCallMES)
                                {
                                    AddWaitTask(batchNo, stationAddress);
                                    //只创建一个待下发任务
                                    return;
                                }
                                else if (!needCallMES && batchNo == materiel)
                                {
                                    if (bind == null) return; //MES未送达,直接退出
                                    if (bind.useNum < bind.sum)
                                    {
                                        //MES送达
                                        UpdateWaitTask(bind, ZFType, stationAddress);
                                        
                                    }
                                }
                            }
                        }
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
                WriteLog.Info($"卷绕机上料请求").Write($"{DateTime.Now}--error,{msg}\n\n", $"卷绕机上料请求");
            }
 
            if (bind != null)
            {
                //更新该任务是最后取料的标识
                var lastTsak = agvRepository.Find(t => t.agv_tasknum == bind.taskid).FirstOrDefault();
                if (lastTsak != null)
                {
                    lastTsak.agv_remark = "true";
                    agvRepository.Update(lastTsak, true);
                }
 
                //复位绑定数据
                bind.sum = 0;
                bind.barcode = "";
                bind.materialtype = "";
                bind.taskid = "";
                bind.barcodes = "";
                bind.size = "-1";
                jrRepository.Update(bind, true);
            }
        }
 
        /// <summary>
        /// 创建和更新任务状态
        /// </summary>
        /// <param name="bind">MES到位信息</param>
        /// <param name="ZFType">正极或负极</param>
        /// <param name="batchNo">工单号</param>
        /// <param name="stationAddress">入料口地址</param>
        private static void UpdateWaitTask(JROutBind bind, string ZFType, string stationAddress)
        {
            VOLContext Context = new VOLContext();
            Idt_agvtaskRepository agvRepository = new dt_agvtaskRepository(Context);
            Idt_task_numberRepository tasknumberRep = new dt_task_numberRepository(Context);
            dt_task_numberService tasknumber = new dt_task_numberService(tasknumberRep);
 
            int useNum = bind.useNum;//当前取的第几个
 
            //等待下发的那条
            var wiatTask = agvRepository.Find(v => v.agv_toaddress.StartsWith(ZFType) && v.agv_toaddress.Contains("JR") && v.agv_taskstate == "WaitStockOut").OrderBy(t => t.agv_createtime).FirstOrDefault();
            if (wiatTask != null)
            {
                wiatTask.agv_fromaddress = bind.Devid;
                wiatTask.agv_taskstate = "Create";
                wiatTask.agv_materbarcode = GetBarcode(bind.barcodes, useNum);
                wiatTask.agv_materielid = bind.materialtype;
                wiatTask.size = bind.size;
                wiatTask.JRunm = (bind.sum - useNum) + ";" + bind.size;//剩余卷数;料卷宽度
                agvRepository.Update(wiatTask, true);
 
                bind.taskid = wiatTask.agv_tasknum;
                bind.useNum++;
            }
            else
            {
                //直接添加
                var haveTask = agvRepository.FindFirst(f => f.agv_toaddress == stationAddress);
                if (haveTask == null)
                {
                    dt_agvtask agvtask = new dt_agvtask();
                    agvtask.agv_fromaddress = bind.Devid;
                    agvtask.agv_toaddress = stationAddress;
                    agvtask.agv_taskstate = "Create";
                    agvtask.agv_barcode = "";
                    agvtask.agv_code = "负极2号AGV";
                    agvtask.agv_createtime = DateTime.Now;
                    agvtask.agv_grade = 1;
                    agvtask.agv_materbarcode = GetBarcode(bind.barcodes, useNum);
                    agvtask.agv_qty = 1;
                    agvtask.agv_tasknum = "KH-" + tasknumber.GetTaskNumber(tasknumberRep);
                    agvtask.agv_tasktype = "TaskType_Outbound";
                    agvtask.agv_userid = "WCS";
                    agvtask.agv_worktype = 1;
                    agvtask.agv_materielid = bind.materialtype;
                    agvtask.size = bind.size;
                    if (useNum > 0)
                    {
                        agvtask.JRunm = (bind.sum - useNum) + ";" + bind.size;//剩余卷数;料卷宽度
                    }
                    agvRepository.Add(agvtask, true);
 
                    bind.taskid = agvtask.agv_tasknum;
                    bind.useNum++;
                }
            }
            WriteLog.Info($"卷绕叫料MES到位").Write($"{DateTime.Now}--任务号:{bind.taskid}\t工单:{bind.materialtype}\tuseNum:{useNum}\t{wiatTask == null}\n\n", $"卷绕叫料MES到位");
        }
 
        /// <summary>
        /// 添加卷绕机待下发任务
        /// </summary>
        /// <param name="batchNo">工单号</param>
        /// <param name="stationAddress">设备口地址</param>
        /// <param name="stationIndex">设备口1-4</param>
        private static void AddWaitTask(string batchNo, string stationAddress)
        {
            VOLContext Context = new VOLContext();
            WebResponseContent responseContent = new WebResponseContent();
            Idt_task_numberRepository tasknumberRep = new dt_task_numberRepository(Context);
            dt_task_numberService tasknumber = new dt_task_numberService(tasknumberRep);
            Idt_agvtaskRepository agvRepository = new dt_agvtaskRepository(Context);
            IJROutBindRepository jrRepository = new JROutBindRepository(Context);
 
            var haveTask = agvRepository.FindFirst(f => f.agv_toaddress == stationAddress);
            if (haveTask == null)
            {
                dt_agvtask agvtask = new dt_agvtask();
                agvtask.agv_toaddress = stationAddress;
                agvtask.agv_barcode = "";
                agvtask.agv_code = "负极2号AGV";
                agvtask.agv_createtime = DateTime.Now;
                agvtask.agv_fromaddress = "nou";
                agvtask.agv_grade = 1;
                agvtask.agv_materbarcode = "daiding";
                agvtask.agv_qty = 1;
                agvtask.agv_tasknum = "KH-" + tasknumber.GetTaskNumber(tasknumberRep);
                agvtask.agv_taskstate = "WaitStockOut";
                agvtask.agv_tasktype = "TaskType_Outbound";
                agvtask.agv_userid = "WCS";
                agvtask.agv_worktype = 1;
                agvtask.agv_materielid = batchNo;
 
                //请求MES送料
                List<string> StockList = new List<string> { "FJXL-FBT001", "FJXL-FBT002" };
                MESback WMSbackresult = MESAPIInvoke.OutNeedStosk(agvtask.agv_tasknum, StockList, 4, agvtask.agv_materielid, "", agvtask.agv_toaddress);
                WriteLog.Info("请求MES送料111").Write($"{agvtask.agv_tasknum}请求送料,MES返回数据:{JsonConvert.SerializeObject(WMSbackresult)},时间:" + DateTime.Now + "   ----------\n", $"请求MES送料111");
                if (WMSbackresult?.Code == 0)
                {
                    agvRepository.Add(agvtask, true);
                }
            }
        }
 
        /// <summary>
        /// 获取物料号
        /// </summary>
        /// <param name="barcode"></param>
        /// <param name="useNum"></param>
        /// <returns></returns>
        private static string GetBarcode(string barcode, int useNum)
        {
            if (string.IsNullOrEmpty(barcode))
            {
                return "";
            }
            var barcodes = barcode.Split(',');
            if (barcodes.Length - 1 >= useNum)
            {
                return barcodes[useNum];
            }
            else
            {
                return "index_out";
            }
        }
    }
}