Repository navigation
Expand file tree
/
Copy pathWoTTDParser.cs
More file actions
173 lines (149 loc) · 5.8 KB
/
Copy pathWoTTDParser.cs
File metadata and controls
173 lines (149 loc) · 5.8 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
namespace Opc.Ua.Cloud.Publisher
{
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using Opc.Ua.Cloud.Publisher.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
public class WoTTDParser
{
private readonly ILogger _logger;
private static readonly Regex TemplateRegex = new Regex(@"\{\{([^}]+)\}\}", RegexOptions.Compiled);
public WoTTDParser(ILogger logger)
{
_logger = logger;
}
public List<VariableModel> ParseWoTThingDescription(byte[] wotBytes)
{
Dictionary<string, VariableModel> opcNodes = new();
try
{
string wotContent = Encoding.UTF8.GetString(wotBytes).Trim('\uFEFF'); // strip BOM, if present
JObject wotTD = JObject.Parse(wotContent);
// Extract properties from the WoT Thing Description
JToken properties = wotTD["properties"];
if (properties == null || !properties.HasValues)
{
_logger.LogWarning("No properties found in WoT Thing Description");
return opcNodes.Values.ToList();
}
foreach (JProperty property in properties.Children<JProperty>())
{
string propertyName = property.Name;
JObject propertyValue = property.Value as JObject;
if (propertyValue == null)
{
continue;
}
JValue nodeId = propertyValue["uav:mapToNodeId"] as JValue;
// safely extract the WoT TD name; fall back to "asset" when not present
string thingName = (wotTD["name"] as JValue)?.Value?.ToString() ?? "asset";
string id;
if (nodeId != null)
{
id = "nsu=http://opcfoundation.org/UA/" + thingName + "/;" + nodeId.Value.ToString();
}
else
{
id = "nsu=http://opcfoundation.org/UA/" + thingName + "/;s=" + propertyName;
}
VariableModel variable = new()
{
Id = id,
OpcPublishingInterval = 1000,
OpcSamplingInterval = 1000,
HeartbeatInterval = 0,
SkipFirst = false
};
if (!opcNodes.ContainsKey(id))
{
opcNodes.Add(id, variable);
_logger.LogInformation($"Added property '{propertyName}' with Node ID '{nodeId}'");
}
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to parse WoT Thing Description");
}
return opcNodes.Values.ToList();
}
public bool IsThingModel(string wotContent)
{
try
{
JObject wotObject = JObject.Parse(wotContent);
// Check if @type contains "tm:ThingModel"
JToken typeToken = wotObject["@type"];
if (typeToken != null)
{
if (typeToken is JArray typeArray)
{
foreach (var type in typeArray)
{
if (type.ToString().Contains("ThingModel", StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
}
else if (typeToken.ToString().Contains("ThingModel", StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
// Check if content contains template placeholders {{}}
return wotContent.Contains("{{") && wotContent.Contains("}}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to determine if WoT file is a Thing Model");
return false;
}
}
public List<string> ExtractTemplates(string wotContent)
{
var templates = new HashSet<string>();
try
{
// Regular expression to find {{template}} patterns
MatchCollection matches = TemplateRegex.Matches(wotContent);
foreach (Match match in matches)
{
if (match.Groups.Count > 1)
{
templates.Add(match.Groups[1].Value.Trim());
}
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to extract templates from WoT content");
}
return templates.ToList();
}
public string ReplaceTemplates(string wotContent, Dictionary<string, string> templateValues)
{
string result = wotContent;
try
{
foreach (var kvp in templateValues)
{
string placeholder = "{{" + kvp.Key + "}}";
result = result.Replace(placeholder, kvp.Value);
}
// turn the thing model into a thing description
result = result.Replace("tm:ThingModel", "Thing");
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to replace templates in WoT content");
}
return result;
}
}
}