PLCCoreExtension.cs 4.29 KB
using HHECS.BllModel;
using HHECS.Communication.PLC;
using HHECS.Communication.PLC.PLCComponent;
using HHECS.Model.Entities;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace HHECS.Executor
{
    /// <summary>
    /// PLC读写扩展类
    /// </summary>
    public static class PLCCoreExtension
    {
        public static event Action<EquipmentProp[], long, BllResult> ReadRecord;
        public static event Action<EquipmentProp[], long, BllResult> WriteRecord;

        public static BllResult Reads(this PLCCore plc, params EquipmentProp[] equipmentProps)
        {
            var temp = equipmentProps.Select(t => new PLCAddressObj()
            {
                Index = t.Id,
                IP = t.Equipment.IP,
                Address = t.Address,
                Value = t.Value,
            }).ToList();
            Stopwatch stopwatch = Stopwatch.StartNew();
            var result = plc.Reads(temp);
            stopwatch.Stop();
            if (result.Success)
            {
                temp.ForEach(t => equipmentProps.First(a => a.Id == t.Index).Value = t.Value);
                ReadRecord?.Invoke(equipmentProps, stopwatch.ElapsedMilliseconds, result);
                return BllResultFactory.Success(equipmentProps);
            }
            else
            {
                ReadRecord?.Invoke(equipmentProps, stopwatch.ElapsedMilliseconds, result);
                return BllResultFactory.Error(result.Msg, result.ErrorCode);
            }
        }

        public static BllResult Write(this PLCCore plc, EquipmentProp equipmentProps)
        {
            return Writes(plc, new List<EquipmentProp> { equipmentProps });
        }

        public static BllResult Writes(this PLCCore plc, List<EquipmentProp> equipmentProps)
        {
            return Writes(plc, equipmentProps.ToArray());
        }

        /// <summary>
        /// 写入后再次读取,如果不符合,则报错
        /// </summary>
        /// <param name="plc"></param>
        /// <param name="equipmentProps"></param>
        /// <returns></returns>
        public static BllResult Writes(this PLCCore plc, params EquipmentProp[] equipmentProps)
        {
            var temp = equipmentProps.Select(t => new PLCAddressObj()
            {
                Index = t.Id,
                IP = t.Equipment.IP,
                Address = t.Address,
                Value = t.Value,
            }).ToList();
            Stopwatch stopwatch = Stopwatch.StartNew();
            var result = plc.Writes(temp);
            if (!result.Success)
            {
                return BllResultFactory.Error(result.Msg);
            }
            //foreach (var item in temp)
            //{
            //    var prop = equipmentProps.First(a => a.Id == item.Index);
            //    var tempValue = item.Value;
            //    var result = plc.Write(item);
            //    if (!result.Success)
            //    {
            //        return BllResultFactory.Error(result.Msg, result.ErrorCode);
            //    }
            //    if (prop.EquipmentTypePropTemplateCode != StationProps.WCSReplyMessage.ToString()
            //        && prop.EquipmentTypePropTemplateCode != StationProps.WCSACKMessage.ToString()
            //        && prop.EquipmentTypePropTemplateCode != StationProps.WCSControlMessage.ToString()
            //        && prop.EquipmentTypePropTemplateCode != SRMProps.WCSForkFlag.ToString())
            //    {
            //        result = plc.Read(item);
            //        if (!result.Success)
            //        {
            //            return BllResultFactory.Error($"反读校验出错,属性:{prop.EquipmentTypePropTemplateCode},设备:{prop.Equipment};详情:" + result.Msg, result.ErrorCode);
            //        }
            //        if (item.Value.Trim() != tempValue)
            //        {
            //            return BllResultFactory.Error($"反读校验写入前后不一致,地址{item.Address},属性:{prop.EquipmentTypePropTemplateCode},设备:{prop.Equipment.Code}");
            //        }
            //    }
            //}
            stopwatch.Stop();
            WriteRecord?.Invoke(equipmentProps, stopwatch.ElapsedMilliseconds, BllResultFactory.Success());
            return BllResultFactory.Success(equipmentProps);
        }
    }
}