StationExcute.cs 9.53 KB
using HHECS.Bll;
using HHECS.Model;
using HHECS.Model.BllModel;
using HHECS.Model.Entities;
using HHECS.Model.Enums;
using HHECS.OPC;
using S7.Net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HHECS.EquipmentExcute
{
    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)
        {
            string number = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "RequestNumber").Value;
            string weight = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "RequestWeight").Value;
            string lenght = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "RequestLength").Value;
            string width = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "RequestWidth").Value;
            string height = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "RequestHeight").Value;
            var result = SendAddressReplyToPlc(station, plc, "6", "0", number, barcode, weight, lenght, width, height, station.BackAddress, "");
            if (!result.Success)
            {
                Logger.Log($"{station.Name}回退失败", LogLevel.Error);
            }
            else
            {
                Logger.Log($"{station.Name}回退成功", LogLevel.Success);

            }
        }


        #region 旧版本的站台处理方法

        /// <summary>
        /// 写入WCS站台区地址
        /// </summary>
        /// <param name="station"></param>
        /// <param name="plc"></param>
        /// <param name="flag"></param>
        /// <param name="type"></param>
        /// <param name="plcno"></param>
        /// <param name="loadstatus"></param>
        /// <param name="stationno"></param>
        /// <param name="taskno"></param>
        /// <param name="barcode"></param>
        /// <param name="toaddress"></param>
        /// <param name="backup"></param>
        /// <returns></returns>
        public BllResult WriteWCSStationDataAddress(Equipment station, OPCHelp plc, string flag, string type, string plcno, string loadstatus, string stationno, string taskno, string barcode, string toaddress, string backup)
        {
            List<EquipmentProp> props = new List<EquipmentProp>();
            var wcsflag = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSFlag");
            wcsflag.Value = flag;
            var wcstype = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSType");
            wcstype.Value = type;
            var wcsplcno = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSPLCNo");
            wcsplcno.Value = plcno;
            var wcsloadstatus = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "LoadStatus");
            wcsloadstatus.Value = loadstatus;
            var wcsstationno = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSStationNo");
            wcsstationno.Value = stationno;
            var wcstaskno = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSTaskNo");
            wcstaskno.Value = taskno;
            var wcsbarcode = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSBarcode");
            wcsbarcode.Value = barcode;
            var wcstoaddress = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSToAddress");
            wcstoaddress.Value = toaddress;
            var wcsbackup = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSBackUp");
            wcsbackup.Value = backup;
            props.Add(wcsflag);
            props.Add(wcstype);
            props.Add(wcsplcno);
            props.Add(wcsloadstatus);
            props.Add(wcsstationno);
            props.Add(wcstaskno);
            props.Add(wcsbarcode);
            props.Add(wcstoaddress);
            props.Add(wcsbackup);
            return plc.WriteAddress(props);
        }

        /// <summary>
        /// 入库口回退
        /// </summary>
        /// <param name="deviceAddresses"></param>
        /// <returns></returns>
        public void StationBack(Equipment station, OPCHelp plc)
        {
            var plcNo = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "PLCNo").Value;
            var stationNo = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "StationNo").Value;
            var taskNo = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "TaskNo").Value;
            var barcode = station.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Barcode").Value;
            var result = WriteWCSStationDataAddress(station, plc, "1", "6", plcNo, "0", stationNo, taskNo, barcode, station.BackAddress, "0");
            if (result.Success)
            {
                Logger.Log($"{station.Name}回退成功", LogLevel.Success);
            }
            else
            {
                Logger.Log($"{station.Name}回退失败", LogLevel.Error); 
            }
        }

        #endregion
    }
}