Good evening All
How to get average per day for a point using AFSDK?
If it is for just one PIPoint, I would recommend taking a look at the PIPoint methods. In particular, you might be interested in the summaries method.
If you have many PI Points, and have performance concerns, you might want to look at the using paging configuration and use the PIPointList Class.
Reference code :
PIServers piServers = new PIServers();
PIServer piServer = piServers["PIServerName"];
PISystems piAFServers = new PISystems();
PISystem piAFserver = piAFServers["AFServerName"];
AFDatabase aFDatabase = piAFserver.Databases["DatabaseNAme"];
AFTimeRange timeRange = new AFTimeRange();
timeRange.StartTime = new AFTime(DateTime.UtcNow.AddHours(-24));
timeRange.EndTime = AFTime.Now;
AFAttribute myAttribute = AFAttribute.FindAttribute(@"sinusoid", piServer);
IDictionary<AFSummaryTypes, AFValue> TimeWeightedAverage = myAttribute.Data.Summary(timeRange, AFSummaryTypes.Average, AFCalculationBasis.TimeWeighted, AFTimestampCalculation.Auto);
IDictionary<AFSummaryTypes, AFValue> EventWeightedAverage = myAttribute.Data.Summary(timeRange, AFSummaryTypes.Average, AFCalculationBasis.EventWeighted, AFTimestampCalculation.Auto);
If it is a true PI Point and not mapped in AF, I probably would just stick with a PIPoint object and call the Summary or Summaries method on it.
PIPoint myPoint = PIPoint.FindPIPoint(piServer, @"sinusoid");
From the PIPoint, you can directly call PIPoint.Summary or PIPoint.Summaries to get the average on multiple days.
The code shared by Lal Babu Shaik shows how to generate averages. A couple of things to note in his example.
When I think of daily average, I do not think of a running calculation that changes its end time. Rather, daily to me means a time range from midnight to midnight. Others may have their own definition, but again, that is mine. If you want midnight to midnight, I would suggest using something like:
new AFTimeRange("y", "t")
Which works whether your day was 23, 24, or 25 hours. This would mean you would want to avoid something like:
new AFTimeRange("t-24h", "t")
Because during a Spring Forward DST transition, when the day is 23 hours long, the average would start from 11:00 PM on the day before yesterday. And for a Fall Forward DST Transition, where the day is 25 hours, the average would start at 1:00 AM yesterday.
What Lal did not demonstrate is peeling back the specific AFSummaryTypes. The Summary method returns a dictionary keyed on AFSummaryTypes and returning the AFValue. Note if an exception was thrown during the calculation that this exception is passed inside the AFValue's Value property. To extract the daily average from the dictionary, you would use something like:
AFValue dailyAvg = TimeWeightedAverage[AFSummaryTypes.Average];
double average = dailyAvg.ValueAsDouble();
// You may want to read up on DisplayValue method AFValue.DisplayValue Method (Double, Int32, IFormatProvider)
Exception exception = dailyAvg.Value as Exception;
// You may want to do something with this exception
Retrieving data ...