PIDummy Example

 

This example shows how a PIDummy object will be created when a PIModuleList is restored from a persistence string after one of the modules in the list has been deleted. This preserves the index location of the other modules in the list.

 

Build the PIDummy example as follows:

1. Create a new project and add a reference to the PISDK Library.

2. Add two command buttons and a text box.

3. Cut and paste the code below into the project.

 

'
' If you click on Command1, this program adds two modules to the default PIServer and to a PIModuleList.
' It then persists the PIModuleList, putting the persistence string into the text box.
'
' If you click on Command2, this program removes the first of the PIModule's added and then restores the
' PIModuleList from the persistence string. Since one of the PIModule's no longer exists, it is replaced
' in the restored PIModuleList with a PIDummy object.
'
' 2002-Mar-28 KEC> Written
'
Option Explicit
Dim g_PIModule1 As PIModule
Dim g_PIModule2 As PIModule
Dim g_RootModules As PIModules ' get root modules collection once

Private Sub Command1_Click()
'
' This subroutine adds two modules to the default PIServer and to a PIModuleList.
' It then persists the PIModuleList, putting the persistence string into the text box.
'
Dim modName As String ' module name
Dim curDate As Date ' date used to generate unique module name
Dim myList As PIModuleList ' PIModuleList to persist
Dim IPIP As IPIPersist ' IPIPersist interface
Dim myServer As Server ' get server once

Set myServer = PISDK.Servers.DefaultServer
Set g_RootModules = myServer.PIModuleDB.PIModules
'
' Add the two modules by generating a unique name for each
'
curDate = Now
modName = "PIDummy1" & "_" & Format(curDate, "h:m:s") ' generate unique name for module to add
Set g_PIModule1 = myServer.PIModuleDB.PIModules.Add(modName)
modName = "PIDummy2" & "_" & Format(curDate, "h:m:s") ' generate unique name for module to add
Set g_PIModule2 = myServer.PIModuleDB.PIModules.Add(modName)
'
' Give them descriptions so we can check on them later
'
g_PIModule1.Description = "Module1 Description"
g_PIModule2.Description = "Module2 Description"
'
' Add the two modules to a PIModuleList
'
Set myList = New PIModuleList
myList.Insert g_PIModule1
myList.Insert g_PIModule2
'
' Persist the list and put the persistence string into the text box
'
Set IPIP = myList ' get the IPIPersist interface from the list
Text1.Text = IPIP.Persist ' save persistence string to text box

End Sub

Private Sub Command2_Click()
'
' This subroutine removes the first of the PIModule's added and then restores the
' PIModuleList from the persistence string. Since one of the PIModule's no longer exists, it is replaced
' in the restored PIModuleList with a PIDummy object.
'
Dim restoredList As PIModuleList ' restored module list
Dim IPGR As IPIGlobalRestorer ' interface to get global restorer object
Dim restorer As PIGlobalRestorer ' global restorer object
Dim myDummy As PIDummy ' PIDummy interface for checking items in the list
Dim curMod As PIModule ' current PIModule (or dummy)
Dim loopInx As Long ' loop index
'
' First remove the first module from the root PIModules collection
'
g_RootModules.Remove g_PIModule1
'
' Get global restorer from PISDK object
'
Set IPGR = PISDK.PISDK
Set restorer = IPGR.restorer
'
' Restore module list
'
Set restoredList = restorer.RestoreObject(Text1.Text)
'
' The count should be two even though one module has been deleted
'
MsgBox "Restored PIModuleList count = " & restoredList.Count
'
' Loop through items in the list, the first one should be the dummy.
' Since PIDummy implements the PIModule interface, it can be retrieved in a
' "For Next" loop of PIModules.
'
loopInx = 1
For Each curMod In restoredList
On Error Resume Next
Set myDummy = curMod
On Error GoTo 0
If (Not myDummy Is Nothing) Then
MsgBox "Item#: " & loopInx & " is a PIDUMMY object, UniqueID: " & myDummy.UniqueID
Else
MsgBox "Item#: " & loopInx & " is a PIModule object, Description: " & curMod.Description
End If
loopInx = loopInx + 1
Err.Clear
Set myDummy = Nothing
Next
Err.Clear

End Sub



Enabling Operational Intelligence