Skip to content

Commit

Permalink
Fix nodesfile.json array values (#399)
Browse files Browse the repository at this point in the history
* Cosmetic

* Cast JArray to .net array

* Add Int32 array
  • Loading branch information
luiscantero authored Oct 17, 2024
1 parent 656fadf commit 6727236
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
57 changes: 47 additions & 10 deletions src/PluginNodes/UserDefinedPluginNodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ namespace OpcPlc.PluginNodes;

using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Opc.Ua;
using OpcPlc.Helpers;
using OpcPlc.PluginNodes.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;


/// <summary>
/// Nodes that are configured via JSON file.
/// </summary>
Expand Down Expand Up @@ -52,11 +53,10 @@ private void AddNodes(FolderState folder)
{
string json = File.ReadAllText(_nodesFileName);

var cfgFolder = JsonConvert.DeserializeObject<ConfigFolder>(json, new JsonSerializerSettings
{
var cfgFolder = JsonConvert.DeserializeObject<ConfigFolder>(json, new JsonSerializerSettings {
TypeNameHandling = TypeNameHandling.All,
});

});

_logger.LogInformation($"Processing node information configured in {_nodesFileName}");

Nodes = AddNodes(folder, cfgFolder).ToList();
Expand Down Expand Up @@ -86,7 +86,7 @@ private IEnumerable<NodeWithIntervals> AddNodes(FolderState folder, ConfigFolder

if (!isDecimal && !isString)
{
_logger.LogError($"The type of the node configuration for node with name {node.Name} ({node.NodeId.GetType()}) is not supported. Only decimal, string, and guid are supported. Defaulting to string.");
_logger.LogError($"The type of the node configuration for node with name {node.Name} ({node.NodeId.GetType()}) is not supported. Only decimal, string, and GUID are supported. Defaulting to string.");
node.NodeId = node.NodeId.ToString();
}

Expand All @@ -102,6 +102,11 @@ private IEnumerable<NodeWithIntervals> AddNodes(FolderState folder, ConfigFolder
: isGuid
? $"g={node.NodeId.ToString()}"
: $"s={node.NodeId.ToString()}";

if (node.ValueRank == 1 && node.Value is JArray jArrayValue)
{
node.Value = UpdateArrayValue(node, jArrayValue);
}

if (string.IsNullOrEmpty(node.Name))
{
Expand Down Expand Up @@ -132,8 +137,40 @@ private IEnumerable<NodeWithIntervals> AddNodes(FolderState folder, ConfigFolder
{
yield return childNode;
}
}

}

private static object UpdateArrayValue(ConfigNode node, JArray jArrayValue)
{
object arrayValue = jArrayValue;

if (node.DataType == "String")
{
arrayValue = jArrayValue.ToObject<string[]>();
}

if (node.DataType == "Boolean")
{
arrayValue = jArrayValue.ToObject<bool[]>();
}

if (node.DataType == "Float")
{
arrayValue = jArrayValue.ToObject<float[]>();
}

if (node.DataType == "UInt32")
{
arrayValue = jArrayValue.ToObject<uint[]>();
}

if (node.DataType == "Int32")
{
arrayValue = jArrayValue.ToObject<int[]>();
}

return arrayValue;
}

private IEnumerable<NodeWithIntervals> AddFolders(FolderState folder, ConfigFolder cfgFolder)
{
if (cfgFolder.FolderList is null)
Expand All @@ -157,7 +194,7 @@ public void CreateBaseVariable(NodeState parent, ConfigNode node)
{
if (!Enum.TryParse(node.DataType, out BuiltInType nodeDataType))
{
_logger.LogError($"Value '{node.DataType}' of node '{node.NodeId}' cannot be parsed. Defaulting to 'Int32'");
_logger.LogError($"Value {node.DataType} of node {node.NodeId} cannot be parsed. Defaulting to Int32");
node.DataType = "Int32";
}

Expand All @@ -169,7 +206,7 @@ public void CreateBaseVariable(NodeState parent, ConfigNode node)
}
catch
{
_logger.LogError($"AccessLevel '{node.AccessLevel}' of node '{node.Name}' is not supported. Defaulting to 'CurrentReadOrWrite'");
_logger.LogError($"AccessLevel {node.AccessLevel} of node {node.Name} is not supported. Defaulting to CurrentReadOrWrite");
node.AccessLevel = "CurrentRead";
accessLevel = AccessLevels.CurrentReadOrWrite;
}
Expand Down
10 changes: 10 additions & 0 deletions src/nodesfile.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@
"Name": "SampleBooleanNode",
"DataType": "Boolean",
"Value": true
},
{
"NodeId": "1048",
"Name": "Int32 array",
"DataType": "Int32",
"ValueRank": 1,
"ArrayDimensions": [ 5 ],
"AccessLevel": "CurrentReadOrWrite",
"Description": "Test array of 5 Int32 elements",
"Value": [ 1, 2, 3, 4, 5 ]
}
]
}

0 comments on commit 6727236

Please sign in to comment.