using PdmSwPlugin.Common.Util; using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using System.Reflection; using System.Xml; using System.Xml.Serialization; namespace PdmSwPlugin.Common.Setting { public class PluginSetting { public static PluginSetting Instance { get; private set; } private Dictionary DataMap; public int TimeOut { get; private set; } = 600; public int MaxWorkThreads { get; private set; } = 10; public int MaxWaitThreads { get; private set; } = 100; public bool LogDebug { get; private set; } = true; public bool ClassicLogin { get; private set; } = true; public string Mode { get; private set; } = "prod"; public string BaseAddress { get; private set; } = ""; public string SwFilePath { get; private set; } = ""; public string UserPartsDir { get; private set; } = ""; public string LocalCacheRootPath { get; private set; } = ""; public string Prefixes { get; private set; } = "C55,P55,L55,LH,SKD,C00"; public string XCSuffixes { get; private set; } = "-XC,-HJ,-ZP"; public string MNSuffixes { get; private set; } = "-MN"; public string Customer { get; private set; } = ""; public string ImportListenName { get; private set; } = ""; public string CheckInListenName { get; private set; } = ""; public int ScanInterval { get; private set; } = 500; public string HistoryDate { get; private set; } = ""; [XmlIgnore] public List prefixes { get; private set; } = new List(); [XmlIgnore] public List xcSuffixes { get; private set; } = new List(); [XmlIgnore] public List mnSuffixes { get; private set; } = new List(); [XmlIgnore] public long HistoryDateFlag { get; private set; } [XmlIgnore] public string this[string field] { get => DataMap?.Get(field); } private void Init() { // 处理加工件前缀 string[] temp = string.IsNullOrEmpty(Prefixes) ? new string[0] : Prefixes.Split(','); foreach (string prefix in temp) { string trim = prefix.Trim(); if (string.IsNullOrEmpty(trim)) continue; prefixes.Add(trim); } // 处理型材后缀 temp = string.IsNullOrEmpty(XCSuffixes) ? new string[0] : XCSuffixes.Split(','); foreach (string suffix in temp) { string trim = suffix.Trim(); if (string.IsNullOrEmpty(trim)) continue; xcSuffixes.Add(trim); } // 处理模拟件后缀 temp = string.IsNullOrEmpty(MNSuffixes) ? new string[0] : MNSuffixes.Split(','); foreach (string suffix in temp) { string trim = suffix.Trim(); if (string.IsNullOrEmpty(trim)) continue; mnSuffixes.Add(trim); } BindingFlags flags = BindingFlags.Public | BindingFlags.Instance; var props = typeof(PluginSetting).GetProperties(flags).Where(prop => !Attribute.IsDefined(prop, typeof(XmlIgnoreAttribute))).ToArray(); DataMap = new Dictionary(); foreach (var prop in props) { string value = prop.GetValue(this).ToString(); DataMap.Add(prop.Name, value); } if (!string.IsNullOrEmpty(HistoryDate)) { try { HistoryDateFlag = long.Parse(HistoryDate); } catch (Exception e) { HistoryDateFlag = -1; } } else { HistoryDateFlag = -1; } } static PluginSetting() { Instance = LoadFromXml(null); } private static PluginSetting LoadFromXml(string xmlPath) { if (xmlPath == null) { string dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); xmlPath = Path.Combine(dir, "Settings", "PluginSetting.xml"); } PluginSetting setting = new PluginSetting(); if (File.Exists(xmlPath)) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlPath); XmlNode xmlRoot = xmlDoc.DocumentElement; BindingFlags flags = BindingFlags.Public | BindingFlags.Instance; var props = typeof(PluginSetting).GetProperties(flags).ToArray(); foreach (var prop in props) { string name = prop.Name; string value = xmlRoot.SelectSingleNode(name)?.InnerText; if (value == null) { continue; } Type type = prop.PropertyType; if (type == typeof(int)) { int.TryParse(value, out int temp); prop.SetValue(setting, temp); } else if (type == typeof(bool)) { bool.TryParse(value, out bool temp); prop.SetValue(setting, temp); } else if (type == typeof(string)) { prop.SetValue(setting, value); } } } setting.Init(); return setting; } /// /// 强制刷新配置 /// /// /// public static PluginSetting RefreshFromXml(string xmlPath) { Instance = LoadFromXml(xmlPath); return Instance; } } }