7 Replies Latest reply on Nov 14, 2016 1:46 AM by PFC5478

    FindAttributesByPath in C# Problem

    PFC5478

      Dear all,

       

      I have used PI AF SDK for around two years but only used on PI Data Archive (Namespace: Osisoft.AF.PI) but not AF Server.

       

      To make myself get familiar for using PI AF SDK in AF Server, I am learning from below examples:

      GitHub - osisoft/PI-AF-SDK-Basic-Samples: Self-contained and reproducible samples of basic operations using PI AF SDK.

       

      I am successful to use "AFAttributeExamples\FindAttributeWalkDownExample.cs" to query the result. However,

      I fail to use "AFAttributeExamples\FindAttributesByPathExample.cs" as below (<AFServer> has been replaced to the AF Server that I currently test):

       

                 string processFeedRate = @"\\<AFSERVER>\NuGreen\NuGreen\Houston\Cracking Process\Equipment\B-210|Process Feedrate";
                  string waterFlow = @"\\<AFSERVER>\NuGreen\NuGreen\Houston\Cracking Process\Equipment\B-210|Water Flow";
      
                  AFKeyedResults<string,AFAttribute> results = AFAttribute.FindAttributesByPath(new[] { processFeedRate, waterFlow }, null);
      
                  AFAttribute processFeedRateAttribute = results[processFeedRate];
                  AFAttribute waterFlowAttribute = results[waterFlow];
                  AFAttributeList attrList = new AFAttributeList(new[] { processFeedRateAttribute, waterFlowAttribute });
      
                  // Make a bulk call to get values.
                  AFValues values = attrList.GetValue();
                  foreach (AFValue val in values)
                  {
                      Console.WriteLine("Attribute: {0}", val.Attribute);
                      Console.WriteLine("Timestamp: {0}, Value: {1}", val.Timestamp, val.Value.ToString());
                      Console.WriteLine();
                  }     
      

       

      Previously I doubt this is due to escape character '\' in C#. I have tried to minor modify the above source code such as replacing the backslash '\' to Unicode '\u005c'

      but also fail. The error message is "Object for path....was not found".

       

      My development environment is:

      Visual Studio 2013 Professional Update 4

      .Net Framework 4.6.2

      PI AF SDK 2.8.5

      PI AF Server 2015

       

      All comments are welcomed!

       

      Thanks and Regards,

      Paul Fung

        • Re: FindAttributesByPath in C# Problem
          PFC5478

          After numerous trial and error, below coding is working which do not use "FindAttributesByPath" (i.e. no bulk call in FindAttribute)

           

                      PISystems piSystems = new PISystems();
                      PISystem piSystem = piSystems["<AFSERVER>"];
          
                      string processFeedRate = @"NuGreen\NuGreen\Houston\Cracking Process\Equipment\B-210|Process Feedrate";
                      string waterFlow = @"NuGreen\NuGreen\Houston\Cracking Process\Equipment\B-210|Water Flow";
          
                      AFAttribute processFeedRateAttribute = AFAttribute.FindAttribute(processFeedRate, piSystem);
                      AFAttribute waterFlowAttribute = AFAttribute.FindAttribute(waterFlow, piSystem);
                      AFAttributeList attrList = new AFAttributeList(new[] { processFeedRateAttribute, waterFlowAttribute });
          
                      // Make a bulk call to get values.
                      AFValues values = attrList.GetValue();
                      foreach (AFValue val in values)
                      {
                          Console.WriteLine("Attribute: {0}", val.Attribute);
                          Console.WriteLine("Timestamp: {0}, Value: {1}", val.Timestamp, val.Value.ToString());
                          Console.WriteLine();
                      }         
          
          • Re: FindAttributesByPath in C# Problem
            Rick Davin

            Hi Paul,

             

            You still need to drill one level deeper to get the actual Results dictionary:

             

            AFAttribute processFeedRateAttribute = results.Results[processFeedRate];
            AFAttribute waterFlowAttribute = results.Results[waterFlow];
            
              • Re: FindAttributesByPath in C# Problem
                PFC5478

                Thanks Rick!

                 

                However, I still fail to get the expected result.

                 

                I have also tried to modify the original code from

                 

                             AFKeyedResults<string,AFAttribute> results = AFAttribute.FindAttributesByPath(new[] { processFeedRate, waterFlow }, null);

                             AFAttribute processFeedRateAttribute = results[processFeedRate];
                             AFAttribute waterFlowAttribute = results[waterFlow];

                             AFAttributeList attrList = new AFAttributeList(new[] { processFeedRateAttribute, waterFlowAttribute });

                 

                to

                 

                             IDictionary<string, string> errors;

                             AFAttributeList attrList = AFAttribute.FindAttributesByPath(new[] { processFeedRate, waterFlow }, null, out errors);

                 

                And it shows "Object for path....was not found" in variable "errors".

                  • Re: FindAttributesByPath in C# Problem
                    Rick Davin

                    Okay Paul, let's try some things, both of which involve passing something for the relativeFrom argument:

                     

                    First you could try relativeFrom an Asset Server:

                     

                    PISystem piSystem = piSystems["<AFSERVER>"]; 
                    
                    string processFeedRate = @"NuGreen\NuGreen\Houston\Cracking Process\Equipment\B-210|Process Feedrate";  
                    string waterFlow = @"NuGreen\NuGreen\Houston\Cracking Process\Equipment\B-210|Water Flow";  
                    
                    AFKeyedResults<string,AFAttribute> results = AFAttribute.FindAttributesByPath(new[] { processFeedRate, waterFlow }, piSystem);
                    AFAttribute processFeedRateAttribute = results.Results[processFeedRate];
                    AFAttribute waterFlowAttribute = results.Results[waterFlow];
                    

                     

                    Another thing to try would be relativeFrom a Database:

                     

                    PISystem piSystem = piSystems["<AFSERVER>"]; 
                    AFDatabase afDatabase = piSystem.Databases["NuGreen"];
                    
                    string processFeedRate = @"NuGreen\Houston\Cracking Process\Equipment\B-210|Process Feedrate";  
                    string waterFlow = @"NuGreen\Houston\Cracking Process\Equipment\B-210|Water Flow";  
                    
                    AFKeyedResults<string,AFAttribute> results = AFAttribute.FindAttributesByPath(new[] { processFeedRate, waterFlow }, afDatabase);
                    AFAttribute processFeedRateAttribute = results.Results[processFeedRate];
                    AFAttribute waterFlowAttribute = results.Results[waterFlow];
                    

                     

                    See if either of those work for you.  Be sure again to use results.Results[string] when setting the AFAttribute instance.

                      • Re: FindAttributesByPath in C# Problem
                        PFC5478

                        Thank you Rick again to study the problem.

                        However, I still fail to get the problem solved by either method. Indeed it is just a learning process for me and it is not a must for me to use bull call in FindAttribute.

                        For reference, I attach the full source code below for the latest trial which still get error (only replace the AF server name to "<AFServer>").

                         

                                    PISystems piSystems = new PISystems();
                                    PISystem piSystem = piSystems["<AFServer>"];

                                    piSystem.Connect();  // Trial of Explicit Connection

                                    string processFeedRate = @"NuGreen\NuGreen\Houston\Cracking Process\Equipment\B-210|Process Feedrate";
                                    string waterFlow = @"NuGreen\NuGreen\Houston\Cracking Process\Equipment\B-210|Water Flow";

                                    AFKeyedResults<string, AFAttribute> results = AFAttribute.FindAttributesByPath(new[] { processFeedRate, waterFlow }, piSystem);

                                     AFAttribute processFeedRateAttribute = results.Results[processFeedRate];
                                     AFAttribute waterFlowAttribute = results.Results[waterFlow];

                                     AFAttributeList attrList = new AFAttributeList(new[] { processFeedRateAttribute, waterFlowAttribute });

                                     // Make a bulk call to get values.
                                     AFValues values = attrList.GetValue();
                         
                                     foreach (AFValue val in values)
                                     {
                                         Console.WriteLine("Attribute: {0}", val.Attribute);
                                         Console.WriteLine("Timestamp: {0}, Value: {1}", val.Timestamp, val.Value.ToString());
                                         Console.WriteLine();
                                     }  

                  • Re: FindAttributesByPath in C# Problem
                    Rick Davin

                    Hi Paul,

                     

                    The good news is there is nothing wrong with your code.  The problem is your PIFD database needs some SQL maintenance.

                     

                    KB 107611 - FindAttributesByPath is not always working

                     

                    The above offers a one-time fix to rebuild indexes.  There is a mention of some internal notes, which are not public.  What I may tell you about the internal notes it that it mentions you may enable AF Maintenance to run once a week to rebuild such indexes on a routine basis.  That would be another stored procedure in the PIFD database:

                     

                         [PIFD].[dbo].[usp_maintenanceJob]

                     

                    For help on how to enable a job, see this MSDN Article – Disable or Enable a Job.

                     

                    That should fix the issue,

                    Rick