Click or drag to resize
OSIsoft, LLC

AFSearchTFindObjectIds Method

This method will return a list of the ID for each object that matches the search tokens.

Namespace:  OSIsoft.AF.Search
Assembly:  OSIsoft.AFSDK (in OSIsoft.AFSDK.dll) Version: 2.10.8.440
Syntax
public override IEnumerable<Guid> FindObjectIds(
	int startIndex = 0,
	int pageSize = 0
)

Parameters

startIndex (Optional)
Type: SystemInt32
The starting index(zero based) of the items to be returned.
pageSize (Optional)
Type: SystemInt32
The page size used for retrieving objects from the server. If this parameter is less than or equal to zero, then the page size will be set to the current value of theCollectionPageSize setting.

Return Value

Type: IEnumerableGuid
Returns a list of the ID for each object that matches the search tokens for this search object.

Implements

IAFSearchTFindObjectIds(Int32, Int32)
Exceptions
ExceptionCondition
FormatException This exception is thrown if the ThrowOnError property is and there is a format error in the search query.
NotSupportedException This exception is thrown if the ThrowOnError property is and one of the filters is not supported or the query is too complex to be evaluated by the server.
Remarks
The QuerySearchAnalysis feature can be checked to determine if this search method is supported by the server. If it is not supported by the server, then it will be implemented in the SDK.
Examples
// Get the Database
PISystems myPISystems = new PISystems();
PISystem myPISystem = myPISystems.DefaultPISystem;
if (myPISystem == null)
    throw new InvalidOperationException("Default PISystem was not found.");
AFDatabase myDB = myPISystem.Databases[dbName];
if (myDB == null)
    throw new InvalidOperationException("Database was not found.");

// Use attribute value query to find a list of tanks that are 
// In Service (tanks which are out-of-service have their 
// Status attribute set to "OS").
int totalCount = 0;
using (var search = new AFElementSearch(myDB, "FindTanks", @"Template:'TankAdvanced' |Status:<>'OS'"))
{
    search.CacheTimeout = TimeSpan.FromMinutes(10);

    // Because we are only going to use a few of the attributes for each
    // of the elements found, we can reduce the load time and memory space
    // by just loading the attributes we need.  Note that if we inadvertently
    // use attributes other than these, we will incur a load penalty.
    AFElementTemplate tankTemplate = myDB.ElementTemplates["Tank"];
    AFNamedCollectionList<AFAttributeTemplate> myAttributes =
        new AFNamedCollectionList<AFAttributeTemplate>();
    myAttributes.Add(tankTemplate.AttributeTemplates["Volume"]);
    myAttributes.Add(tankTemplate.AttributeTemplates["Level"]);
    myAttributes.Add(tankTemplate.AttributeTemplates["Level|HighLimit"]);
    myAttributes.Add(tankTemplate.AttributeTemplates["Level|LowLimit"]);

    // Get today's date and UOMs to be used for converting values.
    AFTime today = new AFTime("T", CultureInfo.CurrentCulture);
    UOM bbl = myDB.PISystem.UOMDatabase.UOMs["bbl"];
    UOM meter = myDB.PISystem.UOMDatabase.UOMs["m"];

    const int pageSize = 1000;
    int startIndex = 0;
    IList<Guid> myElementIds = new List<Guid>(pageSize);
    do
    {
        // Find the element IDs and load the attributes we will be using.
        var results = search.FindObjectIds(startIndex, pageSize: pageSize);
        myElementIds.Clear();
        int c = 0;
        foreach (Guid id in results)
        {
            totalCount++;
            myElementIds.Add(id);
            c++;
            if (c >= pageSize)
                break;
        }

        if (myElementIds.Count == 0) break;
        AFNamedCollectionList<AFElement> elements =
            AFElement.LoadAttributes(myPISystem, myElementIds, myAttributes);

        // Once the elements are loaded, we can process them.
        foreach (AFElement element in elements)
        {
            try
            {
                AFAttributeList attributes = new AFAttributeList();
                attributes.Add(element.Attributes["Volume"]);
                attributes.Add(element.Attributes["Level"]);
                attributes.Add(element.Attributes["Level|HighLimit"]);
                attributes.Add(element.Attributes["Level|LowLimit"]);
                AFValues values = attributes.GetValue(today);

                if (values[0].IsGood && values[1].IsGood &&
                    values[2].IsGood && values[3].IsGood)
                {
                    double volume = (double)values[0].Convert(bbl).Value;
                    double level = (double)values[1].Convert(meter).Value;
                    double high = (double)values[2].Convert(meter).Value;
                    double low = (double)values[3].Convert(meter).Value;

                    Console.WriteLine("Tank Inventory for '{0}' is {1} bbl.  %Full={2}",
                        element.Name, volume, 100 * (high - low) / level);
                }
                else
                {
                    Console.WriteLine("Bad data in Tank '{0}'", element.Name);
                }
            }
            catch (FormatException)
            {
                Console.WriteLine("Error in Tank '{0}'", element.Name);
            }
        }

        startIndex += myElementIds.Count; // Advance to next page.
    } while (myElementIds.Count > 0 && myElementIds.Count >= pageSize);
}

Console.WriteLine("Processed {0} Elements.", totalCount);
Version Information

AFSDK

Supported in: 2.10.5
See Also
Enabling Operational Intelligence