PLCCoreExtension.cs 5.3 KB
using HHECS.BllModel;
using HHECS.Communication.PLC;
using HHECS.Communication.PLC.PLCComponent;
using HHECS.Executor.EquipmentHandler;
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()) ///按照轮询的顺序,这里重新读,也不会被流程刷成 0
                //{
                    result = plc.Read(item);
                    if (!result.Success)
                    {
                        return BllResultFactory.Error($"反读校验出错,属性:{prop.EquipmentTypePropTemplateCode},设备:{prop.Equipment};详情:" + result.Msg, result.ErrorCode);
                    }
                    if (item.Value.Trim() != tempValue)
                    {
                        //item.Value = tempValue;
                        //var result1 = plc.Write(item); ///写两次确保可以写成功
                        //if (!result.Success)
                        //{
                        //    return BllResultFactory.Error(result.Msg, result.ErrorCode);
                        //}
                        //else
                        //{
                        //    if (item.Value.Trim() != tempValue)
                        //    {
                        return BllResultFactory.Error($"反读校验写入前后不一致,地址{item.Address},属性:{prop.EquipmentTypePropTemplateCode},设备:{prop.Equipment.Code}");
                        //    }
                        //}
                    }
                    //else {
                    //    //再写一遍
                    //    item.Value= tempValue;
                    //    result = plc.Read(item);
                    //    if (!result.Success)
                    //    {
                    //        return BllResultFactory.Error($"反读校验出错,属性:{prop.EquipmentTypePropTemplateCode},设备:{prop.Equipment};详情:" + result.Msg, result.ErrorCode);
                    //    }

                    //}
              //  }
            }
            stopwatch.Stop();
            WriteRecord?.Invoke(equipmentProps, stopwatch.ElapsedMilliseconds, BllResultFactory.Success());
            return BllResultFactory.Success(equipmentProps);
        }
    }
}