-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCamstarCommon.cs
More file actions
482 lines (428 loc) · 16 KB
/
CamstarCommon.cs
File metadata and controls
482 lines (428 loc) · 16 KB
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web;
using InSiteXmlClient4Core.Api;
using InSiteXmlClient4Core.InterFace;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
namespace InSiteXmlClient4Core
{
public class CamstarCommon
{
private CsiClient _client;
private ICsiConnection _connection;
private ICsiSession _session;
private ICsiDocument _document;
private ICsiService _service;
private Guid _sessionId;
public Guid SessionId => _sessionId;
public static IConfiguration Configuration { get; set; }
/// <summary>
/// 初始化
/// </summary>
/// <param name="factory">工厂</param>
public CamstarCommon(string factory,IConfiguration configuration)
{
if (string.IsNullOrEmpty(factory))
{
throw new Exception("工厂参数不能为空");
}
var model = GetConfigModel(factory,configuration);
if (model is InsiteLoginModel loginModel)
{
_session = null;
_client = null;
_client = new CsiClient();
_connection = null;
_sessionId = Guid.NewGuid();
_connection = _client.CreateConnection(loginModel.Host, loginModel.Port);
_session = _connection.CreateSession(loginModel.User, loginModel.Password, _sessionId.ToString());
}
}
private InsiteLoginModel GetConfigModel(string factory ,IConfiguration configuration=null)
{
if (configuration!=null)
{
Configuration = configuration;
}
else
{
Configuration = new ConfigurationBuilder()
.Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true })
.Build();
}
var model = new InsiteLoginModel();
model.Host = Configuration["MESFactory:" + factory + ":Host"];
model.Port =Convert.ToInt32( Configuration["MESFactory:" + factory + ":Port"]??"2281");
model.User= Configuration["MESFactory:" + factory + ":User"];
model.Password= Configuration["MESFactory:" + factory + ":Password"];
return model;
}
///// <summary>
///// 初始化
///// </summary>
///// <param name="factory">工厂</param>
///// <param name="employee"></param>
///// <param name="password"></param>
//public CamstarCommon(string factory,string employee,string password)
//{
// var model = GetConfigModel(factory);
// if (model is InsiteLoginModel loginModel)
// {
// loginModel.Password = password;
// loginModel.User = employee;
// _session = null;
// _client = null;
// _client = new CsiClient();
// _connection = null;
// _sessionId = Guid.NewGuid();
// _connection = _client.CreateConnection(loginModel.Host, loginModel.Port);
// _session = _connection.CreateSession(loginModel.User, loginModel.Password, _sessionId.ToString());
// }
//}
/// <summary>
///
/// </summary>
/// <param name="host"></param>
/// <param name="port"></param>
/// <param name="userName"></param>
/// <param name="password"></param>
public CamstarCommon(string host, int port, string userName, string password)
{
_session = null;
_client = null;
_client = new CsiClient();
_connection = null;
_sessionId = Guid.NewGuid();
_connection = _client.CreateConnection(host, port);
_session = _connection.CreateSession(userName, password, _sessionId.ToString());
}
#region Document操作
/// <summary>
/// 打印Document文档
/// </summary>
public void Print()
{
_document.Print(true);
}
/// <summary>
/// 提交文档
/// </summary>
/// <returns></returns>
public ICsiDocument Submit()
{
return _document.Submit();
}
/// <summary>
/// 提交并返回实体
/// </summary>
/// <typeparam name="T">泛型实体类</typeparam>
/// <returns></returns>
public T SubmitAndRequestModel<T>() where T : class, new()
{
var model = new T();
var properties = typeof(T).GetProperties();
var requestData = RequestData();
foreach (var p in properties)
{
requestData.RequestField(p.Name);
}
var requestDoc = Submit();
if (requestDoc.CheckErrors()) return null;
foreach (var field in requestDoc.GetService().ResponseData().GetResponseFields())
{
var fieldType = field.GetType().Name;
var name = ((ICsiXmlElement)field).GetElementName();
var p = properties.FirstOrDefault(m => m.Name.ToLower() == name.ToLower());
if (p == null)
{
continue;
}
switch (fieldType)
{
case "CsiDataField":
var data1 = (ICsiDataField)field;
p.SetValue(model, Convert.ChangeType(data1.GetValue(), p.PropertyType));
break;
case "CsiDataList":
var data2 = (ICsiDataList)field;
if (data2.HasChildren())
{
}
break;
case "CsiNamedObject":
var data3 = (ICsiNamedObject)field;
p.SetValue(model, Convert.ChangeType(data3.GetRef(), p.PropertyType));
break;
case "CsiNamedObjectList":
var data4 = (ICsiNamedObjectList)field;
if (data4.HasChildren())
{
p.GetSetMethod().Invoke(model, new[] { (from ICsiNamedObject item in data4.GetListItems() select item.GetRef()).ToList() });
}
break;
case "CsiRevisionedObject":
var data5 = (ICsiRevisionedObject)field;
p.SetValue(model, Convert.ChangeType(data5.GetName(), p.PropertyType));
break;
case "CsiRevisionedObjectList":
var data6 = (ICsiRevisionedObjectList)field;
if (data6.HasChildren())
{
p.GetSetMethod().Invoke(model, new[] { (from ICsiRevisionedObject item in data6.GetListItems() select item.GetName()).ToList() });
}
break;
case "CsiContainer":
var data7 = (ICsiContainer)field;
p.SetValue(model, Convert.ChangeType(data7.GetName(), p.PropertyType));
break;
case "CsiContainerList":
var data8 = (ICsiContainerList)field;
if (data8.HasChildren())
{
p.GetSetMethod().Invoke(model, new[] { (from ICsiContainer item in data8.GetListItems() select item.GetName()).ToList() });
}
break;
}
}
return model;
}
/// <summary>
/// 创建文档和服务
/// </summary>
/// <param name="documentName">文档名称</param>
/// <param name="serviceName">服务名称</param>
public void CreateDocumentAndService(string documentName, string serviceName)
{
if (!string.IsNullOrEmpty(documentName.Trim()))
{
_session.RemoveDocument(documentName);
if (_service != null)
{
_service = null;
}
_document = _session.CreateDocument(documentName);
if (!string.IsNullOrEmpty(serviceName.Trim()))
{
_service = _document.CreateService(serviceName);
}
}
}
/// <summary>
/// 建立查询
/// </summary>
/// <returns></returns>
public ICsiQuery CreateQuery()
{
return _document.CreateQuery();
}
#endregion
#region Service操作封装
/// <summary>
/// 执行service的setExecute
/// </summary>
public void Execute()
{
_service.SetExecute();
}
/// <summary>
/// 执行service的setExecute,并提交文档到服务器,获取返回结果和信息
/// </summary>
/// <param name="action">获取信息的方法</param>
/// <returns></returns>
public bool Execute(Action<string> action)
{
Execute();
RequestData().RequestField("CompletionMsg");
var responseDocument = Submit();
return !responseDocument.CheckErrors();
}
/// <summary>
/// 执行service的setExecute,并提交文档到服务器,获取返回结果信息
/// </summary>
/// <returns></returns>
public (bool Status, string Message) ExecuteResult()
{
try
{
Execute();
RequestData().RequestField("CompletionMsg");
var responseDocument = Submit();
var b = false;
var msg = "";
b = !responseDocument.CheckErrors(s => msg = s);
return (Status: b, Message: msg);
}
catch (Exception e)
{
throw new Exception(e.Message + ":request:" + this._document.AsXml() + "\nresponse:" +
this._document.ResponseData()?.GetOwnerDocument()?.AsXml());
;
}
}
/// <summary>
/// 指定service执行的事件
/// </summary>
/// <param name="type">事件类型</param>
/// <returns></returns>
public ICsiPerform Perform(PerformType type)
{
switch (type)
{
case PerformType.Load:
return _service.Perform("Load");
case PerformType.New:
return _service.Perform("New");
case PerformType.Change:
return _service.Perform("Load");
case PerformType.Delete:
return _service.Perform("delete");
case PerformType.NewRev:
return _service.Perform("NewRev");
default:
return null;
}
}
/// <summary>
/// 执行事件
/// </summary>
/// <param name="evenName">事件名称</param>
/// <returns></returns>
public ICsiPerform Perform(string evenName)
{
return _service.Perform(evenName);
}
/// <summary>
/// 返回service的requestData
/// </summary>
/// <returns></returns>
public ICsiRequestData RequestData()
{
return _service.RequestData();
}
/// <summary>
/// 创建InputData
/// </summary>
/// <returns></returns>
public ICsiObject InputData()
{
return _service.InputData();
}
/// <summary>
/// 创建服务,默认文档名称为服务名加Doc
/// </summary>
/// <param name="serviceName">服务名称</param>
public void CreateService(string serviceName)
{
CreateDocumentAndService(serviceName + "Doc", serviceName);
}
private void GetConfig()
{
Configuration = new ConfigurationBuilder()
.Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true })
.Build();
}
/// <summary>
/// 更改命名对象
/// </summary>
/// <typeparam name="name"></typeparam>
/// <returns></returns>
public ICsiNamedObject Changes(string name)
{
this.InputData().NamedObjectField("ObjectToChange").SetRef(name);
this.Perform(PerformType.Load);
return this.InputData().NamedObjectField("ObjectChanges");
}
/// <summary>
/// 删除命名对象
/// </summary>
/// <typeparam name="name"></typeparam>
/// <returns></returns>
public (bool Status, string Message) Delete(string name)
{
this.InputData().NamedObjectField("ObjectToChange").SetRef(name);
this.Perform(PerformType.Delete);
return this.ExecuteResult();
}
/// <summary>
/// 更改版本对象
/// </summary>
/// <param name="name">名称</param>
/// <param name="rev">版本</param>
/// <param name="useRor">是否使用默认版本</param>
public ICsiRevisionedObject Changes(string name,string rev,bool useRor)
{
this.InputData().RevisionedObjectField("ObjectToChange").SetRef(name,rev,useRor);
this.Perform(PerformType.Load);
return this.InputData().RevisionedObjectField("ObjectChanges");
}
/// <summary>
/// 删除版本对象
/// </summary>
/// <param name="name">名称</param>
/// <param name="rev">版本</param>
/// <param name="deleteAllRev">删除所有</param>
public (bool Status, string Message) Delete(string name, string rev,bool deleteAllRev=false)
{
this.InputData().RevisionedObjectField("ObjectToChange").SetRef(name, rev, false);
this.Perform(PerformType.Delete);
return this.ExecuteResult();
}
/// <summary>
/// 新建ObjectChanges对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public ICsiNamedObject New(string name)
{
this.Perform(PerformType.New);
var objectChanges= this.InputData().NamedObjectField("ObjectChanges");
objectChanges.DataField("Name").SetValue(name);
return objectChanges;
}
/// <summary>
/// 新建ObjectChanges对象
/// </summary>
/// <param name="name">名称</param>
/// <param name="rev">版本</param>
/// <param name="useRor">是否设置为默认版本</param>
public ICsiRevisionedObject New(string name, string rev, bool useRor)
{
this.Perform(PerformType.New);
var objectChanges = this.InputData().RevisionedObjectField("ObjectChanges");
objectChanges.DataField("Name").SetValue(name);
objectChanges.DataField("Revision").SetValue(rev);
objectChanges.DataField("IsRevofRcd").SetValue(useRor);
return objectChanges;
}
#endregion
public enum PerformType
{
/// <summary>
/// 载入
/// </summary>
Load,
/// <summary>
/// 新建
/// </summary>
New,
/// <summary>
/// 修改
/// </summary>
Change,
/// <summary>
/// 删除
/// </summary>
Delete,
/// <summary>
/// 添加新版本
/// </summary>
NewRev
}
}
}