ConfigHelper.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. 
  2. using S7.Net;
  3. using System.Xml.Linq;
  4. namespace xicheji
  5. {
  6. public class ConfigHelper
  7. {
  8. private const string configFileName = "config.xml";
  9. private static Config? _config;
  10. public static Config GetConfig()
  11. {
  12. _config ??= LoadConfig();
  13. return _config;
  14. }
  15. private static Config LoadConfig()
  16. {
  17. if (!File.Exists(configFileName))
  18. {
  19. return new Config();
  20. }
  21. var xdoc = XDocument.Load(configFileName);
  22. if (xdoc.Root == null)
  23. {
  24. return new Config();
  25. }
  26. var config = new Config
  27. {
  28. ServicePort = xdoc.Root!.Element("Service")?.Attribute("Port")?.Value ?? "9999",
  29. DBString = xdoc.Root!.Element("Service")?.Attribute("DBString")?.Value ?? "DataSource=xicheji.db",
  30. Server = xdoc.Root!.Element("Platform")?.Attribute("Server")?.Value ?? "",
  31. UploadInterval = int.Parse(xdoc.Root!.Element("Platform")?.Attribute("ReceiveInterval")?.Value ?? "2"),
  32. };
  33. var device = xdoc.Root!.Element("Device");
  34. config.PLCIP = device?.Attribute("IP")?.Value ?? "";
  35. config.PLCPort = device?.Attribute("Port")?.Value ?? "";
  36. config.PLCType = Enum.Parse<CpuType>(device?.Attribute("Type")?.Value ?? "S7200");
  37. config.PLCReceiveInterval = int.Parse(device?.Attribute("ReceiveInterval")?.Value ?? "2");
  38. config.PLCMacAddress = device?.Attribute("MacAddress")?.Value ?? "";
  39. var camera = xdoc.Root!.Element("Camera");
  40. config.CameraIP = camera?.Attribute("IP")?.Value ?? "";
  41. config.CameraPort = camera?.Attribute("Port")?.Value ?? "";
  42. config.CameraUserName = camera?.Attribute("UserName")?.Value ?? "";
  43. config.CameraPassword = camera?.Attribute("Password")?.Value ?? "";
  44. var variablesRoot = xdoc.Root!.Element("Variables");
  45. if (variablesRoot != null)
  46. {
  47. config.DBName = variablesRoot.Attribute("DBName")?.Value ?? "DB1";
  48. var variables = variablesRoot.Elements("Variable");
  49. foreach (XElement item in variables)
  50. {
  51. var variable = new Variable
  52. {
  53. Name = item.Attribute("Name")?.Value ?? "",
  54. Key = config.DBName + "." + item.Attribute("Key")?.Value ?? "",
  55. Desc = item.Attribute("Desc")?.Value ?? ""
  56. };
  57. if (string.IsNullOrEmpty(variable.Name) || string.IsNullOrEmpty(variable.Key))
  58. {
  59. continue;
  60. }
  61. config.Variables.Add(variable);
  62. }
  63. }
  64. return config;
  65. }
  66. public static void SaveConfig(Config config)
  67. {
  68. var xdoc = XDocument.Load(configFileName);
  69. var xService = xdoc.Root!.Element("Service");
  70. xService?.SetAttributeValue("Port", config.ServicePort);
  71. xService?.SetAttributeValue("DBString", config.DBString);
  72. var xPlatform = xdoc.Root!.Element("Platform");
  73. xPlatform?.SetAttributeValue("Server", config.Server);
  74. xPlatform?.SetAttributeValue("UploadInterval", config.UploadInterval);
  75. var xDevice = xdoc.Root!.Element("Device");
  76. xDevice?.SetAttributeValue("IP", config.PLCIP);
  77. xDevice?.SetAttributeValue("Port", config.PLCPort);
  78. xDevice?.SetAttributeValue("Type", config.PLCType.ToString());
  79. xDevice?.SetAttributeValue("ReceiveInterval", config.PLCReceiveInterval);
  80. xDevice?.SetAttributeValue("MacAddress", config.PLCMacAddress);
  81. var xCamera = xdoc.Root!.Element("Camera");
  82. xCamera?.SetAttributeValue("IP", config.CameraIP);
  83. xCamera?.SetAttributeValue("Port", config.CameraPort);
  84. xCamera?.SetAttributeValue("UserName", config.CameraUserName);
  85. xCamera?.SetAttributeValue("Password", config.CameraPassword);
  86. xdoc.Save(configFileName);
  87. }
  88. public static Variable? FindVariable(string variableName)
  89. {
  90. var config = GetConfig();
  91. var variable = config.Variables.Find(x => x.Name == variableName);
  92. return variable;
  93. }
  94. }
  95. public class Config
  96. {
  97. public string ServicePort { get; set; } = string.Empty;
  98. public string DBString { get; set; } = string.Empty;
  99. public string Server { get; set; } = string.Empty;
  100. public int UploadInterval { get; internal set; }
  101. public string PLCIP { get; set; } = string.Empty;
  102. public string PLCPort { get; set; } = string.Empty;
  103. public CpuType PLCType { get; set; } = CpuType.S7200;
  104. public string PLCMacAddress { get; set; } = string.Empty;
  105. public int PLCReceiveInterval { get; set; } = 2;
  106. public string CameraIP { get; set; } = string.Empty;
  107. public string CameraPort { get; set; } = string.Empty;
  108. public string CameraUserName { get; set; } = string.Empty;
  109. public string CameraPassword { get; set; } = string.Empty;
  110. public string DBName { get; set; } = string.Empty;
  111. public List<Variable> Variables { get; set; } = new();
  112. }
  113. public class Variable
  114. {
  115. public string Name { get; set; } = string.Empty;
  116. public string Key { get; set; } = string.Empty;
  117. public string Desc { get; set; } = string.Empty;
  118. }
  119. }