ReplenPersistentManager.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using Aitex.Common.Util;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.Util;
  4. using MECF.Framework.Common.ToolLayout;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace MECF.Framework.Common.Persistent.Reservoirs
  13. {
  14. public class ReplenPersistentManager : Singleton<ReplenPersistentManager>
  15. {
  16. #region 内部变量
  17. private Dictionary<string, Dictionary<string, ReplenPersistentValue>> _persistentValueDic = new Dictionary<string, Dictionary<string, ReplenPersistentValue>>();
  18. private Dictionary<string, Dictionary<string, string>> _persistentValuePathDic = new Dictionary<string, Dictionary<string, string>>();
  19. private int _replenNum = 0;
  20. #endregion
  21. /// <summary>
  22. /// 初始化
  23. /// </summary>
  24. public void Initialize()
  25. {
  26. try
  27. {
  28. List<string> installedModules = ReservoirItemManager.Instance.InstalledModules;
  29. foreach (string module in installedModules)
  30. {
  31. ReservoirItem reservoirItem = ReservoirItemManager.Instance.GetReservoirItem(module);
  32. if (reservoirItem.ChemReplenType == "" || reservoirItem.ChemReplenPumps == 0)
  33. {
  34. continue;
  35. }
  36. else
  37. {
  38. _persistentValueDic[module] = new Dictionary<string, ReplenPersistentValue>();
  39. _persistentValuePathDic[module] = new Dictionary<string, string>();
  40. _replenNum = reservoirItem.ChemReplenPumps;
  41. string foldStr = $"{PathManager.GetCfgDir()}Persistent\\Reservoirs\\{module}DosingSystem";
  42. if (!Directory.Exists(foldStr))
  43. {
  44. Directory.CreateDirectory(foldStr);
  45. }
  46. for (int i = 0; i < _replenNum; i++)
  47. {
  48. string replenName = "Replen"+(i+1).ToString();
  49. string str = $"{foldStr}\\{replenName}Persistent.xml";
  50. _persistentValuePathDic[module][replenName] = str;
  51. if (File.Exists(str))
  52. {
  53. ReplenPersistentValue replenPersistentValue = CustomXmlSerializer.Deserialize<ReplenPersistentValue>(new FileInfo(str));
  54. if (replenPersistentValue != null)
  55. {
  56. _persistentValueDic[module][replenName] = replenPersistentValue;
  57. }
  58. }
  59. else
  60. {
  61. ReplenPersistentValue persistentValue = new ReplenPersistentValue();
  62. persistentValue.ReplenPumpFactor = 1;
  63. persistentValue.ReplenName = replenName;
  64. persistentValue.AutoDosingStartAmpHour = 0;
  65. persistentValue.AutoDosingStartTime = DateTime.MinValue;
  66. persistentValue.IsDosingRunning = false;
  67. persistentValue.CurrentDosingVolume = 0;
  68. persistentValue.TargetDosingVolume = 0;
  69. persistentValue.RemainDosingVolume = 0;
  70. persistentValue.OperatingMode = "Manual";
  71. _persistentValueDic[module][replenName] = persistentValue;
  72. UpdatePersistentValue(module, replenName);
  73. }
  74. }
  75. }
  76. }
  77. }
  78. catch (Exception ex)
  79. {
  80. LOG.WriteLog(eEvent.ERR_RESERVOIR, "System", "Load ReplenPersistent xml exception");
  81. }
  82. }
  83. /// <summary>
  84. /// 获取Replen Persistent数值
  85. /// </summary>
  86. /// <returns></returns>
  87. public ReplenPersistentValue GetReplenPersistentValue(string module, string replenName)
  88. {
  89. if (_persistentValueDic.ContainsKey(module) && _persistentValueDic[module].ContainsKey(replenName))
  90. {
  91. return _persistentValueDic[module][replenName];
  92. }
  93. else
  94. {
  95. return null;
  96. }
  97. }
  98. /// <summary>
  99. /// 更新PersistentValue
  100. /// </summary>
  101. /// <returns></returns>
  102. public void UpdatePersistentValue(string module, string replenName)
  103. {
  104. if (_persistentValueDic.ContainsKey(module) && _persistentValueDic[module].ContainsKey(replenName))
  105. {
  106. try
  107. {
  108. CustomXmlSerializer.Serialize(_persistentValueDic[module][replenName], _persistentValuePathDic[module][replenName]);
  109. }
  110. catch (Exception ex)
  111. {
  112. LOG.WriteLog(eEvent.ERR_RESERVOIR, module, "Update ReplenPersistent xml file excepetion");
  113. }
  114. }
  115. else
  116. {
  117. LOG.WriteLog(eEvent.ERR_RESERVOIR, module, "Update ReplenPersistent xml file excepetion");
  118. }
  119. }
  120. }
  121. }