I'm trying to expose nested structures and keep them synchronized. I followed the example of the DemoServer and I am able to expose the the members of a structure if the members are primitive data types, for this I've overwritten the GetModelHandle function and added getters and setters for the primitive data type members.
I tried it to use the same idea as with primitive data types to get the complex working: getters and setters for a DataValue that contains the NestedStructure, but I was not able to solve it.
So, how do I handle complex data types or nested structures?
Thanks in advance, here is some example code of how I've done it (similar to the DemoServer Example, I used the UaModeler to create the basic programm):
My classes:
Code: Select all
Structure {
int StructureID;
NestedStructure NestedStructure;
}
Code: Select all
public partial class StructureModel : BaseDataVariableModel
{
private StructureModel m_model;
public override ModelHandle GetModelHandle(StringTable namespaceUris, object instance)
{
m_model = instance as StructureModel;
if (m_model == null)
return null;
if (m_model.Value == null)
m_model.Value = new Structure();
ModelHandle handle = ModelMapper.GetModelHandle(namespaceUris, m_model);
if (handle.Mappings.Count > 0)
{
ushort nsIdx = (ushort)namespaceUris.IndexOf(namespaceUri);
foreach (ModelMapping mapping in handle.Mappings)
{
if (mapping.BrowsePath.Length == 1)
{
if (mapping.BrowsePath[0] == new QualifiedName(BrowseNames.StructureID, nsIdx))
{
mapping.MappingData = new Delegate[] {
new ModelMappingGetValueHandler(GetStructureID),
new ModelMappingSetValueHandler(SetStructureID)
};
}
}
}
}
return handle;
}
private DataValue GetStructureID(IMapperContext context)
{
return new DataValue()
{
WrappedValue = new Variant(m_model.Value.StructureID),
SourceTimestamp = DateTime.UtcNow,
ServerTimestamp = DateTime.UtcNow
};
}
private void SetStructureID(IMapperContext context, DataValue value)
{
m_model.Value.StructureID = value.WrappedValue.ToInt32();
}
}