Part 7 - Reading data
This article is a part of "Creating an Equipment model" tutorial
Now that you have some data into your database, the next step is to fetch both single and historical values. Even though data can also be accessed using Engineering UI, this article focuses on reading data using VtrinLib. Engineering UI will be covered in the next article.
Just like before, we need to first connect to the database. We will use the same setup as in previuos tutorials. Start a new Visual Studio project and make sure that you can connect to the database. If you need any help, read again Part 1 - Getting started and Part 2 - Connecting to RTDB.
Fetching current values
All the defined equipment properties are accessible through the equipment instances, as shown in the example below. Here we are reading the value of the "Current power" property for the "Pump" and writing the results out to the console output.
var pump = RTDBDriver
.Classes["Path"]
.Instances
.GetInstanceByName("Example site.Water transfer system.Pump section.Pump");
var power = pump["Current power"];
System.Console.WriteLine(string.Format("The pump has a current power of {0,3:F2} W", power));Fetching historical values
If a property is selected to be historized, the property values over a time interval can be fetched from the time series database. The following code snippet shows how to fetch the "Current power" property values of "Pump" over the last hour.
var pump = RTDBDriver.Classes["Path"].Instances.GetInstanceByName("Example site.Water transfer system.Pump section.Pump");
var graphFetchParams = ABB.Vtrin.Drivers.cGraphFetchParameters.CreateRawFetch(
RTDBDriver.Classes["EquipmentHistory"],
RTDBDriver.ServerTimeInUTC.AddHours(-1),
RTDBDriver.ServerTimeInUTC,
int.MaxValue,
"Path=? AND Property=?",
pump,
"Current power");
// Get iterator for the data
var iterator = RTDBDriver.GetGraphDataIterator(graphFetchParams);
// Print all the rows of data
while (iterator.MoveNext())
{
System.Console.WriteLine(iterator.XValue + ": " + iterator.YValue);
}And the output should look a bit like this:
05/11/2019 10.13.59: 470,862883497519
05/11/2019 10.14.05: 472,862864309678
05/11/2019 10.14.11: 475,944717085626
05/11/2019 10.14.17: 479,985578080536
05/11/2019 10.14.25: 485,695239861002
05/11/2019 10.14.35: 495,110722695446
05/11/2019 10.14.53: 511,764303622577
05/11/2019 10.15.01: 518,620811813927
05/11/2019 10.15.09: 523,554912943548
05/11/2019 10.15.15: 526,776390029169
05/11/2019 10.15.21: 528,930376942111
05/11/2019 10.15.27: 529,931001021456
But what If you want to fetch all data at once? That can be done by using the FetchGraphData function. graphFetchParams are exactly same as in the previous example above.
// Fetch all the data
var data = RTDBDriver.FetchGraphData(new ABB.Vtrin.Drivers.cGraphFetchParameters[] { graphFetchParams });
// Loop through the data
foreach (ABB.Vtrin.Drivers.cValueRecord record in data[0])
{
System.Console.WriteLine(record.Value);
}Updated 5 months ago
