chr
2024-08-12 e9d7a5ef4c17e4804fb988dd193ff7d1fa36d52b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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<string, string> 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<string> prefixes { get; private set; } = new List<string>();
        [XmlIgnore]
        public List<string> xcSuffixes { get; private set; } = new List<string>();
        [XmlIgnore]
        public List<string> mnSuffixes { get; private set; } = new List<string>();
        [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<string, string>();
            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;
        }
 
        /// <summary>
        /// 强制刷新配置
        /// </summary>
        /// <param name="xmlPath"></param>
        /// <returns></returns>
        public static PluginSetting RefreshFromXml(string xmlPath)
        {
            Instance = LoadFromXml(xmlPath);
            return Instance;
        }
    }
}