RJKSolutions

Module Database to AF conversion

Discussion created by RJKSolutions on Jun 16, 2009
Latest reply on Jun 17, 2009 by andreas

I needed a MDB to AF conversion for a project and as the OSI conversion tool is not quite there yet I wrote some code that others may find useful.  Feel free to criticise the code etc...it did what I needed at the time and works well.  Essentially it converts PIModules to AFElements, PIProperties to AFAttributes and PIAliases to AFAttributes configured with the PI Point DR based on the PIAlias datasource and retains hierarchy.

 

Because of the nature of the project I needed to retain "PIProperties" & "PIAliases" (as both are named the same are correspond to each other e.g. Pump1 PIProperty and Pump1 PIAlias) so for each Module I created PIProperties & PIAliases AFAttributes then created child attributes.

 

Noticed that AFElements cannot contain certain characters so code checks for those and replaces with spaces. 

 

This does not take care of all PIModule properties, more focused just on the structure of MDB.

 

Anyway, make of it as you want...thought I would share it with the community


Private myMDB As PISDK.PIModuleDB
Private mySRV As PISDK.Server
Private mySDK As PISDK.PISDK

Private myDB As AFDatabase
Private myPISystems As New PISystems

Private RegExChars As String = "[\\\/:*?{}""<>|]"

Private Sub StartConversion()
    CreateSDK()
    If ConnectToMDB() And ConnectToAFDB() Then
        If myDB.Elements("ROOT") Is Nothing Then myDB.Elements.Add("ROOT")
        For Each PIMod As PISDK.PIModule In myMDB.PIModules
            If Not PIMod.Name.ToUpper.Equals("%OSI") Then ' Ignore the OSI PIModule that cannot be deleted.
                Dim NewElement As Asset.AFElement = myDB.Elements("ROOT").Elements.Add(RemoveIllegalChars(PIMod.Name))
                Call ParsePIModules(PIMod, NewElement)
            End If
        Next
        myDB.CheckIn()
    End If
End Sub

Private Sub CreateSDK()
    mySDK = New PISDK.PISDK()
End Sub

Private Function ConnectToMDB() As Boolean
    mySRV = mySDK.Servers("my-pi-server-name")
    If Not mySRV.Connected Then mySRV.Open()

    myMDB = mySRV.PIModuleDB
    Return Not myMDB Is Nothing
End Function

Private Function ConnectToAFDB() As Boolean
    myDB = myPISystems("my-pi-system-name").Databases("MDB")
    Return Not myDB Is Nothing
End Function

' Evaluating the PIModules belonging to a PIModule
' MDB - PIModule may contain collection of PIModules
' AF - Add new AFElement to Parent AFElement
Private Sub ParsePIModules(ByVal TheModule As PISDK.PIModule, ByVal TheElement As Asset.AFElement)
    ParsePIProperties(TheModule, TheElement)
    For Each SubModule As PISDK.PIModule In TheModule.PIModules
        Dim NewElement As Asset.AFElement = TheElement.Elements.Add(RemoveIllegalChars(SubModule.Name))
        Call ParsePIModules(SubModule, NewElement)
        Call ParsePIAliases(SubModule, NewElement)
    Next
    myDB.CheckIn()
End Sub

' Evaluating the PIProperties belonging to a PIProperty
' MDB - PIPropery may contain collection of PIProperties
' AF - Add new AFAttribute to Parent AFAttribute
Private Sub ParsePIProperties(ByVal TheProperty As PISDK.PIProperty, ByVal TheAttribute As Asset.AFAttribute)
    For Each SubProperty As PISDK.PIProperty In TheProperty.PIProperties
        Dim NewAttribute As Asset.AFAttribute = TheAttribute.Attributes.Add(RemoveIllegalChars(SubProperty.Name))
        NewAttribute.SetValue(SubProperty.Value, Nothing)
        Call ParsePIProperties(SubProperty, NewAttribute)
    Next
End Sub

' Evaluating the PIProperties belonging to a PIModule
' MDB - PIModule may contain collection of PIProperties
' AF - Add new AFAttribute to Parent AFElement
Private Sub ParsePIProperties(ByVal TheModule As PISDK.PIModule, ByVal TheElement As Asset.AFElement)
    If TheModule.PIProperties.Count > 0 Then
        Dim PropertiesPlaceholder As Asset.AFAttribute = TheElement.Attributes.Add("PIProperties")
        For Each SubProperty As PISDK.PIProperty In TheModule.PIProperties
            Dim NewAttribute As Asset.AFAttribute = PropertiesPlaceholder.Attributes.Add(RemoveIllegalChars(SubProperty.Name))
            NewAttribute.SetValue(SubProperty.Value, Nothing)
            Call ParsePIProperties(SubProperty, NewAttribute)
        Next
    End If
End Sub

' Evaluating the PIAliases belonging to a PIModule
' MDB - PIModule may contain collection of PIAliases down to 1 level
' AF - Add new AFAttribute to Parent AFElement, set DataReferencePlugIn to "PI Point"
Private Sub ParsePIAliases(ByVal TheModule As PISDK.PIModule, ByVal TheElement As Asset.AFElement)
    If TheModule.PIAliases.Count > 0 Then
        Dim AliasesPlaceholder As Asset.AFAttribute = TheElement.Attributes.Add("PIAliases")
        For Each ModAlias As PISDK.PIAlias In TheModule.PIAliases
            Dim NewAttribute As Asset.AFAttribute = AliasesPlaceholder.Attributes.Add(RemoveIllegalChars(ModAlias.Name))
            NewAttribute.DataReferencePlugIn = myPISystems("my-pi-system-name").DataReferencePlugIns("PI Point")
            NewAttribute.ConfigString = "\\" & ModAlias.DataSource.Server.Name & "\" & ModAlias.DataSource.Name
        Next
    End If
End Sub

Private Function RemoveIllegalChars(ByVal OriginalName As String) As String
    If Regex.IsMatch(OriginalName, RegExChars) Then
        Return Regex.Replace(OriginalName, RegExChars, " ")
    Else
        Return OriginalName
    End If
End Function


You can find more details here.

Outcomes