StationOutExcute.cs 6.58 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.Model.BllModel;
using HHECS.Model.Common;
using HHECS.Model.Entities;
using HHECS.Model.Enums;
using HHECS.OPC;
using S7.Net;

namespace HHECS.EquipmentExcute
{
    /// <summary>
    /// 标准只出站台
    /// </summary>
    public class StationOutExcute : StationExcute
    {
        public override BllResult Excute(List<Equipment> equipments, OPCHelp plc)
        {
            List<Equipment> stations = equipments.Where(a => a.EquipmentType.Id == EquipmentType.Id).ToList();
            foreach (var station in stations)
            {
                InnerExcute(station, plc);
            }
            return BllResultFactory.Sucess();
        }

        private void InnerExcute(Equipment station, OPCHelp plc)
        {
            //判断位置到达
            //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")
                {
                    string number = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "ArriveRealAddress").Value;
                    //回复位置到达
                    var result = SendAckToPlc(station, plc, "8", "1", number, "0");
                    if (result.Success)
                    {
                        Logger.Log($"响应站台{station.Name}位置到达成功", LogLevel.Info);
                        return;
                    }
                    else
                    {
                        Logger.Log($"响应站台{station.Name}位置到达失败", LogLevel.Error);
                        return;
                    }
                    ////获取条码
                    //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()}");
                    //if (taskResult.Success)
                    //{
                    //    if (taskResult.Data.Count > 1)
                    //    {
                    //        Logger.Log($"站台{station.Name}根据条码{barcode}查找到了多条任务,请检查数据", LogLevel.Error);
                    //        return;
                    //    }
                    //    else
                    //    {
                    //        var task = taskResult.Data[0];
                    //        var result = AppSession.Bll.CompleteTask(task.Id.ToString(), AppSession.Client, AppSession.Urls);
                    //        if (result.Success)
                    //        {
                    //            String message = "1";
                    //            //确认到达的时候,如果是空盘出库回复1,否则就是3
                    //            //String message = task.Type == 600 ? "1":"3";

                    //            String number = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "ArriveRealAddress").Value;
                    //            //回复位置到达
                    //            result = SendAckToPlc(station, plc, "8", message, number, "0");
                    //            if (result.Success)
                    //            {
                    //                Logger.Log($"响应站台{station.Name}位置到达成功,完成任务成功,条码{barcode},任务{task.Id}", LogLevel.Info);
                    //                //发送LED
                    //                AppSession.Bll.SendLED(station, task, AppSession.LEDExcute);
                    //                return;
                    //            }
                    //            else
                    //            {
                    //                Logger.Log($"响应站台{station.Name}位置到达失败,但完成任务成功,条码{barcode},任务{task.Id}", LogLevel.Error);
                    //                return;
                    //            }
                    //        }
                    //        else
                    //        {
                    //            Logger.Log($"响应站台{station.Name}位置到达请求失败,完成任务失败,条码{barcode},任务{task.Id},详情:{result.Msg}", LogLevel.Error);
                    //            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;
                }
            }
        }

    }
}