Skip to content

Commit 63cc854

Browse files
committed
修复OPC服务器端预制件定义的问题。 🙏
1 parent a2b794d commit 63cc854

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

externals/opc/src/OpcServer.Manager.cs

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ internal class NodeManager : CustomNodeManager2
4949
private volatile uint _nodeId;
5050
private OpcServerOptions.StorageOptions _options;
5151
private IList<IReference> _objectReferences;
52-
private IList<IReference> _serverReferences;
5352
private Dictionary<Type, NodeId> _types = new();
5453
#endregion
5554

@@ -192,6 +191,9 @@ public IEnumerable<KeyValuePair<NodeId, object>> GetValues(IEnumerable<NodeId> i
192191
AttributeId = Attributes.Value,
193192
}).ToArray();
194193

194+
if(request.Length == 0)
195+
yield break;
196+
195197
var result = new DataValue[request.Length];
196198
var errors = new ServiceResult[request.Length];
197199

@@ -247,6 +249,9 @@ public IEnumerable<Failure> SetValues(IEnumerable<KeyValuePair<NodeId, object>>
247249
Value = new DataValue(new Variant(entry.Value), StatusCodes.Good),
248250
}).ToArray();
249251

252+
if(request.Length == 0)
253+
return [];
254+
250255
var errors = new ServiceResult[request.Length];
251256
this.Write(this.SystemContext.OperationContext, request, errors);
252257
return errors.Where(ServiceResult.IsBad).Select(err => Failure.GetFailure(err.StatusCode));
@@ -259,9 +264,6 @@ public override void CreateAddressSpace(IDictionary<NodeId, IList<IReference>> e
259264
if(!externalReferences.TryGetValue(ObjectIds.ObjectsFolder, out _objectReferences))
260265
externalReferences[ObjectIds.ObjectsFolder] = _objectReferences = new List<IReference>();
261266

262-
if(!externalReferences.TryGetValue(ObjectIds.Server, out _serverReferences))
263-
externalReferences[ObjectIds.Server] = _serverReferences = new List<IReference>();
264-
265267
base.CreateAddressSpace(externalReferences);
266268
}
267269

@@ -401,26 +403,26 @@ public BaseObjectState DefineObject(NodeState parent, NodeId identifier, object
401403
if(string.IsNullOrEmpty(name))
402404
name = instance.GetType().Name;
403405

404-
var type = this.DefineType(instance.GetType());
405-
var state = new BaseObjectState(parent)
406+
var typeDefinition = this.DefineType(instance.GetType());
407+
var instanceDefinition = new BaseObjectState(parent)
406408
{
407409
NodeId = identifier,
408410
SymbolicName = name,
409-
BrowseName = new QualifiedName(name, this.NamespaceIndex),
411+
BrowseName = new(name, this.NamespaceIndex),
410412
DisplayName = name,
413+
TypeDefinitionId = typeDefinition.NodeId,
411414
ReferenceTypeId = ReferenceTypeIds.HasComponent,
412-
TypeDefinitionId = type.NodeId,
413415
};
414416

415417
var children = new List<BaseInstanceState>();
416-
type.GetChildren(this.SystemContext, children);
418+
typeDefinition.GetChildren(this.SystemContext, children);
417419

418420
foreach(var child in children)
419421
{
420-
this.AppendChildren(state, child);
422+
this.AppendChildren(instanceDefinition, child);
421423
}
422424

423-
return state;
425+
return instanceDefinition;
424426
}
425427

426428
public BaseObjectTypeState DefineType(Type type)
@@ -445,7 +447,9 @@ public BaseObjectTypeState DefineType(Type type)
445447
this.DefineProperty(definition, property);
446448
}
447449

448-
definition.AddReference(definition.NodeId, true, ObjectIds.ObjectTypesFolder);
450+
//definition.AddReference(definition.NodeId, true, ObjectIds.ObjectTypesFolder);
451+
definition.AddReference(ReferenceTypes.Organizes, true, ObjectIds.ObjectTypesFolder);
452+
449453
if(!this.PredefinedNodes.ContainsKey(definition.NodeId))
450454
this.AddPredefinedNode(this.SystemContext, definition);
451455

@@ -454,23 +458,23 @@ public BaseObjectTypeState DefineType(Type type)
454458
}
455459
}
456460

457-
private PropertyState DefineProperty(BaseObjectTypeState type, PropertyInfo property)
461+
private PropertyState DefineProperty(NodeState parent, PropertyInfo property)
458462
{
459463
var propertyType = TypeExtension.IsNullable(property.PropertyType, out var underlyingType) ? underlyingType : property.PropertyType;
460464
var elementType = TypeExtension.GetCollectionElementType(propertyType);
461465

462-
var propertyState = new PropertyState(type)
466+
var propertyState = new PropertyState(parent)
463467
{
464468
NodeId = this.GenerateId(),
465469
ReferenceTypeId = ReferenceTypes.HasProperty,
466470
TypeDefinitionId = VariableTypeIds.PropertyType,
467471
SymbolicName = property.Name,
468-
BrowseName = property.Name,
472+
BrowseName = new(property.Name, this.NamespaceIndex),
469473
DisplayName = property.Name,
470474
DataType = Utility.GetDataType(elementType ?? propertyType, out var rank),
471475
ValueRank = rank,
472-
AccessLevel = AccessLevels.CurrentReadOrWrite,
473-
UserAccessLevel = AccessLevels.CurrentReadOrWrite,
476+
AccessLevel = property.CanWrite ? AccessLevels.CurrentReadOrWrite : AccessLevels.CurrentRead,
477+
UserAccessLevel = property.CanWrite ? AccessLevels.CurrentReadOrWrite : AccessLevels.CurrentRead,
474478
MinimumSamplingInterval = MinimumSamplingIntervals.Indeterminate,
475479
OnWriteValue = this.OnWriteValue,
476480
};
@@ -488,7 +492,8 @@ private PropertyState DefineProperty(BaseObjectTypeState type, PropertyInfo prop
488492
this.AddPredefinedNode(this.SystemContext, definition);
489493
}
490494

491-
type.AddChild(propertyState);
495+
propertyState.AddReference(ReferenceTypes.HasModellingRule, false, ObjectIds.ModellingRule_Mandatory);
496+
parent.AddChild(propertyState);
492497
return propertyState;
493498
}
494499

@@ -502,6 +507,15 @@ private void AppendChildren(BaseInstanceState owner, BaseInstanceState child)
502507
instance.DisplayName = child.DisplayName;
503508
instance.Description = child.Description;
504509

510+
if(replica is PropertyState property)
511+
{
512+
property.DataType = ((PropertyState)child).DataType;
513+
property.Value = ((PropertyState)child).Value;
514+
property.ValueRank = ((PropertyState)child).ValueRank;
515+
property.AccessLevel = ((PropertyState)child).AccessLevel;
516+
property.UserAccessLevel = ((PropertyState)child).UserAccessLevel;
517+
}
518+
505519
owner.AddChild(instance);
506520

507521
var children = new List<BaseInstanceState>();
@@ -594,7 +608,7 @@ private NodeId GenerateId(string identifier, out string name)
594608
if(string.IsNullOrWhiteSpace(identifier))
595609
{
596610
var result = this.GenerateId(out var id);
597-
name = $"{id}";
611+
name = id.ToString();
598612
return result;
599613
}
600614

@@ -603,7 +617,7 @@ private NodeId GenerateId(string identifier, out string name)
603617
if(identifier.Length == 1)
604618
{
605619
var result = this.GenerateId(out var id);
606-
name = $"{id}";
620+
name = id.ToString();
607621
return result;
608622
}
609623

0 commit comments

Comments
 (0)