1 Reply Latest reply on Dec 11, 2017 9:09 PM by tramachandran

    Search and retrieve AF database asynchronously

    elia-acc

       

      I'm building an application which need to synchronize an AF database with external systems.

      As it is an ASP.NET application I need to be as keen as possible with the thread pool; especially it would be better to free up the thread before invoking I/O –bound operations.

      Nowadays, this is done via the async/await framework (like StreamReader.ReadToEndAsync).

      I saw that OsiSoft made improvements on that topic for the “point data” retrieval part.

      (https://pisquare.osisoft.com/community/developers-club/blog/2016/10/24/async-with-pi-af-sdk-introduction)

      Unfortunately there is no such approach for simple element search and I’m wondering what would be the best implementation of the following method :

      public async Task<IEnumerable<Guid>> GetElementsByTemplateAsync(string templateName)

      {

          AFSearchToken templateToken = new AFSearchToken(AFSearchFilter.TemplateName, AFSearchOperator.Equal, templateName);

          AFSearchToken allDescendants = new AFSearchToken(AFSearchFilter.AllDescendants, AFSearchOperator.Equal, Boolean.TrueString);

          AFElementSearch elementSearch = new AFElementSearch(_piAfDatabase, nameof(GetEquipmentAfIdsByTemplate), new[]{templateToken, allDescendants});

          IEnumerable<Guid> returnVAlue =  elementSearch.FindObjectIds();

          return returnValue;

      }

        • Re: Search and retrieve AF database asynchronously
          tramachandran

          I am not aware of async AFSearch methods so encapsulating them in an async Task as you have done is one way to get around this.

          I prefer to avoid the usage of AFSearchTokens and use a query string instead.

          AFElementSearch search = new AFElementSearch(_piAFDatabase, "NameofMySearch", @"Template:=templateName AllDescendants:=true");

          return new List<Guid>(search.FindObjectIds())

          Note:

          -     AllDescendants: If not specified, this filter is enabled by default.

          -    When the elements are returned from a find operation, they are only partially loaded into memory, typically enough to display their inherent properties, such as Name, Description,      Template, Type, etc.  When a piece of information is accessed in the element that requires more  information, an RPC to the server is made to fully load the element.  By having the      search return the object IDs, then the LoadElements can be called to load all information in bulk and reduce the number of RPCs made to retrieve this information.

               AFNamedCollection<AFElement> elements = AFElement.LoadElements(_piAFDatabase, returnValue, null);