public LookupDataTypeModel GetAttributeDataType(ProfileTypeDefinitionModel profileItem, IDALContext dalContext) { var attributeDataType = dalContext.GetDataType(_model.DisplayName?.FirstOrDefault()?.Text); if (attributeDataType == null) { //TODO: SC - Question - MH - Please review this. //At this point, can we add the custom data type record (the profile item and the data type record) //Note we first do need to insert the custom data type as a profile item. //Possibly wrap ImportProfileItemAsync and a ImportCustomDataTypeAsync into one call. // MH: DataType.ImportProfileItemAsync now creates the Data Type lookup entry var dataTypeProfile = this.ImportProfileItem(dalContext); if (dataTypeProfile == null) { throw new Exception($"Undefined data type {_model.DisplayName?.FirstOrDefault()?.Text} {_model.NodeId} in profile {profileItem.Name} ({profileItem.ID})"); } attributeDataType = dalContext.GetCustomDataTypeAsync(dataTypeProfile).Result; if (attributeDataType == null) { // TODO expand LookupDataTypeModel with object reference attributeDataType = new LookupDataTypeModel { Name = dataTypeProfile.Name, Code = "custom", CustomType = dataTypeProfile }; } } return(attributeDataType); }
protected override void UpdateProfileItem(ProfileTypeDefinitionModel profileItem, IDALContext dalContext) { profileItem.TypeId = (int)ProfileItemTypeEnum.CustomDataType; base.UpdateProfileItem(profileItem, dalContext); if (profileItem.TypeId != (int)ProfileItemTypeEnum.CustomDataType) { throw new Exception(); } var attributeNamespace = NodeModelUtils.GetNamespaceFromNodeId(_model.NodeId); if (_model.StructureFields?.Any() == true || _model.HasBaseType("nsu=http://opcfoundation.org/UA/;i=22")) { profileItem.TypeId = (int)ProfileItemTypeEnum.Structure; if (profileItem.Attributes == null) { profileItem.Attributes = new List <ProfileAttributeModel>(); } foreach (var field in _model.StructureFields ?? new List <DataTypeModel.StructureField>()) { var fieldDataType = field.DataType; if (fieldDataType == null) { throw new Exception($"Unable to resolve data type {field.DataType?.DisplayName}"); } var attributeDataType = fieldDataType.GetAttributeDataType(profileItem, dalContext); if (attributeDataType == null) { throw new Exception($"{fieldDataType} not resolved"); } var attribute = new ProfileAttributeModel { IsActive = true, Name = field.Name, BrowseName = $"{attributeNamespace};{field.Name}", //No SymbolicName for structure fields Namespace = attributeNamespace, IsRequired = !field.IsOptional, Description = field.Description?.FirstOrDefault()?.Text, AttributeType = new LookupItemModel { ID = (int)AttributeTypeIdEnum.StructureField }, DataType = attributeDataType, OpcNodeId = NodeModelUtils.GetNodeIdIdentifier(_model.NodeId), }; profileItem.Attributes.Add(attribute); } } if (_model.EnumFields?.Any() == true) { profileItem.TypeId = (int)ProfileItemTypeEnum.Enumeration; if (profileItem.Attributes == null) { profileItem.Attributes = new List <ProfileAttributeModel>(); } foreach (var field in _model.EnumFields) { var int64DataType = dalContext.GetDataType("Int64"); if (int64DataType == null /* || int64DataType.ID == 0*/) { throw new Exception($"Unable to resolve Int64 data type."); } var attribute = new ProfileAttributeModel { IsActive = true, Name = field.Name, BrowseName = $"{attributeNamespace};{field.Name}", // No SymbolicName for enum fields DisplayName = field.DisplayName?.FirstOrDefault()?.Text, Description = field.Description?.FirstOrDefault()?.Text, AttributeType = new LookupItemModel { ID = (int)AttributeTypeIdEnum.EnumField }, DataType = int64DataType, EnumValue = field.Value, Namespace = attributeNamespace, }; profileItem.Attributes.Add(attribute); } } }