ConfigHelper.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. Server = xdoc.Root!.Element("Platform")?.Attribute("Server")?.Value ?? ""
  30. };
  31. var device = xdoc.Root!.Element("Device");
  32. config.PLCIP = device?.Attribute("IP")?.Value ?? "";
  33. config.PLCPort = device?.Attribute("Port")?.Value ?? "";
  34. config.PLCType = Enum.Parse<CpuType>(device?.Attribute("Type")?.Value ?? "S7200");
  35. config.PLCMacAddress = device?.Attribute("MacAddress")?.Value ?? "";
  36. config.PLCReceiveInterval = int.Parse(device?.Attribute("ReceiveInterval")?.Value ?? "2");
  37. var camera = xdoc.Root!.Element("Camera");
  38. config.CameraIP = camera?.Attribute("IP")?.Value ?? "";
  39. config.CameraPort = camera?.Attribute("Port")?.Value ?? "";
  40. config.CameraUserName = camera?.Attribute("UserName")?.Value ?? "";
  41. config.CameraPassword = camera?.Attribute("Password")?.Value ?? "";
  42. var variablesRoot = xdoc.Root!.Element("Variables");
  43. if (variablesRoot != null)
  44. {
  45. config.DBName = variablesRoot.Attribute("DBName")?.Value ?? "DB1";
  46. var variables = variablesRoot.Elements("Variable");
  47. foreach (XElement item in variables)
  48. {
  49. var variable = new Variable
  50. {
  51. Name = item.Attribute("Name")?.Value ?? "",
  52. Key = config.DBName + "." + item.Attribute("Key")?.Value ?? "",
  53. Desc = item.Attribute("Desc")?.Value ?? ""
  54. };
  55. if (string.IsNullOrEmpty(variable.Name) || string.IsNullOrEmpty(variable.Key))
  56. {
  57. continue;
  58. }
  59. config.Variables.Add(variable);
  60. }
  61. }
  62. return config;
  63. }
  64. public static void SaveConfig(Config config)
  65. {
  66. //TODO: 写入xml
  67. }
  68. public static Variable? FindVariable(string variableName)
  69. {
  70. var config = GetConfig();
  71. var variable = config.Variables.Find(x => x.Name == variableName);
  72. return variable;
  73. }
  74. }
  75. public class Config
  76. {
  77. public string ServicePort { get; set; } = string.Empty;
  78. public string Server { get; set; } = string.Empty;
  79. public string PLCIP { get; set; } = string.Empty;
  80. public string PLCPort { get; set; } = string.Empty;
  81. public CpuType PLCType { get; set; } = CpuType.S7200;
  82. public string PLCMacAddress { get; set; } = string.Empty;
  83. public int PLCReceiveInterval { get; set; } = 2;
  84. public string CameraIP { get; set; } = string.Empty;
  85. public string CameraPort { get; set; } = string.Empty;
  86. public string CameraUserName { get; set; } = string.Empty;
  87. public string CameraPassword { get; set; } = string.Empty;
  88. public string DBName { get; set; } = string.Empty;
  89. public List<Variable> Variables { get; set; } = new();
  90. }
  91. public class Variable
  92. {
  93. public string Name { get; set; } = string.Empty;
  94. public string Key { get; set; } = string.Empty;
  95. public string Desc { get; set; } = string.Empty;
  96. }
  97. }