using ESG.Core.Access.Client; using ESG.Db.Server.Shared; using ESG.Db.ServerAccess; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace DbClientTest { class Program { static async Task Main(string[] args) { try { // // 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(HistoryDbAccessClient.EndpointConfiguration.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. ESG.Db.Server.Shared.KeyValuePair[][] keys = new ESG.Db.Server.Shared.KeyValuePair[][] { new ESG.Db.Server.Shared.KeyValuePair[] { new ESG.Db.Server.Shared.KeyValuePair(true, "DPGuid","338E2882-D72B-4D17-A241-73E8BC30F458"), new ESG.Db.Server.Shared.KeyValuePair(true, "StationName", "AAABBB") }, new ESG.Db.Server.Shared.KeyValuePair[] { new ESG.Db.Server.Shared.KeyValuePair(true, "DPGuid", "CC80211D-3D29-4CC2-91A2-F69483D566B5"), new ESG.Db.Server.Shared.KeyValuePair(true, "StationName", "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.Values) { Console.WriteLine("{0}->{1} [{2}] {3}", value.UtcTimeStamp, value.GoodThrough, value.HistoryValueType, value.DoubleValue /* Use value type according to the Hvt (History Value Type) */ ); } } valueOffset = result.NextValueOffset; variableOffset = result.NextVariableOffset; } while (valueOffset != -1); } while (variableOffset != -1); } } catch (Exception exc) { Console.WriteLine("Exception: {0}", exc.ToString()); } Console.WriteLine("DB communication finished"); Console.ReadLine(); } } }