We already know how to get current value from the Attribute, but now we want to get value form a specific time.

I will use code from 4th chapter and just modify it. This is what I am using nows:

PISystems myPIsystems = new PISystems();
AFDatabases myDatabases = myPIsystems["W-SRV1"].Databases;
AFDatabase myDatabase = myDatabases["test"];
AFElement myElement = myDatabase.Elements["Element1\\child_Element1"];
AFAttribute myAttribute = myElement.Attributes["Attribute1"];

 

We need to write some more code lines now. We need to build time variable as first

 

Building the AFTime

AFTIme is type of time variable which can be used in AF SDK methods.

 

First we need to add using of Time part of the library

using OSIsoft.AF.Time;

 

 

We can construct AFTime variable now and we can print the time immediately:

AFTime myTime = new AFTime("04/14/2016 13:00");
Console.WriteLine(myTime.LocalTime);

 

Result is:

NOTE: If you put method name and open the bracket, a help is displayed. Sometimes there is multiple options for the input parameters number or type. It calls overloading of the method. You can navigate between options using up and down arrows.

Get the value

Now we have time, so it is time to get value. We can use the GetValue method as before, but in that case we get NoData if there is no value in the exact time. Instead of this we can use one of different functions. We just need to add:

using OSIsoft.AF.Data;

to use them.

 

Here is showed how to get value at the given time or after this time:

AFValue myValue1 = myAttribute.Data.RecordedValue(myTime, AFRetrievalMode.AtOrAfter, myAttribute.DefaultUOM);

Now we can modify just one parameter and get value at or before specified time.

AFValue myValue2 = myAttribute.Data.RecordedValue(myTime, AFRetrievalMode.AtOrBefore, myAttribute.DefaultUOM);

We can get value at exact time too, even if there is no value in the archive with this timestamp. We can use method for interpolated value:

AFValue myValue3 = myAttribute.Data.InterpolatedValue(myTime, myAttribute.DefaultUOM);

 

And we would like to print the values:

Console.WriteLine(myAttribute.PIPoint.Name);
Console.WriteLine("Input time: {0}", myTime.LocalTime);
Console.WriteLine("Value1: {0} at time {1}", myValue1.Value, myValue1.Timestamp);
Console.WriteLine("Value2: {0} at time {1}", myValue2.Value, myValue2.Timestamp);
Console.WriteLine("Value3: {0} at time {1}", myValue3.Value, myValue3.Timestamp);

 

Results

Recapitulation

So today we learned how to get values from the archive at the specified time. And now complete code from this chapter:

// Get servers list
PISystems myPIsystems = new PISystems();
// Get databases list from specified server
AFDatabases myDatabases = myPIsystems["W-SRV1"].Databases;
// Specify the database
AFDatabase myDatabase = myDatabases["test"];
// Choose the element
AFElement myElement = myDatabase.Elements["Element1"];
// Choose the attribute of the specified element
AFAttribute myAttribute = myElement.Attributes["Attribute1"];

// Specifying the time
FTime myTime = new AFTime("04/14/2016 13:00:00");

// Get value at specified time or first value after this time
AFValue myValue1 = myAttribute.Data.RecordedValue(myTime, AFRetrievalMode.AtOrAfter, myAttribute.DefaultUOM);
// Get value at specified time or first value before this time
AFValue myValue2 = myAttribute.Data.RecordedValue(myTime, AFRetrievalMode.AtOrBefore, myAttribute.DefaultUOM);
// Get interpolated value at specified time
AFValue myValue3 = myAttribute.Data.InterpolatedValue(myTime, myAttribute.DefaultUOM);

//Printing
Console.WriteLine(myAttribute.PIPoint.Name);
Console.WriteLine("Input time: {0}", myTime.LocalTime);
Console.WriteLine("Value1: {0} at time {1}", myValue1.Value, myValue1.Timestamp);
Console.WriteLine("Value2: {0} at time {1}", myValue2.Value, myValue2.Timestamp);
Console.WriteLine("Value3: {0} at time {1}", myValue3.Value, myValue3.Timestamp);

 

End

I hope that you enjoyed this chapter. I will be happy, if you suggest, what you are interested in for next chapters.