2
schangxiang@126.com
2024-11-22 76fc40d4f1e0dbd15383c0f3e29fd8bcb6dcff2a
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
using Admin.NET.Application;
using iWareCC.Common.Helper;
using iWareCC.StationService;
using iWareCommon.Common.Globle;
using iWareCommon.Utils;
using iWareModel;
using iWareSql.DBModel;
using iWareSql.WmsDBModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Reflection.Emit;
using System.ServiceModel.Configuration;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using XiGang.Core.Model;
 
namespace iWareCC.ThreadService
{
    /// <summary>
    /// BZ01工位(清灰,洗板后工位)
    /// </summary>
    public static class DataProcess_BZ01
    {
        /// <summary>
        /// BZ01工位(清灰,洗板后工位)
        /// </summary>
        public static async void Handler()
        {
            var alertMsg = "";
            LogType logType = LogType.DataProcess_BZ01;
            while (true)
            {
                Thread.Sleep(2000);//休眠2秒,将休眠写到前面,是为了下面的continue方法执行后不显示错误信息的提示!!!【EditBy shaocx,2022-05-24】
                SystemWarningMsg._lbl_alert_DataProcess_BZ01 = string.Empty;
                try
                {
                    if (SystemValue.isAllowRuning_DataProcess_BZ01 && SystemValue.isStartedModel)
                    {
                        if (FormCC.IsAllowRunOutMode == true)
                        {
                            SystemWarningMsg._lbl_alert_DataProcess_BZ01 = $"正在干出库模式,此方法暂停。。 ";
                            continue;
                        }
 
                        var rgvLocation = StationLocationEnum.BZ01.ToString();
                        /*
                         * 1、从数据库中判断是否齐套
                         * 2、根据齐套结果,推送给PLC
                         */
                        var obj = FormCC.stationView.R_StationForReadCommList.Where(x => x.StationCode == rgvLocation.ToString()).FirstOrDefault();
                        if (obj.R_ReqParseData)
                        {
                            var result = obj.R_ReadCodeResult;
                            if (string.IsNullOrEmpty(result))
                            {
                                SystemWarningMsg._lbl_alert_DataProcess_BZ01 = $"{rgvLocation.ToString()}-请求了解码,但是结果是空的,此次循环结束";
                                continue;
                            }
                            var upiCode = result;
                            var qitaoReault = false;
                            using (WmsDBModel wmsDB = new WmsDBModel())
                            {
                                var upiObj = wmsDB.mes_batchOrderUPI_new.Where(x => x.UPI == upiCode).FirstOrDefault();
                                if (upiObj == null)
                                {
                                    SystemWarningMsg._lbl_alert_DataProcess_BZ01 = $"{rgvLocation.ToString()},根据UPI:{upiCode}没有找到对象 ";
                                    continue;
                                }
 
                                var isAllow = IsAllloRunOutTaskMode(wmsDB, upiObj);
                                if (isAllow)
                                {
                                    SystemWarningMsg._lbl_alert_DataProcess_BZ01 = $"{rgvLocation.ToString()},判断要执行出库模式了,此方法暂停。。 ";
                                    continue;
                                }
 
                                //查询是否齐包
                                var allList = wmsDB.mes_batchOrderUPI_new.Where(x => x.PackageCode == upiObj.PackageCode).ToList();
                                var isQiTaoList = allList.Where(x => x.AreaCode == (int)AreaCodeEnum.待缓存分拣区
                                ||
                                x.AreaCode == (int)AreaCodeEnum.缓存分拣区
                                 ||
                                x.AreaCode == (int)AreaCodeEnum.码垛区域
                                  ||
                                x.AreaCode == (int)AreaCodeEnum.机器人岛缓存区域
                                ).ToList();
                                if (isQiTaoList.Count() != allList.Count())
                                {//不齐包
                                    qitaoReault = false;
                                    upiObj.AreaCode = (int)AreaCodeEnum.缓存分拣区;
                                }
                                else
                                {//齐包
                                    qitaoReault = true;
                                    upiObj.AreaCode = (int)AreaCodeEnum.码垛区域;
                                }
 
                                using (StationServiceClient client = new StationServiceClient())
                                {
                                    var res = await client.WriteQiTaoInfoAsync((int)EDevice.Station, rgvLocation, qitaoReault);
                                    if (!res.result)
                                    {
                                        SystemWarningMsg._lbl_alert_DataProcess_BZ01 = $"{rgvLocation.ToString()}-推送齐套结果失败,WriteQiTaoInfoAsync返回:{res.resMsg}";
                                        continue;
                                    }
                                    else
                                    {
                                        Log4NetHelper.WriteInfoLog(logType, $"{rgvLocation.ToString()}-推送齐套结果成功,WriteQiTaoInfoAsync,参数: 站点{rgvLocation},齐套结果:{qitaoReault}");
                                    }
                                }
 
                                //更新UPI状态
                                var upiObjList = wmsDB.mes_batchOrderUPI_new.Where(x => x.UPI == upiCode).ToList();
                                foreach (var item in upiObjList)
                                {
                                    if (qitaoReault)
                                    {
                                        item.UpiStatus = (int)UpiStatusEnum.已齐包;
                                    }
                                    else
                                    {
                                        item.UpiStatus = (int)UpiStatusEnum.不齐包;
                                    }
                                }
                                //更新 最后一次在 BZ_01的系统处理的板件 数据
                                var rbRunMode = wmsDB.wms_rbline_runmode.FirstOrDefault();
                                rbRunMode.PlanNo = upiObj.PlanNo;
                                rbRunMode.OrderId = upiObj.OrderId;
                                rbRunMode.PackageCode = upiObj.PackageCode;
 
                                wmsDB.SaveChanges();
                            }
                        }
                        else
                        {
                            using (WmsDBModel wmsDB = new WmsDBModel())
                            {
                                var isAllow = IsAllloRunOutTaskMode(wmsDB, null);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    SystemWarningMsg._lbl_alert_DataProcess_BZ01 += " 出现异常:" + ex.Message + SysGloble.SPLIT_STR;
                    Log4NetHelper.WriteErrorLog(logType, " 出现异常:" + ex.Message, ex);
                }
            }
        }
 
 
        /// <summary>
        /// 是否可以执行出库模式
        /// </summary>
        /// <param name="wmsDB"></param>
        /// <param name="curUpiObj"></param>
        /// <returns></returns>
        private static bool IsAllloRunOutTaskMode(WmsDBModel wmsDB, mes_batchOrderUPI_new curUpiObj)
        {
            //首先查询数据库中是否有 要出库的数据
            var isExistOutTaskForNewCreated = wmsDB.wms_rbline_task.Where(x => x.RbTaskType == (int)RbTaskTypeEnum.出库任务
               && x.TaskStatus == (int)TaskStatusEnum.新建
              ).Count();
            if (isExistOutTaskForNewCreated == 0)
            {
                FormCC.IsAllowRunOutMode = false;
                return false;//不能执行 出库模式
            }
 
            var rbRunMode = wmsDB.wms_rbline_runmode.FirstOrDefault();
            var lastUpi = rbRunMode.UPI;
            var lastPackageCode = rbRunMode.PackageCode;
 
            if (curUpiObj == null)
            {//表示此时无新板过来
                if (!string.IsNullOrEmpty(lastUpi))
                {
                    var lastObj = wmsDB.mes_batchOrderUPI_new.Where(x => x.UPI == lastUpi).FirstOrDefault();
                    if (lastObj.UpiStatus == (int)UpiStatusEnum.不齐包 || lastObj.UpiStatus == (int)UpiStatusEnum.初始)
                    {//表示最后过去的板是不齐包的板
                        //可以运行执行 出库模式
                    }
                    else
                    {//表示最后过去的板是齐包的板
                        //要继续判断后面 待分拣区域还存在相同包的板数据吗
                        var isExistSamePackageCode = wmsDB.mes_batchOrderUPI_new.Where(x => x.PackageCode == lastPackageCode
                                && x.AreaCode == (int)AreaCodeEnum.待缓存分拣区).Count();
                        if (isExistSamePackageCode > 0)
                        {
                            FormCC.IsAllowRunOutMode = false;
                            return false;//不能执行 出库模式
                        }
                        else
                        {
                            //可以运行执行 出库模式
                        }
                    }
                }
                else
                {
                    //可以运行执行 出库模式
                }
            }
            else
            {//表示此时有新板过来
                if (!string.IsNullOrEmpty(lastUpi))
                {
                    if (lastPackageCode == curUpiObj.PackageCode)
                    {
                        FormCC.IsAllowRunOutMode = false;
                        return false;//不能执行 出库模式
                    }
                    else
                    {//新包过来了
                        //可以运行执行 出库模式
                    }
                }
                else
                {
                    //可以运行执行 出库模式
                }
            }
 
            FormCC.IsAllowRunOutMode = true;
            return true;
        }
 
    }
}