Please ensure Javascript is enabled for purposes of website accessibility
Powered by Zoomin Software. For more details please contactZoomin

AF SDK Getting Started

Lesson 4: Building an AF hierarchy

  • Last UpdatedMay 16, 2023
  • 2 minute read

After completing this lesson you will be able to:

  • Use AF SDK to build a PI AF hierarchy.

  • Create a PI AF database.

  • Create element and attribute templates.

  • Create an asset hierarchy, and link to PI Points from the attribute configuration.

  • Use recommended AF SDK patterns such as checking in changes in bulk.

Help for methods and data types used in this lesson

Introduction

When attempting to add an element to a database, you should always check to see if an element with the same name already exists in the database before adding it. Attempting to add an element that already exists in the database will generate an exception.

The following code shows one way to add an element named Meters to the database:

// Attempts to retrieve the element "Meters", and adds the element after checking
// to see if it already exists
AFElement meters = database.Elements["Meters"]
if (meters == null)
meters = database.Elements.Add("Meters");

Alternatively, the previous operation could be written using a single line by using the C# coalesce operator: ??, as shown here:

AFElement meters = database.Elements["Meters"] ?? database.Elements.Add("Meters")

Shown below is a simple example that shows how to create a new PI AF database, PI AF element, and PI AF attribute, and then check in the changes. Refer to the Example 4 exercises section for more detailed examples.

PISystem assetServer = new PISystems().DefaultPISystem;

AFDatabase database = assetServer.Databases.Add("MyDb");

AFElement newEl = database.Elements[“MyElement”] ?? database.Elements.Add(“MyElement”);

AFAttribute newAttr = newEl.Attributes["MyAttribute"] ?? newE1.Attributes.Add(MyAttribute");

database.CheckIn();

Example

The following example shows how to create an element template called FeederTemplate. The example creates one attribute template called District which contains no data reference, then creates another attribute template called Power. The default UOM is set to watt and the Type to Single. The example also sets the data reference to PI Point.

See the following links for more information:

  • Configuration of data references

  • Units of measure in PI AF

  • PI point data reference

    static void CreateElementTemplate(AFDatabase database)
    {
    string templateName = "FeederTemplate";
    AFElementTemplate feederTemplate;
    if (database.ElementTemplates.Contains(templateName))
    return;
    else
    feederTemplate = database.ElementTemplates.Add(templateName);

    AFAttributeTemplate cityAttributeTemplate =
    feederTemplate.AttributeTemplates.Add("City");
    cityattributeTemplate.Type = typeof(string);

    AFAttributeTemplate power = feederTemplate.AttributeTemplates.Add("Power");
    power.Type = typeof(Single);

    power.DefaultUOM = database.assetServer.UOMDatabase.UOMs["watt"];
    power.DataReferencePlugIn =
    database.PISystem.DataReferencePlugIns["PI Point"];

    database.CheckIn();
    }

TitleResults for “How to create a CRG?”Also Available in