| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 | using Aitex.Core.RT.IOCore;using Aitex.Core.RT.Log;using Aitex.Core.RT.SCCore;using Aitex.Core.Util;using DocumentFormat.OpenXml.Wordprocessing;using MECF.Framework.Common.Beckhoff.IOAxis;using MECF.Framework.Common.Beckhoff.ModuleIO;using MECF.Framework.Common.Equipment;using MECF.Framework.Common.Utilities;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace MECF.Framework.Common.TwinCat{    public class BeckhoffCounterManager : Singleton<BeckhoffCounterManager>    {        #region 常量        private const string IO_TYPE = "counter";        private const string COUNTER_VALUE = "CounterValue";        private const string COUNTER_START = "Start";        private const string COUNTER_STOP = "Stop";        private const string COUNTER_RESET = "Reset";        #endregion        #region 内部变量        private BeckhoffCfg _cfg;         private int _size = 0;        private Dictionary<string, BeckhoffCounter> _moduleCounterDic = new Dictionary<string, BeckhoffCounter>();        private Dictionary<string, BeckhoffCounterValue> _moduleCounterValueDic = new Dictionary<string, BeckhoffCounterValue>();        private Dictionary<string, Dictionary<string, object>> _moduleVariableValueDic = new Dictionary<string, Dictionary<string, object>>();        private Dictionary<string, BeckhoffDelegate.OnUpdateModuleVariableValue> _moduleVariableActionDic = new Dictionary<string, BeckhoffDelegate.OnUpdateModuleVariableValue>();        #endregion        #region 属性        public int Size { get { return _size; } set { _size = value; } }        #endregion        /// <summary>        /// 初始化        /// </summary>        /// <param name="cfg"></param>        public void Initialize(BeckhoffCfg cfg)        {            _cfg = cfg;            foreach (BeckhoffCounter item in cfg.Controller.Counters)            {                _moduleCounterDic[item.Name] = item;                string key = item.Name;                string inputName = $"{key}.{COUNTER_VALUE}";                int dataSize = DataTypeUtil.GetDataTypeSize(item.DataType);                 if (!_moduleVariableValueDic.ContainsKey(key))                {                    _moduleVariableValueDic[key] = new Dictionary<string, object>();                }                _moduleCounterValueDic[inputName] = new BeckhoffCounterValue(item);                _moduleCounterValueDic[inputName].OnUpdateVariableCounterValue += BeckhoffCounterManager_OnUpdateVariableCounterValue;                _moduleVariableValueDic[key][COUNTER_VALUE] = null;                BeckhoffItemManager.Instance.InitBeckhoffItem(inputName, item.Address, item.DataType, item.Scaling, IO_TYPE, dataSize, false, 0, false);                _size += dataSize;                foreach (BeckhoffCounterOutput output in item.Outputs)                {                    string outputName = $"{item.Name}.{output.Type}";                    _moduleVariableValueDic[item.Name][output.Type] = null;                    int writeDataSize = DataTypeUtil.GetDataTypeSize(output.DataType);                    BeckhoffItemManager.Instance.InitBeckhoffItem(outputName, output.Address, output.DataType, IO_TYPE, writeDataSize, false, 0, false);                    BeckhoffItemManager.Instance.InitWriteBeckoffItem(outputName, output.Address, output.DataType, IO_TYPE, writeDataSize, false, 0, false);                    _size += writeDataSize;                }            }        }        /// <summary>        /// Counter 管理触发事件        /// </summary>        /// <param name="name"></param>        /// <param name="counterValue"></param>        private void BeckhoffCounterManager_OnUpdateVariableCounterValue(string name, int counterValue)        {            string[] strAry = name.Split('.');            if (strAry.Length < 2)            {                return;            }            string moduleName = $"{strAry[0]}";            string variableName = name.Replace($"{moduleName}.", "");            string innerModuleName = BeckhoffModuleIOManager.Instance.GetInnerModuleNameByIOName(moduleName);            if (!string.IsNullOrEmpty(innerModuleName))            {                if (_moduleVariableValueDic[moduleName].ContainsKey(variableName))                {                    _moduleVariableValueDic[moduleName][variableName] = counterValue;                    string str = $"{innerModuleName}.{variableName}";                    if (_moduleVariableActionDic.ContainsKey(str))                    {                        _moduleVariableActionDic[str].Invoke(str, counterValue);                    }                }            }        }        /// <summary>        /// 更新变量数值        /// </summary>        /// <param name="name"></param>        /// <param name="value"></param>        public void SetCounterValue(string name, object value)        {            bool _enableRead = SC.GetValue<bool>("Twincat.EnableReadLog");            if (_enableRead)            {                LOG.WriteBackgroundLog(eEvent.INFO_TWINCAT, "System", $"read {name} value {value}");            }            string[] strAry = name.Split('.');            if (strAry.Length < 2)            {                return;            }            string moduleName = $"{strAry[0]}";            string variableName = name.Replace($"{moduleName}.", "");            object setValue = value;            if(_moduleCounterValueDic.ContainsKey(name)&&int.TryParse(value.ToString(),out int intValue))            {                _moduleCounterValueDic[name].SetValue(intValue);                setValue = _moduleCounterValueDic[name].GetValue();            }            string innerModuleName = BeckhoffModuleIOManager.Instance.GetInnerModuleNameByIOName(moduleName);            if (!string.IsNullOrEmpty(innerModuleName))            {                if (_moduleVariableValueDic[moduleName].ContainsKey(variableName))                {                    _moduleVariableValueDic[moduleName][variableName] = setValue;                    string str = $"{innerModuleName}.{variableName}";                    if (_moduleVariableActionDic.ContainsKey(str))                    {                        _moduleVariableActionDic[str].Invoke(str, setValue);                    }                }            }        }        /// <summary>        /// 注册变量数值发生变化回调        /// </summary>        /// <param name="moduleName"></param>        /// <param name="variable"></param>        /// <param name="onUpdateModuleVariableValue"></param>        public void SubscribeModuleVariable(string moduleName, string variable, BeckhoffDelegate.OnUpdateModuleVariableValue onUpdateModuleVariableValue)        {            string name = $"{moduleName}.{variable}";            _moduleVariableActionDic[name] = onUpdateModuleVariableValue;            //根据模块变量名称获取相应的IO变量名称            string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName(moduleName);            if (!string.IsNullOrEmpty(ioName))            {                if (_moduleVariableValueDic.ContainsKey(ioName) && _moduleVariableValueDic[ioName].ContainsKey(variable))                {                    if (_moduleVariableValueDic[ioName][variable] != null)                    {                        _moduleVariableActionDic[name].Invoke($"{moduleName}.{variable}", _moduleVariableValueDic[ioName][variable]);                    }                }            }        }        /// <summary>        /// 启动Counter        /// </summary>        /// <param name="counterName"></param>        /// <returns></returns>        public bool StartCounter(string counterName)        {            TwincatAdoManager.Instance.WriteValue($"{counterName}.{COUNTER_STOP}", false);            return TwincatAdoManager.Instance.WriteValue($"{counterName}.{COUNTER_START}", true);        }        /// <summary>        /// 启动Counter        /// </summary>        /// <param name="counterName"></param>        /// <returns></returns>        public bool StopCounter(string counterName)        {            TwincatAdoManager.Instance.WriteValue($"{counterName}.{COUNTER_START}", false);            return TwincatAdoManager.Instance.WriteValue($"{counterName}.{COUNTER_STOP}", true);        }        /// <summary>        /// 重新计数        /// </summary>        /// <param name="counterName"></param>        /// <returns></returns>        public bool ResetCounter(string counterName,uint value)        {            if(_moduleCounterValueDic.ContainsKey($"{counterName}.{COUNTER_VALUE}"))            {                _moduleCounterValueDic[$"{counterName}.{COUNTER_VALUE}"].Reset();            }            return TwincatAdoManager.Instance.WriteValue($"{counterName}.{COUNTER_RESET}", value);        }        /// <summary>        /// 获取Counter对象        /// </summary>        /// <param name="counterName"></param>        /// <returns></returns>        public BeckhoffCounter GetBeckhoffCounter(string counterName)        {            string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName(counterName);            if(_moduleCounterDic.ContainsKey(ioName))            {                return _moduleCounterDic[ioName];            }            return null;        }    }}
 |