333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
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
using IWareCC.CacheInfo;
using IWareCC.CacheInfo.Container;
using IWareCC.ORM;
using IWareCC.Properties;
using IWareCC.SRM.Entity;
using IWareCommon.Enum.Common;
using IWareCommon.Enum.Conveyor;
using IWareCommon.Help;
using IWareCommon.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace IWareCC.SRM.Handle.DoStackerTaskAuto
{
    public class 选择一个未执行的堆垛机的任务 : IHandler
    {
        /// <summary>
        /// 该节点的下一个节点
        /// </summary>
        private IHandler nextHandler = null;
        public IHandler NextHandler
        {
            set { nextHandler = value; }
        }
 
        private SrmEntity Stacker;
 
        /// <summary>
        /// 任务容器
        /// </summary>
        private PartTaskContainer DecompositionTaskContainer;
 
 
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="resultContainer">结果容器</param>
        /// <param name="decompositionTaskContainer">WIP任务容器</param>
        public 选择一个未执行的堆垛机的任务(PartTaskContainer decompositionTaskContainer)
        {
 
            this.DecompositionTaskContainer = decompositionTaskContainer;
        }
 
 
        public void Handle()
        {
 
            using (var dbModel = new DbModel())
            {
                try
                {
                    string msg;
                    var task = dbModel.PartTasks.OrderBy(x => x.priority).ThenBy(x => x.updatetime).FirstOrDefault(x => x.eqtype == (int)EPartTaskType.堆垛机任务 && x.isreleased == (int)EYesOrNo.否 && x.isfinished == (int)EYesOrNo.否);
 
                    if (task == null)
                    {
                        // LogTextHelper.WriteLine(Resources.LogDir + @"/自动执行堆垛机任务流程/", "选择一个未执行的堆垛机的任务:{0}", "未找到相应的堆垛机任务");
                        return;
                    }
                    int tdevid = (int)((EDeviceId)task.eqid);
                    var srm = CacheEntity.Srms.FirstOrDefault(x => x.DeviceId == tdevid);
                    Stacker = srm;
                    task.updatetime = DateTime.Now;
                    dbModel.SaveChanges();
 
                    if (!Stacker.CanSendTask(Stacker.RSrmNo, out msg))
                    {
                        LogTextHelper.WriteLine(Resources.LogDir + @"/自动执行堆垛机任务流程/" + Stacker.SrmName, "选择一个未执行的堆垛机的任务:堆垛机{0}不能执行任务--{1}", Stacker.SrmName, msg);
                        if (task.filereason != msg)
                        {
                            task.filereason = msg;
                            dbModel.SaveChanges();
                        }
                        return;
                    }
 
                    #region 判定出库口是否有货
                    string name = "";
                    if (task.maintasktype == (int)EMainTaskType.组盘出库 || task.maintasktype == (int)EMainTaskType.空托出库)
                    {
                        if (task.storageno == (int)EStorageno.大盘库)
                        {
 
                            name = "Line" + "1032";
                        }
                        else
                        {
 
                            name = "Line" + "1030";
                        }
                    }
                    if (task.maintasktype == (int)EMainTaskType.常规出库)
                    {
                        if (task.storageno == (int)EStorageno.大盘库)
                        {
 
                            name = "Line" + "1013";
                        }
                        else
                        {
 
                            name = "Line" + "1015";
                        }
                    }
                    if (!string.IsNullOrEmpty(name))
                    {
                        var conveyor = CacheEntity.Conveyors.FirstOrDefault(x => x.ConveyorName == name);
 
                        if (conveyor == null)
                        {
                            LogTextHelper.WriteLine(Resources.LogDir + @"/自动执行堆垛机任务流程/" + conveyor.ConveyorName, "选择一个未执行的堆垛机的任务:{0}", "PLC通信中断");
                            msg = "PLC通信中断";
                            if (task.filereason != msg)
                            {
                                task.filereason = msg;
                                dbModel.SaveChanges();
                            }
                            return;
                        }
 
                        if (conveyor.RState != (int)EConveyorState.空闲)
                        {
                            LogTextHelper.WriteLine(Resources.LogDir + @"/自动执行堆垛机任务流程/" + conveyor.ConveyorName, "选择一个未执行的堆垛机的任务:{0}", "输送线状态不为空闲");
                            msg = "输送线状态不为空闲";
                            if (task.filereason != msg)
                            {
                                task.filereason = msg;
                                dbModel.SaveChanges();
                            }
                            return;
                        }
 
                        if (!( conveyor.RPh1 == 2 && conveyor.RPh2 == 2 && conveyor.RPh3 == 2))
                        {
                            LogTextHelper.WriteLine(Resources.LogDir + @"/自动执行堆垛机任务流程/" + conveyor.ConveyorName, "选择一个未执行的堆垛机的任务:{0}", "出库口光电检测有货");
                            msg = "出库口光电检测有货";
                            if (task.filereason != msg)
                            {
                                task.filereason = msg;
                                dbModel.SaveChanges();
                            }
                            return;
                        }
 
                    }
                    #endregion
                    var otherdotask = dbModel.PartTasks.OrderBy(x => x.priority).ThenBy(x => x.updatetime).FirstOrDefault(x => x.eqtype == (int)EPartTaskType.堆垛机任务 && x.isreleased == (int)EYesOrNo.是 && x.isfinished == (int)EYesOrNo.否 && x.eqid == Stacker.DeviceId);
                    if (otherdotask != null)
                    {
                        LogTextHelper.WriteLine(Resources.LogDir + @"/自动执行堆垛机任务流程/" + Stacker.SrmName, "选择一个未执行的堆垛机的任务:{0}", "请先确认当前堆垛机任务是否完成");
                        return;
                    }
 
                    DecompositionTaskContainer.DecompositionTask = task;
 
                    LogTextHelper.WriteLine(Resources.LogDir + @"/自动执行堆垛机任务流程/" + Stacker.SrmName, "选择一个未执行的堆垛机的任务:{0}", DecompositionTaskContainer.DecompositionTask.id);
 
                    if (nextHandler != null)
                    {
                        nextHandler.Handle();
                    }
 
                }
                catch (Exception ex)
                {
                    LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "Handle", ex.Message);
                }
            }
 
 
        }
    }
}