BeckhoffIOManager.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using FestoDebugger.Common;
  2. using FestoDebugger.Service;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using TwinCAT.TypeSystem;
  9. namespace FestoDebugger.Beckoff
  10. {
  11. public class BeckhoffIOManager : Singleton<BeckhoffIOManager>
  12. {
  13. #region 内部常量
  14. private const string DI_ITEMLIST = "System.DIItemList";
  15. private const string DO_ITEMLIST = "System.DOItemList";
  16. private const string DIGITAL = "Digital";
  17. #endregion
  18. #region 内部变量
  19. private Dictionary<string, BeckhoffDIAccessor> _diMap = new Dictionary<string, BeckhoffDIAccessor>();
  20. private Dictionary<string, BeckhoffDOAccessor> _doMap = new Dictionary<string, BeckhoffDOAccessor>();
  21. private Dictionary<string, List<NotifiableIoItem>> _ioItemList = new Dictionary<string, List<NotifiableIoItem>>();
  22. private int _size = 0;
  23. /// <summary>
  24. /// 模块变量更新委托字典(key-模块.变量,value-变量委托事件
  25. /// </summary>
  26. private Dictionary<string, BeckhoffDelegate.OnUpdateModuleVariableValue> _moduleVariableActionDic = new Dictionary<string, BeckhoffDelegate.OnUpdateModuleVariableValue>();
  27. /// <summary>
  28. /// 变量数值字典(key-io变量名称,value-数值)
  29. /// </summary>
  30. private Dictionary<string, object> _nameVariableValueDic = new Dictionary<string, object>();
  31. /// <summary>
  32. /// do对应地址(多个do对应同一址)
  33. /// </summary>
  34. private List<string> _doAddress = new List<string>();
  35. #endregion
  36. #region 属性
  37. /// <summary>
  38. /// IO数据长度
  39. /// </summary>
  40. public int Size { get { return _size; } }
  41. /// <summary>
  42. /// 模块变量-数值字典
  43. /// </summary>
  44. private Dictionary<string, object> _moduleNameVariableValueDic = new Dictionary<string, object>();
  45. #endregion
  46. /// <summary>
  47. /// 初始化
  48. /// </summary>
  49. /// <param name="cfg"></param>
  50. public void Initialize(BeckhoffCfg cfg)
  51. {
  52. SubscribeIoItemList();
  53. InitialAccessors(cfg);
  54. }
  55. public Dictionary<string, object> GetNameValueDic()
  56. {
  57. _moduleNameVariableValueDic.Clear();
  58. List<string> keys = _nameVariableValueDic.Keys.ToList();
  59. foreach (string item in keys)
  60. {
  61. if(!_nameVariableValueDic.ContainsKey(item))
  62. {
  63. continue;
  64. }
  65. string ModuleName = BeckhoffModuleIOManager.Instance.GetInnerModuleNameByIOName(item);
  66. _moduleNameVariableValueDic.Add(ModuleName, _nameVariableValueDic[item]);
  67. }
  68. return _moduleNameVariableValueDic;
  69. }
  70. /// <summary>
  71. /// 初始化
  72. /// </summary>
  73. /// <param name="cfg"></param>
  74. private void InitialAccessors(BeckhoffCfg cfg)
  75. {
  76. foreach (BeckhoffInput item in cfg.Controller.Inputs)
  77. {
  78. string key = $"{item.Name}";
  79. if (item.Type == DIGITAL)
  80. {
  81. if (!_diMap.ContainsKey(key))
  82. {
  83. BeckhoffDIAccessor accessor = new BeckhoffDIAccessor(key, item.Address, item.Scaling);
  84. _diMap[key] = accessor;
  85. _ioItemList[DI_ITEMLIST].Add(new NotifiableIoItem()
  86. {
  87. Address = accessor.Address,
  88. Name = key
  89. });
  90. BeckhoffItemManager.Instance.InitBeckhoffItem(key, item.Address, item.DataType, "di", 1, false, 0, item.Invert);
  91. _size++;
  92. }
  93. }
  94. }
  95. foreach (BeckhoffOutput item in cfg.Controller.Outputs)
  96. {
  97. string key = $"{item.Name}";
  98. if (item.Type == DIGITAL)
  99. {
  100. if (!_doMap.ContainsKey(key))
  101. {
  102. BeckhoffDOAccessor accessor = new BeckhoffDOAccessor(key, item.Address, item.Scaling, item.BitOperated, item.Bit);
  103. _doMap[key] = accessor;
  104. _ioItemList[DO_ITEMLIST].Add(new NotifiableIoItem()
  105. {
  106. Address = accessor.Address,
  107. Name = key
  108. });
  109. BeckhoffItemManager.Instance.InitBeckhoffItem(key, item.Address, item.DataType, "do", 1, item.BitOperated, item.Bit, item.Invert);
  110. BeckhoffItemManager.Instance.InitWriteBeckoffItem(key, item.Address, item.DataType, "do", 1, item.BitOperated, item.Bit, item.Invert);
  111. _size++;
  112. }
  113. }
  114. }
  115. }
  116. /// <summary>
  117. /// 写DO变量数值
  118. /// </summary>
  119. /// <param name="arg1"></param>
  120. /// <param name="args"></param>
  121. /// <returns></returns>
  122. public bool WriteDoOperation(string arg1, object[] args)
  123. {
  124. string moduleName = (string)args[0];
  125. bool setpoint = (bool)args[1];
  126. string IOName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName(moduleName);
  127. BeckhoffItem item = BeckhoffItemManager.Instance.GetWriteBeckhoffItem(IOName);
  128. return TwincatAdoManager.Instance.WriteValue(IOName, setpoint);
  129. }
  130. /// <summary>
  131. /// 订阅IO集合
  132. /// </summary>
  133. private void SubscribeIoItemList()
  134. {
  135. if (!_ioItemList.ContainsKey(DI_ITEMLIST))
  136. {
  137. _ioItemList[DI_ITEMLIST] = new List<NotifiableIoItem>();
  138. }
  139. if (!_ioItemList.ContainsKey(DO_ITEMLIST))
  140. {
  141. _ioItemList[DO_ITEMLIST] = new List<NotifiableIoItem>();
  142. }
  143. }
  144. #region 获取Accessor和Item
  145. public BeckhoffDIAccessor GetDIAccessor(string name)
  146. {
  147. return _diMap.ContainsKey(name) ? _diMap[name] : null;
  148. }
  149. public BeckhoffDOAccessor GetDOAccessor(string name)
  150. {
  151. return _doMap.ContainsKey(name) ? _doMap[name] : null;
  152. }
  153. #endregion
  154. /// <summary>
  155. /// 注册变量数值发生变化回调
  156. /// </summary>
  157. /// <param name="moduleName"></param>
  158. /// <param name="variable"></param>
  159. /// <param name="onUpdateModuleVariableValue"></param>
  160. public void SubscribeModuleVariable(string moduleName, string variable, BeckhoffDelegate.OnUpdateModuleVariableValue onUpdateModuleVariableValue)
  161. {
  162. string name = $"{moduleName}.{variable}";
  163. _moduleVariableActionDic[name] = onUpdateModuleVariableValue;
  164. //根据模块变量名称获取相应的IO变量名称
  165. string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName(name);
  166. if (!string.IsNullOrEmpty(ioName))
  167. {
  168. if (_nameVariableValueDic.ContainsKey(ioName))
  169. {
  170. _moduleVariableActionDic[name].Invoke(variable, _nameVariableValueDic[ioName]);
  171. }
  172. }
  173. }
  174. /// <summary>
  175. /// 更新IO数值
  176. /// </summary>
  177. /// <param name="name"></param>
  178. /// <param name="value"></param>
  179. public void UpdateIoValue(string name, object value)
  180. {
  181. _nameVariableValueDic[name] = value;
  182. //根据io变量名称获取模块变量名称
  183. string innerModuleName = BeckhoffModuleIOManager.Instance.GetInnerModuleNameByIOName(name);
  184. if (!string.IsNullOrEmpty(innerModuleName))
  185. {
  186. if (_moduleVariableActionDic.ContainsKey(innerModuleName))
  187. {
  188. string[] strAry = innerModuleName.Split('.');
  189. if (strAry.Length != 0)
  190. {
  191. _moduleVariableActionDic[innerModuleName].Invoke(strAry[strAry.Length - 1], value);
  192. }
  193. }
  194. }
  195. }
  196. }
  197. }