PIGlobalRestorer Example

 

This example shows how to persist a PIModule to a persistence string and restore it from the same string.

When the first button is clicked it will persist a module in the PIModule database of the default server and write the persistence string to a text box. When the second button is pressed, the string in the text box will be used to restore the module using a PIGlobalRestorer object.

 

Build the PIGlobalRestorer 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.

 

'
' This program persists a PIModule to a persistence string that is displayed
' in a text box. The module is then restored from the persistence string.
' The module name to be persisted and the server it comes from are displayed in message boxes.
' The restored module name and the server it comes from are displayed in message boxes.
'
' 2001-Dec-20 KEC> Written
'
Option Explicit

Private Sub Command1_Click()
'
' This subroutine gets the first module in the module database and persists it
'
Dim module As PIModule ' module to persist
Dim IPIP As IPIPersist ' IPIPersist interface

Screen.MousePointer = vbHourglass ' the next statement will log in automatically
Set module = PISDK.Servers.DefaultServer.PIModuleDB.PIModules(1) ' get first module
Screen.MousePointer = vbDefault ' restore cursor
MsgBox module.Name, vbOKOnly, "Module to be Persisted" ' show the name
MsgBox module.Server.Name, vbOKOnly, "Server of Module to be Persisted" ' show name of server
Set IPIP = module ' get the IPIPersist interface from the module
Text1.Text = IPIP.Persist ' save persistence string to text box

End Sub

Private Sub Command2_Click()
'
' This subroutine restores the module from the persistence string in the text box. If you want to cause
' an error, edit the persistence string
'
Dim module As PIModule ' module to restore
Dim IPGR As IPIGlobalRestorer ' interface to get global restorer object
Dim restorer As PIGlobalRestorer ' global restorer object
Dim errors As PIErrors ' error collection to hold any errors
Dim curError As PIError ' current error
Dim nvsServers As NamedValues ' named value collection to hold the servers
Dim nvServer As NamedValue ' current named value
Dim curServer As Server ' current server
'
' Get global restorer from PISDK object
'
Set IPGR = PISDK.PISDK
Set restorer = IPGR.restorer
'
' Restore module
'
Set module = restorer.RestoreObject(Text1.Text)
MsgBox module.Name, vbOKOnly, "Module Restored"

Set nvsServers = restorer.RestoreServers(Text1.Text, errors)
'
' Display any errors
'
For Each curError In errors
MsgBox curError.Cause & " " & curError.Description, vbCritical, "ERROR"
Next
'
' Display servers
'
For Each nvServer In nvsServers
Set curServer = nvServer.Value ' get the server
MsgBox curServer.Name, vbOKOnly, "Servers"
Next

End Sub



Enabling Operational Intelligence