Lesson 2 exercise solutions
- Last UpdatedMay 16, 2023
- 2 minute read
- PI System
- AF SDK 2.10
- PI Server
-
Exercise 1: Find meters by template
static void FindMetersByTemplate(AFDatabase database, string templateName)
{
Console.WriteLine("Find Meters by Template: {0}", templateName);
using (AFElementSearch elementQuery =
new AFElementSearch(database, "TemplateSearch",
string.Format("template:\"{0}\"", templateName)))
{
elementQuery.CacheTimeout = TimeSpan.FromMinutes(5);
int countDerived = 0;
foreach (AFElement element in elementQuery.FindElements())
{
Console.WriteLine("Element: {0}, Template: {1}", element.Name,
element.Template.Name);
if (element.Template.Name != templateName)
countDerived++;
}
Console.WriteLine(" Found {0} derived templates", countDerived);
Console.WriteLine();
}
} -
Exercise 2: Find meters by substation
static void FindMetersBySubstation(AFDatabase database, string substationLocation)
{
Console.WriteLine("Find Meters by Substation: {0}", substationLocation);
string templateName = "MeterBasic";
string attributeName = "Substation";
using (AFElementSearch elementQuery =
new AFElementSearch(database, "AttributeValueEQSearch",
string.Format("template:\"{0}\" \"|{1}\":\"{2}\"",
templateName, attributeName, substationLocation)))
{
elementQuery.CacheTimeout = TimeSpan.FromMinutes(5);
int countNames = 0;
foreach (AFElement element in elementQuery.FindElements())
{
Console.Write("{0}{1}", countNames++ == 0 ? string.Empty : ", ",
element.Name);
}
Console.WriteLine();
}
} -
Exercise 3: Find meters with above-average usage:
static void FindMetersAboveUsage(AFDatabase database, double val)
{
Console.WriteLine("Find Meters above Usage: {0}", val);
string templateName = "MeterBasic";
string attributeName = "Energy Usage";
AFElementSearch elementquery = new AFElementSearch(database,
"AttributeValueGTSearch",
string.Format("template:\"{0}\" \"|{1}\":>{2}", templateName,
attributeName, val));
int countNames = 0;
foreach (AFElement element in elementquery.FindElements())
{
Console.Write("{0}{1}", countNames++ ==
0 ? string.Empty : ", ", element.Name);
}
Console.WriteLine();
} -
Exercise 4: Find building information
static void FindBuildingInfo(AFDatabase database, string templateName)
{
Console.WriteLine("Find Building Info: {0}", templateName);
AFElementTemplate elemTemp = database.ElementTemplates[templateName];
AFCategory buildingInfoCat = database.AttributeCategories["Building Info"];
AFSearchToken token = new AFSearchToken(AFSearchFilter.Template,
AFSearchOperator.Equal, elemTemp.GetPath());
AFElementSearch search =
new AFElementSearch(database, "search", new[] { token });
IEnumerable<AFElement> foundElements = search.FindElements();
AFNamedCollectionList<AFAttribute> foundAttributes =
new AFNamedCollectionList<AFAttribute>();
foreach (AFElement foundElem in foundElements)
{
foreach (AFAttribute attr in foundElem.Attributes)
{
if (attr.Categories.Contains(buildingInfoCat))
{
foundAttributes.Add(attr);
}
}
}
Console.WriteLine("Found {0} attributes.", foundAttributes.Count);
Console.WriteLine();
}