| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 | using System;using System.Collections.Generic;using System.Diagnostics;using System.Linq;using System.Threading;using System.Xml;using Aitex.Core.RT.DataCenter;using Aitex.Core.RT.Event;using Aitex.Core.RT.IOCore;using Aitex.Core.RT.Log;using Aitex.Core.Util;using MECF.Framework.Common.Event;namespace MECF.Framework.RT.Core.IoProviders{    public abstract class IoProvider : IIoProvider, IAlarmHandler    {        public string Module { get; set; }        public string Name { get; set; }        public bool IsOpened        {            get { return State == IoProviderStateEnum.Opened; }        }        public IoProviderStateEnum State { get; set; }        public virtual AlarmEventItem DisconnectAlarm { get; set; }        public virtual AlarmEventItem CommunicationErrorAlarm { get; set; }        public event Action<string, AlarmEventItem> OnDeviceAlarmStateChanged;        protected PeriodicJob _thread;        protected IIoBuffer _buffer;        protected R_TRIG _trigError = new R_TRIG();        protected RD_TRIG _trigNotConnected = new RD_TRIG();        protected string _source;        protected XmlElement _nodeParameter;        protected List<IoBlockItem> _blockSections;        protected abstract void SetParameter(XmlElement nodeParameter);        protected abstract void Open();        protected abstract void Close();        protected abstract bool[] ReadDi(int offset, int size);        protected abstract short[] ReadAi(int offset, int size);        protected abstract void WriteDo(int offset, bool[] buffer);        protected abstract void WriteAo(int offset, short[] buffer);        protected virtual float[] ReadAiFloat(int offset, int size)        {            return null;        }        protected virtual void WriteAoFloat(int offset, float[] buffer)        {        }        /// <summary>        /// 单腔体        /// </summary>        /// <param name="module"></param>        /// <param name="name"></param>        /// <param name="lstBuffers"></param>        /// <param name="buffer"></param>        /// <param name="nodeParameter"></param>        /// <param name="ioMappingPathFile"></param>        public virtual void Initialize(string module, string name,List<IoBlockItem> lstBuffers , IIoBuffer buffer, XmlElement nodeParameter, Dictionary<int, string> ioMappingPathFile)        {            Module = module;            Name = name;            _source = module + "." + name;            _buffer = buffer;             _nodeParameter = nodeParameter;            _blockSections = lstBuffers;            buffer.SetBufferBlock(_source, lstBuffers);            buffer.SetIoMap(_source, ioMappingPathFile);            SetParameter(nodeParameter);            State = IoProviderStateEnum.Uninitialized;            _thread = new PeriodicJob(50, OnTimer, name);        }        /// <summary>        /// 多腔体        /// </summary>        /// <param name="module"></param>        /// <param name="name"></param>        /// <param name="lstBuffers"></param>        /// <param name="buffer"></param>        /// <param name="nodeParameter"></param>        /// <param name="ioMappingPathFile"></param>        /// <param name="ioModule"></param>        public virtual void Initialize(string module, string name, List<IoBlockItem> lstBuffers, IIoBuffer buffer, XmlElement nodeParameter, string ioMappingPathFile, string ioModule )        {            Module = module;            Name = name;            _source = module + "." + name;            _buffer = buffer;            _nodeParameter = nodeParameter;            _blockSections = lstBuffers;            buffer.SetBufferBlock(_source, lstBuffers);            buffer.SetIoMapByModule(_source, 0, ioMappingPathFile, ioModule);            SetParameter(nodeParameter);            State = IoProviderStateEnum.Uninitialized;            _thread = new PeriodicJob(50, OnTimer, name);        }        protected void SetState(IoProviderStateEnum newState)        {            State = newState;        }        public void TraceArray(bool[] data)        {            string[] values = new string[data.Length];             for (int i = 0; i < data.Length; i++)            {                values[i++] = data[i] ? "1" : "0";            }            System.Diagnostics.Trace.WriteLine(string.Join(",", values));        }        protected virtual bool OnTimer()        {            if (State == IoProviderStateEnum.Uninitialized)            {                SetState(IoProviderStateEnum.Opening);                Open();            }            if (State == IoProviderStateEnum.Opened)            {                try                {                    bool isAIOFloatType = false;                    foreach (var bufferSection in _blockSections)                    {                        if (bufferSection.Type == IoType.DI)                        {                            bool[] diBuffer = ReadDi(bufferSection.Offset, bufferSection.Size);                            if (diBuffer != null)                            {                                _buffer.SetDiBuffer(_source, bufferSection.Offset, diBuffer);                                //TraceArray(diBuffer);                            }                        }                        else if (bufferSection.Type == IoType.AI)                        {                            short[] aiBuffer = ReadAi(bufferSection.Offset, bufferSection.Size);                            if (aiBuffer != null)                            {                                _buffer.SetAiBuffer(_source, bufferSection.Offset, aiBuffer);                                if (bufferSection.AIOType == typeof(float))                                {                                    isAIOFloatType = true;                                    _buffer.SetAiBufferFloat(_source, bufferSection.Offset, Array.ConvertAll(aiBuffer, x => (float)x).ToArray());                                }                            }                        }                    }                    Dictionary<int, short[]> aos = _buffer.GetAoBuffer(_source);                    if (aos != null)                    {                        foreach (var ao in aos)                        {                            WriteAo(ao.Key, ao.Value);                            if (isAIOFloatType)                                WriteAoFloat(ao.Key, Array.ConvertAll(ao.Value, x => (float)x).ToArray());                        }                    }                    Dictionary<int, bool[]> dos = _buffer.GetDoBuffer(_source);                    if (dos != null)                    {                        foreach (var doo in dos)                        {                            WriteDo(doo.Key, doo.Value);                        }                    }                }                catch (Exception ex)                {                    LOG.Write(ex);                    SetState(IoProviderStateEnum.Error);                    Close();                }            }            _trigError.CLK = State == IoProviderStateEnum.Error;            if (_trigError.Q)            {                EV.PostAlarmLog(Module, $"{_source} error");            }            _trigNotConnected.CLK = State != IoProviderStateEnum.Opened;            if (_trigNotConnected.T)            {                EV.PostInfoLog(Module, $"{_source} connected");            }            if (_trigNotConnected.R)            {                EV.PostWarningLog(Module, $"{_source} not connected");            }            return true;        }        public virtual bool ResetAlarm()        {            return true;        }        public virtual void Reset()        {            _trigError.RST = true;            _trigNotConnected.RST = true;        }         public void Start()        {            if(_thread != null)                _thread.Start();        }        public void Stop()        {            SetState(IoProviderStateEnum.Closing);            Close();            _thread.Stop();        }        public virtual bool SetValue(AOAccessor aoItem, short value)        {            return true;        }        public virtual bool SetValueFloat(AOAccessor aoItem, float value)        {            return true;        }        public virtual bool SetValue(DOAccessor doItem, bool value)        {            return true;        }        protected AlarmEventItem SubscribeAlarm(string name, string description, Func<bool> resetChecker, EventLevel level = EventLevel.Alarm)        {            AlarmEventItem item = new AlarmEventItem(Module, name, description, resetChecker, this);            item.Level = level;            EV.Subscribe(item);            return item;        }        public void AlarmStateChanged(AlarmEventItem args)        {            if (OnDeviceAlarmStateChanged != null)            {                OnDeviceAlarmStateChanged($"{Module}.{Name}", args);            }        }    }}
 |