ConfigHelper.cs 4.0 KB

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