How to get the sampled data from an AF Attribute using C# Code.
Sure. Just remember that there are two PI AF SDK assemblies. Make to use version 184.108.40.206 compatible with .NET Framework 4.5 otherwise it won't work. Here is a possible solution for your issue:
AFAttribute attribute = AFObject.FindObject(@"\\MARC-PI2014\AFSDK Test\Cdt158|Sinusoid") as AFAttribute;
AFValues values = attribute.Data.InterpolatedValues(new OSIsoft.AF.Time.AFTimeRange("*-1d","*"),new OSIsoft.AF.Time.AFTimeSpan(0,0,0,1),null, string.Empty,true);
foreach(AFValue value in values)
If you want to learn more about PI AF SDK, we provide an online workshop. There is also a lot of examples available on the vCampus Live 2013 - Hands-on Labs package.
Hope it helps!
Thanks for the reply.
But the version that I use doesn't support this method.
Can you please suggest a method for AF SDK Version 220.127.116.1139 and .NET Framework 4? Is it possible with AFAnalysis.GetResults method?
I changed the AF SDK Version to 18.104.22.168 and it works!!
But when I try to get the minimum value from AFValues, it's NULL. Even all the values in obj_AFValues_HourlyConsumption is NULL.
AFAttribute afattr = new AFAttribute(obj_AFDatabase);
AFValues obj_AFValues_HourlyConsumption = afattr.Data.InterpolatedValues(new OSIsoft.AF.Time.AFTimeRange("t-1d", "t-1h"), new OSIsoft.AF.Time.AFTimeSpan(0, 0, 0,1), null, string.Empty, true);
AFValue obj_AFValue_MinValue = obj_AFValues_HourlyConsumption.Min();
In order to get the minimum value within a period of time, you can use the Summary method:
var result = attribute.Data.Summary(new OSIsoft.AF.Time.AFTimeRange("*-1d", "*"), AFSummaryTypes.Minimum, AFCalculationBasis.EventWeighted, AFTimestampCalculation.Auto);
var minValue = result[AFSummaryTypes.Minimum];
Let us know if you still need assistance!
The IList.Min() approach uses LINQ to determine which AFValue is "smallest". However, it doesn't know what to order (is it AFValue.Value, AFValue.Timestamp, etc,) so at minimum, you'd need to specify something to order (x => x.Value for example). However, even if you do, it is not necessarily the optimal approach because the processing is performed client-side and you are also getting many other values back over the network when only the min was requested.
So the recommended approach is Marco's approach using a Summary call which does the processing on the server and only returns the client the value that is requested.
Thanks Marcos and Barry.
But when I apply this method, the value of MinFlow is "Not enough values for summary calculation."
When I try to manually check, I got "The attribute doesn't support summary values". The value type of the attribute is DOUBLE.
Can we apply this method for non-numeric attributes?
Which data reference are you using for this attribute? Could please paste the entire config string?
It seems that your attribute is already making a summary. Note that AFSDK currently does not support doing a summary on top of another summary.
Please let us know if this is the case.
The data reference to this attribute is actually a Data Access Component (.dll).
What do you mean by Data Access Component? Is it a custom data reference developed in Visual Studio? Or is this attribute using the PI Point Data Reference?
Please paste the attribute config string that you can copy using PI System Explorer here!
You likely have a custom data reference, one that is not developed by OSIsoft. The nature of the Summaries call (and any other data access method) is an implementation detail of the data reference.
For example, in the Summaries call for the PIPointDR, it will ask the PI Server if possible to perform the summaries and return the results to a client. For a data reference pointing to a SQL server, then implementation would be very different as you can infer.
AF SDK simply calls the appropriate methods (if they exist) that are implemented by the data reference developer.
You can check if the developer of the "Data Access Component" custom data reference has implemented a Summaries call via inspecting the AFDataReference.SupportedDataMethods Property. Usually, Summaries are not implemented. Given your screenshot above, it summary calls have not been implemented.
In this case, you can use your first approach and perform the summary client-side. To get the minimum in a list of AFValues, you can probably use my first suggestion, calling Min() on AFValues with the appropriate delegate that returns the property you want to be compared (in order to define the meaning of "Min"). E.g. Min(x => x.Value) or Min(x=>x.ValueAsDouble) if you have AF SDK 2.7.5.
There is probably an even better and recommended approach too. You can call the Summaries (and related) methods on the raw AFValues returned to the client. See AFValues.Summaries Method
It is a Custom data reference and is not a PIPoint Reference, so this attribute doesn't support InterpolatedValues and RecordedValues method.
But I am able to get the same through System Explorer. So what will be the equivalent AFSDK Method?
The CDR has probably implemented GetValue and GetValues so you can try those. It is what PSE is using.
I am able to get Archived Values using GetValues method. But similarly I need to get Sampled data.
When you click on the Sampled tab, it will call GetValues() or InterpolatedValues method from RDA according to have you overriden on the SupportedDataMethods property.
According to my tests, if you add AFDataMethods.InterpolatedValues, it will call the RDA methods. If you have only AFDataMethods.RecordedValues for this property, it will call GetValues().
You might want to debug the Custom Data Reference with Visual Studio so you will understand better what happens under the hood.
Retrieving data ...