Skip to content

Commit 41ccb55

Browse files
committed
重构类型注册,将值类型注册为 DataTypes/Structure,而非 ObjectTypes 下面。
1 parent 412f4ab commit 41ccb55

File tree

1 file changed

+74
-7
lines changed

1 file changed

+74
-7
lines changed

externals/opc/src/OpcServer.Manager.cs

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -418,12 +418,49 @@ public BaseObjectState DefineObject(NodeState parent, NodeId identifier, object
418418
return instanceDefinition;
419419
}
420420

421-
public BaseObjectTypeState DefineType(Type type)
421+
public BaseTypeState DefineType(Type type)
422422
{
423423
lock(this.Lock)
424424
{
425425
if(_types.TryGetValue(type, out var identifer))
426-
return this.FindPredefinedNode(identifer, null) as BaseObjectTypeState;
426+
return this.FindPredefinedNode(identifer, null) as BaseTypeState;
427+
428+
if(type.IsValueType)
429+
{
430+
var structure = new StructureDefinition()
431+
{
432+
BaseDataType = DataTypeIds.Structure,
433+
StructureType = StructureType.Structure,
434+
};
435+
436+
foreach(var property in type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy))
437+
{
438+
structure.Fields.Add(new StructureField()
439+
{
440+
Name = property.Name,
441+
DataType = Utility.GetDataType(property.PropertyType, out var rank),
442+
ValueRank = rank,
443+
});
444+
}
445+
446+
var typeState = new DataTypeState()
447+
{
448+
NodeId = this.GenerateId(),
449+
SymbolicName = $"{type.Name}Type",
450+
BrowseName = new QualifiedName($"{type.Namespace}.{type.Name}", this.NamespaceIndex),
451+
DisplayName = $"{type.Namespace}.{type.Name}",
452+
SuperTypeId = DataTypeIds.Structure,
453+
DataTypeDefinition = new(structure),
454+
};
455+
456+
typeState.AddReference(ReferenceTypes.Organizes, true, DataTypeIds.Structure);
457+
458+
if(!this.PredefinedNodes.ContainsKey(typeState.NodeId))
459+
this.AddPredefinedNode(this.SystemContext, typeState);
460+
461+
_types.TryAdd(type, typeState.NodeId);
462+
return typeState;
463+
}
427464

428465
var definition = new BaseObjectTypeState()
429466
{
@@ -472,9 +509,9 @@ private PropertyState DefineProperty(NodeState parent, PropertyInfo property)
472509

473510
if(propertyState.DataType == DataTypeIds.ObjectTypeNode)
474511
{
475-
var definition = this.DefineType(elementType ?? propertyType);
476-
propertyState.DataType = definition.NodeId;
477-
propertyState.TypeDefinitionId = definition.NodeId;
512+
var typeState = this.DefineType(elementType ?? propertyType);
513+
propertyState.DataType = typeState.NodeId;
514+
propertyState.TypeDefinitionId = typeState.NodeId;
478515
}
479516

480517
propertyState.AddReference(ReferenceTypes.HasModellingRule, false, ObjectIds.ModellingRule_Mandatory);
@@ -484,10 +521,38 @@ private PropertyState DefineProperty(NodeState parent, PropertyInfo property)
484521

485522
private void GenerateProperties(BaseObjectState instanceDefinition, object instance)
486523
{
487-
var typeDefinition = this.FindPredefinedNode(instanceDefinition.TypeDefinitionId, typeof(BaseObjectTypeState));
524+
var typeDefinition = this.FindPredefinedNode(instanceDefinition.TypeDefinitionId, typeof(BaseTypeState));
488525
if(typeDefinition == null)
489526
return;
490527

528+
if(typeDefinition is DataTypeState dataType)
529+
{
530+
if(dataType.DataTypeDefinition.Body is StructureDefinition structure)
531+
{
532+
foreach(var field in structure.Fields)
533+
{
534+
var propertyState = new PropertyState(instanceDefinition)
535+
{
536+
BrowseName = field.Name,
537+
DisplayName = field.Name,
538+
Description = field.Description,
539+
SymbolicName = field.Name,
540+
TypeDefinitionId = (NodeId)field.TypeId,
541+
ReferenceTypeId = ReferenceTypeIds.HasProperty,
542+
DataType = field.DataType,
543+
ValueRank = field.ValueRank,
544+
};
545+
546+
if(Reflection.Reflector.TryGetValue(ref instance, field.Name, out var value))
547+
propertyState.Value = value;
548+
549+
instanceDefinition.AddChild(propertyState);
550+
}
551+
}
552+
553+
return;
554+
}
555+
491556
var children = new List<BaseInstanceState>();
492557
typeDefinition.GetChildren(this.SystemContext, children);
493558

@@ -520,7 +585,7 @@ private void GenerateProperties(BaseObjectState instanceDefinition, object insta
520585

521586
if(Utility.GetDataType(propertyState.DataType, propertyState.ValueRank) == typeof(object))
522587
{
523-
var propertyType = this.FindPredefinedNode(propertyState.DataType, typeof(BaseObjectTypeState));
588+
var propertyType = this.FindPredefinedNode(propertyState.DataType, typeof(BaseTypeState));
524589

525590
if(Reflection.Reflector.TryGetValue(ref instance, child.SymbolicName, out var value))
526591
{
@@ -534,6 +599,8 @@ private void GenerateProperties(BaseObjectState instanceDefinition, object insta
534599
foreach(var item in list)
535600
propertyState.AddChild(item);
536601
}
602+
603+
//propertyState.Value = value;
537604
}
538605
}
539606
else if(Reflection.Reflector.TryGetValue(ref instance, child.SymbolicName, out var value))

0 commit comments

Comments
 (0)