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 { #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 _moduleCounterDic = new Dictionary(); private Dictionary _moduleCounterValueDic = new Dictionary(); private Dictionary> _moduleVariableValueDic = new Dictionary>(); private Dictionary _moduleVariableActionDic = new Dictionary(); #endregion #region 属性 public int Size { get { return _size; } set { _size = value; } } #endregion /// /// 初始化 /// /// 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(); } _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; } } } /// /// Counter 管理触发事件 /// /// /// 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); } } } } /// /// 更新变量数值 /// /// /// public void SetCounterValue(string name, object value) { bool _enableRead = SC.GetValue("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); } } } } /// /// 注册变量数值发生变化回调 /// /// /// /// 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]); } } } } /// /// 启动Counter /// /// /// public bool StartCounter(string counterName) { TwincatAdoManager.Instance.WriteValue($"{counterName}.{COUNTER_STOP}", false); return TwincatAdoManager.Instance.WriteValue($"{counterName}.{COUNTER_START}", true); } /// /// 启动Counter /// /// /// public bool StopCounter(string counterName) { TwincatAdoManager.Instance.WriteValue($"{counterName}.{COUNTER_START}", false); return TwincatAdoManager.Instance.WriteValue($"{counterName}.{COUNTER_STOP}", true); } /// /// 重新计数 /// /// /// 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); } /// /// 获取Counter对象 /// /// /// public BeckhoffCounter GetBeckhoffCounter(string counterName) { string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName(counterName); if(_moduleCounterDic.ContainsKey(ioName)) { return _moduleCounterDic[ioName]; } return null; } } }