using System; using System.Linq; using System.Threading.Tasks; using MervisDb_GetTransformedData_Example.MervisDbService; namespace MervisDb_GetTransformedData_Example { class Program { static void Main(string[] args) { // // Demo of asynchronous method. // 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", "http://db.unipi.technology/dbaccess")) { // Authentication credentials in the database. Credentials credentials = new Credentials { Name = "XXXXX", Password = "YYYYY" }; // Specification of the variables through Key-Value. // Here we use 2 variables. KeyValuePair[][] variableKeys = new KeyValuePair[][] { new KeyValuePair[] { new KeyValuePair (false, /*Key*/ "DPGuid", /*Value*/ "338E2882-D72B-4D17-A241-73E8BC30F458"), new KeyValuePair (false, /*Key*/ "StationName", /*Value*/ "AAABBB") }, new KeyValuePair[] { new KeyValuePair (false, /*Key*/ "DPGuid", /*Value*/ "CC80211D-3D29-4CC2-91A2-F69483D566B5"), new KeyValuePair (false, /*Key*/ "StationName", /*Value*/ "AAABBB") } }; // Aggregation request var aggregation = new AggregationRequest(); aggregation.Types = new AggregationType[] { AggregationType.Last }; aggregation.Interpolation = InterpolationType.None; //aggregation.MaxNeighborDistance = ..only for interpolation; aggregation.IsoPattern = new string[] { "PT15M" }; // 15 min period (ISO 8601 duration pattern) aggregation.From = new DateTime(2018, 11, 24, 0, 0, 0, 0, DateTimeKind.Unspecified); aggregation.To = new DateTime(2018, 11, 25, 0, 0, 0, 0, DateTimeKind.Unspecified); aggregation.TimeZone = @"Europe/Prague"; // Retrieving the data goes through cycling of intervals and variables. The server returns recommended intervals 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 intervalOffset; // the offset of the interval int intervalCount = 1000; // maximal values in on request Console.WriteLine("Reading values..."); do { intervalOffset = 0; do { // Execute the request. var result = await client.GetTransformedDataAsyncAsync(credentials, aggregation, variableKeys, variableOffset, variableCount, intervalOffset, intervalCount); // 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. if (result.Data != null) { foreach (VariableAggregate vag in result.Data) { Console.WriteLine("Variable: {0}", String.Concat(vag.Keys.Select((i) => { return String.Format("{0}={1}; ", i.Key, i.Value); }))); foreach (Interval value in vag.Data) { Console.Write(" {0}->{1} quality: {2:0.00} ", value.Begin, value.End, value.DataQuality); if (value.Last != null) { Console.Write(" Last Value: {0} (stamp {1}, origin {2})", value.Last.Value, value.Last.Stamp, value.Last.Origin); } Console.WriteLine(); } } } variableOffset = result.NextVariableOffset; intervalOffset = result.NextIntervalOffset; } while (intervalOffset != -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(); } } }