123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
-
- 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<CpuType>(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<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;
- }
- }
|