13 Replies Latest reply on Nov 9, 2012 4:01 PM by Marcos Vainer Loeff

    PI Server Connection removal

    rolw

      Is there a way via the SDK to remove old retired server names and add the new desired name, even setting the default for the connection list?

        • Re: PI Server Connection removal
          hanyong

          Hi Robert,

           

          Yes there is a way to do this. If you are using VB.NET or C#, once you have initiated the root PISDK object, you can make the above changes through the PISDK.Servers collection

          PISDK.PISDK pisdk = new PISDK.PISDK();

          // remove server from known servers table
          pisdk.Servers.Remove("name");

          // add new server 
          Server server = pisdk.Servers.Add("name", null, "PI3", true);

          // setting default server
          pisdk.Servers.DefaultServer = server ;

          It is also possible to set a new server as the default server as you add it to the known servers table, you need to set the appropriate connectstring parameter (I've put that as null in the above code snippet). You can refer to the method in the PI SDK Programming Reference about that.

            • Re: PI Server Connection removal
              rolw

              how about the setting for autoadd? I would like to disable that as well to force displays opened with the old server to prompt the user to enter the new name from the pulldown.

                • Re: PI Server Connection removal
                  rolw

                  so I put this code together to attempt to set the AutoAdd feature, it does not seem to like it, any help here?

                   

                         svr = New PISDK.PISDK().Servers

                   

                         For Each server In svr

                   

                             If UCase(server.name) = "LHNT01" Then

                   

                                 retval = MsgBox("Found LHNT01 in the server list, would you like to remove it?", vbYesNo)

                   

                                 If retval = True Then

                   

                                     svr.Remove(server.name)

                   

                                     'MsgBox(server.name & " was removed")

                   

                                     clnLHNT01 = True

                   

                                 End If

                   

                             End If

                   

                         Next server

                   

                         svr = Nothing

                   

                         '

                   

                         ' Set the default PI server

                   

                         svr = New PISDK.PISDK().Servers

                   

                         svr.DefaultServer = svr("LHVNTPHIST01")

                   

                         svr = Nothing

                   

                   

                   

                         PISDK.IPISDKOptions.SetDirectoryOptions(pnvsOptions:=AutoAdd, bPersist:=True)

                    • Re: PI Server Connection removal
                      hanyong

                      Hi Robert,

                       

                      I guess by AutoAdd, you mean when you have an tasks that requires a connection to a server that is not in the Known Servers Table, you want to add the server automatically?

                       

                      I think this feature is available in the newer version of PI SDK (I can't recall which is the exact version though). You should be able to see this Server Name Resolution option in PI SDK Utility or AboutPI-SDK it it is available.

                       

                      All you have to do is to attempt to get the Server object reference from the PISDK.Servers collection based on the name of the new server you want to add.

                      server = pisdk.Servers["<new server name>"];

                      I think it will always try to find and identify the server on the network before adding, so if your server is not available or the server name is wrong, you will get an exception.

                        • Re: PI Server Connection removal
                          rolw

                          Yes, I am trying to set the Autoadd missing or new servers that are referenced by a Process Book display. The last line of my code below is what I am trying and the method is not known and gives an error. I am running 1.4.0.416 of the SDK (11/21/2011).

                           

                          PISDK.IPISDKOptions.SetDirectoryOptions(pnvsOptions:=AutoAdd, bPersist:=True)

                            • Re: PI Server Connection removal

                              Hello Robert,

                               

                              I was curious on my own and managed to Get and Set the AutoAdd and the CheckIP directory option. All I can say for now is that you are on the right track. I will post an example later today.

                               

                              Gregor

                                • Re: PI Server Connection removal

                                  Hello Robert,

                                   

                                  I have created 2 examples GetSDKDirectoryOptions and SetSDKDirectoryOptions as console applications. Both require references to PISDK and PISDKCommon library.

                                   

                                  The second one expects 2 parameters - AutoAdd (True / False) and CheckIP (True / False). For details please check the PI SDK Programming Reference for IPISDKOptions Interface. Please also note that SetDirectoryOptions method requires a parameter of type Boolean specifying if the change is persistent.

                                   

                                  # 1 GetSDKDirectoryOptions

                                   
                                  Imports PISDK
                                  Imports PISDKCommon
                                  
                                  Module Module1
                                  
                                      Sub Main()
                                          Try
                                              Dim mySDK As PISDK.PISDK
                                              Dim myOpt As PISDK.IPISDKOptions
                                              Dim myNVS As NamedValues
                                              Dim sReturn As String
                                              If mySDK Is Nothing Then
                                                  mySDK = New PISDK.PISDK
                                              End If
                                              If myOpt Is Nothing Then
                                                  myOpt = mySDK
                                              End If
                                              ' Without the following line of code I get an exception
                                              ' 0x800407A0 "The Servers collection or the ServerManager have not yet been instantiated."
                                              Console.WriteLine("Default Server: " & mySDK.Servers.DefaultServer.Name)
                                              myNVS = myOpt.GetDirectoryOptions()
                                              For Each myNV As NamedValue In myNVS
                                                  sReturn = myNV.Name & ": "
                                                  Select Case myNV.Value
                                                      Case -1
                                                          sReturn = sReturn & "True"
                                                      Case 0
                                                          sReturn = sReturn & "False"
                                                  End Select
                                                  Console.WriteLine(sReturn)
                                              Next
                                          Catch ex As Exception
                                              Console.WriteLine(ex.Message)
                                          Finally
                                              Console.Write("Press any key to close ...")
                                              Console.ReadKey()
                                          End Try
                                      End Sub
                                  
                                  End Module
                                  

                                   # 2 SetSDKDirectoryOptions

                                   
                                  Imports PISDK
                                  Imports PISDKCommon
                                  Module Module1
                                  
                                      Sub Main()
                                          Try
                                              Dim args() As String = System.Environment.GetCommandLineArgs()
                                              Dim mySDK As PISDK.PISDK
                                              Dim myOPT As PISDK.IPISDKOptions
                                              Dim nvs As New NamedValues
                                              Dim piErrs As PIErrors
                                              Dim bValid As Boolean = True
                                              If args.Length = 3 Then
                                                  Select Case args(1)
                                                      Case "True"
                                                          nvs.Add("AUTOADD", -1)
                                                      Case "False"
                                                          nvs.Add("AUTOADD", 0)
                                                      Case "-1"
                                                          nvs.Add("AUTOADD", -1)
                                                      Case "0"
                                                          nvs.Add("AUTOADD", 0)
                                                      Case Else
                                                          bValid = False
                                                  End Select
                                  
                                                  Select Case args(2)
                                                      Case "True"
                                                          nvs.Add("CHECKIP", -1)
                                                      Case "False"
                                                          nvs.Add("CHECKIP", 0)
                                                      Case "-1"
                                                          nvs.Add("CHECKIP", -1)
                                                      Case "0"
                                                          nvs.Add("CHECKIP", 0)
                                                      Case Else
                                                          bValid = False
                                                  End Select
                                  
                                                  If bValid = True Then
                                                      If mySDK Is Nothing Then
                                                          mySDK = New PISDK.PISDK
                                                      End If
                                                      If myOPT Is Nothing Then
                                                          myOPT = mySDK
                                                      End If
                                                      ' Without the following line of code I get an exception
                                                      ' 0x800407A1 "Either Servers or ServerManager must be instantiated first."
                                                      Console.WriteLine("Default Server: " & mySDK.Servers.DefaultServer.Name)
                                                      piErrs = myOPT.SetDirectoryOptions(nvs, True)
                                                      If piErrs.Count = 0 Then Console.WriteLine("DirectoryOptions have been set succesfully.")
                                                      For Each myErr As PIError In piErrs
                                                          Console.WriteLine(myErr.Number & ": " & myErr.Cause)
                                                          Console.WriteLine(myErr.Description)
                                                          Console.WriteLine()
                                                      Next
                                                  Else
                                                      Console.WriteLine("You've entered an invalid parameter. Please try again.")
                                                  End If
                                              Else
                                                  Console.WriteLine("Please enter 2 parameters. The first one specifies if you would")
                                                  Console.WriteLine("like to enable AutoAdd and the second one if in case of AutoAdd")
                                                  Console.WriteLine("the new server reference should be resolved (CheckIP).")
                                              End If
                                          Catch ex As Exception
                                              Console.WriteLine(ex.Message)
                                          Finally
                                              Console.Write("Press any key to close ...")
                                              Console.ReadKey()
                                          End Try
                                      End Sub
                                  
                                  End Module
                                  

                                   Gregor 

                                    • Re: PI Server Connection removal
                                      Marcos Vainer Loeff

                                      Hello Robert,

                                       

                                      I have already written my answer when I saw Gregor´s examples, so I will post it anyway.

                                       

                                      If you take a look on the PI SDK Programming Reference, you will find:

                                       

                                      "This method takes a  NamedValues collection of directory option names and their intended state and sets the corresponding in-memory flags used to control behaviors.  The method returns a PIErrors collection. Before calling this method, the user must first instantiate either the PI SDK object and the Servers collection or a ServerManager object.  "

                                       

                                      The syntax is: object.SetDirectoryOptions pnvsDirectoryOptions, bPersist

                                       

                                      pnvsDirectoryOptions is a NamedValues variable and bPersist is a boolean variable.

                                       

                                      Please take a look at the example below (VB.NET) in order to change this option using PI SDK:

                                       

                                       

                                       

                                       

                                       
                                              Dim sdkroot As New PISDK.PISDK
                                              Dim myPIServers As Servers = sdkroot.Servers
                                      
                                              Dim ipiopt As PISDK.IPISDKOptions = Nothing
                                      
                                              If ipiopt Is Nothing Then
                                                  ipiopt = sdkroot
                                              End If
                                      
                                      
                                              Dim GetDir As New NamedValues
                                              GetDir = ipiopt.GetDirectoryOptions()
                                      
                                              For Each nv As NamedValue In GetDir
                                                  Console.WriteLine(nv.Name & "," & nv.Value)
                                              Next
                                      
                                              Dim SetDir As New NamedValues
                                              SetDir.Add("AUTOADD", 0)
                                      
                                              ipiopt.SetDirectoryOptions(SetDir, True)
                                      

                                       

                                       

                                      I am using the method GetDirectoryOptions to view the current configuration before changing it with the method SetDirectoryOptions(SetDir, True).

                                       

                                      Remember that if you don´t want to change this setting on the registry, but only on your current session, you should use:

                                       

                                      ipiopt.SetDirectoryOptions(SetDir, False)

                                       

                                      Any question I am available,

                                       

                                      Regards,

                                       

                                       

                                       

                                       

                                        • Re: PI Server Connection removal
                                          rolw

                                          both worked great, many thanks. Now, let's challenge one more item. I need to set the PRIORITY of a Collective member (not the primary but one of the secondary members) to a 1 and set the Primary to 2. I want to be able to set selected users to hit the secondary server first because of their location.

                                           

                                          How do you get to the section of the collective members and those parameters?

                                            • Re: PI Server Connection removal

                                              Hello Robert,

                                               

                                              Please check the PI SDK Programming Reference for the IPICollective Interface.

                                               

                                              Gregor

                                                • Re: PI Server Connection removal
                                                  Marcos Vainer Loeff

                                                  Hello Robert,

                                                   

                                                   

                                                   

                                                  I have created another example for you using Gregor´s suggestion (IPICollective interface):

                                                   

                                                   

                                                   

                                                   

                                                   

                                                   

                                                   
                                                  Imports PISDK
                                                  Imports PISDKCommon
                                                  
                                                  
                                                  
                                                  Module Module1
                                                  
                                                      Sub Main()
                                                  
                                                          Dim sdkroot As New PISDK.PISDK()
                                                          Dim MyPIServers As Servers = sdkroot.Servers
                                                  
                                                          'Change priorities of the members of the collective
                                                          Dim ipcol As IPICollective = sdkroot.Servers.Item("COLLECTIVENAME")
                                                  
                                                          ipcol.ListMembers().Item("PRIMARY").Priority = 1
                                                          ipcol.ListMembers().Item("SECONDARY").Priority = 2
                                                  
                                                          'View settings of the members of the collective
                                                  
                                                          For Each MyServer As Server In MyPIServers
                                                              Dim ipicol As IPICollective = MyServer
                                                              Console.WriteLine("PI Server Name:" + MyServer.Name)
                                                              Console.WriteLine("Is Collective Member? " & ipicol.IsCollectiveMember)
                                                              If (ipicol.IsCollectiveMember) Then
                                                                  Console.WriteLine("Connection Preference: " + ipicol.ConnectionPreference)
                                                                  Console.WriteLine("Collective Name: " + ipicol.CollectiveName)
                                                                  Dim collist As CollectiveList = ipicol.ListMembers()
                                                                  For Each colmem As CollectiveMember In collist
                                                                      Console.WriteLine("Collective Member Name: " & colmem.Name)
                                                                      Console.WriteLine("Collective Member Path: " & colmem.Path)
                                                                      Console.WriteLine("Collective Member Priority: " & colmem.Priority)
                                                                      Console.WriteLine("Collective Member ServerID: " & colmem.ServerID)
                                                                      Console.WriteLine("Collective Member Servertype: " & colmem.ServerType)
                                                                      Console.WriteLine("Collective Member Timeout: " & colmem.Timeout)
                                                                      Console.WriteLine("Collective Member Connection Timeout: " & colmem.ConnectTimeout)
                                                                  Next
                                                              End If
                                                          Next
                                                          Console.ReadKey()
                                                  
                                                      End Sub
                                                  
                                                  End Module
                                                  

                                                   

                                                   

                                                  If you have any question, please don't hesitate to ask.

                                                   

                                                  Regards,