Exposing and synchronizing nested structures

Questions regarding the use of the .NET SDK 2.0 for Server or Client development or integration into customer products ...

Moderator: uasdknet

Post Reply
richmont
Jr. Member
Jr. Member
Posts: 4
Joined: 17 Nov 2016, 08:19

Exposing and synchronizing nested structures

Post by richmont »

Hi,

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;
}
My Model:

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();
    }
  }

Post Reply