using System; using System.Linq; using System.Threading.Tasks; using MervisDb_GetData_Example.MervisDbService; namespace MervisDb_GetData_Example { class Program { static void Main(string[] args) { // // Demo of asynchronous methods. // var task = Task.Run(async() => { // // Create access to the real server. // Without SSL connections, you have to remove binding parameters "" in the App.config. // The client is automatically generated from the WSDL file available here: https://kb.mervis.info/doku.php/en:mervis-db:10-api // using(HistoryDbAccessClient client = new HistoryDbAccessClient("HistoryAccess", "https://localhost:9876/RcWareDbAccess")) { // Authentication credetials in the database. Credentials credentials = new Credentials { Name = "XXXX", Password = "YYYY" }; // Specification of the variables through Key-Value. // Here we use 2 variables. KeyValuePair[][] keys = new KeyValuePair[][] { new KeyValuePair[] { new KeyValuePair { Key = "DPGuid", Value = "338E2882-D72B-4D17-A241-73E8BC30F458" }, new KeyValuePair { Key = "StationName", Value = "AAABBB" } }, new KeyValuePair[] { new KeyValuePair { Key = "DPGuid", Value = "CC80211D-3D29-4CC2-91A2-F69483D566B5" }, new KeyValuePair { Key = "StationName", Value = "AAABBB" } } }; // From-To dates. The dates must be specified in the UTC time zone. DateTime utcTo = DateTime.UtcNow; DateTime utcFrom = DateTime.UtcNow.AddDays(-1); // Retrieving the data goes through cycling of values and variables. The server returns recommended values needed for the the next cycle. int variableOffset = 0; // the offset of the variable int variableCount = 10; // maximal number of variables returned in one request int valueOffset; // the offset of the value int valueCount = 1000; // maximal values in on request Console.WriteLine("Reading values..."); do { valueOffset = 0; do { // Execute the request. var result = await client.GetDataAsyncAsync(credentials, keys, utcFrom, utcTo, variableOffset, variableCount, valueOffset, valueCount); // Check the return code. "0;OK" is what we want. if (!result.ReturnCode.StartsWith("0;")) { Console.WriteLine("Error on reading: {0}", result.ReturnCode); } // Cycle through the data and print it out. foreach(var valRecord in result.Data) { Console.WriteLine("Variable: {0}", String.Concat(valRecord.Keys.Select((i) => { return String.Format("{0}={1}; ", i.Key, i.Value); }))); foreach(var value in valRecord.Vals) { Console.WriteLine("{0}->{1} [{2}] {3}", value.Ts, value.Gt, value.Hvt, value.Dv /* Use value type according to the Hvt (History Value Type) */ ); } } valueOffset = result.NextValueOffset; variableOffset = result.NextVariableOffset; } while (valueOffset != -1); } while (variableOffset != -1); } }); Console.WriteLine("DB communication is running in background"); try { task.Wait(); } catch (Exception exc) { Console.WriteLine("Exception: {0}", exc.ToString()); } Console.WriteLine("DB communication finished"); Console.ReadLine(); } } }