ThorntonConfigManager.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using Aitex.Common.Util;
  2. using Aitex.Core.RT.DataCenter;
  3. using Aitex.Core.Util;
  4. using MECF.Framework.Common.Utilities;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.IO.Ports;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace MECF.Framework.Common.Device.ResistivityProbe
  13. {
  14. public class ThorntonConfigManager : Singleton<ThorntonConfigManager>
  15. {
  16. #region 常量
  17. private const string THORNTON_DEVICELIST = "ThorntonDeviceList";
  18. private const string RESISTIVITY_VALUE = "ResistivityValue";
  19. private const string IS_CONNECTED = "IsConnected";
  20. #endregion
  21. #region 内部变量
  22. /// <summary>
  23. /// 模块设备字典
  24. /// </summary>
  25. private Dictionary<string, ThorntonDeviceConfig> _thorntonDeviceConfigDic = new Dictionary<string, ThorntonDeviceConfig>();
  26. /// <summary>
  27. /// 模块数值字典
  28. /// </summary>
  29. private Dictionary<string, string> _thorntonResistivityValueDic = new Dictionary<string, string>();
  30. /// <summary>
  31. /// 模块串口设备字典
  32. /// </summary>
  33. private Dictionary<string, ThorntonSerialDevice> _thorntonSerialDeviceDic = new Dictionary<string, ThorntonSerialDevice>();
  34. #endregion
  35. /// <summary>
  36. /// 初始化
  37. /// </summary>
  38. public void Initialize()
  39. {
  40. string xmlPath = PathManager.GetCfgDir() + "Devices\\ThorntonCfg.xml";
  41. ThorntonConfig cfg = CustomXmlSerializer.Deserialize<ThorntonConfig>(new FileInfo(xmlPath));
  42. if (cfg != null)
  43. {
  44. foreach (ThorntonDeviceConfig config in cfg.ThorntonDeviceConfigs)
  45. {
  46. DATA.Subscribe($"{config.Name}.{THORNTON_DEVICELIST}", () => config, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  47. ThorntonSerialDevice thorntonSerialDevice = new ThorntonSerialDevice(config.Name, config.Port, config.BaudRate, (StopBits)config.StopBit,
  48. config.Data, SerialPortUtil.GetParity(config.Parity), true);
  49. thorntonSerialDevice.OnDataChanged += ThorntonSerialDevice_OnDataChanged;
  50. _thorntonDeviceConfigDic[config.Name] = config;
  51. foreach(ThorntonDevice item in config.ThorntonDevices)
  52. {
  53. _thorntonResistivityValueDic[item.Name] = "";
  54. DATA.Subscribe($"{item.Name}.{RESISTIVITY_VALUE}", () => _thorntonResistivityValueDic[item.Name], SubscriptionAttribute.FLAG.IgnoreSaveDB);
  55. _thorntonSerialDeviceDic[item.Name] = thorntonSerialDevice;
  56. DATA.Subscribe($"{item.Name}.{IS_CONNECTED}", () => thorntonSerialDevice.Connected,SubscriptionAttribute.FLAG.IgnoreSaveDB);
  57. }
  58. }
  59. }
  60. }
  61. /// <summary>
  62. /// 接收到数据
  63. /// </summary>
  64. /// <param name="name"></param>
  65. /// <param name="lstContent"></param>
  66. private void ThorntonSerialDevice_OnDataChanged(string name, List<string> lstContent)
  67. {
  68. if(_thorntonDeviceConfigDic.ContainsKey(name))
  69. {
  70. ThorntonDeviceConfig thorntonDeviceConfig = _thorntonDeviceConfigDic[name];
  71. for(int i=0;i<thorntonDeviceConfig.ThorntonDevices.Count;i++)
  72. {
  73. ThorntonDevice thorntonDevice = thorntonDeviceConfig.ThorntonDevices[i];
  74. if(_thorntonResistivityValueDic.ContainsKey(thorntonDevice.Name)&&lstContent.Count>i)
  75. {
  76. _thorntonResistivityValueDic[thorntonDevice.Name] = lstContent[i];
  77. }
  78. }
  79. }
  80. }
  81. /// <summary>
  82. /// 初始化设备
  83. /// </summary>
  84. /// <param name="name"></param>
  85. public void InitialDevice(string name)
  86. {
  87. if (_thorntonSerialDeviceDic.ContainsKey(name))
  88. {
  89. _thorntonSerialDeviceDic[name].Start();
  90. }
  91. }
  92. /// <summary>
  93. /// 查询连接状态
  94. /// </summary>
  95. /// <param name="name"></param>
  96. /// <returns></returns>
  97. public bool GetDeviceConnect(string name)
  98. {
  99. if (_thorntonSerialDeviceDic.ContainsKey(name))
  100. {
  101. return _thorntonSerialDeviceDic[name].Connected;
  102. }
  103. return false;
  104. }
  105. /// <summary>
  106. /// 获取数值
  107. /// </summary>
  108. /// <param name="name"></param>
  109. /// <returns></returns>
  110. public string GetResisitivityValueByName(string name)
  111. {
  112. return _thorntonResistivityValueDic.ContainsKey(name) ? _thorntonResistivityValueDic[name] : "";
  113. }
  114. }
  115. }