14 Replies Latest reply on Jul 11, 2014 2:55 PM by Gregor

    Simple question

    davegardner99

      Hi, I was wondering how to use the SDK to get the servers description. I would imagine that i'm missing something really obvious but after 3 minutes with no luck you guys are my last resort.

       

      Thanks,

        • Re: Simple question
          davegardner99

          LOL, that is supposed to read 3 hours sorry!

            • Re: Simple question
              Roger Palmen

              The PISDK does have the CollectiveMember.Description property, but i guess that only works if you have a collective.

               

              Should be possible one would say, as the PISDKUtility can show the PI Server description.

              • Re: Simple question
                xwang

                Hi David,

                 

                From my experience, your goal should be to read the description of PI AF Server via PI AF SDK, is that right please?  As I cannot see anything about "descirption" as a PI Server property.

                 

                If I am right, please read the PI AF SDK menu, Namespaces -> OSIsoft.AF Namespace -> PI System Class -> PISystem Properties -> Description Property.  However, if I am wrong, could you please make a simple screenshot on how you config a "description" to a PI Server?  I am sorry about this and many thanks

                 

                Xi Wang

                 

                vCampus team

                  • Re: Simple question
                    davegardner99

                    Hi Xi,

                     

                    I am after the PI Server description which is normally here:

                     

                    dl.dropboxusercontent.com/.../scrnShot.png

                      • Re: Simple question

                        Hi David,

                         

                        You raised a simple question but a challenging one

                         

                        I have no glue if this is the intended way to lift the secret but it works and there's nothing tricky with the code itself.

                         

                         

                         
                        using System;
                        using System.Collections.Generic;
                        using System.Linq;
                        using System.Text;
                        using PISDK;
                        
                        namespace GetPISDKInfo
                        {
                            class Program
                            {
                                static void Main(string[] args)
                                {
                                    try
                                    {
                                        PISDK.PISDK mySDK = new PISDK.PISDK();
                                        PISDK.Server mySrv = mySDK.Servers.DefaultServer;
                                        IPICollective myCol = mySrv as IPICollective;
                                        Console.WriteLine(myCol.CollectiveDescription);
                                    }
                                    catch (Exception ex)
                                    {
                                        Console.ForegroundColor = ConsoleColor.Red;
                                        Console.WriteLine(ex.Message);
                                    }
                                    finally
                                    {
                                        Console.Write("Done. Press any key to quit ...");
                                        Console.ReadKey();
                                    }
                                }
                            }
                        }
                        

                         

                          • Re: Simple question

                            Someone might be interested on how to set the description of a PI Data Archive (PI Server).

                             

                            I've used the following piconfig commands:  

                             
                            * (Ls - ) PIconfig> @table piserver
                            * (Ls - PISERVER) PIconfig> @mode edit
                            * (Ed - PISERVER) PIconfig> @istr Name, Description
                            * (Ed - PISERVER) PIconfig> MyPIDataArcive, Description of my PI Data Archive
                            * (Ed - PISERVER) PIconfig> @ends
                            * (Ed - PISERVER) PIconfig> @bye
                            

                             

                              • Re: Simple question
                                Roger Palmen

                                Is every PI server member of a collective, even if no collective defined? At least seen from code. Well, if you tested this, it probably is!

                                  • Re: Simple question

                                    Hi Roger,

                                     

                                    No, this is not the case but the Description is a property that exists with the IPICollective interface. You need to cast the PISDK.Server as IPICollective. I've tested the code with a standalone PI Server. It's required to connect first to get the PI Servers description.

                                      • Re: Simple question
                                        Rick Davin

                                        As Gregor said, he did not test his code and in later remarks he says that the server must be connected.  While I appreciate the use of try-catch, if one works on the safe assumption that your default server is set and it can be connected to, then this code will work:

                                         

                                         

                                         
                                                static void Main(string[] args)
                                                {
                                                    var mySDK = new PISDK.PISDK();
                                                    var mySrv = mySDK.Servers.DefaultServer;
                                        
                                                    // Later the CollectiveDescription requires an open server.
                                                    mySrv.Open();
                                                    
                                                    var myCol = mySrv as IPICollective;
                                                    
                                                    // Note that myCol is not null but may not be an actual collective.
                                                    // A stand-alone server will have a null CollectiveDescription.
                                                    // If you want a non-null value, then you must check to see if the
                                                    // server is actually a collective member.
                                                    string description = (myCol.IsCollectiveMember) ? myCol.CollectiveDescription : "";
                                        
                                                    Console.WriteLine("Description = " + description);
                                        
                                                    Console.Write("Done. Press any key to quit ...");
                                                    Console.ReadKey();
                                                }
                                        

                                         The myCol object should not be null, even for stand-alone servers.  Thus the myCol.CollectiveDescription will not throw an exception ... unless the server has not been connected.  However, one can still check myCol.IsCollectiveMember to verify whether or not the server is stand-alone or a member of a collection.

                                          • Re: Simple question

                                            Hi Rick,

                                             

                                            Sorry, but there must be a misunderstanding. I did not say the code was untested.

                                             

                                            I know there exist different opinions about using try-catch blocks. Personally, I feel " ... unhandled exception ..." looks ugly but I know some "hard core" developers prefer using a just in time debugger. I am wondering if where try-catch makes sense and where not might be an interesting topic for a discussion at General .NET development forum.

                                             

                                            I've further refined the example, that I posted earlier and like to share the result. 

                                             
                                                class Program
                                                {
                                                    struct myReturnValue
                                                    {
                                                        public Boolean bSuccess;
                                                        public Boolean bCollective;
                                                        public string sResult;
                                                    }
                                            
                                                    static void Main(string[] args)
                                                    {
                                                        PISDK.PISDK mySDK = new PISDK.PISDK();
                                                        foreach (Server mySrv in mySDK.Servers)
                                                        {
                                                            myReturnValue myRV = GetSrvDesc(mySrv);
                                                            if (myRV.bCollective) 
                                                            {
                                                                Console.ForegroundColor = ConsoleColor.Green;
                                                            }
                                                            else
                                                            {
                                                                Console.ForegroundColor = ConsoleColor.Yellow;
                                                            }
                                                            if (myRV.bSuccess)
                                                            {
                                                                Console.WriteLine(myRV.sResult);
                                                            }
                                                            else
                                                            {
                                                                Console.ForegroundColor = ConsoleColor.Red;
                                                                Console.WriteLine(myRV.sResult);
                                                            }
                                                        }
                                                        Console.Write("Done. Press any key to quit ...");
                                                        Console.ReadKey();
                                                    }
                                            
                                                    static myReturnValue GetSrvDesc(Server Srv) 
                                                    {
                                                        myReturnValue RetVal = new myReturnValue();
                                                        string tmpS = Srv.Name;
                                                        try
                                                        {
                                                            Srv.Open();
                                                            IPICollective myCol = Srv as IPICollective;
                                                            if (myCol.IsCollectiveMember)
                                                            {
                                                                RetVal.bCollective = true;
                                                                tmpS = tmpS + " is a Collective \r\n";
                                                                tmpS = tmpS + "Description: " + myCol.CollectiveDescription + "\r\n";
                                                                CollectiveList ColList = myCol.ListMembers();
                                                                int iMember = 1;
                                                                foreach (CollectiveMember ColMember in ColList)
                                                                {
                                                                    tmpS = tmpS + "    # " + iMember.ToString() + " " + ColMember.Name;
                                                                    tmpS = tmpS + " " + ColMember.NodeType.ToString() + "\r\n";
                                                                    tmpS = tmpS + "    Description: " + ColMember.Description + "\r\n";
                                                                    iMember ++;
                                                                }
                                                            }
                                                            else
                                                            {
                                                                RetVal.bCollective = false;
                                                                tmpS = tmpS + " is Standalone \r\n";
                                                                tmpS = tmpS + "  Description: " + myCol.CollectiveDescription + "\r\n";
                                                            }
                                                            RetVal.sResult = tmpS;
                                                            RetVal.bSuccess = true;
                                                            return RetVal;
                                                        }
                                                        
                                                        catch (Exception ex)
                                                        {
                                                            tmpS = tmpS + ": " + ex.HResult.ToString() + ": " + ex.Message;
                                                            RetVal.sResult = tmpS;
                                                            RetVal.bSuccess = false;
                                                            return RetVal;
                                                        }
                                                    }
                                                }
                                            

                                             

                                              • Re: Simple question
                                                Roger Palmen

                                                Almost looks like a customer project. Ask a simple question, get a complex piece of code and technical discussions.  ;-)

                                                  • Re: Simple question

                                                    Well, I recognized with a PI Server Collective there's a Description for the Collective and each single member. Above code takes this into account.

                                                      • Re: Simple question
                                                        Rick Davin

                                                        Right you are, Roger.  A simple question deserves a simple answer: doesn't appear the Description is available for a stand-alone PI server.  A Description is available for a collective but requires you connect to the collective first.

                                                          • Re: Simple question

                                                            Hi Rick,

                                                             

                                                            The description is available for standalone PI Servers but it doesn't exist as property of PISDK.Server but for the IPICollective interface. Hence PISDK.Server object needs to be casted to IPICollective after connecting. Connection is a requirement because the Description is not stored in registry but within .DAT files belonging to the PI Data Archive (PI Server).

                                                             

                                                            You may recognize answering the simple question isn't that trivial and hence I found it justified creating a more extensive code example that shows how it works.