MetalPersistentManager.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using Aitex.Common.Util;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.Util;
  4. using MECF.Framework.Common.Persistent.Prewet;
  5. using MECF.Framework.Common.Persistent.Rinse;
  6. using MECF.Framework.Common.Persistent.SRD;
  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.Reservoirs
  15. {
  16. public class MetalPersistentManager : Singleton<MetalPersistentManager>
  17. {
  18. #region 内部变量
  19. private Dictionary<string, MetalPersistentValue> _persistentValueDic = new Dictionary<string, MetalPersistentValue>();
  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= MetalItemManager.Instance.InstalledModules;
  30. foreach (string module in installedModules)
  31. {
  32. string foldStr = PathManager.GetCfgDir() + $"Persistent\\Metals";
  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. MetalPersistentValue metalPersistentValue = CustomXmlSerializer.Deserialize<MetalPersistentValue>(new FileInfo(str));
  42. if (metalPersistentValue != null)
  43. {
  44. _persistentValueDic[module] = metalPersistentValue;
  45. }
  46. }
  47. else
  48. {
  49. MetalPersistentValue persistentValue = new MetalPersistentValue();
  50. persistentValue.Name = module;
  51. _persistentValueDic[module] = persistentValue;
  52. UpdatePersistentValue(module);
  53. }
  54. }
  55. }
  56. catch (Exception ex)
  57. {
  58. LOG.WriteLog(eEvent.ERR_METAL, "System", "Load MetalsPersistent xml exception");
  59. }
  60. }
  61. /// <summary>
  62. /// 获取Metal Persistent数值
  63. /// </summary>
  64. /// <returns></returns>
  65. public MetalPersistentValue GetMetalPersistentValue(string module)
  66. {
  67. if( _persistentValueDic.ContainsKey(module))
  68. {
  69. return _persistentValueDic[module];
  70. }
  71. else
  72. {
  73. return null;
  74. }
  75. }
  76. /// <summary>
  77. /// 更新PersistentValue
  78. /// </summary>
  79. /// <returns></returns>
  80. public void UpdatePersistentValue(string module)
  81. {
  82. if (_persistentValueDic.ContainsKey(module))
  83. {
  84. try
  85. {
  86. CustomXmlSerializer.Serialize(_persistentValueDic[module], _persistentValuePathDic[module]);
  87. }
  88. catch (Exception ex)
  89. {
  90. LOG.WriteLog(eEvent.ERR_METAL, module, "Update MetalsPersistent xml file excepetion");
  91. }
  92. }
  93. else
  94. {
  95. LOG.WriteLog(eEvent.ERR_METAL, module, "Update MetalsPersistent xml file excepetion");
  96. }
  97. }
  98. }
  99. }