I have been (successfully) using GetValues to get a number of values for an AF Element's attributes. Now I would like to get the average (and if possible, the maximum) for a given time range. How can I do this with the AF SDK?
As long as your attributes data reference is a PI Point - you may use the following code:
OSIsoft.AF.Asset.AFAttribute myAttribute;myAttribute = myElement.Attributes["Tagattribute"];PISDK.PIPoint myPIPoint;myPIPoint = (PISDK.PIPoint)myAttribute.RawPIPoint;MessageBox.Show(myPIPoint.Data.Summary("Y", "T", PISDK.ArchiveSummaryTypeConstants.astMaximum, PISDK.CalculationBasisConstants.cbTimeWeighted, null).Value.ToString());
Another way would be to define the PIPoint Data Reference to perform an average calculation when a time range (AFTimeRange) is passed to GetValue() method. For this option, the PIPoint Data Reference needs to be configured first.
If you see the setting of PIPoint Data Reference in PI System Explorer, look at the "By Time Range" configuration:
I'm running into the same issue and in our case the attribute is not a PIpoint, it's a formula data reference and we want to get a standard set of time weighted statistics(average, min, max, standard deviation).
You will have to use a mix of PI SDK and AF SDK. As suggested in Andreas' post above, AF SDK can be thought of as the "metadata" SDK (surfacing the PI Asset Framework, or AF) and the PI SDK as the "time series data manipulation" SDK. In his example, he went from the AF Element (with a PI Point Data Reference) to the underlying raw PI Point, to access more advanced data manipulation and aggregation capabilities. (although Han Yong showed that this is possible in AF itself)
In a similar fashion, you could read the values from an AF Attribute configured with the Formula Data Reference, then throw these AF values (OSIsoft.AF.Asset.AFValues) into a PI values collection (PISDK.PIValues), then do the desired calculation/aggregation:
AFValues vals = someAFAttribute.GetValues(someAFTimeRange, 0, null);PISDK.PIValues pvals = (PISDK.PIValues)vals.ToPIValues();PISDK.PIValue avg = pvals.Summary( pvals.TimeStamp, pvals[pvals.Count].TimeStamp, PISDK.ArchiveSummaryTypeConstants.astAverage, PISDK.CalculationBasisConstants.cbTimeWeighted, null);
As you can see, I am using the timestamps of the first and the last values in the PIValues collection to run the Summary call. That's because you would get an error if you ask for a time frame that's larger than the time frame covered by the values contained in the PIValues collection. And chances are you won't have values returned at the exact start and end times of your AFTimeRange (in the GetValues call).
Hope this helps!
Retrieving data ...