StationExecute.cs 10.3 KB
using HaHRCS.Rcs.Executor.Enums;
using HaHRCS.Rcs.Model.Entities;
using HHECS.BllModel;
using HHECS.Communication;
using Rcs.Executor.PLC;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Reflection.Metadata;
using System.Threading;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace HaHRCS.Rcs.Executor.PLC
{
    /// <summary>
    /// 站台顶层抽象类
    /// </summary>
    public abstract class StationExecute 
    {
        #region 属性
        /// <summary>
        /// 用于标记站台的类型
        /// </summary>
        public EquipmentType EquipmentType { get; set; }

        /// <summary>
        /// 用于记录错误文本
        /// </summary>
        public string ErrorText { get; set; }

        /// <summary>
        /// 中控的http请求的head中的origin(中控的安全校验会校验head)
        /// </summary>
        public string origin;

        /// <summary>
        /// 中控的http请求的head中的referer(中控的安全校验会校验head)
        /// </summary>
        public string referer;

        /// <summary>
        /// 叫料的URL
        /// </summary>
        public string callMaterialUrl;

        /// <summary>
        /// 工位的叫料的处理结果
        /// </summary>
        Dictionary<string, List<string>> dicCallInfo = new Dictionary<string, List<string>>();

        #endregion

        protected StationExecute(EquipmentType equipmentType)
        {
            EquipmentType = equipmentType;
            //this.origin= origin;
            //this.referer = referer;
            //this.callMaterialUrl = callMaterialUrl;
            //this.dicCallInfo = dicCallInfo;
        }

        /// <summary>
        /// 具体的站台实现逻辑
        /// </summary>
        /// <param name="stations"></param>
        /// <param name="plcs"></param>
        /// <returns></returns>
        public virtual void Excute(List<Equipment> stations, EquipmentCommunicationHub equipmentCommunicationHub, List<Equipment> allEquipments)
        {
            try
            {
                foreach (var station in stations)
                {
                    BllResult result = BllResultFactory.Success();
                    //var OperationModel = station[StationProps.OperationModel.ToString()];
                    //var TotalError = station[StationProps.TotalError.ToString()];
                    //if (OperationModel.Value != OperationModelFlag.Online.GetIndexString() || TotalError.Value != TotalErrorFlag.Normal.GetIndexString())
                    //{
                    //    result = BllResultFactory.Create(BllResultCode.Info, $"处理工位【{station.WorkStationCode}】的设备【{station.Name}】失败,请保持设备的联机并且无故障");
                    //    continue;
                    //}

                    #region 判断PLC地址请求
                    #region 清除放货信号
                    var PutRequest = station[StationProps.PutRequest.ToString()];
                    var PutRuning = station[StationProps.PutRuning.ToString()];
                    var PutDone = station[StationProps.PutDone.ToString()];
                    var PutConveyorReady = station[StationProps.PutConveyorReady.ToString()];
                    var AllowPut = station[StationProps.AllowPut.ToString()];
                    //放货
                    if (PutRequest != null && PutRuning != null && PutDone != null && PutConveyorReady != null && AllowPut !=null)
                    {
                        if (PutRequest.Value == "False" && PutRuning.Value == "False" && PutDone.Value == "True" && PutConveyorReady.Value == "False" && AllowPut.Value == "False")
                        {
                            //清除PutDone
                            result = ExcutePutClear(equipmentCommunicationHub, station);
                        }
                        else
                        {
                             BllResultFactory.Error("等待电气清除信号");
                        }
                    }
                    #endregion
                    #region 清除取货信号
                    //取货
                    var PickRequest = station[StationProps.PickRequest.ToString()];
                    var PickRuning =station[StationProps.PickRuning.ToString()];
                    var PickDone = station[StationProps.PickDone.ToString()];
                    var PickConveyorReady = station[StationProps.PickConveyorReady.ToString()];
                    var AllowPick = station[StationProps.AllowPick.ToString()];
                    if (PickRequest != null && PickRuning != null && PickDone != null && PickConveyorReady != null && AllowPick != null)
                    {
                        if (PickRequest.Value == "False" && PickRuning.Value == "False" && PickDone.Value =="True" && PickConveyorReady.Value == "False" && AllowPick.Value == "False")
                        {
                            result = ExcutePickClear(equipmentCommunicationHub,station);
                        }
                        else
                        {
                            BllResultFactory.Error("等待电气清除信号");
                        }
                    }
                    #region 清除缩回信号
                    
                    var Retracted = station[CylinderProps.Retracted.ToString()];
                    if (Retracted != null)
                    {
                        if (Retracted.Value == "True")
                        {
                            result = ExcutConvingClear(equipmentCommunicationHub, station);
                        }
                    }
                    
                    #endregion

                    var Extended = station[CylinderProps.Extended.ToString()];
                    if (Extended != null)
                    {
                        if (Extended.Value == "True")
                        {
                            result = ExcutConvingTClear(equipmentCommunicationHub, station);
                        }
                    }
                    #endregion
                    #endregion
                }
            }
            catch (Exception ex)
            {

                BllResultFactory.Create(BllResultCode.Exception, ex, $"程序处理逻辑类出现异常:{ex.Message}", "Error04");
            }

        }

        #region 实现逻辑的方法
        /// <summary>
        /// 放货信号完成清除信号
        /// </summary>
        /// <param name="plc"></param>
        /// <param name="station"></param>
        /// <returns></returns>
        private BllResult ExcutePutClear(EquipmentCommunicationHub plc, Equipment station)
        {
            List<EquipmentProp> props = new List<EquipmentProp>();

            SetStationPropList(props, station, StationProps.PutDone, "0");

            return plc.Writes(props.ToArray());
        }
        /// <summary>
        /// 取货信号完成清除信号
        /// </summary>
        /// <param name="plc"></param>
        /// <param name="station"></param>
        /// <returns></returns>
        private BllResult ExcutePickClear(EquipmentCommunicationHub plc, Equipment station)
        {
            List<EquipmentProp> props = new List<EquipmentProp>();

            SetStationPropList(props, station, StationProps.PickDone, "0");

            return plc.Writes(props.ToArray());
        }
        /// <summary>
        /// 缩回请求信号清除
        /// </summary>
        /// <param name="plc"></param>
        /// <param name="station"></param>
        /// <returns></returns>
        private BllResult ExcutConvingClear(EquipmentCommunicationHub plc, Equipment station)
        {
            List<EquipmentProp> props = new List<EquipmentProp>();

            SetStationPropListTo(props, station, CylinderProps.AirExtended, "0");

            return plc.Writes(props.ToArray());
        }
        /// <summary>
        /// 伸出请求信号清除
        /// </summary>
        /// <param name="plc"></param>
        /// <param name="station"></param>
        /// <returns></returns>
        private BllResult ExcutConvingTClear(EquipmentCommunicationHub plc, Equipment station)
        {
            List<EquipmentProp> props = new List<EquipmentProp>();

            SetStationPropListTo(props, station, CylinderProps.AirRetracted, "0");

            return plc.Writes(props.ToArray());
        }
        /// <summary>
        /// 设置设备属性列表
        /// </summary>
        /// <param name="list">设备属性列表</param>
        /// <param name="station">设备</param>
        /// <param name="propKey">要设置的属性Key</param>
        /// <param name="propValue">要设置的属性值</param>
        /// <param name="insert">是否用Inset插入到列表,默认为Add方法</param>
        public void SetStationPropList(List<EquipmentProp> list, Equipment station, StationProps propKey, string propValue, bool insertFirst = false)
        {
            var propItem = station[propKey.ToString()];
            if (propItem != null && propValue != null)
            {
                propItem.Value = propValue;
                if (insertFirst)
                {
                    list.Insert(0, propItem);
                }
                else
                {
                    list.Add(propItem);
                }
            }
        }
        /// <summary>
        /// 设置设备属性列表
        /// </summary>
        /// <param name="list">设备属性列表</param>
        /// <param name="station">设备</param>
        /// <param name="propKey">要设置的属性Key</param>
        /// <param name="propValue">要设置的属性值</param>
        /// <param name="insert">是否用Inset插入到列表,默认为Add方法</param>
        public void SetStationPropListTo(List<EquipmentProp> list, Equipment station, CylinderProps propKey, string propValue, bool insertFirst = false)
        {
            var propItem = station[propKey.ToString()];
            if (propItem != null && propValue != null)
            {
                propItem.Value = propValue;
                if (insertFirst)
                {
                    list.Insert(0, propItem);
                }
                else
                {
                    list.Add(propItem);
                }
            }
        }
        #endregion
    }
}