198 lines
7.4 KiB
C#
198 lines
7.4 KiB
C#
using EonaCat.Connections.OpcUa.Models;
|
|
using Opc.Ua;
|
|
using Opc.Ua.Server;
|
|
using System.Collections.Concurrent;
|
|
|
|
namespace EonaCat.Connections.OpcUa.Server;
|
|
|
|
internal sealed class OpcUaAddressSpace : CustomNodeManager2
|
|
{
|
|
private readonly IReadOnlyCollection<OpcUaNodeDefinition> _nodeDefinitions;
|
|
private readonly IReadOnlyCollection<OpcUaMethodDefinition> _methodDefinitions;
|
|
private readonly ConcurrentDictionary<string, BaseDataVariableState> _variables = new(StringComparer.Ordinal);
|
|
|
|
public OpcUaAddressSpace(
|
|
IServerInternal server,
|
|
ApplicationConfiguration configuration,
|
|
string namespaceUri,
|
|
IReadOnlyCollection<OpcUaNodeDefinition> nodeDefinitions,
|
|
IReadOnlyCollection<OpcUaMethodDefinition> methodDefinitions)
|
|
: base(server, configuration, namespaceUri)
|
|
{
|
|
_nodeDefinitions = nodeDefinitions;
|
|
_methodDefinitions = methodDefinitions;
|
|
SystemContext.NodeIdFactory = this;
|
|
}
|
|
|
|
public event EventHandler<OpcUaValueChangedEventArgs>? ValueChanged;
|
|
|
|
public override void CreateAddressSpace(IDictionary<NodeId, IList<IReference>> externalReferences)
|
|
{
|
|
lock (Lock)
|
|
{
|
|
if (!externalReferences.TryGetValue(ObjectIds.ObjectsFolder, out var references))
|
|
{
|
|
references = new List<IReference>();
|
|
externalReferences[ObjectIds.ObjectsFolder] = references;
|
|
}
|
|
|
|
var root = new FolderState(null)
|
|
{
|
|
NodeId = new NodeId("EonaCat", NamespaceIndex),
|
|
BrowseName = new QualifiedName("EonaCat", NamespaceIndex),
|
|
DisplayName = (LocalizedText)"EonaCat",
|
|
TypeDefinitionId = ObjectTypeIds.FolderType,
|
|
EventNotifier = EventNotifiers.None
|
|
};
|
|
root.AddReference(ReferenceTypeIds.Organizes, true, ObjectIds.ObjectsFolder);
|
|
references.Add(new NodeStateReference(ReferenceTypeIds.Organizes, false, root.NodeId));
|
|
|
|
foreach (var definition in _nodeDefinitions)
|
|
{
|
|
AddVariable(root, definition);
|
|
}
|
|
|
|
foreach (var definition in _methodDefinitions)
|
|
{
|
|
AddMethod(root, definition);
|
|
}
|
|
|
|
AddPredefinedNode(SystemContext, root);
|
|
}
|
|
}
|
|
|
|
public NodeId ResolveNodeId(string identifier) => new(identifier, NamespaceIndex);
|
|
|
|
public bool TrySetValue(string identifier, object? value)
|
|
{
|
|
if (!_variables.TryGetValue(identifier, out var variable))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
lock (Lock)
|
|
{
|
|
variable.Value = new Variant(value);
|
|
variable.StatusCode = StatusCodes.Good;
|
|
variable.Timestamp = DateTime.UtcNow;
|
|
variable.ClearChangeMasks(SystemContext, false);
|
|
}
|
|
|
|
RaiseValueChanged(variable);
|
|
return true;
|
|
}
|
|
|
|
private void AddVariable(NodeState parent, OpcUaNodeDefinition definition)
|
|
{
|
|
var variable = new BaseDataVariableState(parent)
|
|
{
|
|
NodeId = ResolveNodeId(definition.Identifier),
|
|
BrowseName = new QualifiedName(definition.DisplayName, NamespaceIndex),
|
|
DisplayName = (LocalizedText)definition.DisplayName,
|
|
Description = (LocalizedText)definition.Description,
|
|
TypeDefinitionId = VariableTypeIds.BaseDataVariableType,
|
|
ReferenceTypeId = ReferenceTypeIds.HasComponent,
|
|
DataType = definition.DataType,
|
|
ValueRank = ValueRanks.Scalar,
|
|
AccessLevel = definition.AccessLevel,
|
|
UserAccessLevel = definition.AccessLevel,
|
|
Historizing = false,
|
|
Value = new Variant(definition.InitialValue),
|
|
StatusCode = StatusCodes.Good,
|
|
Timestamp = DateTime.UtcNow
|
|
};
|
|
variable.OnSimpleWriteValue = HandleWrite;
|
|
parent.AddChild(variable);
|
|
_variables[definition.Identifier] = variable;
|
|
}
|
|
|
|
private void AddMethod(NodeState parent, OpcUaMethodDefinition definition)
|
|
{
|
|
var method = new MethodState(parent)
|
|
{
|
|
NodeId = ResolveNodeId(definition.Identifier),
|
|
BrowseName = new QualifiedName(definition.DisplayName, NamespaceIndex),
|
|
DisplayName = (LocalizedText)definition.DisplayName,
|
|
Description = (LocalizedText)definition.Description,
|
|
ReferenceTypeId = ReferenceTypeIds.HasComponent,
|
|
Executable = true,
|
|
UserExecutable = true
|
|
};
|
|
|
|
method.InputArguments = CreateArguments(method, "InputArguments", definition.InputArguments);
|
|
method.OutputArguments = CreateArguments(method, "OutputArguments", definition.OutputArguments);
|
|
method.OnCallMethod = (_, _, inputArguments, outputArguments) =>
|
|
{
|
|
try
|
|
{
|
|
var inputs = new object?[inputArguments.Count];
|
|
for (var index = 0; index < inputs.Length; index++)
|
|
{
|
|
inputs[index] = inputArguments[index].Value;
|
|
}
|
|
|
|
var results = definition.Handler(inputs);
|
|
for (var index = 0; index < Math.Min(results.Count, outputArguments.Count); index++)
|
|
{
|
|
outputArguments[index] = new Variant(results[index]);
|
|
}
|
|
return ServiceResult.Good;
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
return new ServiceResult(exception, StatusCodes.BadUnexpectedError);
|
|
}
|
|
};
|
|
|
|
parent.AddChild(method);
|
|
}
|
|
|
|
private PropertyState<ArrayOf<Argument>> CreateArguments(
|
|
NodeState parent,
|
|
string name,
|
|
IReadOnlyList<OpcUaMethodArgument> definitions)
|
|
{
|
|
var property = PropertyState<ArrayOf<Argument>>.With<StructureBuilder<Argument>>(parent);
|
|
property.NodeId = new NodeId($"{parent.NodeId.Identifier}.{name}", NamespaceIndex);
|
|
property.BrowseName = (QualifiedName)(name == "InputArguments" ? BrowseNames.InputArguments : BrowseNames.OutputArguments);
|
|
property.DisplayName = (LocalizedText)name;
|
|
property.TypeDefinitionId = VariableTypeIds.PropertyType;
|
|
property.ReferenceTypeId = ReferenceTypeIds.HasProperty;
|
|
property.DataType = DataTypeIds.Argument;
|
|
property.ValueRank = ValueRanks.OneDimension;
|
|
property.Value = definitions.Select(definition => new Argument
|
|
{
|
|
Name = definition.Name,
|
|
DataType = definition.DataType,
|
|
ValueRank = ValueRanks.Scalar,
|
|
Description = (LocalizedText)definition.Description
|
|
}).ToArray();
|
|
return property;
|
|
}
|
|
|
|
private ServiceResult HandleWrite(ISystemContext context, NodeState node, ref Variant value)
|
|
{
|
|
if (node is not BaseDataVariableState variable || (variable.AccessLevel & AccessLevels.CurrentWrite) == 0)
|
|
{
|
|
return StatusCodes.BadNotWritable;
|
|
}
|
|
|
|
variable.Value = value;
|
|
variable.Timestamp = DateTime.UtcNow;
|
|
variable.StatusCode = StatusCodes.Good;
|
|
RaiseValueChanged(variable);
|
|
return ServiceResult.Good;
|
|
}
|
|
|
|
private void RaiseValueChanged(BaseDataVariableState variable)
|
|
{
|
|
ValueChanged?.Invoke(this, new OpcUaValueChangedEventArgs
|
|
{
|
|
NodeId = variable.NodeId,
|
|
Value = variable.Value.Value,
|
|
StatusCode = variable.StatusCode,
|
|
SourceTimestamp = variable.Timestamp.ToDateTime()
|
|
});
|
|
}
|
|
}
|