hanyong

AFCalculation Class in AF SDK 2012

Blog Post created by hanyong Employee on Jan 11, 2013

In PI SDK, we can pass a Performance Equation (PE) expression to PI Server to perform server side calculation through the IPICalculation interface supported by Server object.

 

An equivalent of IPICalculation interface in AF SDK 2012 is AFCalculation class. So an equivalent code to calculate a simple expression like “’TagA’ + ‘TagB’” every 5 minutes for the last 2 hours is:

 
       static void AFCalculationForPIPointsPE()
        {
            string expression = "'TagA' + 'TagB'";

            AFTimeRange timerange = new AFTimeRange("*-2h", "*"); // timerange of last 2 hours
            AFTimeSpan timespan = new AFTimeSpan(0,0,0,0,5); // timespan of 5 min
            PIServer piserver = new PIServers().DefaultPIServer;
            AFValues values = AFCalculation.CalculateAtIntervals(piserver, expression, timerange, timespan);

            foreach (AFValue value in values)
            {
                Console.WriteLine("{0}:{1}", value.Timestamp.LocalTime, value.Value);
            }
        }

 

 

An additional capability of AFCalculation is that we write a PE expression based on attributes instead of tags. Assuming we have an element ‘Element1’ with 2 attributes ‘A’ and ‘B’, and we want to find the sum of these attribute values. The PE expression will be “’A’ + ‘B’” and the code to run it is:

 
        static void AFCalculationForAttributePE()
        {
            PISystems pisys = new PISystems();
            PISystem afserver = pisys["name of AF Server"];
            AFDatabase afdb = afserver.Databases["name of AF Database"];

            AFElement e = afdb.Elements[@"path to Element1"];

            AFTimeRange timerange = new AFTimeRange("*-2h", "*"); // timerange of last 2 hours
            AFTimeSpan timespan = new AFTimeSpan(0,0,0,0,5); // timespan of 5 min

            string expression = "'A' + 'B'";
            AFValues values = AFCalculation.CalculateAtIntervals(e, expression, timerange, timespan);

            foreach (AFValue value in values)
            {
                Console.WriteLine("{0}:{1}", value.Timestamp.LocalTime, value.Value);
            }
        }

 

 

It is possible to calculate attributes from different elements as well. Assuming we have ‘Element1’ and Element2’ and we want to find the sum of attributes ‘A’ from both elements:

 
        static void AFCalculationForAttributeFullPathPE()
        {
            PISystems pisys = new PISystems();
            PISystem afserver = pisys["name of AF Server"];
            AFDatabase afdb = afserver.Databases["name of AF Database"];

            AFElement e1 = afdb.Elements["path to Element1"];
            AFElement e2 = afdb.Elements["path to Element2"];
            AFAttribute a = e1.Attributes["A"];
            AFAttribute b = e2.Attributes["A"];

            AFTimeRange timerange = new AFTimeRange("*-2h", "*"); // timerange of last 2 hours
            AFTimeSpan timespan = new AFTimeSpan(0, 0, 0, 0, 5); // timespan of 5 min

            string expression = String.Format(CultureInfo.InvariantCulture, "'{0}' + '{1}'", a.GetPath(), b.GetPath());
            AFValues values = AFCalculation.CalculateAtIntervals(afdb, expression, timerange, timespan);

            foreach (AFValue value in values)
            {
                Console.WriteLine("{0}:{1}", value.Timestamp.LocalTime, value.Value);
            }
        }

 

 

AFCalculation class has other calculation methods that are useful as well. You can refer to the AF SDK Programming Reference for more information

Outcomes