-
Notifications
You must be signed in to change notification settings - Fork 7
/
UACloudLibraryClient.cs
210 lines (187 loc) · 9.77 KB
/
UACloudLibraryClient.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
namespace Opc.Ua.Edge.Translator
{
using Newtonsoft.Json;
using Opc.Ua.Cloud.Library.Models;
using Opc.Ua.Export;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text;
public class UACloudLibraryClient
{
public List<string> _nodeSetFilenames = new List<string>();
private HttpClient _client = new HttpClient();
public Dictionary<string, string> NamespacesInCloudLibrary { get; private set; } = new Dictionary<string, string>();
public void Login(string uaCloudLibraryUrl, string clientId, string secret)
{
if ((NamespacesInCloudLibrary.Count == 0) && !string.IsNullOrEmpty(clientId) && !string.IsNullOrEmpty(secret))
{
_client.DefaultRequestHeaders.Remove("Authorization");
_client.DefaultRequestHeaders.Add("Authorization", "basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(clientId + ":" + secret)));
if (!uaCloudLibraryUrl.EndsWith('/'))
{
uaCloudLibraryUrl += "/";
}
// get namespaces
string address = uaCloudLibraryUrl + "infomodel/namespaces";
HttpResponseMessage response = _client.Send(new HttpRequestMessage(HttpMethod.Get, address));
string[] identifiers = JsonConvert.DeserializeObject<string[]>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
if (identifiers != null)
{
foreach (string nodeset in identifiers)
{
string[] tuple = nodeset.Split(",");
if (NamespacesInCloudLibrary.ContainsKey(tuple[0]))
{
NamespacesInCloudLibrary[tuple[0]] = tuple[1];
}
else
{
NamespacesInCloudLibrary.Add(tuple[0], tuple[1]);
}
}
}
}
}
public bool DownloadNamespace(string uaCloudLibraryUrl, string namespaceUrl)
{
if (!string.IsNullOrEmpty(uaCloudLibraryUrl) && !string.IsNullOrEmpty(namespaceUrl) && NamespacesInCloudLibrary.ContainsKey(namespaceUrl))
{
if (!uaCloudLibraryUrl.EndsWith('/'))
{
uaCloudLibraryUrl += "/";
}
string address = uaCloudLibraryUrl + "infomodel/download/" + Uri.EscapeDataString(NamespacesInCloudLibrary[namespaceUrl]);
HttpResponseMessage response = _client.Send(new HttpRequestMessage(HttpMethod.Get, address));
try
{
UANameSpace nameSpace = JsonConvert.DeserializeObject<UANameSpace>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
if (!string.IsNullOrEmpty(nameSpace.Nodeset.NodesetXml))
{
// store the file locally
string filePath = Path.Combine(Directory.GetCurrentDirectory(), nameSpace.Title + ".nodeset2.xml");
File.WriteAllText(filePath, nameSpace.Nodeset.NodesetXml);
_nodeSetFilenames.Add(filePath);
return true;
}
}
catch (Exception)
{
return false;
}
}
return false;
}
public string ValidateNamespacesAndModels(string uaCloudLibraryUrl, bool autodownloadreferences)
{
// Collect all models as well as all required/referenced model namespace URIs listed in each file
List<string> models = new List<string>();
List<string> modelreferences = new List<string>();
foreach (string nodesetFile in _nodeSetFilenames)
{
using (Stream stream = new FileStream(nodesetFile, FileMode.Open))
{
UANodeSet nodeSet = UANodeSet.Read(stream);
// validate namespace URIs
if ((nodeSet.NamespaceUris != null) && (nodeSet.NamespaceUris.Length > 0))
{
foreach (string ns in nodeSet.NamespaceUris)
{
if (string.IsNullOrEmpty(ns) || !Uri.IsWellFormedUriString(ns, UriKind.Absolute))
{
return "Nodeset file " + nodesetFile + " contains an invalid Namespace URI: \"" + ns + "\"";
}
}
}
else
{
return "'NamespaceUris' entry missing in " + nodesetFile + ". Please add it!";
}
// validate model URIs
if ((nodeSet.Models != null) && (nodeSet.Models.Length > 0))
{
foreach (ModelTableEntry model in nodeSet.Models)
{
if (model != null)
{
if (Uri.IsWellFormedUriString(model.ModelUri, UriKind.Absolute))
{
// ignore the default namespace which is always present and don't add duplicates
if ((model.ModelUri != "http://opcfoundation.org/UA/") && !models.Contains(model.ModelUri))
{
models.Add(model.ModelUri);
}
}
else
{
return "Nodeset file " + nodesetFile + " contains an invalid Model Namespace URI: \"" + model.ModelUri + "\"";
}
if ((model.RequiredModel != null) && (model.RequiredModel.Length > 0))
{
foreach (ModelTableEntry requiredModel in model.RequiredModel)
{
if (requiredModel != null)
{
if (Uri.IsWellFormedUriString(requiredModel.ModelUri, UriKind.Absolute))
{
// ignore the default namespace which is always required and don't add duplicates
if ((requiredModel.ModelUri != "http://opcfoundation.org/UA/") && !modelreferences.Contains(requiredModel.ModelUri))
{
modelreferences.Add(requiredModel.ModelUri);
}
}
else
{
return "Nodeset file " + nodesetFile + " contains an invalid referenced Model Namespace URI: \"" + requiredModel.ModelUri + "\"";
}
}
}
}
}
}
}
else
{
return "'Model' entry missing in " + nodesetFile + ". Please add it!";
}
}
}
// now check if we have all references for each model we want to load
foreach (string modelreference in modelreferences)
{
if (!models.Contains(modelreference))
{
if (!autodownloadreferences)
{
return "Referenced OPC UA model " + modelreference + " is missing from selected list of nodeset files, please add the corresponding nodeset file to the list of loaded files!";
}
else
{
try
{
// try to auto-download the missing references from the UA Cloud Library
if (!uaCloudLibraryUrl.EndsWith('/'))
{
uaCloudLibraryUrl += "/";
}
string address = uaCloudLibraryUrl + "infomodel/download/" + Uri.EscapeDataString(NamespacesInCloudLibrary[modelreference]);
HttpResponseMessage response = _client.Send(new HttpRequestMessage(HttpMethod.Get, address));
UANameSpace nameSpace = JsonConvert.DeserializeObject<UANameSpace>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
// store the file
string filePath = Path.Combine(Directory.GetCurrentDirectory(), nameSpace.Category.Name + ".nodeset2.xml");
File.WriteAllText(filePath, nameSpace.Nodeset.NodesetXml);
_nodeSetFilenames.Add(filePath);
}
catch (Exception ex)
{
return "Could not download referenced nodeset " + modelreference + ": " + ex.Message;
}
}
}
}
return string.Empty; // no error
}
}
}