chr
7 天以前 43a0207d207390abdeeb3ab9155eebf03edd7b1a
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
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; }
        }
    }
}