-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
924 lines (835 loc) · 52.1 KB
/
Program.cs
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Xml;
using System.Xml.Schema;
using Azure;
using Azure.DigitalTwins.Core;
using Azure.Identity;
namespace CreateAndInitialization
{
struct Equipment
{
public string EquipmentID;
public string EquipmentTagName;
public string EquipmentComponentClass;
public string[] NodeID;
public string ChildEquipmentID;
public string ChildEquipmentComponentClass;
public string ChildTagName;
public Position Position;
public GenericAttribute[] EquipmentGenericAttribute;
}
struct ProcessInstrumentationFunction
{
public string ProcessInstrumentationFunctionID;
public string ProcessInstrumentationFunctionTagName;
public string ProcessInstrumentationFunctionComponentClass;
public GenericAttribute[] ProcessInstrumentationFunctionGenericAttribute;
}
struct PipingNetworkSegment
{
public string PipingNetworkSegmentID;
public string PipingNetworkSegmentTagName;
public string PipingNetworkSegmentComponentClass;
public List<Equipment> PipingNetworkSegmentComponent;
public List<Connection> PipingNetworkSegmentConnection;
public GenericAttribute[] PipingNetworkSegmentGenericAttribute;
}
struct Connection
{
public string ConnectionFromID;
public string ConnectionFromNode;
public string ConnectionToID;
public string ConnectionToNode;
}
struct Position
{
public Cooridnate Location;
public Cooridnate Axis;
public Cooridnate Reference;
}
struct Cooridnate
{
public string x;
public string y;
public string z;
}
struct GenericAttribute
{
public string GenericAttributeName;
public string GenericAttributeFormat;
public string GenericAttributeValue;
}
class XMLExtract
{
public static string tokenLocation = @"D:\Study\LRZSyncShare\Semesterarbeit -- Yu Mu\Material\MyJogurt\token.txt";
private static string xmlLocation = @"D:\Study\LRZSyncShare\Semesterarbeit -- Yu Mu\Material\MyJogurt\DEXPI\kopie.xml";
public static List<Equipment> EQ = new List<Equipment>();
public static List<PipingNetworkSegment> PNS = new List<PipingNetworkSegment>();
public static List<ProcessInstrumentationFunction> PIF = new List<ProcessInstrumentationFunction>();
// Extract the information from DEXPI XML, and save in two types of struct respectively.
public static async Task AddTwin()
{
XmlDocument Document = new XmlDocument();
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = true;//Ignore commends in the documents
XmlReader reader = XmlReader.Create(xmlLocation, settings);
Document.Load(reader);
string token;
// Read token from local file
using (StreamReader readtext = new StreamReader(tokenLocation))
{
token = readtext.ReadLine();
}
// select root node
XmlNode root = Document.SelectSingleNode("PlantModel");
// get all child nodes of root node
if (root != null)
{
XmlElement PlantModel = (XmlElement)root;
XmlNodeList PlantModelChild = PlantModel.ChildNodes;
for (int a = 0; a < PlantModelChild.Count; a++)
{
string pointname1 = PlantModelChild.Item(a).Name;
if (pointname1 == "Equipment")
{
Equipment newEQ = new Equipment();
newEQ.EquipmentID = PlantModelChild.Item(a).Attributes["ID"].Value;
newEQ.EquipmentComponentClass = PlantModelChild.Item(a).Attributes["ComponentClass"].Value;
newEQ.EquipmentTagName = PlantModelChild.Item(a).Attributes["TagName"].Value;
XmlNodeList EquipmentChild = PlantModelChild.Item(a).ChildNodes;
for (int b = 0; b < EquipmentChild.Count; b++)
{
string pointname2 = EquipmentChild.Item(b).Name;
if (pointname2 == "ConnectionPoints")
{
XmlNodeList Node = EquipmentChild.Item(b).ChildNodes;
newEQ.NodeID = new string[Node.Count];
for (int c = 0; c < Node.Count; c++)
{
newEQ.NodeID[c] = Node.Item(c).Attributes["ID"].Value;
}
}
else if (pointname2 == "GenericAttributes")
{
XmlNodeList GenericAttributes = EquipmentChild.Item(b).ChildNodes;
newEQ.EquipmentGenericAttribute = new GenericAttribute[GenericAttributes.Count];
for (int c = 0; c < GenericAttributes.Count; c++)
{
string pointname3 = GenericAttributes.Item(c).Name;
if (pointname3 == "GenericAttribute")
{
newEQ.EquipmentGenericAttribute[c].GenericAttributeName = GenericAttributes.Item(c).Attributes["Name"].Value;
newEQ.EquipmentGenericAttribute[c].GenericAttributeFormat = GenericAttributes.Item(c).Attributes["Units"].Value; // incert units into Format
newEQ.EquipmentGenericAttribute[c].GenericAttributeValue = GenericAttributes.Item(c).Attributes["Value"].Value;
}
}
}
else if (pointname2 == "Equipment")
{
newEQ.ChildEquipmentID = EquipmentChild.Item(b).Attributes["ID"].Value;
newEQ.ChildEquipmentComponentClass = EquipmentChild.Item(b).Attributes["ComponentClass"].Value;
if (EquipmentChild.Item(b).Attributes["TagName"] is null)
{
newEQ.ChildTagName = "";
}
else
{
newEQ.ChildTagName = EquipmentChild.Item(b).Attributes["TagName"].Value;
}
}
}
EQ.Add(newEQ);
}
else if (pointname1 == "PipingNetworkSystem")
{
XmlNodeList PipingNetworkSegment = PlantModelChild.Item(a).ChildNodes;
for (int b = 0; b < PipingNetworkSegment.Count; b++)
{
string pointname2 = PipingNetworkSegment.Item(b).Name;
if (pointname2 == "PipingNetworkSegment")
{
PipingNetworkSegment newPNS = new PipingNetworkSegment();
newPNS.PipingNetworkSegmentComponentClass = PipingNetworkSegment.Item(b).Attributes["ComponentClass"].Value;
newPNS.PipingNetworkSegmentID = PipingNetworkSegment.Item(b).Attributes["ID"].Value;
newPNS.PipingNetworkSegmentTagName = PipingNetworkSegment.Item(b).Attributes["TagName"].Value;
newPNS.PipingNetworkSegmentComponent = new List<Equipment>();
newPNS.PipingNetworkSegmentConnection = new List<Connection>();
XmlNodeList PipingNetworkSegmentChild = PipingNetworkSegment.Item(b).ChildNodes;
for (int c = 0; c < PipingNetworkSegmentChild.Count; c++)
{
string pointname3 = PipingNetworkSegmentChild.Item(c).Name;
if (pointname3 == "GenericAttributes")
{
XmlNodeList GenericAttributes = PipingNetworkSegmentChild.Item(c).ChildNodes;
newPNS.PipingNetworkSegmentGenericAttribute = new GenericAttribute[GenericAttributes.Count];
for (int d = 0; d < GenericAttributes.Count; d++)
{
string pointname4 = GenericAttributes.Item(d).Name;
if (pointname4 == "GenericAttribute")
{
if (GenericAttributes.Item(d).Attributes["Name"] is null)
{
newPNS.PipingNetworkSegmentGenericAttribute[d].GenericAttributeName = "";
}
else
{
newPNS.PipingNetworkSegmentGenericAttribute[d].GenericAttributeName = GenericAttributes.Item(d).Attributes["Name"].Value;
}
if (GenericAttributes.Item(d).Attributes["Format"] is null)
{
newPNS.PipingNetworkSegmentGenericAttribute[d].GenericAttributeFormat = "";
}
else
{
newPNS.PipingNetworkSegmentGenericAttribute[d].GenericAttributeFormat = GenericAttributes.Item(d).Attributes["Format"].Value;
}
if (GenericAttributes.Item(d).Attributes["Value"] is null)
{
newPNS.PipingNetworkSegmentGenericAttribute[d].GenericAttributeValue = "";
}
else
{
newPNS.PipingNetworkSegmentGenericAttribute[d].GenericAttributeValue = GenericAttributes.Item(d).Attributes["Value"].Value;
}
}
}
}
else if (pointname3 == "PipingComponent")
{
Equipment newComponent = new Equipment();
newComponent.EquipmentID = PipingNetworkSegmentChild.Item(c).Attributes["ID"].Value;
newComponent.EquipmentComponentClass = PipingNetworkSegmentChild.Item(c).Attributes["ComponentClass"].Value;
newComponent.EquipmentTagName = PipingNetworkSegmentChild.Item(c).Attributes["TagName"].Value;
XmlNodeList PipingComponent = PipingNetworkSegmentChild.Item(c).ChildNodes;
for (int d = 0; d < PipingComponent.Count; d++)
{
string pointname4 = PipingComponent.Item(d).Name;
if (pointname4 == "ConnectionPoints")
{
XmlNodeList Node = PipingComponent.Item(d).ChildNodes;
newComponent.NodeID = new string[Node.Count];
for (int e = 0; e < Node.Count; e++)
{
string pointname5 = Node.Item(e).Name;
if (pointname5 == "Node")
{
newComponent.NodeID[e] = Node.Item(e).Attributes["ID"].Value;
}
}
}
else if (pointname4 == "GenericAttributes")
{
XmlNodeList GenericAttributes = PipingComponent.Item(d).ChildNodes;
newComponent.EquipmentGenericAttribute = new GenericAttribute[GenericAttributes.Count];
for (int e = 0; e < GenericAttributes.Count; e++)
{
string pointname5 = GenericAttributes.Item(e).Name;
if (pointname5 == "GenericAttribute")
{
if (GenericAttributes.Item(e).Attributes["Name"] is null)
{
newComponent.EquipmentGenericAttribute[e].GenericAttributeName = "";
}
else
{
newComponent.EquipmentGenericAttribute[e].GenericAttributeName = GenericAttributes.Item(e).Attributes["Name"].Value;
}
if (GenericAttributes.Item(e).Attributes["Value"] is null)
{
newComponent.EquipmentGenericAttribute[e].GenericAttributeValue = "";
}
else
{
newComponent.EquipmentGenericAttribute[e].GenericAttributeValue = GenericAttributes.Item(e).Attributes["Value"].Value;
}
if (GenericAttributes.Item(e).Attributes["Format"] is null)
{
newComponent.EquipmentGenericAttribute[e].GenericAttributeFormat = "";
}
else
{
newComponent.EquipmentGenericAttribute[e].GenericAttributeFormat = GenericAttributes.Item(e).Attributes["Format"].Value;
}
}
}
}
}
newPNS.PipingNetworkSegmentComponent.Add(newComponent);
}
else if (pointname3 == "Connection")
{
Connection newConnection = new Connection();
if (PipingNetworkSegmentChild.Item(c).Attributes["FromID"] is null)
{
newConnection.ConnectionFromID = "";
}
else
{
newConnection.ConnectionFromID = PipingNetworkSegmentChild.Item(c).Attributes["FromID"].Value;
}
if (PipingNetworkSegmentChild.Item(c).Attributes["FromNode"] is null)
{
newConnection.ConnectionFromNode = "";
}
else
{
newConnection.ConnectionFromNode = PipingNetworkSegmentChild.Item(c).Attributes["FromNode"].Value;
}
if (PipingNetworkSegmentChild.Item(c).Attributes["ToID"] != null) // how to extend to anthoer variable?
{
newConnection.ConnectionToID = PipingNetworkSegmentChild.Item(c).Attributes["ToID"].Value;
}
else
{
newConnection.ConnectionToID = "";
}
if (PipingNetworkSegmentChild.Item(c).Attributes["ToNode"] != null)
{
newConnection.ConnectionToNode = PipingNetworkSegmentChild.Item(c).Attributes["ToNode"].Value;
}
else
{
newConnection.ConnectionToNode = "";
}
newPNS.PipingNetworkSegmentConnection.Add(newConnection);
}
}
PNS.Add(newPNS);
}
}
}
else if (pointname1 == "ProcessInstrumentationFunction")
{
ProcessInstrumentationFunction newPIF = new ProcessInstrumentationFunction();
newPIF.ProcessInstrumentationFunctionID = PlantModelChild.Item(a).Attributes["ID"].Value;
newPIF.ProcessInstrumentationFunctionComponentClass = PlantModelChild.Item(a).Attributes["ComponentClass"].Value;
newPIF.ProcessInstrumentationFunctionTagName = PlantModelChild.Item(a).Attributes["TagName"].Value;
XmlNodeList ProcessInstrumentationFunctionChild = PlantModelChild.Item(a).ChildNodes;
for (int b = 0; b < ProcessInstrumentationFunctionChild.Count; b++)
{
string pointname2 = ProcessInstrumentationFunctionChild.Item(b).Name;
if (pointname2 == "GenericAttributes")
{
XmlNodeList GenericAttributes = ProcessInstrumentationFunctionChild.Item(b).ChildNodes;
newPIF.ProcessInstrumentationFunctionGenericAttribute = new GenericAttribute[GenericAttributes.Count];
for (int c = 0; c < GenericAttributes.Count; c++)
{
string pointname3 = GenericAttributes.Item(c).Name;
if (pointname3 == "GenericAttribute")
{
newPIF.ProcessInstrumentationFunctionGenericAttribute[c].GenericAttributeName = GenericAttributes.Item(c).Attributes["Name"].Value;
newPIF.ProcessInstrumentationFunctionGenericAttribute[c].GenericAttributeFormat = GenericAttributes.Item(c).Attributes["Format"].Value;
newPIF.ProcessInstrumentationFunctionGenericAttribute[c].GenericAttributeValue = GenericAttributes.Item(c).Attributes["Value"].Value;
}
}
PIF.Add(newPIF);
}
}
}
}
}
string postUrl = "https://Yogurtmachine.api.wcus.digitaltwins.azure.net/digitaltwins/THISMODEL?api-version=2020-10-31"; // post link
string EQjson = "{ \"$metadata\": {" +
" \"$model\": \"dtmi:dtdl:K1;1\" }, " +
"\"ID\": \"K3\"," +
"\"TagName\": \"K4\"}";
string PNSJson = "{ \"$metadata\": {" +
" \"$model\": \"dtmi:dtdl:K1;1\" }, " +
"\"ID\": \"K2\"," +
"\"TagName\": \"K3\"," +
"\"ComponentClass\": \"K4\"," +
"\"ColorCodeAssignmentClass\": \"K5\"," +
"\"NominalDiameterRepresentationAssignmentClass\":\"K6\"}";
string PIFJson ="{ \"$metadata\": {" +
" \"$model\": \"dtmi:dtdl:K1;1\" }, " +
"\"ID\": \"K2\"," +
"\"TagName\": \"K3\"," +
"\"ComponentClass\": \"K4\"," +
"\"ProcessInstrumentationFunctionNumberAssignmentClass\": \"K5\"," +
"\"ProcessInstrumentationFunctionCategoryAssignmentClass\":\"K6\","+
"\"ProcessInstrumentationFunctionModifierAssignmentClass\":\"K7\"}";
// extract info from struct and merge into a json string only for creating DT (not DTDL models) without relationships
if (root != null)
{
for (int a = 0; a < EQ.Count; a++)
{
string tmp = EQjson;
string url = postUrl;
if (EQ[a].ChildEquipmentID != null) // Acquire the subequipment of a equipment. Such as heater or motor.
{
//Console.WriteLine(EQ[a].ChildEquipmentID);
tmp = tmp.Replace("K1", EQ[a].ChildEquipmentComponentClass).Replace("K3", EQ[a].ChildEquipmentID).Replace("K4", EQ[a].ChildTagName);
url = url.Replace("THISMODEL", EQ[a].ChildEquipmentID);
Console.WriteLine(tmp);
Console.WriteLine(url);
await Upload(url, tmp, token);
tmp = EQjson;
url = postUrl;
}
tmp = tmp.Replace("K1", EQ[a].EquipmentComponentClass).Replace("K3", EQ[a].EquipmentID).Replace("K4", EQ[a].EquipmentTagName);
url = url.Replace("THISMODEL", EQ[a].EquipmentID);
Console.WriteLine(tmp);
Console.WriteLine(url);
await Upload(url,tmp, token);
}
for (int a = 0; a < PNS.Count; a++)
{
for (int b = 0; b < PNS[a].PipingNetworkSegmentComponent.Count; b++)
{
string tmp = PNSJson;
string url = postUrl;
if (PNS[a].PipingNetworkSegmentComponent[b].EquipmentComponentClass == "CustomOperatedValve")
{
tmp = tmp.Replace("K1", "CustomOperatedValve");
}
else
{
tmp = tmp.Replace("K1", "StandardOperatedValve");
}
string color = "";
string NominalDiameter = "";
for (int c = 0; c < PNS[a].PipingNetworkSegmentGenericAttribute.Length; c++)
{
if (PNS[a].PipingNetworkSegmentGenericAttribute[c].GenericAttributeName == "ColorCodeAssignmentClass")
{
color = PNS[a].PipingNetworkSegmentGenericAttribute[c].GenericAttributeValue;
}
else if (PNS[a].PipingNetworkSegmentGenericAttribute[c].GenericAttributeName == "NominalDiameterRepresentationAssignmentClass")
{
NominalDiameter = PNS[a].PipingNetworkSegmentGenericAttribute[c].GenericAttributeValue;
}
}
tmp = tmp.Replace("K2", PNS[a].PipingNetworkSegmentComponent[b].EquipmentID).Replace("K3", PNS[a].PipingNetworkSegmentComponent[b].EquipmentTagName).Replace("K4", PNS[a].PipingNetworkSegmentComponent[b].EquipmentComponentClass).Replace("K5", color).Replace("K6", NominalDiameter);
url = url.Replace("THISMODEL", PNS[a].PipingNetworkSegmentComponent[b].EquipmentID);
Console.WriteLine(tmp);
Console.WriteLine(url);
await Upload(url, tmp, token);
// ToDo: how to extract the attribute for customed value
}
}
for (int a = 0; a < PIF.Count; a++)
{
string tmp = PIFJson;
string url = postUrl;
tmp = tmp.Replace("K1", "Sensor").Replace("K2", PIF[a].ProcessInstrumentationFunctionID).Replace("K3", PIF[a].ProcessInstrumentationFunctionTagName).Replace("K4", PIF[a].ProcessInstrumentationFunctionComponentClass);
foreach (var x in PIF[a].ProcessInstrumentationFunctionGenericAttribute)
{
if(x.GenericAttributeName == "ProcessInstrumentationFunctionNumberAssignmentClass")
{
tmp = tmp.Replace("K5", x.GenericAttributeValue);
}
else if(x.GenericAttributeName == "ProcessInstrumentationFunctionCategoryAssignmentClass")
{
tmp = tmp.Replace("K6", x.GenericAttributeValue);
}
else if(x.GenericAttributeName == "ProcessInstrumentationFunctionModifierAssignmentClass")
{
tmp = tmp.Replace("K7", x.GenericAttributeValue);
}
else
{
Console.WriteLine("new attributes are found");
}
}
url = url.Replace("THISMODEL", PIF[a].ProcessInstrumentationFunctionID);
Console.WriteLine(url);
Console.WriteLine(tmp);
await Upload(url, tmp, token);
}
}
}
public static async Task AddRelationship()
{
XmlDocument Document = new XmlDocument();
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = true;//Ignore commends in the documents
XmlReader reader = XmlReader.Create(xmlLocation, settings);
Document.Load(reader);
string token;
// Read token from local file
using (StreamReader readtext = new StreamReader(tokenLocation))
{
token = readtext.ReadLine();
}
// select root node
XmlNode root = Document.SelectSingleNode("PlantModel");
// get all child nodes of root node
if (root != null)
{
XmlElement PlantModel = (XmlElement)root;
XmlNodeList PlantModelChild = PlantModel.ChildNodes;
for (int a = 0; a < PlantModelChild.Count; a++)
{
string pointname1 = PlantModelChild.Item(a).Name;
if (pointname1 == "Equipment")
{
Equipment newEQ = new Equipment();
newEQ.EquipmentID = PlantModelChild.Item(a).Attributes["ID"].Value;
newEQ.EquipmentComponentClass = PlantModelChild.Item(a).Attributes["ComponentClass"].Value;
newEQ.EquipmentTagName = PlantModelChild.Item(a).Attributes["TagName"].Value;
XmlNodeList EquipmentChild = PlantModelChild.Item(a).ChildNodes;
for (int b = 0; b < EquipmentChild.Count; b++)
{
string pointname2 = EquipmentChild.Item(b).Name;
if (pointname2 == "ConnectionPoints")
{
XmlNodeList Node = EquipmentChild.Item(b).ChildNodes;
newEQ.NodeID = new string[Node.Count];
for (int c = 0; c < Node.Count; c++)
{
newEQ.NodeID[c] = Node.Item(c).Attributes["ID"].Value;
}
}
else if (pointname2 == "GenericAttributes")
{
XmlNodeList GenericAttributes = EquipmentChild.Item(b).ChildNodes;
newEQ.EquipmentGenericAttribute = new GenericAttribute[GenericAttributes.Count];
for (int c = 0; c < GenericAttributes.Count; c++)
{
string pointname3 = GenericAttributes.Item(c).Name;
if (pointname3 == "GenericAttribute")
{
newEQ.EquipmentGenericAttribute[c].GenericAttributeName = GenericAttributes.Item(c).Attributes["Name"].Value;
newEQ.EquipmentGenericAttribute[c].GenericAttributeFormat = GenericAttributes.Item(c).Attributes["Units"].Value; // incert units into Format
newEQ.EquipmentGenericAttribute[c].GenericAttributeValue = GenericAttributes.Item(c).Attributes["Value"].Value;
}
}
}
else if (pointname2 == "Equipment")
{
newEQ.ChildEquipmentID = EquipmentChild.Item(b).Attributes["ID"].Value;
newEQ.ChildEquipmentComponentClass = EquipmentChild.Item(b).Attributes["ComponentClass"].Value;
if (EquipmentChild.Item(b).Attributes["TagName"] is null)
{
newEQ.ChildTagName = "";
}
else
{
newEQ.ChildTagName = EquipmentChild.Item(b).Attributes["TagName"].Value;
}
}
}
EQ.Add(newEQ);
}
else if (pointname1 == "PipingNetworkSystem")
{
XmlNodeList PipingNetworkSegment = PlantModelChild.Item(a).ChildNodes;
for (int b = 0; b < PipingNetworkSegment.Count; b++)
{
string pointname2 = PipingNetworkSegment.Item(b).Name;
if (pointname2 == "PipingNetworkSegment")
{
PipingNetworkSegment newPNS = new PipingNetworkSegment();
newPNS.PipingNetworkSegmentComponentClass = PipingNetworkSegment.Item(b).Attributes["ComponentClass"].Value;
newPNS.PipingNetworkSegmentID = PipingNetworkSegment.Item(b).Attributes["ID"].Value;
newPNS.PipingNetworkSegmentTagName = PipingNetworkSegment.Item(b).Attributes["TagName"].Value;
newPNS.PipingNetworkSegmentComponent = new List<Equipment>();
newPNS.PipingNetworkSegmentConnection = new List<Connection>();
XmlNodeList PipingNetworkSegmentChild = PipingNetworkSegment.Item(b).ChildNodes;
for (int c = 0; c < PipingNetworkSegmentChild.Count; c++)
{
string pointname3 = PipingNetworkSegmentChild.Item(c).Name;
if (pointname3 == "GenericAttributes")
{
XmlNodeList GenericAttributes = PipingNetworkSegmentChild.Item(c).ChildNodes;
newPNS.PipingNetworkSegmentGenericAttribute = new GenericAttribute[GenericAttributes.Count];
for (int d = 0; d < GenericAttributes.Count; d++)
{
string pointname4 = GenericAttributes.Item(d).Name;
if (pointname4 == "GenericAttribute")
{
if (GenericAttributes.Item(d).Attributes["Name"] is null)
{
newPNS.PipingNetworkSegmentGenericAttribute[d].GenericAttributeName = "";
}
else
{
newPNS.PipingNetworkSegmentGenericAttribute[d].GenericAttributeName = GenericAttributes.Item(d).Attributes["Name"].Value;
}
if (GenericAttributes.Item(d).Attributes["Format"] is null)
{
newPNS.PipingNetworkSegmentGenericAttribute[d].GenericAttributeFormat = "";
}
else
{
newPNS.PipingNetworkSegmentGenericAttribute[d].GenericAttributeFormat = GenericAttributes.Item(d).Attributes["Format"].Value;
}
if (GenericAttributes.Item(d).Attributes["Value"] is null)
{
newPNS.PipingNetworkSegmentGenericAttribute[d].GenericAttributeValue = "";
}
else
{
newPNS.PipingNetworkSegmentGenericAttribute[d].GenericAttributeValue = GenericAttributes.Item(d).Attributes["Value"].Value;
}
}
}
}
else if (pointname3 == "PipingComponent")
{
Equipment newComponent = new Equipment();
newComponent.EquipmentID = PipingNetworkSegmentChild.Item(c).Attributes["ID"].Value;
newComponent.EquipmentComponentClass = PipingNetworkSegmentChild.Item(c).Attributes["ComponentClass"].Value;
newComponent.EquipmentTagName = PipingNetworkSegmentChild.Item(c).Attributes["TagName"].Value;
XmlNodeList PipingComponent = PipingNetworkSegmentChild.Item(c).ChildNodes;
for (int d = 0; d < PipingComponent.Count; d++)
{
string pointname4 = PipingComponent.Item(d).Name;
if (pointname4 == "ConnectionPoints")
{
XmlNodeList Node = PipingComponent.Item(d).ChildNodes;
newComponent.NodeID = new string[Node.Count];
for (int e = 0; e < Node.Count; e++)
{
string pointname5 = Node.Item(e).Name;
if (pointname5 == "Node")
{
newComponent.NodeID[e] = Node.Item(e).Attributes["ID"].Value;
}
}
}
else if (pointname4 == "GenericAttributes")
{
XmlNodeList GenericAttributes = PipingComponent.Item(d).ChildNodes;
newComponent.EquipmentGenericAttribute = new GenericAttribute[GenericAttributes.Count];
for (int e = 0; e < GenericAttributes.Count; e++)
{
string pointname5 = GenericAttributes.Item(e).Name;
if (pointname5 == "GenericAttribute")
{
if (GenericAttributes.Item(e).Attributes["Name"] is null)
{
newComponent.EquipmentGenericAttribute[e].GenericAttributeName = "";
}
else
{
newComponent.EquipmentGenericAttribute[e].GenericAttributeName = GenericAttributes.Item(e).Attributes["Name"].Value;
}
if (GenericAttributes.Item(e).Attributes["Value"] is null)
{
newComponent.EquipmentGenericAttribute[e].GenericAttributeValue = "";
}
else
{
newComponent.EquipmentGenericAttribute[e].GenericAttributeValue = GenericAttributes.Item(e).Attributes["Value"].Value;
}
if (GenericAttributes.Item(e).Attributes["Format"] is null)
{
newComponent.EquipmentGenericAttribute[e].GenericAttributeFormat = "";
}
else
{
newComponent.EquipmentGenericAttribute[e].GenericAttributeFormat = GenericAttributes.Item(e).Attributes["Format"].Value;
}
}
}
}
}
newPNS.PipingNetworkSegmentComponent.Add(newComponent);
}
else if (pointname3 == "Connection")
{
Connection newConnection = new Connection();
if (PipingNetworkSegmentChild.Item(c).Attributes["FromID"] is null)
{
newConnection.ConnectionFromID = "";
}
else
{
newConnection.ConnectionFromID = PipingNetworkSegmentChild.Item(c).Attributes["FromID"].Value;
}
if (PipingNetworkSegmentChild.Item(c).Attributes["FromNode"] is null)
{
newConnection.ConnectionFromNode = "";
}
else
{
newConnection.ConnectionFromNode = PipingNetworkSegmentChild.Item(c).Attributes["FromNode"].Value;
}
if (PipingNetworkSegmentChild.Item(c).Attributes["ToID"] != null) // how to extend to anthoer variable?
{
newConnection.ConnectionToID = PipingNetworkSegmentChild.Item(c).Attributes["ToID"].Value;
}
else
{
newConnection.ConnectionToID = "";
}
if (PipingNetworkSegmentChild.Item(c).Attributes["ToNode"] != null)
{
newConnection.ConnectionToNode = PipingNetworkSegmentChild.Item(c).Attributes["ToNode"].Value;
}
else
{
newConnection.ConnectionToNode = "";
}
newPNS.PipingNetworkSegmentConnection.Add(newConnection);
}
}
PNS.Add(newPNS);
}
}
}
else if (pointname1 == "ProcessInstrumentationFunction")
{
ProcessInstrumentationFunction newPIF = new ProcessInstrumentationFunction();
newPIF.ProcessInstrumentationFunctionID = PlantModelChild.Item(a).Attributes["ID"].Value;
newPIF.ProcessInstrumentationFunctionComponentClass = PlantModelChild.Item(a).Attributes["ComponentClass"].Value;
newPIF.ProcessInstrumentationFunctionTagName = PlantModelChild.Item(a).Attributes["TagName"].Value;
XmlNodeList ProcessInstrumentationFunctionChild = PlantModelChild.Item(a).ChildNodes;
for (int b = 0; b < ProcessInstrumentationFunctionChild.Count; b++)
{
string pointname2 = ProcessInstrumentationFunctionChild.Item(b).Name;
if (pointname2 == "GenericAttributes")
{
XmlNodeList GenericAttributes = ProcessInstrumentationFunctionChild.Item(b).ChildNodes;
newPIF.ProcessInstrumentationFunctionGenericAttribute = new GenericAttribute[GenericAttributes.Count];
for (int c = 0; c < GenericAttributes.Count; c++)
{
string pointname3 = GenericAttributes.Item(c).Name;
if (pointname3 == "GenericAttribute")
{
newPIF.ProcessInstrumentationFunctionGenericAttribute[c].GenericAttributeName = GenericAttributes.Item(c).Attributes["Name"].Value;
newPIF.ProcessInstrumentationFunctionGenericAttribute[c].GenericAttributeFormat = GenericAttributes.Item(c).Attributes["Format"].Value;
newPIF.ProcessInstrumentationFunctionGenericAttribute[c].GenericAttributeValue = GenericAttributes.Item(c).Attributes["Value"].Value;
}
}
PIF.Add(newPIF);
}
}
}
}
}
// create json for relationship.
string relationship = "{ " +
"\"$targetId\": \"myTargetTwin\"," +
"\"$relationshipName\": \"myRelationship\" }";
string AddRelationshipUrl = "https://Yogurtmachine.api.wcus.digitaltwins.azure.net/digitaltwins/SourceTwin/relationships/relationshipId?api-version=2020-10-31"; // post relationship link
if (root != null)
{
string tmp = relationship;
string url = AddRelationshipUrl;
for (int a = 0; a < EQ.Count; a++)
{
//Console.WriteLine(EQ[a].ChildEquipmentID);
if (EQ[a].ChildEquipmentID != null)
{
tmp = tmp.Replace("myRelationship", "has").Replace("myTargetTwin", EQ[a].ChildEquipmentID);
url = url.Replace("SourceTwin", EQ[a].EquipmentID).Replace("relationshipId", EQ[a].EquipmentID + "Has" + EQ[a].ChildEquipmentID);
Console.WriteLine(tmp);
Console.WriteLine(url);
// update Equipment's relationship.(has)
await Upload(url, tmp, token);
}
tmp = relationship;
url = AddRelationshipUrl;
}
for (int a = 0; a < PNS.Count; a++)
{
foreach (var x in PNS[a].PipingNetworkSegmentConnection)
{
tmp = relationship;
url = AddRelationshipUrl;
if (x.ConnectionToID != "")
{
tmp = tmp.Replace("myRelationship", "To").Replace("myTargetTwin", x.ConnectionToID);
url = url.Replace("SourceTwin", x.ConnectionFromID).Replace("relationshipId", x.ConnectionFromID + "To" + x.ConnectionToID);
Console.WriteLine(tmp);
Console.WriteLine(url);
await Upload(url, tmp, token);
}
}
}
for (int a = 0; a < PIF.Count;a++)
{
tmp = relationship;
url = AddRelationshipUrl;
string PIFID = PIF[a].ProcessInstrumentationFunctionID.Split("S")[0];
foreach (var x in EQ)
{
if (x.EquipmentID == PIFID)
{
tmp = tmp.Replace("myRelationship", "measuredBy").Replace("myTargetTwin",PIF[a].ProcessInstrumentationFunctionID);
url = url.Replace("SourceTwin", x.EquipmentID).Replace("relationshipId", x.EquipmentID + "MeasuredBy" + PIF[a].ProcessInstrumentationFunctionID);
Console.WriteLine(tmp);
Console.WriteLine(url);
await Upload(url, tmp, token);
}
}
}
}
}
private static async Task Upload(string postUrl,string json, string token)
{
Console.WriteLine($"Add Twin");
var httpWebRequest = (HttpWebRequest)WebRequest.Create(postUrl);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "PUT";
// OAuth 2.0 authentication using bearer token
httpWebRequest.PreAuthenticate = true;
httpWebRequest.Accept = "application/json";
httpWebRequest.Headers.Add("Authorization", "Bearer " + token);
// Read local Json file
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(json);
}
// Catch response from server
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}
}
// main class of this program
class Program
{
public static string tokenLocation = @"D:\Study\LRZSyncShare\Semesterarbeit -- Yu Mu\Material\MyJogurt\token.txt";
private const string adtInstanceUrl = "https://Yogurtmachine.api.wcus.digitaltwins.azure.net/models?api-version=2020-10-31";
static async Task Main(string[] args)
{
string token;
// Read token from local file
using (StreamReader readtext = new StreamReader(tokenLocation))
{
token = readtext.ReadLine();
}
// Upload models
//await CreateModel(token);
//Add Twins using REST API
await XMLExtract.AddTwin();
//Add Twins' Relationships
await XMLExtract.AddRelationship();
}
private static async Task CreateModel(string token)
{
Console.WriteLine($"Upload a model");
var httpWebRequest = (HttpWebRequest)WebRequest.Create(adtInstanceUrl);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
// OAuth 2.0 authentication using bearer token
httpWebRequest.PreAuthenticate = true;
httpWebRequest.Accept = "application/json";
httpWebRequest.Headers.Add("Authorization", "Bearer " + token);
// Read local Json file
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
StreamReader model = new StreamReader(@"D:\Study\LRZSyncShare\Semesterarbeit -- Yu Mu\Material\MyJogurt\DTDLmodels\DTDL.json");
string json = model.ReadToEnd();
streamWriter.Write(json);
}
// Catch response from server
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}
}
}