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", DBString = xdoc.Root!.Element("Service")?.Attribute("DBString")?.Value ?? "DataSource=xicheji.db", Server = xdoc.Root!.Element("Platform")?.Attribute("Server")?.Value ?? "", UploadInterval = int.Parse(xdoc.Root!.Element("Platform")?.Attribute("ReceiveInterval")?.Value ?? "2"), }; var device = xdoc.Root!.Element("Device"); config.PLCIP = device?.Attribute("IP")?.Value ?? ""; config.PLCPort = device?.Attribute("Port")?.Value ?? ""; config.PLCType = Enum.Parse(device?.Attribute("Type")?.Value ?? "S7200"); config.PLCReceiveInterval = int.Parse(device?.Attribute("ReceiveInterval")?.Value ?? "2"); config.PLCMacAddress = device?.Attribute("MacAddress")?.Value ?? ""; 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) { var xdoc = XDocument.Load(configFileName); var xService = xdoc.Root!.Element("Service"); xService?.SetAttributeValue("Port", config.ServicePort); xService?.SetAttributeValue("DBString", config.DBString); var xPlatform = xdoc.Root!.Element("Platform"); xPlatform?.SetAttributeValue("Server", config.Server); xPlatform?.SetAttributeValue("UploadInterval", config.UploadInterval); var xDevice = xdoc.Root!.Element("Device"); xDevice?.SetAttributeValue("IP", config.PLCIP); xDevice?.SetAttributeValue("Port", config.PLCPort); xDevice?.SetAttributeValue("Type", config.PLCType.ToString()); xDevice?.SetAttributeValue("ReceiveInterval", config.PLCReceiveInterval); xDevice?.SetAttributeValue("MacAddress", config.PLCMacAddress); var xCamera = xdoc.Root!.Element("Camera"); xCamera?.SetAttributeValue("IP", config.CameraIP); xCamera?.SetAttributeValue("Port", config.CameraPort); xCamera?.SetAttributeValue("UserName", config.CameraUserName); xCamera?.SetAttributeValue("Password", config.CameraPassword); xdoc.Save(configFileName); } 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 DBString { get; set; } = string.Empty; public string Server { get; set; } = string.Empty; public int UploadInterval { get; internal set; } 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 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; } }