SRDPersistentManager.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using Aitex.Common.Util;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.Util;
  4. using DocumentFormat.OpenXml;
  5. using MECF.Framework.Common.Persistent.Prewet;
  6. using MECF.Framework.Common.Persistent.Reservoirs;
  7. using MECF.Framework.Common.ToolLayout;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. namespace MECF.Framework.Common.Persistent.SRD
  15. {
  16. public class SRDPersistentManager : Singleton<SRDPersistentManager>
  17. {
  18. #region 内部变量
  19. private Dictionary<string, SRDPersistentValue> _persistentValueDic = new Dictionary<string, SRDPersistentValue>();
  20. private Dictionary<string, string> _persistentValuePathDic = new Dictionary<string, string>();
  21. #endregion
  22. /// <summary>
  23. /// 初始化
  24. /// </summary>
  25. public void Initialize()
  26. {
  27. try
  28. {
  29. List<string> installedModules = SrdItemManager.Instance.InstalledModules;
  30. foreach (string module in installedModules)
  31. {
  32. string foldStr = PathManager.GetCfgDir() + $"Persistent\\SRD";
  33. if (!Directory.Exists(foldStr))
  34. {
  35. Directory.CreateDirectory(foldStr);
  36. }
  37. string str = $"{foldStr}\\{module}Persistent.xml";
  38. _persistentValuePathDic[module] = str;
  39. if (File.Exists(str))
  40. {
  41. SRDPersistentValue srdPersistentValue = CustomXmlSerializer.Deserialize<SRDPersistentValue>(new FileInfo(str));
  42. if (srdPersistentValue != null)
  43. {
  44. _persistentValueDic[module] = srdPersistentValue;
  45. }
  46. }
  47. else
  48. {
  49. SRDPersistentValue persistentValue = new SRDPersistentValue();
  50. persistentValue.Name = module;
  51. _persistentValueDic[module] = persistentValue;
  52. UpdateModulePersistentValue(module);
  53. }
  54. }
  55. }
  56. catch
  57. {
  58. LOG.WriteLog(eEvent.ERR_SRD, "System", "Load SRDPersistent xml exception");
  59. }
  60. }
  61. /// <summary>
  62. /// 获取PersistentValue值
  63. /// </summary>
  64. /// <param name="module"></param>
  65. /// <returns></returns>
  66. public SRDPersistentValue GetModulePersistentValue(string module)
  67. {
  68. return _persistentValueDic.ContainsKey(module)?_persistentValueDic[module]:null;
  69. }
  70. /// <summary>
  71. /// 更新数值
  72. /// </summary>
  73. /// <param name="module"></param>
  74. public void UpdateModulePersistentValue(string module)
  75. {
  76. if (_persistentValueDic.ContainsKey(module))
  77. {
  78. try
  79. {
  80. CustomXmlSerializer.Serialize(_persistentValueDic[module], _persistentValuePathDic[module]);
  81. }
  82. catch (Exception ex)
  83. {
  84. LOG.WriteLog(eEvent.ERR_SRD, module, "Save SRD Persistent xml file excepetion");
  85. }
  86. }
  87. else
  88. {
  89. LOG.WriteLog(eEvent.ERR_METAL, module, "Update SRD Persistent xml file excepetion");
  90. }
  91. }
  92. /// <summary>
  93. /// 更新操作模式状态
  94. /// </summary>
  95. /// <param name="module" operstionMode="currentOperationMode"></param>
  96. /// <returns></returns>
  97. public void UpdateOperationModeValue(string module,string currentOperationMode)
  98. {
  99. _persistentValueDic[module].OperatingMode = currentOperationMode;
  100. try
  101. {
  102. CustomXmlSerializer.Serialize(_persistentValueDic[module], _persistentValuePathDic[module]);
  103. }
  104. catch (Exception ex)
  105. {
  106. LOG.WriteLog(eEvent.ERR_SRD, "System", "Update SRDPersistent xml file excepetion");
  107. }
  108. }
  109. /// <summary>
  110. /// 更新Recipe操作模式状态
  111. /// </summary>
  112. /// <returns></returns>
  113. public void UpdateRecipeOperationModeValue(string module, string currentRecipeOperationMode)
  114. {
  115. _persistentValueDic[module].RecipeOperatingMode = currentRecipeOperationMode;
  116. try
  117. {
  118. CustomXmlSerializer.Serialize(_persistentValueDic[module], _persistentValuePathDic[module]);
  119. }
  120. catch (Exception ex)
  121. {
  122. LOG.WriteLog(eEvent.ERR_SRD, "System", "Update SRDPersistent xml file excepetion");
  123. }
  124. }
  125. }
  126. }