StationExcute.cs 5.75 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 StationExcute
    {
        /// <summary>
        /// 用于标记站台的类型
        /// </summary>
        public EquipmentType EquipmentType { get; set; }

        /// <summary>
        /// 具体的站台实现逻辑
        /// </summary>
        /// <param name="stations"></param>
        /// <param name="plcs"></param>
        /// <returns></returns>
        public abstract BllResult Excute(List<Equipment> stations, OPCHelp plc);

        /// <summary>
        /// 实现地址回复
        /// </summary>
        /// <param name="station"></param>
        /// <param name="plc"></param>
        /// <param name="message"></param>
        /// <param name="loadStatus"></param>
        /// <param name="number"></param>
        /// <param name="barcode"></param>
        /// <param name="weight"></param>
        /// <param name="lenght"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="address"></param>
        /// <param name="backup"></param>
        /// <returns></returns>
        public BllResult SendAddressReplyToPlc(Equipment station, OPCHelp plc, string message, string loadStatus, string number, string barcode, string weight, string lenght, string width, string height, string address, string backup)
        {
            var prop1 = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSReplyMessage");
            prop1.Value = message;
            var prop2 = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSReplyLoadStatus");
            prop2.Value = loadStatus;
            var prop3 = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSReplyNumber");
            prop3.Value = number;
            var prop4 = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSReplyBarcode");
            prop4.Value = barcode;
            var prop5 = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSReplyWeight");
            prop5.Value = weight;
            var prop6 = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSReplyLength");
            prop6.Value = lenght;
            var prop7 = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSReplyWidth");
            prop7.Value = width;
            var prop8 = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSReplyHeight");
            prop8.Value = height;
            var prop9 = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSReplyAddress");
            prop9.Value = address;
            //var prop10 = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSReplyBackUp");
            //prop10.Value = backup;
            List<EquipmentProp> props = new List<EquipmentProp> { prop2, prop3, prop4, prop5, prop6, prop7, prop8, prop9, prop1 };
            //return S7Helper.PlcSplitWrite(plc, props, 20);
            return plc.WriteAddress(props);
        }

        /// <summary>
        /// 实现ACK回复,包括地址到达和控制指令
        /// </summary>
        /// <param name="station"></param>
        /// <param name="plc"></param>
        /// <param name="message"></param>
        /// <param name="loadStatus"></param>
        /// <param name="number"></param>
        /// <param name="backup"></param>
        /// <returns></returns>
        public BllResult SendAckToPlc(Equipment station, OPCHelp plc, string message, string loadStatus, string number, string backup)
        {
            var prop1 = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSACKMessage");
            prop1.Value = message;
            var prop2 = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSACKLoadStatus");
            prop2.Value = loadStatus;
            var prop3 = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSACKNumber");
            prop3.Value = number;
            //var prop4 = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSACKBackup");
            //prop4.Value = backup;
            List<EquipmentProp> props = new List<EquipmentProp>() { prop2, prop3, prop1 };
            //return S7Helper.PlcSplitWrite(plc, props, 20);
            return plc.WriteAddress(props);
        }

        /// <summary>
        /// 地址请求时,回退
        /// </summary>
        /// <param name="station"></param>
        /// <param name="plc"></param>
        /// <param name="barcode"></param>
        public void SendBack(Equipment station, OPCHelp plc, string barcode)
        {
            var result = SendAddressReplyToPlc(station, plc, "6", "0", station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "RequestNumber").Value, barcode,
                station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "RequestWeight").Value, 
                station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "RequestLength").Value, 
                station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "RequestWidth").Value, 
                station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "RequestHeight").Value, 
                station.BackAddress, station.BackAddress);//电气的请进地址和后退地址其实都是一个地址
            if (!result.Success)
            {
                Logger.Log($"{station.Name}回退失败", LogLevel.Error);
            }
            else
            {
                Logger.Log($"{station.Name}回退成功", LogLevel.Success);

            }
        }
    }
}