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;
|
/// <summary>
|
/// 模式
|
/// </summary>
|
public string Mode
|
{
|
get { return _Mode; }
|
private set { _Mode = value; }
|
}
|
|
private int _Timeout = 300;
|
/// <summary>
|
/// 请求超时时间
|
/// </summary>
|
public int Timeout
|
{
|
get { return _Timeout; }
|
private set { _Timeout = value; }
|
}
|
|
private string _BaseAddress;
|
/// <summary>
|
/// 请求URK
|
/// </summary>
|
public string BaseAddress
|
{
|
get { return _BaseAddress; }
|
private set { _BaseAddress = value; }
|
}
|
|
private int _MaxWorkThreads;
|
/// <summary>
|
/// PDM 多线程
|
/// </summary>
|
public int MaxWorkThreads
|
{
|
get { return _MaxWorkThreads; }
|
private set { _MaxWorkThreads = value; }
|
}
|
|
private int _MaxWaitThreads;
|
/// <summary>
|
/// PDM 多线程
|
/// </summary>
|
public int MaxWaitThreads
|
{
|
get { return _MaxWaitThreads; }
|
private set { _MaxWaitThreads = value; }
|
}
|
|
private bool _LogDebug;
|
/// <summary>
|
/// 开启Debug日志
|
/// </summary>
|
public bool LogDebug
|
{
|
get { return _LogDebug; }
|
private set { _LogDebug = value; }
|
}
|
|
private string _SwFilePath;
|
/// <summary>
|
/// 图纸共享盘文件夹
|
/// </summary>
|
public string SwFilePath
|
{
|
get { return _SwFilePath; }
|
private set { _SwFilePath = value; }
|
}
|
|
private string _UserPartsDir;
|
/// <summary>
|
/// ?
|
/// </summary>
|
public string UserPartsDir
|
{
|
get { return _UserPartsDir; }
|
private set { _UserPartsDir = value; }
|
}
|
|
private string _LocalCacheRootPath;
|
/// <summary>
|
/// ?
|
/// </summary>
|
public string LocalCacheRootPath
|
{
|
get { return _LocalCacheRootPath; }
|
private set { _LocalCacheRootPath = value; }
|
}
|
|
private string _Prefixes;
|
/// <summary>
|
/// 加工件前缀
|
/// </summary>
|
public string Prefixes
|
{
|
get { return _Prefixes; }
|
private set
|
{
|
_Prefixes = value;
|
if (!string.IsNullOrEmpty(value))
|
{
|
PrefixSet = new HashSet<string>(value.Split(','));
|
}
|
else {
|
PrefixSet = new HashSet<string>();
|
}
|
}
|
}
|
|
private string _XCSuffixes;
|
/// <summary>
|
/// 型材,焊接件后缀
|
/// </summary>
|
public string XCSuffixes
|
{
|
get { return _XCSuffixes; }
|
private set
|
{
|
_XCSuffixes = value;
|
if (!string.IsNullOrEmpty(value))
|
{
|
XCSuffixSet = new HashSet<string>(value.Split(','));
|
}
|
else
|
{
|
XCSuffixSet = new HashSet<string>();
|
}
|
}
|
}
|
|
private string _MNSuffixes;
|
/// <summary>
|
/// 模拟件后缀
|
/// </summary>
|
public string MNSuffixes
|
{
|
get { return _MNSuffixes; }
|
private set {
|
_MNSuffixes = value;
|
if (!string.IsNullOrEmpty(value))
|
{
|
MNSuffixSet = new HashSet<string>(value.Split(','));
|
}
|
else
|
{
|
MNSuffixSet = new HashSet<string>();
|
}
|
}
|
}
|
|
private HashSet<string> _PrefixSet;
|
[XmlIgnore]
|
public HashSet<string> PrefixSet
|
{
|
get { return _PrefixSet; }
|
private set { _PrefixSet = value; }
|
}
|
|
private HashSet<string> _XCSuffixSet;
|
[XmlIgnore]
|
public HashSet<string> XCSuffixSet
|
{
|
get { return _XCSuffixSet; }
|
private set { _XCSuffixSet = value; }
|
}
|
|
private HashSet<string> _MNSuffixSet;
|
[XmlIgnore]
|
public HashSet<string> MNSuffixSet
|
{
|
get { return _MNSuffixSet; }
|
private set { _MNSuffixSet = value; }
|
}
|
}
|
}
|