TCPersistentManager.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using Aitex.Common.Util;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.Util;
  4. using MECF.Framework.Common.Device.ResistivityProbe;
  5. using MECF.Framework.Common.Device.TemperatureController;
  6. using MECF.Framework.Common.Persistent.SRD;
  7. using MECF.Framework.Common.ToolLayout;
  8. using MECF.Framework.Common.Utilities;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.IO.Ports;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. namespace MECF.Framework.Common.Persistent.Temperature
  17. {
  18. public class TCPersistentManager : Singleton<TCPersistentManager>
  19. {
  20. #region 内部变量
  21. private Dictionary<string, TCPersistentValue> _persistentValueDic = new Dictionary<string, TCPersistentValue>();
  22. private Dictionary<string, string> _persistentValuePathDic = new Dictionary<string, string>();
  23. #endregion
  24. /// <summary>
  25. /// 初始化
  26. /// </summary>
  27. public void Initialize()
  28. {
  29. List<string> installedModules = TemperatureItemManager.Instance.InstalledModules;
  30. try
  31. {
  32. string xmlPath = PathManager.GetCfgDir() + "Devices\\SMCCfg.xml";
  33. TemperatureConfig cfg = CustomXmlSerializer.Deserialize<TemperatureConfig>(new FileInfo(xmlPath));
  34. if (cfg != null)
  35. {
  36. foreach (TemperatureDeviceConfig config in cfg.TemperatureDeviceConfigs)
  37. {
  38. if (installedModules!=null&&installedModules.Contains(config.Name))
  39. {
  40. foreach (TemperatureDevice item in config.TemperatureDevices)
  41. {
  42. string module = item.Name;
  43. string foldStr = PathManager.GetCfgDir() + $"Persistent\\TC";
  44. if (!Directory.Exists(foldStr))
  45. {
  46. Directory.CreateDirectory(foldStr);
  47. }
  48. string str = $"{foldStr}\\{module}Persistent.xml";
  49. _persistentValuePathDic[module] = str;
  50. if (File.Exists(str))
  51. {
  52. TCPersistentValue tcPersistentValue = CustomXmlSerializer.Deserialize<TCPersistentValue>(new FileInfo(str));
  53. if (tcPersistentValue != null)
  54. {
  55. _persistentValueDic[module] = tcPersistentValue;
  56. }
  57. }
  58. else
  59. {
  60. TCPersistentValue persistentValue = new TCPersistentValue();
  61. persistentValue.Name = module;
  62. persistentValue.SetPoint = 10;
  63. persistentValue.SetPointLowLimit = 0;
  64. persistentValue.SetPointHighLimit = 50;
  65. _persistentValueDic[module] = persistentValue;
  66. UpdateModulePersistentValue(module);
  67. }
  68. }
  69. }
  70. }
  71. }
  72. }
  73. catch (Exception ex)
  74. {
  75. LOG.WriteLog(eEvent.ERR_TEMPERATURE, "System", "Load TCPersistent xml exception");
  76. }
  77. }
  78. /// <summary>
  79. /// 更新数值
  80. /// </summary>
  81. /// <param name="module"></param>
  82. public void UpdateModulePersistentValue(string module)
  83. {
  84. if (_persistentValueDic.ContainsKey(module))
  85. {
  86. try
  87. {
  88. CustomXmlSerializer.Serialize(_persistentValueDic[module], _persistentValuePathDic[module]);
  89. }
  90. catch (Exception ex)
  91. {
  92. LOG.WriteLog(eEvent.ERR_TEMPERATURE, module, "Save TC Persistent xml file excepetion");
  93. }
  94. }
  95. else
  96. {
  97. LOG.WriteLog(eEvent.ERR_TEMPERATURE, module, "Update TC Persistent xml file excepetion");
  98. }
  99. }
  100. /// <summary>
  101. /// 获取TCPersistent数值
  102. /// </summary>
  103. /// <returns></returns>
  104. public TCPersistentValue GetTCPersistentValue(string module)
  105. {
  106. TCPersistentValue value = new TCPersistentValue();
  107. if (_persistentValueDic.ContainsKey(module) == false)
  108. {
  109. LOG.WriteLog(eEvent.ERR_TEMPERATURE, "System", $"{module} persistentValue is not exist");
  110. }
  111. else
  112. {
  113. value = _persistentValueDic[module];
  114. }
  115. return value;
  116. }
  117. /// <summary>
  118. /// 更新设置值和高\低limit值
  119. /// </summary>
  120. /// <returns></returns>
  121. public void UpdateTemperatureValue(string module, double setPoint, double setPointLowLimit,double setPointHighLimit)
  122. {
  123. _persistentValueDic[module].SetPoint = setPoint;
  124. _persistentValueDic[module].SetPointLowLimit = setPointLowLimit;
  125. _persistentValueDic[module].SetPointHighLimit = setPointHighLimit;
  126. try
  127. {
  128. CustomXmlSerializer.Serialize(_persistentValueDic[module], _persistentValuePathDic[module]);
  129. }
  130. catch (Exception ex)
  131. {
  132. LOG.WriteLog(eEvent.ERR_TEMPERATURE, "System", "Update TCPersistent xml file excepetion");
  133. }
  134. }
  135. }
  136. }