Skip to content

Commit

Permalink
Avoid server shutdown on DeleteAsset() and make asset naming consistent.
Browse files Browse the repository at this point in the history
  • Loading branch information
barnstee committed May 7, 2024
1 parent 1f74bbe commit 8a00e54
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Samples/pac4200.td.jsonld
Expand Up @@ -20,7 +20,7 @@
"Thing"
],
"name": "modbus-pac4200-sn324",
"base": "modbus+tcp://192.168.178.100:502/1",
"base": "modbus+tcp://192.168.178.94:502/1",
"title": "Siemens SENTRON PAC4200",
"properties": {
"VoltageL1-N": {
Expand Down
4 changes: 4 additions & 0 deletions UAEdgeTranslator.csproj
Expand Up @@ -33,4 +33,8 @@
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<Folder Include="settings\" />
</ItemGroup>

</Project>
58 changes: 38 additions & 20 deletions UANodeManager.cs
Expand Up @@ -424,29 +424,42 @@ private bool CreateAssetNode(string assetName, out NodeState assetNode)
return StatusCodes.BadNodeIdUnknown;
}

string assetName = asset.DisplayName.Text;

_fileManagers.Remove(assetId);

DeleteNode(SystemContext, assetId);

IEnumerable<string> WoTFiles = Directory.EnumerateFiles(Path.Combine(Directory.GetCurrentDirectory(), "settings"), "*.jsonld");
foreach (string file in WoTFiles)
{
try
string fileName = Path.GetFileNameWithoutExtension(file);
if (fileName == assetName)
{
string fileName = Path.GetFileNameWithoutExtension(file);
if (fileName == NodeId.ToExpandedNodeId(assetId, Server.NamespaceUris).ToString())
{
File.Delete(file);
File.Delete(file);
}
}

_ = Task.Run(() => HandleServerRestart());
if (_tags.ContainsKey(assetName))
{
_tags.Remove(assetName);
}

return ServiceResult.Good;
}
if (_assets.ContainsKey(assetName))
{
_assets.Remove(assetName);
}

int i = 0;
while (i < _uaVariables.Count)
{
if (_uaVariables.Keys.ToArray()[i].StartsWith(assetName))
{
_uaVariables.Remove(_uaVariables.Keys.ToArray()[i]);
}
catch (Exception ex)
else
{
Log.Logger.Error(ex.Message, ex);
return new ServiceResult(ex);
i++;
}
}

Expand All @@ -459,9 +472,11 @@ public void AddNodesForWoTProperties(NodeState parent, string contents)
// parse WoT TD file contents
ThingDescription td = JsonConvert.DeserializeObject<ThingDescription>(contents);

List<string> namespaceUris = new(NamespaceUris)
string newNamespace = "http://opcfoundation.org/UA/" + td.Name + "/";
List<string> namespaceUris = new(NamespaceUris);
if (!namespaceUris.Contains(newNamespace))
{
"http://opcfoundation.org/UA/" + td.Name + "/"
namespaceUris.Add(newNamespace);
};

foreach (object ns in td.Context)
Expand Down Expand Up @@ -537,8 +552,10 @@ private void AddNodeForModbusRegister(NodeState assetFolder, KeyValuePair<string
DataTypeState opcuaType = (DataTypeState)Find(ExpandedNodeId.ToNodeId(ParseExpandedNodeId(property.Value.OpcUaType), Server.NamespaceUris));
if (((StructureDefinition)opcuaType?.DataTypeDefinition?.Body).Fields?.Count > 0)
{
ExtensionObject complexTypeInstance = new();
complexTypeInstance.TypeId = opcuaType.NodeId;
ExtensionObject complexTypeInstance = new()
{
TypeId = opcuaType.NodeId
};

BinaryEncoder encoder = new(ServiceMessageContext.GlobalContext);
foreach (StructureField field in ((StructureDefinition)opcuaType?.DataTypeDefinition?.Body).Fields)
Expand Down Expand Up @@ -640,10 +657,9 @@ private string AssetConnectionTest(ThingDescription td, out byte unitId)
assetInterface = client;
}

string assetId = td.Title + " [" + td.Name + "]";
_assets.Add(assetId, assetInterface);
_assets.Add(td.Name, assetInterface);

return assetId;
return td.Name;
}

private ExpandedNodeId ParseExpandedNodeId(string nodeString)
Expand Down Expand Up @@ -701,7 +717,8 @@ private BaseDataVariableState CreateVariable(NodeState parent, string name, Expa

private void UpdateNodeValues(object assetNameObject)
{
while (!_shutdown)
bool assetDeleted = false;
while (!_shutdown && !assetDeleted)
{
Thread.Sleep(1000);

Expand All @@ -710,7 +727,8 @@ private void UpdateNodeValues(object assetNameObject)
string assetId = (string)assetNameObject;
if (string.IsNullOrEmpty(assetId) || !_tags.ContainsKey(assetId) || !_assets.ContainsKey(assetId))
{
throw new Exception("Cannot find asset: " + assetId);
assetDeleted = true;
continue;
}

foreach (AssetTag tag in _tags[assetId])
Expand Down

0 comments on commit 8a00e54

Please sign in to comment.