StationForStockerInOrOutExcute.cs 19.5 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HHECS.Bll;
using HHECS.Model;
using HHECS.OPC;
using S7.Net;

namespace HHECS.Common
{
    /// <summary>
    /// 可由开关控制入出的堆垛机接台
    /// </summary>
    public class StationForStockerInOrOutExcute : StationExcute
    {
        public override BllResult Excute(List<Equipment> stations, OPCHelp plc)
        {
            foreach (var station in stations)
            {
                InnerExcute(station, plc);
            }
            return BllResultFactory.Sucess();
        }

        private void InnerExcute(Equipment station, OPCHelp plc)
        {
            //判断请求
            //PLC有请求,但WCS没有,则WCS还没有响应
            if (station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "RequestMessage").Value == "1" && station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSReplyMessage").Value == "0")
            {
                //此处根据开关量来进行不同请求区分,0时,只入,则响应去向地址为1010;1时,还是原来的流程
                #region //todo:huhai2019-06-03 去掉开关按钮
                //var prop = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSCanOut");
                //if (prop.Value == "True")
                {
                    //获取任务
                    var taskResult = AppSession.Bll.GetCommonModelByCondition<TaskEntity>($"where  lastStatus = {TaskEntityStatus.响应堆垛机库外放货任务完成.GetIndexInt()} " +
                        $"and station={station.StationIndex} and port={station.BackAddress} and zoneCode = 'LK'");
                    if (taskResult.Success)
                    {
                        var tasks = taskResult.Data.Where(t => t.Station == station.StationIndex).ToList();
                        if (tasks.Count == 0)
                        {
                            Logger.Log($"站台{station.Name}根据任务状态{TaskEntityStatus.响应堆垛机库外放货任务完成}和索引{station.StationIndex}没有找到数据,请检查数据", LogLevel.Error);
                            return;
                        }
                        if (taskResult.Data.Count > 1)
                        {
                            Logger.Log($"站台{station.Name}根据任务状态{TaskEntityStatus.响应堆垛机库外放货任务完成}和索引{station.StationIndex}查找到了多条任务,请检查数据", LogLevel.Error);
                            return;
                        }
                        else
                        {
                            var task = taskResult.Data[0];
                            if (task.Type == TaskType.整盘入库.GetIndexInt() || task.Type == TaskType.空容器入库.GetIndexInt())
                            {
                                Logger.Log($"站台{station.Name}不接受整盘入库和空容器入库任务", LogLevel.Error);
                                return;
                            }
                            int tempStatus = task.FirstStatus; //记录原始状态以便回滚
                            string tempGateway = task.Gateway;
                            task.FirstStatus = TaskEntityStatus.响应接出口站台地址请求.GetIndexInt();
                            task.LastStatus = TaskEntityStatus.响应接出口站台地址请求.GetIndexInt();
                            task.Gateway = station.SelfAddress;
                            var temp = AppSession.Bll.UpdateCommonModel<TaskEntity>(task);
                            if (temp.Success)
                            {
                                //回复地址请求
                                temp = SendAddressReplyToPlc(station, plc, "6", "0", station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "RequestNumber").Value, 
                                    task.ContainerCode, station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "RequestWeight").Value,
                                    station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "RequestLength").Value, 
                                    station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "RequestWidth").Value, 
                                    station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "RequestHeight").Value, station.GoAddress, station.BackAddress);
                                if (temp.Success)
                                {
                                    Logger.Log($"响应站台{station.Name}地址请求成功,条码{task.ContainerCode},任务{task.Id}", LogLevel.Info);
                                    return;
                                }
                                else
                                {
                                    Logger.Log($"响应站台{station.Name}地址请求失败,条码{task.ContainerCode},任务{task.Id},回滚任务状态", LogLevel.Error);
                                    task.FirstStatus = tempStatus;
                                    task.LastStatus = tempStatus;
                                    task.Gateway = tempGateway;
                                    AppSession.Bll.UpdateCommonModel<TaskEntity>(task);
                                    return;
                                }
                            }
                            else
                            {
                                Logger.Log($"站台{station.Name},响应请求时更新任务{task.Id}状态失败。", LogLevel.Error);
                                return;
                            }
                        }
                    }
                    else
                    {
                        Logger.Log($"站台{station.Name}根据任务状态{TaskEntityStatus.响应堆垛机库外放货任务完成}没有查询到任务。", LogLevel.Error);
                        return;

                    }
                    //}
                    //else
                    #endregion
                    #region
                    //{
                    //var barcode = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "RequestBarcode").Value;
                    //if (string.IsNullOrWhiteSpace(barcode))
                    //{
                    //    Logger.Log($"站台{station.Name}有地址请求但是没有条码信息", LogLevel.Error);
                    //    return;
                    //}
                    //else
                    //{
                    //    //获取任务
                    //    var taskResult = AppSession.Bll.GetCommonModelByCondition<TaskEntity>($"where containerCode = '{barcode}' and lastStatus < {TaskEntityStatus.任务完成.GetIndexInt()}");
                    //    if (taskResult.Success)
                    //    {
                    //        if (taskResult.Data.Count > 1)
                    //        {
                    //            Logger.Log($"站台{station.Name}根据条码{barcode}查找到了多条任务,请检查数据", LogLevel.Error);
                    //            SendBack(station, plc, barcode);
                    //            return;
                    //        }
                    //        else
                    //        {
                    //            var task = taskResult.Data[0];
                    //            if (task.Type == TaskType.整盘出库.GetIndexInt() || task.Type == TaskType.空容器出库.GetIndexInt())
                    //            {
                    //                Logger.Log($"站台{station.Name}不接受整盘出库和空托盘出库任务", LogLevel.Error);
                    //                SendBack(station, plc, barcode);
                    //                return;
                    //            }
                    //            if (string.IsNullOrWhiteSpace(task.DestinationLocation))
                    //            {
                    //                var result = AppSession.Bll.GetDestinationLocation<string>(task.Id, 0);
                    //                if (result.Success)
                    //                {
                    //                    task.DestinationLocation = result.Data;
                    //                    var a = AppSession.Bll.UpdateCommonModel<TaskEntity>(task);
                    //                    if (a.Success)
                    //                    {
                    //                        Logger.Log($"站台{station.Name},任务{task.Id},获取去向地址:{result.Data}成功", LogLevel.Success);
                    //                    }
                    //                    else
                    //                    {
                    //                        Logger.Log($"站台{station.Name},任务{task.Id},获取去向地址:{result.Data}失败", LogLevel.Success);
                    //                        SendBack(station, plc, barcode);
                    //                    }
                    //                }
                    //                else
                    //                {
                    //                    Logger.Log($"站台:{station.Name},任务{task.Id},未能获取到去向地址:{result.Msg}", LogLevel.Error);
                    //                    SendBack(station, plc, barcode);
                    //                }
                    //            }
                    //            int tempStatus = task.FirstStatus; //记录原始状态以便回滚
                    //                                               //更新任务状态到60
                    //            string weight = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "RequestWeight").Value;

                    //            task.FirstStatus = TaskEntityStatus.拣选台回库.GetIndexInt();
                    //            task.LastStatus = TaskEntityStatus.拣选台回库.GetIndexInt();
                    //            task.UserDef1 = weight;
                    //            task.gateway = station.SelfAddress;
                    //            var temp = AppSession.Bll.UpdateCommonModel<TaskEntity>(task);
                    //            if (temp.Success)
                    //            {
                    //                //回复地址请求
                    //                temp = SendAddressReplyToPlc(station, plc, "6", "0", station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "RequestNumber").Value, barcode, station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "RequestWeight").Value, station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "RequestLength").Value, station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "RequestWidth").Value, station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "RequestHeight").Value, "1010", "");
                    //                if (temp.Success)
                    //                {
                    //                    Logger.Log($"响应站台{station.Name}地址请求成功,条码{barcode},任务{task.Id}", LogLevel.Info);
                    //                    return;
                    //                }
                    //                else
                    //                {
                    //                    Logger.Log($"响应站台{station.Name}地址请求失败,条码{barcode},任务{task.Id},详情:{temp.Msg},回滚任务状态", LogLevel.Error);
                    //                    task.FirstStatus = tempStatus;
                    //                    task.LastStatus = tempStatus;
                    //                    AppSession.Bll.UpdateCommonModel<TaskEntity>(task);
                    //                    SendBack(station, plc, barcode);
                    //                    return;
                    //                }
                    //            }
                    //            else
                    //            {
                    //                Logger.Log($"站台{station.Name},响应请求时更新任务{task.Id}状态失败。详情:{temp.Msg}", LogLevel.Error);
                    //                return;
                    //            }
                    //        }
                    //    }
                    //    else
                    //    {
                    //        Logger.Log($"站台{station.Name}根据条码{barcode}没有查询到任务。", LogLevel.Error);
                    //        SendBack(station, plc, barcode);
                    //        return;
                    //    }
                    //}
                    #endregion
                }
            }

            //PLC没有,WCS有,说明PLC已经清除而WCS没有清除
            if (station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "RequestMessage").Value == "0" 
                && station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSReplyMessage").Value == "6")
            {
                var result = SendAddressReplyToPlc(station, plc, "0", "0", "0", "0", "0", "0", "0", "0", "0", "0");
                if (result.Success)
                {
                    Logger.Log($"站台{station.Name}响应地址请求完成后,清除WCS地址区成功", LogLevel.Info);
                    return;
                }
                else
                {
                    Logger.Log($"站台{station.Name}响应地址请求完成后,清除WCS地址区失败", LogLevel.Error);
                    return;
                }
            }

            //判断位置到达
            //PLC有位置到达,而WCSACK没有回复,则WCS还没有响应
            if (station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "ArriveMessage").Value == "2" 
                && station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSACKMessage").Value == "0")
            {
                var isSuccess = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "ArriveResult").Value;
                if (isSuccess == "1")
                {
                    //获取条码
                    var barcode = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "ArriveBarcode").Value;
                    if (string.IsNullOrWhiteSpace(barcode))
                    {
                        Logger.Log($"站台{station.Name}有位置到达但是没有条码信息", LogLevel.Error);
                        return;
                    }
                    else
                    {
                        var taskResult = AppSession.Bll.GetCommonModelByCondition<TaskEntity>($"where containerCode = '{barcode}' and lastStatus < {TaskEntityStatus.任务完成.GetIndexInt()} and zoneCode = 'LK'");
                        if (taskResult.Success)
                        {
                            if (taskResult.Data.Count > 1)
                            {
                                Logger.Log($"站台{station.Name}根据条码{barcode}查找到了多条任务,请检查数据", LogLevel.Error);
                                return;
                            }
                            else
                            {
                                var task = taskResult.Data[0];
                                //校验任务类型
                                if (task.Type == TaskType.整盘出库.GetIndexInt() || task.Type == TaskType.空容器出库.GetIndexInt())
                                {
                                    Logger.Log($"站台{station.Name}不接受整盘出库和空托盘出库任务", LogLevel.Error);
                                    return;
                                }
                                int tempStatus = task.FirstStatus; //记录原始状态以便回滚
                                string tempGateway = task.Gateway;
                                task.FirstStatus = TaskEntityStatus.响应接入口位置到达.GetIndexInt();
                                task.LastStatus = TaskEntityStatus.响应接入口位置到达.GetIndexInt();
                                task.Gateway = station.SelfAddress;
                                var temp = AppSession.Bll.UpdateCommonModel<TaskEntity>(task);
                                if (temp.Success)
                                {
                                    //回复位置到达
                                    temp = SendAckToPlc(station, plc, "8", "1", station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "ArriveRealAddress").Value, "0");
                                    if (temp.Success)
                                    {
                                        Logger.Log($"响应站台{station.Name}位置到达成功,条码{barcode},任务{task.Id}", LogLevel.Info);
                                        return;
                                    }
                                    else
                                    {
                                        Logger.Log($"响应站台{station.Name}位置到达失败,条码{barcode},任务{task.Id},回滚任务状态", LogLevel.Error);
                                        task.FirstStatus = tempStatus;
                                        task.LastStatus = tempStatus;
                                        AppSession.Bll.UpdateCommonModel<TaskEntity>(task);
                                        return;
                                    }
                                }
                                else
                                {
                                    Logger.Log($"响应站台{station.Name}位置到达请求失败,条码{barcode},任务{task.Id},回滚任务状态", LogLevel.Error);
                                    task.FirstStatus = tempStatus;
                                    task.LastStatus = tempStatus;
                                    task.Gateway = tempGateway;
                                    AppSession.Bll.UpdateCommonModel<TaskEntity>(task);
                                    return;
                                }
                            }

                        }
                        else
                        {
                            Logger.Log($"站台{station.Name}根据条码{barcode}没有查询到任务。", LogLevel.Error);
                            return;
                        }
                    }
                }
                else
                {
                    Logger.Log($"站台{station.Name}位置到达失败", LogLevel.Error);
                    return;
                }
            }

            //PLC没位置到达,而WCSACK有回复,则PLC已经响应但还没有清除
            if (station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "ArriveMessage").Value == "0" && station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSACKMessage").Value == "8" && station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSACKLoadStatus").Value == "1")
            {
                var result = SendAckToPlc(station, plc, "0", "0", "0", "0");
                if (result.Success)
                {
                    Logger.Log($"站台{station.Name}响应位置到达完成后,清除WCS地址区成功", LogLevel.Info);
                    return;
                }
                else
                {
                    Logger.Log($"站台{station.Name}响应位置到达完成后,清除WCS地址区失败", LogLevel.Error);
                    return;
                }
            }
        }
    }
}