Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
en:mervis-db:10-api [2017/07/13 17:24]
m.kulvejt [C#]
en:mervis-db:10-api [2026/07/14 10:36] (current)
z.moustis
Line 3: Line 3:
 Mervis DB supports open [[https://en.wikipedia.org/wiki/SOAP|SOAP]] API described by this WSDL: Mervis DB supports open [[https://en.wikipedia.org/wiki/SOAP|SOAP]] API described by this WSDL:
  
-  * {{:en:mervis-db:mervisdb_wsdl-2017-07-12.xml|mervisdb_wsdl-2017-07-12.xml}}+  * {{:en:mervis-db:mervisdb_wsdl-2026-04-01.xml|mervisdb_wsdl-2026-04-01.xml}}
  
-Contact [[:en:help:support|technical support]] for other non-public APIs that can be used for your projects.+Contact [[:en:help:20-support|technical support]] for other non-public APIs that can be used for your projects.
  
 ===== List of supported functions ===== ===== List of supported functions =====
Line 18: Line 18:
 ^DeleteVariables| | ^DeleteVariables| |
 ^DeleteData| | ^DeleteData| |
 +^ReplaceData|Delete data from time range and save one or multiple records from one or multiple variables in one API call|
 ^EnsureVariables| | ^EnsureVariables| |
-^GetTransformedData/GetTransformedDataAsync| |+^GetTransformedData/GetTransformedDataAsync|Method for reading of aggregated results with given time period, the aggregation may be Min, Max, Integral, Weighted average, First, Last and Median |
 ^GetServerVersion| | ^GetServerVersion| |
 ^GetVariableStats|Obsolete| ^GetVariableStats|Obsolete|
 ^GetVariableStatsAsync|Obsolete| ^GetVariableStatsAsync|Obsolete|
 ^CheckCredentials| | ^CheckCredentials| |
 +
 +===== Object Properties =====
 +  * (UTC)TimeStamp - UTC time stamp of the record
 +  * GoodThrough - UTC time stamp of the end of validity of particular record
 +  * Interval - period of time (in seconds) when next value should be recorded
 +
  
 ===== Examples ===== ===== Examples =====
Line 29: Line 36:
 ==== C# ==== ==== C# ====
  
-{{ :en:mervis-db:mervisdb_getdata_example.zip |}}+{{ :en:mervis-db:mervis_db_csharp.png?direct&800 |}} 
 + 
 +=== Download === 
 + 
 + 
 +{{ :en:mervis-db:mervisdb_getdata_example.zip |}}\\ 
 +{{ :en:mervis-db:mervisdb_savedata_example.zip |}} 
 + 
 +=== Code === 
  
 <code csharp MervisDB_GetData_Example.cs> <code csharp MervisDB_GetData_Example.cs>
 +using ESG.Core.Access.Client;
 +using ESG.Db.Server.Shared;
 +using ESG.Db.ServerAccess;
 using System; using System;
 +using System.Collections.Generic;
 using System.Linq; using System.Linq;
 using System.Threading.Tasks; using System.Threading.Tasks;
-using MervisDb_GetData_Example.MervisDbService; 
  
-namespace MervisDb_GetData_Example+namespace DbClientTest
 { {
  class Program  class Program
  {  {
- static void Main(string[] args)+ static async Task Main(string[] args)
  {  {
- // + try
- // Demo of asynchronous methods. +
- // +
- var task = Task.Run(async () =>+
  {  {
  //  //
Line 53: Line 69:
  // The client is automatically generated from the WSDL file available here: https://kb.mervis.info/doku.php/en:mervis-db:10-api  // 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"))+ using (HistoryDbAccessClient client = new HistoryDbAccessClient(HistoryDbAccessClient.EndpointConfiguration.HistoryAccess, "https://localhost:9876/RcWareDbAccess"))
  {  {
  // Authentication credetials in the database.  // Authentication credetials in the database.
- Credentials credentials = new Credentials { Name = "XXXX", Password = "YYYY" };+ Credentials credentials = new Credentials 
 + { 
 + Name = "XXXX", 
 + Password = "YYYY" 
 + };
  
  // Specification of the variables through Key-Value.  // Specification of the variables through Key-Value.
  // Here we use 2 variables.  // Here we use 2 variables.
- KeyValuePair[][] keys = new KeyValuePair[][] + ESG.Db.Server.Shared.KeyValuePair[][] keys = new ESG.Db.Server.Shared.KeyValuePair[][] { 
- +  new ESG.Db.Server.Shared.KeyValuePair[] { 
- new KeyValuePair[] +  new ESG.Db.Server.Shared.KeyValuePair(true, "DPGuid","338E2882-D72B-4D17-A241-73E8BC30F458"), 
- +  new ESG.Db.Server.Shared.KeyValuePair(true, "StationName", "AAABBB") 
- new KeyValuePair { Key= "DPGuid",Value = "338E2882-D72B-4D17-A241-73E8BC30F458" } new KeyValuePair { Key="StationName", Value = "AAABBB"} +  }, 
- }, +    new ESG.Db.Server.Shared.KeyValuePair[] { 
- new KeyValuePair[] +  new ESG.Db.Server.Shared.KeyValuePair(true, "DPGuid", "CC80211D-3D29-4CC2-91A2-F69483D566B5"), 
- +  new ESG.Db.Server.Shared.KeyValuePair(true, "StationName", "AAABBB") 
- 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.  // From-To dates. The dates must be specified in the UTC time zone.
Line 93: Line 112:
  
  // Check the return code. "0;OK" is what we want.  // Check the return code. "0;OK" is what we want.
- if (!result.ReturnCode.StartsWith("0;")) { Console.WriteLine("Error on reading: {0}", result.ReturnCode); }+ if (!result.ReturnCode.StartsWith("0;")) 
 + { 
 + Console.WriteLine("Error on reading: {0}", result.ReturnCode); 
 + }
  
  // Cycle through the data and print it out.  // Cycle through the data and print it out.
  foreach (var valRecord in result.Data)  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); })));+ 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)+ foreach (var value in valRecord.Values)
  {  {
- Console.WriteLine("{0}->{1} [{2}] {3}", value.Ts, value.Gt, value.Hvt, value.Dv /* Use value type according to the Hvt (History Value Type) */);+ Console.WriteLine("{0}->{1} [{2}] {3}", value.UtcTimeStamp, value.GoodThrough, value.HistoryValueType, value.DoubleValue /* Use value type according to the Hvt (History Value Type) */ );
  }  }
  }  }
Line 113: Line 138:
  while (variableOffset != -1);  while (variableOffset != -1);
  }  }
- }); 
  
- Console.WriteLine("DB communication is running in background");+
 + catch (Exception exc) 
 +
 + Console.WriteLine("Exception: {0}", exc.ToString()); 
 + }
  
 + Console.WriteLine("DB communication finished");
 +
 + Console.ReadLine();
 + }
 +
 + }
 +}
 +</code>
 +
 +<code csharp MervisDB_SaveData_Example.cs>
 +using System;
 +using System.Collections.Generic;
 +using System.Threading.Tasks;
 +using MervisDb_SaveData_Example.MervisDbService;
 +
 +namespace MervisDb_SaveData_Example
 +{
 + class Program
 + {
 + static async Task Main(string[] args)
 + {
  try  try
  {  {
- task.Wait();+ // 
 + // Create access to the real server. 
 + // Without SSL connections, you have to remove binding parameters "<security mode="Transport"/>" 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" }; 
 + 
 + // Adding records... 
 + List<ValueRecord> records = new List<ValueRecord>(); 
 + 
 + DateTime utcTimeStamp = new DateTime(2018, 1, 1, 12, 0, 0, DateTimeKind.Utc); 
 + 
 + // Each value is stored in a ValueRecord structure 
 + ValueRecord rec = new ValueRecord(); 
 + 
 + rec.UtcTimeStamp = utcTimeStamp;                // time stamp of the value, UTC date time must be used 
 + rec.HistoryValueType = HistoryValueType.Double; // type of the value 
 + rec.DoubleValue = 23.4;                         // value itself 
 + rec.Interval = 60.0;                            // sampling period in seconds. When the difference of timestamps of subsequent values if greater than 1.1 multiple of the interval, the data should be considered incomplete. 
 + rec.Keys = new ESG.Db.Server.Shared.KeyValuePair[] // identification of the variable. At least one key-value pair must be "IsKey" to identify a variable. 
 +
 + new ESG.Db.Server.Shared.KeyValuePair(true, "Root", "Building A"), 
 + new ESG.Db.Server.Shared.KeyValuePair(true, "Level1", "Floor 1"), 
 + new ESG.Db.Server.Shared.KeyValuePair(true, "Level2", "Room 7"), 
 + new ESG.Db.Server.Shared.KeyValuePair(true, "Level3", "Temperature"), 
 + new ESG.Db.Server.Shared.KeyValuePair(false, "Description", "Temperature in the room 7"), 
 + new ESG.Db.Server.Shared.KeyValuePair(false, "Unit", "°C"), 
 + }; 
 + 
 + records.Add(rec); 
 + 
 + // Save data method returns the number of stored records  
 + int recordsSaved = await client.SaveDataAsync(credentials, records.ToArray()); 
 + 
 + Console.WriteLine("Records saved: {0}", recordsSaved); 
 +
  }  }
  catch (Exception exc)  catch (Exception exc)
Line 132: Line 220:
  }  }
 } }
 +</code>
  
 +<code csharp MervisDb_ReplaceData_Example.cs>
 +using ESG.Core.Access.Client;
 +using ESG.Db.Server.Shared;
 +using ESG.Db.ServerAccess;
 +using System;
 +using System.Collections.Generic;
  
 +namespace DbClientTest
 +{
 + class Program
 + {
 + static async Task Main(string[] args)
 + {
 + RcWareDbClient dbClient = new RcWareDbClient(false, "RcWareDbAccess", new Uri("http://localhost:9876/RcWareDbAccess"));
 + dbClient.Credentials = new ESG.Db.Server.Shared.Credentials("xxxxxxx", "xxxxxxx");
 +
 + DateTime utcNow = DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(5));
 +
 + ESG.Db.Server.Shared.KeyValuePair[] varKeysNew = new ESG.Db.Server.Shared.KeyValuePair[]
 + {
 + new ESG.Db.Server.Shared.KeyValuePair(true, "DPGuid", "51111111-1234-1234-1234-123456789012"),
 + new ESG.Db.Server.Shared.KeyValuePair(true, "StationName", "db_replace_test")
 + };
 +
 + ESG.Db.Server.Shared.KeyValuePair[] varKeys1 = new ESG.Db.Server.Shared.KeyValuePair[]
 + {
 + new ESG.Db.Server.Shared.KeyValuePair(true, "DPGuid", "60ace716-5e1e-4cf8-ab05-947dce0caf77"),
 + new ESG.Db.Server.Shared.KeyValuePair(true, "SoftPlcRtId", "db_replace_test")
 + };
 +
 + ESG.Db.Server.Shared.KeyValuePair[] varKeys = varKeys1;
 +
 + // generate some records
 + List<ValueRecord> recsToSave = new List<ValueRecord>();
 + TimeSpan recsLength = TimeSpan.FromMinutes(2);
 + int recsPerMinute = 4;
 +
 + Random r = new Random((int)DateTime.UtcNow.Ticks);
 + double rBase = r.NextDouble() * 10;
 +
 + for (int i = 0; i < (recsLength.TotalSeconds / 60) * recsPerMinute; i++)
 + {
 + recsToSave.Add(new ValueRecord(utcNow - recsLength + TimeSpan.FromMinutes(i * (1.0 / recsPerMinute)), rBase + ((i % 2) + 1) * (-4)));
 + recsToSave[recsToSave.Count - 1].Keys = varKeys;
 + recsToSave[recsToSave.Count - 1].Interval = 60 / recsPerMinute;
 +
 + recsToSave.Add(new ValueRecord(utcNow - recsLength + TimeSpan.FromMinutes(i * (1.0 / recsPerMinute)), rBase + ((i % 2) + 1) * (-4)));
 + recsToSave[recsToSave.Count - 1].Keys = varKeysNew;
 + recsToSave[recsToSave.Count - 1].Interval = 60 / recsPerMinute;
 + }
 +
 + var res = await dbClient.ReplaceDataAsync(new List<ESG.Db.Server.Shared.KeyValuePair[]> { varKeys }, utcNow.Subtract(recsLength), utcNow, recsToSave.ToArray());
 + }
 + }
 +}
 </code> </code>
 +
 +<code csharp MervisDb_GetTransformedData_Example.cs>
 +using System;
 +using System.Linq;
 +using System.Threading.Tasks;
 +using MervisDb_GetTransformedData_Example.MervisDbService;
 +
 +namespace MervisDb_GetTransformedData_Example
 +{
 + class Program
 + {
 + static async Task Main(string[] args)
 + {
 + //
 + // Create access to the real server.
 + // Without SSL connections, you have to remove binding parameters "<security mode="Transport"/>" 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
 + //
 + try
 + {
 + using (HistoryDbAccessClient client = new HistoryDbAccessClient(HistoryDbAccessClient.EndpointConfiguration.HistoryAccess, "http://localhost:9876/RcWareDbAccess"))
 + {
 + // 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.
 + ESG.Db.Server.Shared.KeyValuePair[][] variableKeys = new ESG.Db.Server.Shared.KeyValuePair[][] {
 + new ESG.Db.Server.Shared.KeyValuePair[] {
 + new ESG.Db.Server.Shared.KeyValuePair (/*IsKeyId*/ false, /*Key*/ "DPGuid", /*Value*/ "338E2882-D72B-4D17-A241-73E8BC30F458"),
 + new ESG.Db.Server.Shared.KeyValuePair (/*IsKeyId*/ false, /*Key*/ "StationName", /*Value*/ "AAABBB")
 + },
 + new ESG.Db.Server.Shared.KeyValuePair[] {
 + new ESG.Db.Server.Shared.KeyValuePair (/*IsKeyId*/ false, /*Key*/ "DPGuid", /*Value*/ "CC80211D-3D29-4CC2-91A2-F69483D566B5"),
 + new ESG.Db.Server.Shared.KeyValuePair (/*IsKeyId*/ 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");
 +
 + }
 + catch (Exception exc)
 + {
 + Console.WriteLine("Exception: {0}", exc.ToString());
 + }
 +
 + Console.WriteLine("DB communication finished");
 +
 + Console.ReadLine();
 + }
 + }
 +}
 +</code>
 +
  
 ==== Matlab ==== ==== Matlab ====
Line 158: Line 417:
  
 <code matlab> <code matlab>
-help MerbonDatabase.supported_function_name+help MervisDatabase.supported_function_name
 </code> </code>
  
 === Code === === Code ===
  
-MervisDB_GetData_example.m 
  
-<code matlab>+ 
 +<code matlab MervisDB_GetData_example.m>
 % Instance of Mervis DB % Instance of Mervis DB
-mervisDB = MerbonDatabase('username', 'password', 'dbUrl');+mervisDB = MervisDatabase('username', 'password', 'dbUrl');
  
 % Time interval definition % Time interval definition
Line 181: Line 440:
             'StationName', 'YYYYYYY'}; % Keys definition of second variable             'StationName', 'YYYYYYY'}; % Keys definition of second variable
  
-variable1 = MerbonDbVariable(varKeys1); % Create MerbonDbVariable object from defined keys +variable1 = MervisDbVariable(varKeys1); % Create MervisDbVariable object from defined keys 
-variable2 = MerbonDbVariable(varKeys2);+variable2 = MervisDbVariable(varKeys2);
  
-arrayOfVarKeys = [variable1; variable2]; % array of MerbonDbVariables objects+arrayOfVarKeys = [variable1; variable2]; % array of MervisDbVariables objects
  
 % help for function getData % help for function getData
-help MerbonDatabase.getData+help MervisDatabase.getData
  
 % Download required data % Download required data
-[data, time] = db.getData(arrayOfVarKeys, from, to, samplePeriod, outputTimeZone);+[data, time] = mervisDB.getData(arrayOfVarKeys, from, to, samplePeriod, outputTimeZone);
  
 % Get all variables with defined keys % Get all variables with defined keys
-allVaribales = db.getAllVars(varKeys1);+allVaribales = mervisDB.getAllVars(varKeys1);
 </code> </code>
  
 ==== PHP ==== ==== PHP ====
  
-MervisDB_GetData_example.php+=== Code === 
  
-<code php>+<code php MervisDB_GetData_example.php>
 <?php <?php
 /* /*
Line 305: Line 565:
  
 \\ \\
- 
- 
  • © Energocentrum Plus, s.r.o. 2017 - 2026