BeckhoffCounterManager.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using Aitex.Core.RT.IOCore;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.RT.SCCore;
  4. using Aitex.Core.Util;
  5. using DocumentFormat.OpenXml.Wordprocessing;
  6. using MECF.Framework.Common.Beckhoff.IOAxis;
  7. using MECF.Framework.Common.Beckhoff.ModuleIO;
  8. using MECF.Framework.Common.Equipment;
  9. using MECF.Framework.Common.Utilities;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. namespace MECF.Framework.Common.TwinCat
  16. {
  17. public class BeckhoffCounterManager : Singleton<BeckhoffCounterManager>
  18. {
  19. #region 常量
  20. private const string IO_TYPE = "counter";
  21. private const string COUNTER_VALUE = "CounterValue";
  22. private const string COUNTER_START = "Start";
  23. private const string COUNTER_STOP = "Stop";
  24. private const string COUNTER_RESET = "Reset";
  25. #endregion
  26. #region 内部变量
  27. private BeckhoffCfg _cfg;
  28. private int _size = 0;
  29. private Dictionary<string, BeckhoffCounter> _moduleCounterDic = new Dictionary<string, BeckhoffCounter>();
  30. private Dictionary<string, BeckhoffCounterValue> _moduleCounterValueDic = new Dictionary<string, BeckhoffCounterValue>();
  31. private Dictionary<string, Dictionary<string, object>> _moduleVariableValueDic = new Dictionary<string, Dictionary<string, object>>();
  32. private Dictionary<string, BeckhoffDelegate.OnUpdateModuleVariableValue> _moduleVariableActionDic = new Dictionary<string, BeckhoffDelegate.OnUpdateModuleVariableValue>();
  33. #endregion
  34. #region 属性
  35. public int Size { get { return _size; } set { _size = value; } }
  36. #endregion
  37. /// <summary>
  38. /// 初始化
  39. /// </summary>
  40. /// <param name="cfg"></param>
  41. public void Initialize(BeckhoffCfg cfg)
  42. {
  43. _cfg = cfg;
  44. foreach (BeckhoffCounter item in cfg.Controller.Counters)
  45. {
  46. _moduleCounterDic[item.Name] = item;
  47. string key = item.Name;
  48. string inputName = $"{key}.{COUNTER_VALUE}";
  49. int dataSize = DataTypeUtil.GetDataTypeSize(item.DataType);
  50. if (!_moduleVariableValueDic.ContainsKey(key))
  51. {
  52. _moduleVariableValueDic[key] = new Dictionary<string, object>();
  53. }
  54. _moduleCounterValueDic[inputName] = new BeckhoffCounterValue(item);
  55. _moduleCounterValueDic[inputName].OnUpdateVariableCounterValue += BeckhoffCounterManager_OnUpdateVariableCounterValue;
  56. _moduleVariableValueDic[key][COUNTER_VALUE] = null;
  57. BeckhoffItemManager.Instance.InitBeckhoffItem(inputName, item.Address, item.DataType, item.Scaling, IO_TYPE, dataSize, false, 0, false);
  58. _size += dataSize;
  59. foreach (BeckhoffCounterOutput output in item.Outputs)
  60. {
  61. string outputName = $"{item.Name}.{output.Type}";
  62. _moduleVariableValueDic[item.Name][output.Type] = null;
  63. int writeDataSize = DataTypeUtil.GetDataTypeSize(output.DataType);
  64. BeckhoffItemManager.Instance.InitBeckhoffItem(outputName, output.Address, output.DataType, IO_TYPE, writeDataSize, false, 0, false);
  65. BeckhoffItemManager.Instance.InitWriteBeckoffItem(outputName, output.Address, output.DataType, IO_TYPE, writeDataSize, false, 0, false);
  66. _size += writeDataSize;
  67. }
  68. }
  69. }
  70. /// <summary>
  71. /// Counter 管理触发事件
  72. /// </summary>
  73. /// <param name="name"></param>
  74. /// <param name="counterValue"></param>
  75. private void BeckhoffCounterManager_OnUpdateVariableCounterValue(string name, int counterValue)
  76. {
  77. string[] strAry = name.Split('.');
  78. if (strAry.Length < 2)
  79. {
  80. return;
  81. }
  82. string moduleName = $"{strAry[0]}";
  83. string variableName = name.Replace($"{moduleName}.", "");
  84. string innerModuleName = BeckhoffModuleIOManager.Instance.GetInnerModuleNameByIOName(moduleName);
  85. if (!string.IsNullOrEmpty(innerModuleName))
  86. {
  87. if (_moduleVariableValueDic[moduleName].ContainsKey(variableName))
  88. {
  89. _moduleVariableValueDic[moduleName][variableName] = counterValue;
  90. string str = $"{innerModuleName}.{variableName}";
  91. if (_moduleVariableActionDic.ContainsKey(str))
  92. {
  93. _moduleVariableActionDic[str].Invoke(str, counterValue);
  94. }
  95. }
  96. }
  97. }
  98. /// <summary>
  99. /// 更新变量数值
  100. /// </summary>
  101. /// <param name="name"></param>
  102. /// <param name="value"></param>
  103. public void SetCounterValue(string name, object value)
  104. {
  105. bool _enableRead = SC.GetValue<bool>("Twincat.EnableReadLog");
  106. if (_enableRead)
  107. {
  108. LOG.WriteBackgroundLog(eEvent.INFO_TWINCAT, "System", $"read {name} value {value}");
  109. }
  110. string[] strAry = name.Split('.');
  111. if (strAry.Length < 2)
  112. {
  113. return;
  114. }
  115. string moduleName = $"{strAry[0]}";
  116. string variableName = name.Replace($"{moduleName}.", "");
  117. object setValue = value;
  118. if(_moduleCounterValueDic.ContainsKey(name)&&int.TryParse(value.ToString(),out int intValue))
  119. {
  120. _moduleCounterValueDic[name].SetValue(intValue);
  121. setValue = _moduleCounterValueDic[name].GetValue();
  122. }
  123. string innerModuleName = BeckhoffModuleIOManager.Instance.GetInnerModuleNameByIOName(moduleName);
  124. if (!string.IsNullOrEmpty(innerModuleName))
  125. {
  126. if (_moduleVariableValueDic[moduleName].ContainsKey(variableName))
  127. {
  128. _moduleVariableValueDic[moduleName][variableName] = setValue;
  129. string str = $"{innerModuleName}.{variableName}";
  130. if (_moduleVariableActionDic.ContainsKey(str))
  131. {
  132. _moduleVariableActionDic[str].Invoke(str, setValue);
  133. }
  134. }
  135. }
  136. }
  137. /// <summary>
  138. /// 注册变量数值发生变化回调
  139. /// </summary>
  140. /// <param name="moduleName"></param>
  141. /// <param name="variable"></param>
  142. /// <param name="onUpdateModuleVariableValue"></param>
  143. public void SubscribeModuleVariable(string moduleName, string variable, BeckhoffDelegate.OnUpdateModuleVariableValue onUpdateModuleVariableValue)
  144. {
  145. string name = $"{moduleName}.{variable}";
  146. _moduleVariableActionDic[name] = onUpdateModuleVariableValue;
  147. //根据模块变量名称获取相应的IO变量名称
  148. string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName(moduleName);
  149. if (!string.IsNullOrEmpty(ioName))
  150. {
  151. if (_moduleVariableValueDic.ContainsKey(ioName) && _moduleVariableValueDic[ioName].ContainsKey(variable))
  152. {
  153. if (_moduleVariableValueDic[ioName][variable] != null)
  154. {
  155. _moduleVariableActionDic[name].Invoke($"{moduleName}.{variable}", _moduleVariableValueDic[ioName][variable]);
  156. }
  157. }
  158. }
  159. }
  160. /// <summary>
  161. /// 启动Counter
  162. /// </summary>
  163. /// <param name="counterName"></param>
  164. /// <returns></returns>
  165. public bool StartCounter(string counterName)
  166. {
  167. TwincatAdoManager.Instance.WriteValue($"{counterName}.{COUNTER_STOP}", false);
  168. return TwincatAdoManager.Instance.WriteValue($"{counterName}.{COUNTER_START}", true);
  169. }
  170. /// <summary>
  171. /// 启动Counter
  172. /// </summary>
  173. /// <param name="counterName"></param>
  174. /// <returns></returns>
  175. public bool StopCounter(string counterName)
  176. {
  177. TwincatAdoManager.Instance.WriteValue($"{counterName}.{COUNTER_START}", false);
  178. return TwincatAdoManager.Instance.WriteValue($"{counterName}.{COUNTER_STOP}", true);
  179. }
  180. /// <summary>
  181. /// 重新计数
  182. /// </summary>
  183. /// <param name="counterName"></param>
  184. /// <returns></returns>
  185. public bool ResetCounter(string counterName,uint value)
  186. {
  187. if(_moduleCounterValueDic.ContainsKey($"{counterName}.{COUNTER_VALUE}"))
  188. {
  189. _moduleCounterValueDic[$"{counterName}.{COUNTER_VALUE}"].Reset();
  190. }
  191. return TwincatAdoManager.Instance.WriteValue($"{counterName}.{COUNTER_RESET}", value);
  192. }
  193. /// <summary>
  194. /// 获取Counter对象
  195. /// </summary>
  196. /// <param name="counterName"></param>
  197. /// <returns></returns>
  198. public BeckhoffCounter GetBeckhoffCounter(string counterName)
  199. {
  200. string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName(counterName);
  201. if(_moduleCounterDic.ContainsKey(ioName))
  202. {
  203. return _moduleCounterDic[ioName];
  204. }
  205. return null;
  206. }
  207. }
  208. }