schangxiang@126.com
2025-09-17 ff43ddf18764629ff875478e4e47a7281cbd230a
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
using iWareCommon.Utils;
using iWareModel.Entity.MES;
using iWareSql.MyDbContext;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Linq;
using Admin.NET.Core.TaskModule.Enum;
using System.Configuration;
using iWareModel.Entity.WCS;
using iWareCC.Common.Helper;
using Newtonsoft.Json.Bson;
using System.Data.Entity;
using System.Web;
using iWareModel.Entity.WMS;
using iWareCommon;
using System.Threading.Tasks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TrackBar;
using System.Windows.Forms;
using iWareSql.DataAccess;
using iWareModel.Entity;
 
namespace iWareCC
{
    /// <summary>
    /// 下发任务处理的线程
    /// </summary>
    public class IssueTasHandler
    {
        public static List<CurLaneDataObj> curLaneDataObjs = new List<CurLaneDataObj>() {
         new CurLaneDataObj(){ Lane=1, LastRunTaskCategoryFun=null },
         new CurLaneDataObj(){ Lane=2, LastRunTaskCategoryFun=null}
        };
        public static void Handler()
        {
            var errMsg = "";
            while (true)
            {
                try
                {
                    SystemWarningMsg._lbl_Alert_OutPlanTaskHandler = "循环开始...";
                    if (SystemValue.isAllowRuning_OutTaskIssueTaskHandler && SystemValue.isStartedModel)
                    {
                        errMsg = "";//重置
                        try
                        {
 
                            #region 查询堆垛机是否空闲
                            //var utl = string.Format(WcsIp + @"GetDevicesInfo?Timestamp={0}&Creator={1}", DateTime.Now.ToString(), "cc");
                            //var result1 = HttpHelper.Post(utl, "");
 
                            WCSDevicesOutput data1 = null;
                            if (FormCC.IsSimulationPLC)
                            {//如果是模拟
                                data1 = MyExtendHelper.GetSimulationSrmDevicesOutput();
                                Log4NetHelper.WriteInfoLog(LogType.OutPlanTask, "出库处理的线程(OutPlanTaskHandler)查询堆垛机是否空闲,模拟环境");
                                SystemWarningMsg._lbl_Alert_OutPlanTaskHandler = "堆垛机状态查询模拟";
                            }
                            else
                            {
                                //var utl = string.Format(@"GetDevicesInfo?Timestamp={0}&Creator={1}", DateTime.Now.ToString(), "cc");
                                //IDictionary<string, string> parameter = new Dictionary<string, string>();
                                //var result1 = new HTTPService(FormCC.WcsIp).postContentForString(utl, parameter, "");
 
                                //Log4NetHelper.WriteInfoLog(LogType.OutPlanTask, "出库处理的线程(OutPlanTaskHandler)查询堆垛机是否空闲,返回结果:" + result1);
                                //if (result1 == null)
                                //{
                                //    SystemWarningMsg._lbl_Alert_OutPlanTaskHandler = "堆垛机状态查询失败,WCS返回NULL";
                                //    continue;//继续下一次循环
                                //}
                                //data1 = JsonConvert.DeserializeObject<WCSDevicesOutput>(result1);
                                //if (data1 == null || !data1.Success || data1.Data == null)
                                //{
                                //    SystemWarningMsg._lbl_Alert_OutPlanTaskHandler = "堆垛机状态查询失败,WCS返回结果:" + result1;
                                //    continue;//继续下一次循环
                                //}
                            }
 
                            if (data1.Data != null && data1.Data.SRM1 != null)
                            {
                                _Do(data1.Data.SRM1, 1, ref errMsg);
                            }
                            else
                            {
                                SystemWarningMsg._lbl_Alert_OutPlanTaskHandler += "|| 1号巷道无任务或者1号堆垛机不是空闲状态!";
                            }
 
                            if (data1.Data != null && data1.Data.SRM2 != null)
                            {
                                _Do(data1.Data.SRM2, 2, ref errMsg);
                            }
                            else
                            {
                                SystemWarningMsg._lbl_Alert_OutPlanTaskHandler += "|| 2号巷道无任务或者2号堆垛机不是空闲状态!";
                            }
 
                            #endregion
                            // SystemWarningMsg._lbl_Alert_OutPlanTaskHandler = "循环结束...";
                        }
                        catch (Exception ex)
                        {
                            Log4NetHelper.WriteErrorLog(LogType.OutPlanTask, "处理出入库下发=>" + errMsg + ",异常:" + ex.Message, ex);
                            SystemWarningMsg._lbl_Alert_OutPlanTaskHandler = "处理出入库下发=>" + errMsg + ",异常:" + ex.Message;
                        }
                    }
                }
                catch (Exception ex)
                {
                    SystemWarningMsg._lbl_Alert_OutPlanTaskHandler = "出现异常:" + ex.Message;
                    Log4NetHelper.WriteErrorLog(LogType.OutPlanTask, "OutPlanTaskHandler 出库任务 出现异常:" + ex.Message, ex);
                }
                Thread.Sleep(2 * 1000);//休眠2秒
            }
        }
 
 
        private static void _Do(WDO_Data wDO_Data, int flag, ref string errMsg)
        {
            wms_task model = null;
            if (wDO_Data != null)
            {
                int Lane = int.Parse(wDO_Data.SrmNo);
 
                Handler_Issue(model, Lane, flag, ref errMsg);
              
            }
            else
            {
                SystemWarningMsg._lbl_Alert_OutPlanTaskHandler += "|| " + flag + "号巷道无任务或者" + flag + "号堆垛机不是空闲状态!";
            }
        }
 
        /// <summary>
        /// 任务下发
        /// </summary>
        /// <param name="model"></param>
        /// <param name="Lane"></param>
        /// <param name="flag"></param>
        /// <param name="errMsg"></param>
        private static void Handler_Issue(wms_task model, int Lane, int flag, ref string errMsg)
        {
            model = null;
            using (MyDbContext mycontext1 = new MyDbContext())
            {
                model = mycontext1.wms_task.Where(x => ( x.IsDelete == false) && x.TaskStatus == (int)TaskStatusEnum.新建)
                  
                    .OrderByDescending(x => x.TaskPriority).FirstOrDefault();//查询未WCS下发未完成的任务
                if (model != null)
                {
                    int result = IssueTaskHelper.Put(mycontext1, FormCC.WcsIp, Lane, model, ref errMsg);
                    if (!string.IsNullOrEmpty(errMsg))
                    {
                        SystemWarningMsg._lbl_Alert_OutPlanTaskHandler += "|| " + flag + "巷道下发入库任务失败," + errMsg;
                    }
                }
            }
            //判断如果有错误日志,就更新任务表的消息
            if (model != null && !string.IsNullOrEmpty(errMsg))
            {
                TaskHandler.UpdateTaskMsg(model.Id, errMsg);
            }
        }
 
 
    }
}