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

namespace HHECS.Common
{
    public abstract class StockerExcute
    {
        /// <summary>
        /// 对应的设备类型
        /// </summary>
        public EquipmentType EquipmentType { get; set; }

        /// <summary>
        /// 执行堆垛机处理逻辑
        /// </summary>
        /// <param name="stockers"></param>
        /// <param name="plcs"></param>
        /// <returns></returns>
        public abstract BllResult Excute(List<Equipment> stockers, OPCHelp plc);

        /// <summary>
        /// 下发数据到WCS交换区
        /// </summary>
        /// <param name="stocker"></param>
        /// <param name="plc"></param>
        /// <param name="forkAction"></param>
        /// <param name="forkTaskFlag"></param>
        /// <param name="forkRow"></param>
        /// <param name="forkColumn"></param>
        /// <param name="forkLayer"></param>
        /// <param name="forkStation"></param>
        /// <param name="taskNo"></param>
        /// <returns></returns>
        public BllResult SendTaskToStocker(Equipment stocker, OPCHelp plc, ForkAction forkAction, ForkTaskFlag forkTaskFlag, string forkRow, string forkColumn, string forkLayer, string forkStation, string taskNo)
        {
            try
            {
                List<EquipmentProp> propsToWriter = new List<EquipmentProp>();
                var props = stocker.EquipmentProps;
                var action = props.Find(t => t.EquipmentTypePropTemplateCode == "WCSForkAction");
                action.Value = forkAction.GetIndexString();
                var taskFlag = props.Find(t => t.EquipmentTypePropTemplateCode == "WCSFork1TaskFlag");
                taskFlag.Value = forkTaskFlag.GetIndexString();
                var row = props.Find(t => t.EquipmentTypePropTemplateCode == "WCSFork1Row");
                row.Value = forkRow;
                var column = props.Find(t => t.EquipmentTypePropTemplateCode == "WCSFork1Column");
                column.Value = forkColumn;
                var layer = props.Find(t => t.EquipmentTypePropTemplateCode == "WCSFork1Layer");
                layer.Value = forkLayer;
                var station = props.Find(t => t.EquipmentTypePropTemplateCode == "WCSFork1Station");
                station.Value = forkStation;
                var task = props.Find(t => t.EquipmentTypePropTemplateCode == "WCSFork1Task");
                task.Value = taskNo;
                propsToWriter.AddRange(new List<EquipmentProp>() { action, taskFlag, row, column, layer, station, task });
                //return S7Helper.PlcSplitWrite(plc, propsToWriter, 20);
                BllResult bll=plc.WriteAddress(propsToWriter);
                string content = "stocker:"+stocker.Name+ "货叉动作:"+forkAction.GetIndexString()+ "任务标志:"+forkTaskFlag.GetIndexString()
                    + "行:"+forkRow+"列:"+forkColumn+"层:"+forkLayer ;
                Logger.Log(content, LogLevel.Info);
                return bll;
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error($"下发任务出现异常:{ex.Message}");
            }
        }

        /// <summary>
        /// 心跳
        /// </summary>
        /// <param name="stocker"></param>
        /// <param name="plc"></param>
        public void Heartbeat(Equipment stocker, OPCHelp plc)
        {
            var prop = stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSHeartBeat");
            if (prop.Value == "1")
            {
                prop.Value = "0";
            }
            else
            {
                prop.Value = "1";
            }
            //var result = S7Helper.PlcSplitWrite(plc, new List<EquipmentProp>() { prop }, 20);
            var result = plc.WriteAddress(new List<EquipmentProp>() { prop });
            if (!result.Success)
            {
                Logger.Log($"发送堆垛机{stocker.Name}心跳数据失败:{result.Msg}", LogLevel.Error);
            }
        }

        /// <summary>
        /// 清理WCS交换区
        /// </summary>
        /// <param name="stocker"></param>
        /// <param name="plc"></param>
        /// <returns></returns>
        public BllResult ClearWCSData(Equipment stocker, OPCHelp plc)
        {
            //表示堆垛机已经收到WCS发送给他的任务完成信号,此时WCS可以清除自己的交换区地址
            BllResult sendResult = SendTaskToStocker(stocker, plc, ForkAction., ForkTaskFlag.无任务, "0", "0", "0", "0", "0");
            if (sendResult.Success)
            {
                Logger.Log($"任务完成后,清除堆垛机{stocker.Name}交换区地址成功", LogLevel.Info);
                return BllResultFactory.Sucess();
            }
            else
            {
                Logger.Log($"任务完成后,清除堆垛机{stocker.Name}交换区地址失败", LogLevel.Error);
                return BllResultFactory.Error();
            }
        }

        /// <summary>
        /// 通用条件验证
        /// </summary>
        /// <param name="stocker"></param>
        /// <returns></returns>
        public BllResult Validate(Equipment stocker)
        {
            //联机,无故障
            if (stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "OperationModel").Value == OperationModel.联机.GetIndexString() && stocker.EquipmentProps.Where(t => t.EquipmentTypePropTemplate.IsMonitor == true).Count(t => t.Value != t.EquipmentTypePropTemplate.MonitorCompareValue) == 0)
            {
                return BllResultFactory.Sucess();
            }
            else
            {
                return BllResultFactory.Error();
            }
        }
    }
}