123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
-
- using S7.Net;
- using System.Xml.Linq;
- namespace xicheji
- {
- public class ConfigHelper
- {
- private const string configFileName = "config.xml";
- private static Config? _config;
- public static Config GetConfig()
- {
- _config ??= LoadConfig();
- return _config;
- }
- private static Config LoadConfig()
- {
- if (!File.Exists(configFileName))
- {
- return new Config();
- }
- var xdoc = XDocument.Load(configFileName);
- if (xdoc.Root == null)
- {
- return new Config();
- }
- var config = new Config
- {
- ServicePort = xdoc.Root!.Element("Service")?.Attribute("Port")?.Value ?? "9999",
- Server = xdoc.Root!.Element("Platform")?.Attribute("Server")?.Value ?? ""
- };
- var device = xdoc.Root!.Element("Device");
- config.PLCIP = device?.Attribute("IP")?.Value ?? "";
- config.PLCPort = device?.Attribute("Port")?.Value ?? "";
- config.PLCType = Enum.Parse<CpuType>(device?.Attribute("Type")?.Value ?? "S7200");
- config.PLCMacAddress = device?.Attribute("MacAddress")?.Value ?? "";
- config.PLCReceiveInterval = int.Parse(device?.Attribute("ReceiveInterval")?.Value ?? "2");
- var camera = xdoc.Root!.Element("Camera");
- config.CameraIP = camera?.Attribute("IP")?.Value ?? "";
- config.CameraPort = camera?.Attribute("Port")?.Value ?? "";
- config.CameraUserName = camera?.Attribute("UserName")?.Value ?? "";
- config.CameraPassword = camera?.Attribute("Password")?.Value ?? "";
- var variablesRoot = xdoc.Root!.Element("Variables");
- if (variablesRoot != null)
- {
- config.DBName = variablesRoot.Attribute("DBName")?.Value ?? "DB1";
- var variables = variablesRoot.Elements("Variable");
- foreach (XElement item in variables)
- {
- var variable = new Variable
- {
- Name = item.Attribute("Name")?.Value ?? "",
- Key = config.DBName + "." + item.Attribute("Key")?.Value ?? "",
- Desc = item.Attribute("Desc")?.Value ?? ""
- };
- if (string.IsNullOrEmpty(variable.Name) || string.IsNullOrEmpty(variable.Key))
- {
- continue;
- }
- config.Variables.Add(variable);
- }
- }
- return config;
- }
- public static void SaveConfig(Config config)
- {
- //TODO: 写入xml
- }
- public static Variable? FindVariable(string variableName)
- {
- var config = GetConfig();
- var variable = config.Variables.Find(x => x.Name == variableName);
- return variable;
- }
- }
- public class Config
- {
- public string ServicePort { get; set; } = string.Empty;
- public string Server { get; set; } = string.Empty;
- public string PLCIP { get; set; } = string.Empty;
- public string PLCPort { get; set; } = string.Empty;
- public CpuType PLCType { get; set; } = CpuType.S7200;
- public string PLCMacAddress { get; set; } = string.Empty;
- public int PLCReceiveInterval { get; set; } = 2;
- public string CameraIP { get; set; } = string.Empty;
- public string CameraPort { get; set; } = string.Empty;
- public string CameraUserName { get; set; } = string.Empty;
- public string CameraPassword { get; set; } = string.Empty;
- public string DBName { get; set; } = string.Empty;
- public List<Variable> Variables { get; set; } = new();
- }
- public class Variable
- {
- public string Name { get; set; } = string.Empty;
- public string Key { get; set; } = string.Empty;
- public string Desc { get; set; } = string.Empty;
- }
- }
|