Best way of connecting to a specific PI Collective member in C# using AF SDK

I have recently started playing around with development using the PI AF SDK and C#. I have been looking for a way to connect to only one member of a PI Collective. I have been testing the following code snippet, but I am not sure if this is the best way of doing it.

static void Main(string[] args)
{
    string piServerName = "myCollectiveName";

    PIServers myPIServers = new PIServers();
    PIServer myPIServer = myPIServers[piServerName];

    try
    {
        if (myPIServer.Collective.Members.Count > 1)
        {
            Console.WriteLine(myPIServer.Collective.Members.Count);

            //Connect to a specific collective member name.
            PICollectiveMember myPiCollMember = myPIServer.Collective.Members["myMemberName"];
            Console.WriteLine("The currently connected collective member name is: {0}", myPiCollMember.Name);
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error connecting to PI Server: {ex.Message}");
    }
    finally
    {
        myPIServer.Disconnect();
    }
}

This seems to work, and I can specify the member to connect to. My use case for wanting to control the collective member is to pull data only from a specific member where historic archives from more then 10 year back are connected. Please let me know if there are better ways of doing this.

Parents Reply Children
  • Hi ​ if I use the ConnectDirect method as suggested would this keep a connection open to the specified collective member when I try to read a tag value from that member?

  • How you connect to a PIServer instance and whether that connection stays open are 2 different things.

     

    Any connected PIServer is theorectically prone to be disconnected the longer the connection stays open. If you have an app that runs and takes 5 minutes, you may never see this. If your app tries to keep a connection open indefinitely, say for a Windows Service, then you should plan for what to do if the connection breaks. Do you plan to stop processing further or implement failover in your app, i.e. connect to another member? That's up to you.

     

    Side note: a disconnection may occur for many possible reasons. That is one reason why PI Collections exist.

     

    If you have a app that is being occasionally, perhaps on-demand or as a scheduled task, then its purpose is to "run-and-be-done". In such apps, the likelihood of a PIServer disconnect is very remote. But if you plan on keeping that connection open for extended periods, then you may be interested in:

    PIServer.ConnectChanged Event

     

    Again, this may be needed in your code regardless of how you connected to the PIServer object. It might be needed even if you are not using a PICollective.

     

    Also to clarify from a developer's perspective, that the PICollectiveMember.ConnectDirect Method returns a connected PIServer, at which point you should stop thinking of it as a member and instead think of it as a PIServer.