IPIVersion Example 

 

Option Explicit
Dim g_RootModule As PIModule
Dim g_PIServer As Server

Private Sub Command1_Click()
'
' Open Server
'
Set g_PIServer = Servers.DefaultServer
g_PIServer.Open "uid=piadmin"

CreateRootModule
CreateFirstVersion
CreateSecondVersion
ShowBothVersions
Cleanup
End Sub
Private Sub CreateRootModule()
Dim curTime As New PITime
Dim modName As String
'
' Create a root module with a unique name
'
curTime.SetToCurrent
modName = curTime.UTCSeconds
Set g_RootModule = g_PIServer.PIModuleDB.PIModules.Add(modName)
End Sub

Private Sub CreateFirstVersion()
Dim version1 As PIModule
'
' Create the first version of a module at the root level.
' Since we aren't specifying an EffectiveDate, an EffectiveDate of "1" (beginning of time) is used.
'
Set version1 = g_RootModule.PIModules.Add("SubModule")
'
' Add some Aliases and Properties
'
version1.PIAliases.Add "Sine", g_PIServer.PIPoints.Item("Sinusoid")
version1.PIAliases.Add "State", g_PIServer.PIPoints.Item("cdm158")
version1.PIProperties.Add "Data1", "Link to Data1"
version1.PIProperties.Add "Data2", "Link to Data2"
'
' Display EffectiveDate
'
MsgBox "Module Version 1 effective date: " & version1.Version.EffectiveDate.LocalDate _
& ". Version String: " & version1.Version.VersionString
End Sub

Private Sub CreateSecondVersion()
Dim version1 As PIModule
Dim version2 As PIModule
Dim curTime As New PITime
'
' Retrieve Version1 of the Module so we can add another version
' Set the QueryDate of the root module to Nothing, so it will default to the current time
'
g_RootModule.SetQueryDate Nothing
Set version1 = g_RootModule.PIModules("SubModule")
'
' Make a Copy of Version1 to make Version2
'
curTime.SetToCurrent ' use current time - 1 day for second version
curTime.UTCSeconds = curTime.UTCSeconds - 86400
Set version2 = version1.Version.Copy(curTime)
'
' Add some new Aliases and Properties
'
version2.PIAliases.Add "Sine2", g_PIServer.PIPoints.Item("SinusoidU")
version2.PIAliases.Add "State2", g_PIServer.PIPoints.Item("cdm158")
version2.PIProperties.Add "Data3", "Link to Data3"
version2.PIProperties.Add "Data4", "Link to Data4"
'
' Display EffectiveDate
'
MsgBox "Module Version 2 effective date: " & version2.Version.EffectiveDate.LocalDate _
& ". Version String: " & version2.Version.VersionString
End Sub

Private Sub ShowBothVersions()
Dim version1 As PIModule
Dim version2 As PIModule
Dim queryDate As New PITime
'
' To get the different versions, change the QueryDate of g_RootModule
' Version1 has an EffectiveDate of 1 and Version2 has an EffectiveDate of a short time ago.
' Get Version2 first by setting the EffectiveDate of g_RootModule so it will default to the current time
'
g_RootModule.SetQueryDate Nothing
Set version2 = g_RootModule.PIModules("SubModule")
'
' We have two ways to get to Version1.
' First Navigate to it from Version2
'
Set version1 = version2.Version.PrevValue
'
' The second way is to change the QueryDate of g_RootModule and ask for "SubModule" again
' Use 1 second before the EffectiveDate of Version2
'
queryDate.UTCSeconds = version2.Version.EffectiveDate.UTCSeconds - 1
g_RootModule.SetQueryDate queryDate
Set version1 = g_RootModule.PIModules("SubModule")
'
' Display both effectiveDate at the same time on the form
'
Text1.Text = "Module effective date: " & version1.Version.EffectiveDate.LocalDate _
& ". Version String: " & version1.Version.VersionString
Text2.Text = "Module effective date: " & version2.Version.EffectiveDate.LocalDate _
& ". Version String: " & version2.Version.VersionString
End Sub

Private Sub Cleanup()
'
' To clean up, we don't care how many versions "SubModule" has. All we need to do is remove the one reference
' to g_rootModule which only has a single version
'
g_RootModule.PIModules.Remove "SubModule"
'
' Now we can remove g_RootModule from the root collection of the PIModuleDB
'
g_PIServer.PIModuleDB.PIModules.Remove g_RootModule
'
' Now g_RootModule is no longer in the database, so set it to nothing
'
Set g_RootModule = Nothing
End Sub



 

 

Enabling Operational Intelligence