Admin
3 天以前 bd6818fc9d40f343547bafca0743658f3c0379dc
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
using HslCommunication;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using WIDESEA_Common;
using WIDESEA_Common.Tools;
using WIDESEA_Core.Utilities;
using WIDESEA_Entity.DomainModels;
using WIDESEA_Services;
using WIDESEA_Services.IRepositories;
using WIDESEA_Services.Repositories;
using WIDESEA_Services.Services;
using WIDESEA_WCS.WCSClient;
 
namespace WIDESEA_WCS.Jobs.ConveyorLine.InboundArea
{
    public partial class InboundAreaDispatch
    {
        private static string InboundRequestStationNo = "20101";
 
        public static void InboundRequestStationAction(IDt_TaskWCSinfoRepository taskWCSinfoRepository, PLCClient client, string lineCode)
        {
            //读取设备正常
            try
            {
                //string lineCode = "InBound";
                string normalResult = client.ReadValue(CLineInfoDBName.R_Line_DeviceNormal.ToString(), InboundRequestStationNo).ToString();
                if (bool.Parse(normalResult))
                {
                    ComplateOutboundTask(taskWCSinfoRepository, client);
                    //读取逻辑控制值
                    int logicValue = int.Parse(client.ReadValue(CLineInfoDBName.R_Line_Logic.ToString(), lineCode).ToString());
                    //读取RFID值  暂时没启用,取当前待执行的任务
                    string rfidResult = client.ReadValue(CLineInfoDBName.W_Line_Barcode.ToString(), lineCode).ToString();
                    if (string.IsNullOrEmpty(rfidResult) || "0".Equals(rfidResult))
                        rfidResult = client.ReadValue(CLineInfoDBName.R_Line_Barcode.ToString(), InboundRequestStationNo).ToString();
 
                    Dt_TaskWCSinfo wcsInfo = null;
                    if (!string.IsNullOrEmpty(rfidResult) && !"0".Equals(rfidResult))
                        wcsInfo = taskWCSinfoRepository.FindFirst(r => r.wcstask_barcode == rfidResult);
                    else
                    {
                        wcsInfo = taskWCSinfoRepository.FindFirst(r => r.wcstask_startPoint == "20101" && r.wcstask_state == TaskState.TaskState_Assigned.ToString());
                        if (null == wcsInfo)
                            return;
 
                        if (wcsInfo.wcstask_type != TaskType.TaskType_Box_Pallet_Inbound.ToString() &&
                            wcsInfo.wcstask_type != TaskType.TaskType_Empty_Pallet_Inbound.ToString() &&
                            wcsInfo.wcstask_type != TaskType.TaskType_ErrorCheckBackIn.ToString() &&
                            wcsInfo.wcstask_type != TaskType.TaskType_CheckBackIn.ToString()
                            )
                        {
                            return;
                        }
                    }
 
 
                    //线体人工触发的确认信号
                    string confirmResult = client.ReadValue(CLineInfoDBName.R_Line_Confirm_OK.ToString(), InboundRequestStationNo).ToString();
                    if (null == wcsInfo)
                        return;
 
                    if ((logicValue == 1 || logicValue == 2) && bool.Parse(confirmResult))
                    {
                        //这里需要考虑该条任务的入库站台是否 ==> 已经开始了相对应站台的出库测量任务
                        List<Dt_TaskWCSinfo> tmpList = taskWCSinfoRepository.Find(x =>
                        x.wcstask_type == TaskType.TaskType_Box_Pallet_Measure_Out.ToString() &&
                        x.wcstask_endLocation == wcsInfo.wcstask_endPoint &&
                        (x.wcstask_state == TaskState.TaskState_RGV_Received.ToString() || x.wcstask_state == TaskState.TaskState_RGV_Finished.ToString()));
                        if (tmpList.Count != 0)
                        {
                            WriteLog.Info(InboundRequestStationNo).Write($"目的站台{wcsInfo.wcstask_endPoint}存在测量出库的任务,暂时调度", InboundRequestStationNo);
                            return;
                        }
                        WriteTaskInfo.WriteTaskInfoAction(wcsInfo, client, lineCode);
                    }
                    string barcode = client.ReadValue(CLineInfoDBName.W_Line_Barcode.ToString(), lineCode).ToString();
                    if (!wcsInfo.wcstask_barcode.Equals(barcode))
                        return;
                    logicValue = int.Parse(client.ReadValue(CLineInfoDBName.R_Line_Logic.ToString(), lineCode).ToString());
                    //说明站台已经收到任务数据,等待WCS的启动指令
                    if (logicValue == 3)    //读取输送线3,表示
                    {
                        //启动任务
                        client.WriteValue(CLineInfoDBName.W_Line_Logic.ToString(), lineCode, 1);
                    }
                    else if (logicValue == 4 || logicValue == 5)
                    {
                        if (wcsInfo.wcstask_state == TaskState.TaskState_ConveyorLineExecuting.ToString())
                            return;
 
                        wcsInfo.wcstask_state = TaskState.TaskState_ConveyorLineExecuting.ToString();
                        wcsInfo.wcstask_dispatcherTime = DateTime.Now;
                        taskWCSinfoRepository.Update(wcsInfo, true);
 
                        //入空托的时候,默认是托盘号核验一致
                        //bool checkFlag = client.WriteValue(CLineInfoDBName.W_Line_RFID_Done.ToString(), InboundRequestStationNo, true);
                        //读取RFID值,防止在logicValue == 3时更新任务状态失败
                        //是因为出现过写入启动信号,返回值是false,但是线体确又已经收到了启动信号,所以打了这个补丁
                        string str = string.Empty;
                        //启动成功后,在此更新任务状态、上报WMS任务状态
                        WebResponseContent content = WMSApi.PostTaskStateToWMS(wcsInfo.wcstask_barcode, TaskState.TaskState_ConveyorLineExecuting.ToString());
                        if (content.Status)
                            str = $" { DateTime.Now }上报WMS更新任务状态成功【TaskState_ConveyorLineExecuting】,托盘号:{wcsInfo.wcstask_barcode},任务号:{ wcsInfo.wcstask_taskNumber}";
                        else
                            str = $" { DateTime.Now }上报WMS更新任务状态失败【TaskState_ConveyorLineExecuting】,托盘号:{wcsInfo.wcstask_barcode},任务号:{ wcsInfo.wcstask_taskNumber},原因:{content.Message}";
                        WriteLog.Info(InboundRequestStationNo).Write(str, InboundRequestStationNo);
                    }
                }
 
            }
            catch (Exception ex)
            {
                WriteLog.Info(InboundRequestStationNo).Write($"{ DateTime.Now }入库申请站台调度失败:{ex.Message}", InboundRequestStationNo);
            }
        }
        static string[] taskTypes = new string[] { TaskState.TaskState_Empty_Out_Line_Executing.ToString(), TaskState.TaskState_Box_Out_Line_Executing.ToString() };
        private static void ComplateOutboundTask(IDt_TaskWCSinfoRepository taskWCSinfoRepository, PLCClient client)
        {
            try
            {
                //此处校验空托出库的任务在库存中的托盘码和工位RFID读取到的托盘码是否一致
                int taskNumber = int.Parse(client.ReadValue(CLineInfoDBName.R_Line_TaskNumber.ToString(), InboundRequestStationNo).ToString());
                Dt_TaskWCSinfo emptyOutWcsInfo = null;
                if (taskNumber > 0)
                    emptyOutWcsInfo = taskWCSinfoRepository.FindFirst(x => x.wcstask_taskNumber == taskNumber && taskTypes.Contains(x.wcstask_state));
                else
                {
                    string barcode = client.ReadValue(CLineInfoDBName.R_Line_RFID_OK.ToString(), InboundRequestStationNo).ToString();
                    emptyOutWcsInfo = taskWCSinfoRepository.FindFirst(x => x.wcstask_barcode == barcode && taskTypes.Contains(x.wcstask_state));
                }
 
                if (null != emptyOutWcsInfo)
                {
                    if (emptyOutWcsInfo.wcstask_type.Contains(TaskType.TaskType_Empty_Pallet_Outbound.ToString()) || emptyOutWcsInfo.wcstask_type
                        .Contains(TaskType.TaskType_Box_Pallet_Outbound.ToString()) || emptyOutWcsInfo.wcstask_type.Contains(TaskType.TaskType_CheckOutbound.ToString()))
                    {
                        //在此完成该空托出库任务
                        WebResponseContent content = taskWCSinfoRepository.DbContextBeginTransaction(() =>
                        {
                            //上报WMS任务完成
                            content = WMSApi.TellWmsTaskFinished(emptyOutWcsInfo.wcstask_barcode);
                            if (content.Status)
                            {
                                //移动任务到历史表
                                Dt_TaskWCSinfo_HtyRepository taskWCSinfo_HtyRepository = new Dt_TaskWCSinfo_HtyRepository(taskWCSinfoRepository.DbContext);
                                emptyOutWcsInfo.wcstask_state = TaskState.TaskState_Finished.ToString();
                                CommonFunction.AddWcsTaskToHistory(emptyOutWcsInfo, taskWCSinfoRepository, taskWCSinfo_HtyRepository);
                            }
                            else
                                throw new Exception($"{ DateTime.Now }上报WMS任务完成出错,原因:【{content.Message}】");
 
                            return content;
                        });
                        string str = string.Empty;
                        if (content.Status)
                            str = $" { DateTime.Now }上报WMS出库完成成功【TaskState_ConveyorLineExecuting】,托盘号:{emptyOutWcsInfo.wcstask_barcode},任务号:{ emptyOutWcsInfo.wcstask_taskNumber}";
                        else
                            str = $" { DateTime.Now }上报WMS出库完成失败【TaskState_ConveyorLineExecuting】,托盘号:{emptyOutWcsInfo.wcstask_barcode},任务号:{ emptyOutWcsInfo.wcstask_taskNumber}";
 
                        WriteLog.Info(InboundRequestStationNo).Write(str, InboundRequestStationNo);
                    }
 
                }
            }
            catch (Exception ex)
            {
                WriteLog.Info(InboundRequestStationNo).Write($"{ DateTime.Now }上报WMS出库完成任务失败:{ex.Message}", InboundRequestStationNo);
            }
        }
    }
 
 
}