DeviceConfigManager.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Xml;
  7. using Aitex.Core.RT.Log;
  8. using Aitex.Core.Util;
  9. using Npgsql;
  10. namespace Aitex.Core.RT.ConfigCenter
  11. {
  12. public class DeviceConfigManager2 : Singleton<DeviceConfigManager2>
  13. {
  14. public bool HasBoostPump { get; set; }
  15. public bool HasThrottleValve { get; set; }
  16. public void Initialize()
  17. {
  18. string fileDefault = string.Format("{0}\\config\\DeviceConfig.default.xml", Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName));
  19. string fileCurrent = string.Format("{0}\\config\\DeviceConfig.xml", Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName));
  20. if (!File.Exists(fileDefault) && !File.Exists(fileCurrent))
  21. throw new ApplicationException("没有找到DeviceConfig配置文件。\r\n"+fileDefault + "\r\n或者" + fileCurrent);
  22. if (File.Exists(fileDefault))
  23. {
  24. XmlDocument doc = new XmlDocument();
  25. doc.Load(fileDefault);
  26. try
  27. {
  28. ParseNode(doc );
  29. }
  30. catch (Exception ex)
  31. {
  32. LOG.WriteExeption(ex);
  33. }
  34. }
  35. if (File.Exists(fileCurrent))
  36. {
  37. XmlDocument doc = new XmlDocument();
  38. doc.Load(fileCurrent);
  39. try
  40. {
  41. ParseNode(doc );
  42. }
  43. catch (Exception ex)
  44. {
  45. LOG.WriteExeption(ex);
  46. }
  47. }
  48. CONFIG.Subscribe("Device", "HasBoostPump", () => HasBoostPump);
  49. CONFIG.Subscribe("Device", "HasThrottleValve", () => HasThrottleValve);
  50. }
  51. public bool ParseBoolenNode(XmlDocument doc, string type)
  52. {
  53. var node = doc.SelectSingleNode(string.Format("DeviceConfig/DeviceConfigItem[@Type='{0}']", type));
  54. if (node == null)
  55. return false;
  56. var element = node as XmlElement;
  57. if (element == null)
  58. return false;
  59. bool result = false;
  60. if (element.HasAttribute("Value") && !string.IsNullOrEmpty(element.GetAttribute("Value")))
  61. {
  62. if (!bool.TryParse(element.GetAttribute("Value").Trim(), out result))
  63. {
  64. //LOG.Write(string.Format("Error format in device config item, type {0}, value {1}", type, element.GetAttribute("Value").Trim()));
  65. }
  66. }
  67. return result;
  68. }
  69. public void ParseNode(XmlDocument doc)
  70. {
  71. HasBoostPump = ParseBoolenNode(doc, "HasBoostPump");
  72. HasThrottleValve = ParseBoolenNode(doc, "HasThrottleValve");
  73. }
  74. void Update(string type, string value)
  75. {
  76. switch (type)
  77. {
  78. case "HasBoostPump" :
  79. HasBoostPump = bool.Parse(value);
  80. break;
  81. case "HasThrottleValve" :
  82. HasThrottleValve = bool.Parse(value);
  83. break;
  84. }
  85. }
  86. public void SetConfig(string type, string value)
  87. {
  88. string fileCurrent = string.Format("{0}\\config\\DeviceConfig.xml", Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName));
  89. XmlDocument doc = new XmlDocument();
  90. if (File.Exists(fileCurrent))
  91. {
  92. doc.Load(fileCurrent);
  93. }
  94. else
  95. {
  96. doc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\" ?><DeviceConfig></DeviceConfig>");
  97. }
  98. var nodeRoot = doc.SelectSingleNode(string.Format("DeviceConfig"));
  99. if (nodeRoot == null)
  100. {
  101. nodeRoot = doc.CreateElement("DeviceConfig");
  102. doc.AppendChild(nodeRoot);
  103. }
  104. var node = doc.SelectSingleNode(string.Format("DeviceConfig/DeviceConfigItem[@Type='{0}']", type)) as XmlElement;
  105. if (node == null)
  106. {
  107. node = doc.CreateElement("DeviceConfigItem");
  108. nodeRoot.AppendChild(node);
  109. }
  110. node.SetAttribute("Type", type);
  111. node.SetAttribute("Value", value);
  112. doc.Save(fileCurrent);
  113. Update(type, value);
  114. }
  115. }
  116. }