using PdmSwPlugin.Common.Util; using PdmSwPlugin.Common.Util.Http; using System; using System.Collections.Generic; using System.IO; using System.Reflection; using System.Xml; using System.Xml.Serialization; namespace PdmSwPlugin.Util { public class PluginConfig { private PluginConfig() { } public static PluginConfig Instance { get; private set; } private static readonly PluginConfig DEFAULT_CONFIG = new PluginConfig { Mode = "prod", Timeout = 300, BaseAddress = "http://172.16.18.21:4040/pdm-web/", MaxWorkThreads = 10, MaxWaitThreads = 10, LogDebug = false, SwFilePath = "C:\\temp\\轴承\\滚珠轴承\\自动调心轴承", UserPartsDir = "C:\\Work\\PDM\\plugin", LocalCacheRootPath = "", Prefixes = "C55,P55,L55,LH,SKD,C00", XCSuffixes = "-XC,-HJ,-ZP", MNSuffixes = "-XC,-HJ,-ZP" }; private static PluginConfig LoadConfig() { PluginConfig config = new PluginConfig(); BeanUtil.CopyPropertyIgnoreNull(DEFAULT_CONFIG, config); string LocalDllPath = new DirectoryInfo(Assembly.GetExecutingAssembly().Location).Parent.FullName; string configFileXmlPath = Path.Combine(LocalDllPath, "Config", "setting.xml"); try { if (File.Exists(configFileXmlPath)) { LoadFromXml(configFileXmlPath, config); } else { SerialToFile(config, configFileXmlPath); } } catch (Exception e) { } config.InitClientCreator(); return config; } public static PluginConfig getInstance() { if (Instance == null) { Instance = LoadConfig(); } return Instance; } public static void SerialToFile(PluginConfig data, string filePath) { XmlDocument document = new XmlDocument(); //创建XmlDeclaration节点 XmlDeclaration dec = document.CreateXmlDeclaration("1.0", "utf-8", null); //将节点添加到文件中 document.AppendChild(dec); //创建根节点 XmlNode Root = document.CreateElement("Settings"); document.AppendChild (Root); PropertyInfo[] fields = BeanUtil.GetTypeProperties(typeof(PluginConfig), BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance); foreach (PropertyInfo property in fields) { if (property.GetCustomAttribute(typeof(XmlIgnoreAttribute)) != null) { continue; } string Name = property.Name; object Value = property.GetValue(data); if(Value == null || string.IsNullOrEmpty(Value.ToString())) { continue; } XmlNode node = document.CreateElement (Name); node.InnerText = Value.ToString(); Root.AppendChild(node); } document.Save(filePath); } public static void LoadFromXml(string filePath, PluginConfig config) { XmlDocument doc = new XmlDocument(); doc.Load(filePath); XmlNode xn = doc.SelectSingleNode("Settings"); //得到根节点的所有子节点 XmlNodeList nodes = xn.ChildNodes; foreach (XmlNode node in nodes) { string Name = node.Name; string Value = node.InnerText; PropertyInfo info = typeof(PluginConfig).GetProperty(Name); if (info == null || string.IsNullOrEmpty(Value)) { continue; } if (info.PropertyType == typeof(string)) { info.SetValue(config, Value); } else if (info.PropertyType == typeof(bool) && bool.TryParse(Value, out bool data)) { info.SetValue(config, data); } else if (info.PropertyType == typeof(int) && int.TryParse(Value, out int intData)) { info.SetValue(config, intData); } } } private HttpClientCreator _clientCreator; [XmlIgnore] public HttpClientCreator ClientCreator { get { return _clientCreator; } private set { _clientCreator = value; } } private void InitClientCreator() { ClientCreator = new HttpClientCreator(new HttpConfig(Timeout,BaseAddress)); } private string _Mode; /// /// 模式 /// public string Mode { get { return _Mode; } private set { _Mode = value; } } private int _Timeout = 300; /// /// 请求超时时间 /// public int Timeout { get { return _Timeout; } private set { _Timeout = value; } } private string _BaseAddress; /// /// 请求URK /// public string BaseAddress { get { return _BaseAddress; } private set { _BaseAddress = value; } } private int _MaxWorkThreads; /// /// PDM 多线程 /// public int MaxWorkThreads { get { return _MaxWorkThreads; } private set { _MaxWorkThreads = value; } } private int _MaxWaitThreads; /// /// PDM 多线程 /// public int MaxWaitThreads { get { return _MaxWaitThreads; } private set { _MaxWaitThreads = value; } } private bool _LogDebug; /// /// 开启Debug日志 /// public bool LogDebug { get { return _LogDebug; } private set { _LogDebug = value; } } private string _SwFilePath; /// /// 图纸共享盘文件夹 /// public string SwFilePath { get { return _SwFilePath; } private set { _SwFilePath = value; } } private string _UserPartsDir; /// /// ? /// public string UserPartsDir { get { return _UserPartsDir; } private set { _UserPartsDir = value; } } private string _LocalCacheRootPath; /// /// ? /// public string LocalCacheRootPath { get { return _LocalCacheRootPath; } private set { _LocalCacheRootPath = value; } } private string _Prefixes; /// /// 加工件前缀 /// public string Prefixes { get { return _Prefixes; } private set { _Prefixes = value; if (!string.IsNullOrEmpty(value)) { PrefixSet = new HashSet(value.Split(',')); } else { PrefixSet = new HashSet(); } } } private string _XCSuffixes; /// /// 型材,焊接件后缀 /// public string XCSuffixes { get { return _XCSuffixes; } private set { _XCSuffixes = value; if (!string.IsNullOrEmpty(value)) { XCSuffixSet = new HashSet(value.Split(',')); } else { XCSuffixSet = new HashSet(); } } } private string _MNSuffixes; /// /// 模拟件后缀 /// public string MNSuffixes { get { return _MNSuffixes; } private set { _MNSuffixes = value; if (!string.IsNullOrEmpty(value)) { MNSuffixSet = new HashSet(value.Split(',')); } else { MNSuffixSet = new HashSet(); } } } private HashSet _PrefixSet; [XmlIgnore] public HashSet PrefixSet { get { return _PrefixSet; } private set { _PrefixSet = value; } } private HashSet _XCSuffixSet; [XmlIgnore] public HashSet XCSuffixSet { get { return _XCSuffixSet; } private set { _XCSuffixSet = value; } } private HashSet _MNSuffixSet; [XmlIgnore] public HashSet MNSuffixSet { get { return _MNSuffixSet; } private set { _MNSuffixSet = value; } } } }