BeckhoffModuleIOManager.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using Aitex.Core.RT.DataCenter;
  2. using Aitex.Core.Util;
  3. using DocumentFormat.OpenXml;
  4. using MECF.Framework.Common.Beckhoff.IOAxis;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace MECF.Framework.Common.Beckhoff.ModuleIO
  12. {
  13. public class BeckhoffModuleIOManager : Singleton<BeckhoffModuleIOManager>
  14. {
  15. #region 内部变量
  16. private BeckhoffModuleIOCfg _cfg;
  17. /// <summary>
  18. /// 模块IO字典(key-模块名称,value-beckhoff 变量名称
  19. /// </summary>
  20. private Dictionary<string, string> _moduleNameIODic = new Dictionary<string, string>();
  21. /// <summary>
  22. /// Beckhoff IO-内部模块名称字典
  23. /// </summary>
  24. private Dictionary<string, string> _ioModuleNameDic = new Dictionary<string, string>();
  25. #endregion
  26. /// <summary>
  27. /// 初始化
  28. /// </summary>
  29. /// <param name="xmlPath"></param>
  30. public void Initialize(string xmlPath)
  31. {
  32. _cfg = CustomXmlSerializer.Deserialize<BeckhoffModuleIOCfg>(new FileInfo(xmlPath));
  33. if (_cfg != null)
  34. {
  35. foreach(BeckhoffModule item in _cfg.Modules)
  36. {
  37. foreach (BeckhoffModuleIOInfo io in item.IOs)
  38. {
  39. _moduleNameIODic[io.Name] = io.IOName;
  40. _ioModuleNameDic[io.IOName] = io.Name;
  41. }
  42. }
  43. }
  44. }
  45. /// <summary>
  46. /// 通过获取模块名称获取IO名称
  47. /// </summary>
  48. /// <param name="key"></param>
  49. /// <returns></returns>
  50. public string GetIoNameByInnerModuleName(string key)
  51. {
  52. return _moduleNameIODic.ContainsKey(key) ? _moduleNameIODic[key]:"";
  53. }
  54. /// <summary>
  55. /// 通过IO名称获取模块名称
  56. /// </summary>
  57. /// <param name="key"></param>
  58. /// <returns></returns>
  59. public string GetInnerModuleNameByIOName(string key)
  60. {
  61. return _ioModuleNameDic.ContainsKey(key) ? _ioModuleNameDic[key] : "";
  62. }
  63. /// <summary>
  64. /// 订阅模块IO数值
  65. /// </summary>
  66. /// <param name="ioName"></param>
  67. /// <param name="value"></param>
  68. public void SubscribeModuleIoValue(string ioName,object value)
  69. {
  70. string key = _moduleNameIODic.Values.FirstOrDefault(O => O == ioName);
  71. if(!string.IsNullOrEmpty(key)&&key!=ioName)
  72. {
  73. DATA.Subscribe(key, () => value, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  74. }
  75. }
  76. }
  77. }