-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuilder.cs
141 lines (122 loc) · 5.44 KB
/
Builder.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
using MTConnect;
using MTConnect.Devices;
namespace MTCAgentTest;
public class Builder
{
public static (bool, Device, IDataItem) Build(Dictionary<string, Device> devices, string mtConnectPath, string mtConnectSource)
{
var parts = new PathParts(mtConnectPath);
bool wasModified = false;
IDataItem dataItem = null;
Device device = null;
IComponent nextComponent = null;
for (int i = 0; i < parts.Count; i++)
{
var part = parts[i];
if (i == 0) // Device expected
{
if (part.Name != "Device") Console.WriteLine($"\tExpecting 'Device' but found '{part.Name}'");
part.Attributes.TryGetValue("Name", out var deviceName);
if (deviceName is null) deviceName = "undefined";
var deviceExists = devices.TryGetValue(deviceName, out device);
if (!deviceExists)
{
device = new Device();
devices[deviceName] = device;
part.Attributes.TryAdd("Id", Guid.NewGuid().ToString());
part.Attributes.TryAdd("Name", string.Empty);
part.Attributes.TryAdd("Type", part.Name);
foreach (var attribute in part.Attributes)
{
Console.WriteLine($"\t\tProcessing Attribute: {attribute.Key}={attribute.Value}");
if (!SetPropertyFromAttribute(device, attribute.Key, attribute.Value))
{
Console.WriteLine($"\t\tFailed to set property '{attribute.Key}'={attribute.Value}");
}
}
wasModified = true;
}
nextComponent = device;
}
else if (i == parts.Count - 1) // DataItem expected
{
var dataItemType = part.Name.ToUnderscoreUpper();
dataItem = nextComponent.GetDataItemByType(dataItemType, SearchType.Child);
//dataItem = nextComponent.GetDataItemByType($"{part.Name}", SearchType.Child);
if (dataItem is null)
{
dataItem = DataItem.Create($"{part.Name}");
//((DataItem)dataItem).Device = device;
((DataItem)dataItem).Source = new MTConnect.Devices.Source()
{
Value = mtConnectSource
};
part.Attributes.TryAdd("Id", Guid.NewGuid().ToString());
part.Attributes.TryAdd("Name", string.Empty);
//part.Attributes.TryAdd("Type", part.Name);
foreach (var attribute in part.Attributes)
{
Console.WriteLine($"\t\tProcessing Attribute: {attribute.Key}={attribute.Value}");
if (!SetPropertyFromAttribute(dataItem, attribute.Key, attribute.Value))
{
Console.WriteLine($"\t\tFailed to set property '{attribute.Key}'={attribute.Value}");
}
}
try
{
((Component)nextComponent).AddDataItem(dataItem);
}
catch
{
((Device)nextComponent).AddDataItem(dataItem);
}
wasModified = true;
}
}
else // Component expected
{
var childComponent = nextComponent.GetComponent($"{part.Name}", searchType: SearchType.Child);
if (childComponent is null)
{
childComponent = Component.Create($"{part.Name}");
part.Attributes.TryAdd("Id", Guid.NewGuid().ToString());
part.Attributes.TryAdd("Name", string.Empty);
part.Attributes.TryAdd("Type", part.Name);
foreach (var attribute in part.Attributes)
{
Console.WriteLine($"\t\tProcessing Attribute: {attribute.Key}={attribute.Value}");
if (!SetPropertyFromAttribute(childComponent, attribute.Key, attribute.Value))
{
Console.WriteLine($"\t\tFailed to set property '{attribute.Key}'={attribute.Value}");
}
}
try
{
((Component)nextComponent).AddComponent(childComponent);
}
catch
{
((Device)nextComponent).AddComponent(childComponent);
}
wasModified = true;
}
nextComponent = childComponent;
}
}
return (wasModified, device, dataItem);
}
private static bool SetPropertyFromAttribute(object obj, string propertyName, object attributeValue)
{
var property = obj.GetType().GetProperty(propertyName);
if (property is null) return false;
if (property.PropertyType == typeof(DataItemCategory))
{
property.SetValue(obj, Enum.Parse(typeof(DataItemCategory), attributeValue.ToString().ToUpper()));
}
else
{
property.SetValue(obj, attributeValue);
}
return true;
}
}