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

namespace HHECS.Common
{
    /// <summary>
    /// todo:湘电出库站台处理类
    /// </summary>
    public class XiangdianOutStationExcute : IStationExcute
    {
        public EquipmentType EquipmentType { get; set; }

        public BllResult Excute(List<Equipment> stations, List<Plc> plcs)
        {
            try
            {
                foreach(var station in stations)
                {
                    //出库站台的处理逻辑:
                    //step1: 查看是否有出库请求  
                    if(station.EquipmentProps.Find(t=>t.EquipmentTypePropTemplateCode=="RequestMessage").Value()=="01")
                    {
                        //step2: 判断是否已经处理  06表示处理
                        if(station.EquipmentProps.Find(t=>t.EquipmentTypePropTemplateCode=="WCSReplyMessage").Value()!="06")
                        {
                            //step3: 根据条码获取对应的任务类型、以及相应的任务状态
                            var tasksResult = AppSession.Bll.GetCommonModelByCondition<TaskEntity>($"where station={station.Code} and status = {(int)TaskEntityStatus.响应堆垛机库外放货任务完成}");
                            if(tasksResult.Success)
                            {
                                //step4: 回复PLC相关信息
                                TaskEntity taskEntity=tasksResult.Data[0];
                                if(taskEntity!=null)
                                {
                                    List<EquipmentProp> propsToWriter = new List<EquipmentProp>();
                                   //回复PLC考虑标志位 放置最后 控制数据传递的完整性
                                    var number= station.EquipmentProps.Find(t=>t.EquipmentTypePropTemplateCode=="WCSReplyNumber");
                                    number.Value=station.EquipmentProps.Find(t=>t.EquipmentTypePropTemplateCode=="RequestNumber").Value();//地址编码
                                    var loadStatus= station.EquipmentProps.Find(t=>t.EquipmentTypePropTemplateCode=="WCSReplayLoadStatus");
                                    loadStatus.Value=station.EquipmentProps.Find(t=>t.EquipmentTypePropTemplateCode=="RequestLoadStatus").Value();//装载状态
                                    var barcode=station.EquipmentProps.Find(t=>t.EquipmentTypePropTemplateCode=="WCSReplayBarcode");
                                    barcode.Value=taskEntity.ContainerCode;//容器条码
                                    var weight=station.equipmentprops.Find(t=>t.EquipmentTypePropTemplateCode=="WCSReplyWeight").value=0;//重
                                    var length=station.equipmentprops.Find(t=>t.EquipmentTypePropTemplateCode=="WCSReplyLength").value=0;//长
                                    var width=station.equipmentprops.Find(t=>t.EquipmentTypePropTemplateCode=="WCSReplyWidth").value=0;//宽
                                    var height=station.equipmentprops.Find(t=>t.EquipmentTypePropTemplateCode=="WCSReplyHeight").value=0;//高
                                    var address =station.EquipmentProps.Find(t=>t.EquipmentTypePropTemplateCode=="WCSReplyAddress").Value=taskEntity.Port;//目标地址
                                    var backup=station.EquipmentProps.Find(t=>t.EquipmentTypePropTemplateCode=="WCSReplyBackUp").Value=0;//备用
                                    var flag=station.EquipmentProps.Find(t=>t.EquipmentTypePropTemplateCode=="WCSReplayMessage").Value="06";//报文名称
                                    propsToWriter.AddRange(new List<EquipmentProp>() { number, loadStatus,barcode,weight,length,width,height,address,backup,flag });
                                    //判断是那个对应plc
                                    Plc plc=plcs.FirstOrDefault(t=>t.IP==station.IP);
                                    BllResult result=S7Helper.PlcSplitWrite(plc, propsToWriter, 20);
                                    if(result.Success)// 写入成功后更新任务状态为  
                                    {
                                        //todo: 后续更改
                                        taskEntity.FirstStatus=(int)TaskEntityStatus.响应接出口站台地址请求;
                                        AppSession.Bll.UpdateCommonModel<TaskEntity>(taskEntity);
                                        LogExecute.WriteInfoLog(String.Format("任务ID:{0} 到达出库站台:{1} 即将去向出入库口:{2}处理并更新任务状态为:{3}",taskEntity.Id,taskEntity.Station,taskEntity.Port,taskEntity.FirstStatus));
                                    }
                                }
                                else
                                {
                                    LogExecute.WriteLog("出库站台",string.Format("获取站台{0}对应的请求任务转实体类失败",station.Code));
                                } 
                            } 
                            else
                            {
                                LogExecute.WriteLog("出库站台",string.Format("获取站台{0}对应的请求任务未查询到",station.Code));
                            } 
                        }
                    }
                }
                return BllResultFactory.Sucess;
            }
            catch(Exception ex)
            {
                Logger.Log($"出库站台处理过程中出现异常:{ex.Message}", LogLevel.Exception);
                return BllResultFactory.Error();
            }
        }
    }
}