ParameterViewModel.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Aitex.Core.UI.MVVM;
  6. using Aitex.Core.RT.Log;
  7. using System.Xml;
  8. using Aitex.Core.RT.ConfigCenter;
  9. namespace Aitex.Core.UI.View.Common
  10. {
  11. public class ParameterViewModel : ViewModelBase
  12. {
  13. public ParameterViewModel()
  14. {
  15. }
  16. protected virtual string GetConfigXmlContent()
  17. {
  18. return "";
  19. }
  20. public virtual bool SaveConfigSection(string groupName, List<ConfigEntry> lstEntry)
  21. {
  22. return false;
  23. }
  24. //description, navigation, sectionPath
  25. public List<KeyValuePair<string, string>> GetSectionList()
  26. {
  27. List<KeyValuePair<string, string>> result = new List<KeyValuePair<string, string>>();
  28. string xmlContent = GetConfigXmlContent();
  29. if (string.IsNullOrEmpty(xmlContent))
  30. {
  31. LOG.Error("没有读取到SC配置文件");
  32. return result;
  33. }
  34. XmlDocument doc = new XmlDocument();
  35. doc.LoadXml(xmlContent);
  36. XmlNodeList lst = doc.SelectSingleNode("/SCData").ChildNodes;
  37. foreach (XmlElement node in lst)
  38. {
  39. result.Add(new KeyValuePair<string, string>(GetNodeText(node, "Description"), string.Format("/SCData/Section[@Name='{0}']", GetNodeText(node, "Name"))));
  40. }
  41. return result;
  42. }
  43. public List<ConfigEntry> GetConfigEntries(string sectionPath)
  44. {
  45. List<ConfigEntry> result = new List<ConfigEntry>();
  46. string xmlContent = GetConfigXmlContent();
  47. if (string.IsNullOrEmpty(xmlContent))
  48. {
  49. LOG.Error("没有读取到SC配置文件");
  50. return result;
  51. }
  52. XmlDocument doc = new XmlDocument();
  53. doc.LoadXml(xmlContent);
  54. XmlNode parentNode = doc.SelectSingleNode(sectionPath);
  55. XmlNodeList lstNode = parentNode.ChildNodes;
  56. foreach (XmlNode node in lstNode)
  57. {
  58. try
  59. {
  60. result.Add(new ConfigEntry()
  61. {
  62. EntryName = GetNodeText(node, "Name"),
  63. SectionName = GetNodeText(node, "Name"),
  64. Description = GetNodeText(node, "Description"),
  65. Type = GetNodeText(node, "Type"),
  66. Default = GetNodeText(node, "Default"),
  67. RangeLowLimit = GetNodeText(node, "RangeLowLimit"),
  68. RangeUpLimit = GetNodeText(node, "RangeUpLimit"),
  69. SystemType = GetNodeText(parentNode, "Name"),
  70. Unit = GetNodeText(node, "Unit"),
  71. Value = ((string.IsNullOrEmpty(GetNodeText(node, "Type")) || GetNodeText(node, "Type") == "Double") ?
  72. Math.Round(Convert.ToDouble(GetNodeText(node, "Value")), 3).ToString() : GetNodeText(node, "Value")),
  73. XPath = string.Format("{0}/{1}", sectionPath, node.Name),
  74. });
  75. }
  76. catch (Exception ex)
  77. {
  78. LOG.Error("SC配置项错误," + node.Name, ex);
  79. }
  80. }
  81. return result;
  82. }
  83. string GetNodeText(XmlNode parent, string child, string defaultValue = "")
  84. {
  85. return (parent.Attributes[child] == null) ? defaultValue : parent.Attributes[child].Value;
  86. }
  87. }
  88. }