StationInExcute.cs 11.4 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 HHECS.Scan;
using S7.Net;

namespace HHECS.EquipmentExcute
{
    public class StationInExcute : StationExcute
    {
        List<Equipment> equipments = null;

        public override BllResult Excute(List<Equipment> equipments, OPCHelp plc)
        {
            this.equipments = equipments;
            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标记为3,则表示解析有误,这里报警
            if (station.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == "Flag").Value == "3")
            {
                Logger.Log("PLC解析站台" + station.Name + "数据失败,请检查基础数据!", LogLevel.Error);
                return;
            }

            //PLC 有新消息标记 && WCS回复有新消息标记:此时WCS已经回复了消息,等待PLC响应,不做处理,如果PLC新标记为3,表示解析错误
            if (station.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == "Flag").Value == "1" && 
                station.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == "WCSFlag").Value == "1")
            {
                return;
            }

            //PLC没有新消息标记 && WCS有新消息标记:此时PLC已经确认消息,清除WCS回复消息
            if (station.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == "Flag").Value != "1" && 
                station.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == "WCSFlag").Value == "1")
            {
                var result = WriteWCSStationDataAddress(station, plc, "0", "0", "0", "0", "0", "0", "", "0", "0");
                if (result.Success)
                {
                    Logger.Log("清空" + station.Name + "WCS区地址成功", LogLevel.Success);
                }
                else
                {
                    Logger.Log("清空" + station.Name + "WCS区地址失败:" + result.Msg,  LogLevel.Error);
                }
                return;
            }

            //PLC有新消息标记 && WCS没有新消息标记:此时PLC发送了消息而WCS没有响应,写响应逻辑发送地址数据给PLC
            if (station.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == "Flag").Value == "1"
                && station.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == "WCSFlag").Value != "1"
                && station.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == "Type").Value != "0"
                && station.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == "PLCNo").Value != "0"
                && station.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == "StationNo").Value != "0"
                && !String.IsNullOrWhiteSpace(station.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == "Barcode").Value))
            {
                if (station.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == "Type").Value == "1")
                {
                    //获取任务
                    string pallet = station.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == "Barcode").Value;
                    if (pallet == "??????" || pallet == "NoRead")
                    {
                        Logger.Log(station.Name + "未识别的条码", LogLevel.Warning);
                        StationBack(station, plc);
                        return;
                    }
                    var taskResult = AppSession.Bll.GetTaskUncompleteByPalletCode(pallet);
                    if (!taskResult.Success)
                    {
                        Logger.Log(station.Name + " 根据" + pallet + "获取任务失败:" + taskResult.Msg, LogLevel.Error);
                        StationBack(station, plc);
                        return;
                    }
                    TaskEntity taskEntity = taskResult.Data;
                    //if (taskEntity.LastStatus >= 30)
                    //{
                    //    Logger.Log(station.Name + " 根据" + pallet + "获取任务成功,但任务状态已超过30", LogLevel.Error);
                    //    StationBack(station, plc);
                    //    return;
                    //}
                    //判断任务,这里只允许整入和空入库任务,如果托盘带有其他类型任务则报错
                    if (taskEntity.Type != TaskType.整盘入库.GetIndexInt() && taskEntity.Type != TaskType.空容器入库.GetIndexInt())
                    {
                        Logger.Log("站台" + station.Name + ",任务" + taskEntity.Id + ",任务类型不符合要求,入库站台只能整入或者空入", LogLevel.Error);
                        StationBack(station, plc);
                        return;
                    }
                    //if (taskEntity.Station > 20)
                    //{
                    //    Logger.Log("站台" + station.Name + ",任务" + taskEntity.Id + ",任务状态不符合,入库站台任务状态是刚下达或开始执行状态", LogLevel.Error);
                    //    StationBack(station, plc);
                    //    return;
                    //}
                    //电气高度标志
                    int height = Convert.ToInt32(station.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == "Height").Value);
                    if (String.IsNullOrWhiteSpace(taskEntity.DestinationLocation))
                    {
                        BllResult<string> bllResult = AppSession.Bll.GetDestinationLocation<string>(taskEntity.Id, height, AppSession.Client, AppSession.Urls);
                        if (bllResult.Success)
                        {
                            taskEntity.DestinationLocation = bllResult.Data;
                        }
                        else
                        {
                            Logger.Log("站台" + station.Name + ",任务" + taskEntity.Id + ",获取目标库位失败", LogLevel.Error);
                            StationBack(station, plc);
                            return;
                        }

                    }
                    var locationResult = AppSession.Bll.GetAllLocations(null, null, null, null, null, null, taskEntity.DestinationLocation);
                    if (!locationResult.Success)
                    {
                        Logger.Log("站台" + station.Name + "未找到任务" + taskEntity.Id + ",托盘:" + taskEntity.ContainerCode + ",目标库位编码:" + taskEntity.DestinationLocation + "的库位;请检查库位编码是否正确", LogLevel.Error);
                        StationBack(station, plc);
                        return;
                    }
                    var location = locationResult.Data[0];
                    //校验这个库位与电气反馈高度对比
                    //0可以入1到5层,1只能去第五层
                    //int height =Convert.ToInt32(station.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == "Height").Value);
                    switch (height)
                    {
                        case 0:
                            break;
                        case 1:
                            if (location.Layer != 5)
                            {
                                Logger.Log(station.Name + "货物超高" + height, LogLevel.Error);
                                StationBack(station, plc);
                                return;
                            }
                            break;
                        default:
                            Logger.Log(station.Name + "未识别的高度值" + height, LogLevel.Error);
                            StationBack(station, plc);
                            return;
                    }
                    if (!String.IsNullOrEmpty(location.ContainerCode))
                    {
                        Logger.Log(station.Name + "重入", LogLevel.Error);
                        StationBack(station, plc);
                        return;
                    }
                    //获取这个库位的目标巷道,从而得出对应的接入站台
                    var stockerStationIn = equipments.FirstOrDefault(x => x.EquipmentType.Code == "StationForStockerIn" && x.RoadWay == location.Roadway.ToString());
                    if (stockerStationIn == null)
                    {
                        Logger.Log("站台" + station.Name + "未找到任务" + taskEntity.Id + "对应的堆垛机接入站台,巷道:" + location.Roadway + ",托盘:" + taskEntity.ContainerCode, LogLevel.Error);
                        StationBack(station, plc);
                        return;
                    }
                    var plcNo = station.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == "PLCNo").Value; 
                    var stationNo = station.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == "StationNo").Value;
                    var taskNo = station.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == "TaskNo").Value;
                    var barcode = station.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == "Barcode").Value;
                    var result = WriteWCSStationDataAddress(station, plc, "1", "06", plcNo, "0", stationNo, taskNo, barcode, stockerStationIn.SelfAddress.ToString(), "0");
                    if (result.Success)
                    {
                        //这里将任务状态更新为30,表示达到了站台,即入库进行整个流程的后一半
                        AppSession.Bll.SendLED(station, taskEntity, AppSession.LEDExcute);
                        AppSession.Bll.SetTaskStatus(taskEntity.Id, TaskEntityStatus.拣选台回库.GetIndexInt());
                        Logger.Log("写入" + station.Name + "WCS区地址成功,任务:" + taskEntity.Id, LogLevel.Success);
                    }
                    else
                    {
                        Logger.Log("写入" + station.Name + "WCS区地址失败,任务:" + taskEntity.Id + ",消息:" + result.Msg, LogLevel.Error);
                    }
                }

                if (station.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == "Type").Value == "2")
                {
                    //地址请求
                    Logger.Log("入库站台" + station.Name + "暂时没有实现位置到达请求", LogLevel.Warning);
                    return;
                }

                if (station.EquipmentProps.FirstOrDefault(t => t.EquipmentTypePropTemplateCode == "Type").Value == "3")
                {
                    //地址请求
                    Logger.Log("入库站台" + station.Name + "暂时没有实现控制指令请求", LogLevel.Warning);
                    return;
                }
            }
        }











    }
}