ConfigHelper.cs 3.8 KB

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