Differences
This shows you the differences between two versions of the page.
en:mervis-db:10-api [2018/08/03 08:51] m.kulvejt [C#] |
en:mervis-db:10-api [2021/04/08 13:04] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== API ====== | ||
- | |||
- | Mervis DB supports open [[https:// | ||
- | |||
- | * {{: | ||
- | |||
- | Contact [[: | ||
- | |||
- | ===== List of supported functions ===== | ||
- | |||
- | ^Function^Description| | ||
- | ^SaveData|Save one or multiple records from one or multiple variables| | ||
- | ^GetData/ | ||
- | ^GetParticularData/ | ||
- | ^GetVariables|Get the list of variables.| | ||
- | ^GetAllVariables| | | ||
- | ^ServerAlive|Obsolete| | ||
- | ^DeleteVariables| | | ||
- | ^DeleteData| | | ||
- | ^EnsureVariables| | | ||
- | ^GetTransformedData/ | ||
- | ^GetServerVersion| | | ||
- | ^GetVariableStats|Obsolete| | ||
- | ^GetVariableStatsAsync|Obsolete| | ||
- | ^CheckCredentials| | | ||
- | |||
- | ===== Examples ===== | ||
- | |||
- | ==== C# ==== | ||
- | |||
- | {{ : | ||
- | |||
- | === Download === | ||
- | |||
- | |||
- | {{ : | ||
- | {{ : | ||
- | |||
- | === Code === | ||
- | |||
- | |||
- | <code csharp MervisDB_GetData_Example.cs> | ||
- | 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, | ||
- | // The client is automatically generated from the WSDL file available here: https:// | ||
- | // | ||
- | using(HistoryDbAccessClient client = new HistoryDbAccessClient(" | ||
- | // Authentication credetials in the database. | ||
- | | ||
- | Name = " | ||
- | }; | ||
- | |||
- | // Specification of the variables through Key-Value. | ||
- | // Here we use 2 variables. | ||
- | | ||
- | new KeyValuePair[] { | ||
- | new KeyValuePair { | ||
- | Key = " | ||
- | }, new KeyValuePair { | ||
- | Key = " | ||
- | } | ||
- | }, | ||
- | new KeyValuePair[] { | ||
- | new KeyValuePair { | ||
- | Key = " | ||
- | }, new KeyValuePair { | ||
- | Key = " | ||
- | } | ||
- | } | ||
- | }; | ||
- | |||
- | // From-To dates. The dates must be specified in the UTC time zone. | ||
- | | ||
- | | ||
- | |||
- | // 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; | ||
- | int valueCount = 1000; // maximal values in on request | ||
- | |||
- | | ||
- | |||
- | do { | ||
- | valueOffset = 0; | ||
- | do { | ||
- | // Execute the request. | ||
- | var result = await client.GetDataAsyncAsync(credentials, | ||
- | |||
- | // Check the return code. " | ||
- | if (!result.ReturnCode.StartsWith(" | ||
- | Console.WriteLine(" | ||
- | } | ||
- | |||
- | // Cycle through the data and print it out. | ||
- | | ||
- | Console.WriteLine(" | ||
- | | ||
- | }))); | ||
- | |||
- | foreach(var value in valRecord.Vals) { | ||
- | | ||
- | } | ||
- | } | ||
- | |||
- | | ||
- | | ||
- | } | ||
- | while (valueOffset != -1); | ||
- | } | ||
- | while (variableOffset != -1); | ||
- | } | ||
- | }); | ||
- | |||
- | | ||
- | |||
- | try { | ||
- | task.Wait(); | ||
- | } catch (Exception exc) { | ||
- | Console.WriteLine(" | ||
- | } | ||
- | |||
- | | ||
- | |||
- | | ||
- | } | ||
- | } | ||
- | }</ | ||
- | |||
- | <code csharp MervisDB_SetData_Example.cs> | ||
- | using System; | ||
- | using System.Collections.Generic; | ||
- | using System.Threading.Tasks; | ||
- | using MervisDb_SaveData_Example.MervisDbService; | ||
- | |||
- | namespace MervisDb_SaveData_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, | ||
- | // The client is automatically generated from the WSDL file available here: https:// | ||
- | // | ||
- | using (HistoryDbAccessClient client = new HistoryDbAccessClient(" | ||
- | { | ||
- | // Authentication credetials in the database. | ||
- | Credentials credentials = new Credentials { Name = " | ||
- | |||
- | // Adding records... | ||
- | List< | ||
- | |||
- | DateTime utcTimeStamp = new DateTime(2018, | ||
- | |||
- | // Each value is stored in a ValueRecord structure | ||
- | ValueRecord rec = new ValueRecord(); | ||
- | |||
- | rec.UtcTimeStamp = utcTimeStamp; | ||
- | rec.HistoryValueType = HistoryValueType.Double; | ||
- | 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 KeyValuePair[] // | ||
- | { | ||
- | new KeyValuePair { IsKey = true, Key = " | ||
- | new KeyValuePair { IsKey = true, Key = " | ||
- | new KeyValuePair { IsKey = true, Key = " | ||
- | new KeyValuePair { IsKey = true, Key = " | ||
- | new KeyValuePair { IsKey = false, Key = " | ||
- | new KeyValuePair { IsKey = false, Key = " | ||
- | }; | ||
- | |||
- | records.Add(rec); | ||
- | |||
- | // Save data method returns the number of stored records | ||
- | int recordsSaved = await client.SaveDataAsync(credentials, | ||
- | |||
- | Console.WriteLine(" | ||
- | } | ||
- | }); | ||
- | |||
- | Console.WriteLine(" | ||
- | |||
- | try | ||
- | { | ||
- | task.Wait(); | ||
- | } | ||
- | catch (Exception exc) | ||
- | { | ||
- | Console.WriteLine(" | ||
- | } | ||
- | |||
- | Console.WriteLine(" | ||
- | |||
- | Console.ReadLine(); | ||
- | } | ||
- | } | ||
- | }</ | ||
- | ==== Matlab ==== | ||
- | |||
- | {{ : | ||
- | |||
- | === Supported functions === | ||
- | |||
- | ^Function^Description| | ||
- | ^SaveData|Save one or multiple records from one or multiple variables| | ||
- | ^GetData|Read one or multiple records from one or multiple variables.| | ||
- | ^GetDataUnmerged|Read one or multiple records from one or multiple variables. Allow more setting options than GetData function| | ||
- | ^GetDataOrigin|Read data exactly as are stored in the database.| | ||
- | ^DeleteVariable|Delete entire variable or some certain interval.| | ||
- | ^GetRequiredTimestamp|Get time stamp depending on input options.| | ||
- | |||
- | === Toolbox === | ||
- | |||
- | You need to import specialized toolbox to communicate with Mervis DB from Matlab: | ||
- | |||
- | * {{: | ||
- | * There is a help available for all toolbox functions. To display the help use: | ||
- | |||
- | <code matlab> | ||
- | help MervisDatabase.supported_function_name | ||
- | </ | ||
- | |||
- | === Code === | ||
- | |||
- | |||
- | |||
- | <code matlab MervisDB_GetData_example.m> | ||
- | % Instance of Mervis DB | ||
- | mervisDB = MervisDatabase(' | ||
- | |||
- | % Time interval definition | ||
- | from = localMatlab2utcMatlab(datenum(2017, | ||
- | to = localMatlab2utcMatlab(datenum(2017, | ||
- | samplePeriod = 5*60; % 5 minutes | ||
- | outputTimeZone = ' | ||
- | |||
- | % Specifying varibles to download | ||
- | varKeys1 = {' | ||
- | ' | ||
- | varKeys2 = {' | ||
- | ' | ||
- | |||
- | variable1 = MervisDbVariable(varKeys1); | ||
- | variable2 = MervisDbVariable(varKeys2); | ||
- | |||
- | arrayOfVarKeys = [variable1; variable2]; % array of MervisDbVariables objects | ||
- | |||
- | % help for function getData | ||
- | help MervisDatabase.getData | ||
- | |||
- | % Download required data | ||
- | [data, time] = db.getData(arrayOfVarKeys, | ||
- | |||
- | % Get all variables with defined keys | ||
- | allVaribales = db.getAllVars(varKeys1); | ||
- | </ | ||
- | |||
- | ==== PHP ==== | ||
- | |||
- | === Code === | ||
- | |||
- | |||
- | <code php MervisDB_GetData_example.php> | ||
- | <?php | ||
- | /* | ||
- | Mervis DB PHP example - getdata | ||
- | */ | ||
- | try { | ||
- | |||
- | $soapClient = new SoapClient(' | ||
- | ' | ||
- | ' | ||
- | ' | ||
- | )); | ||
- | $soapClient-> | ||
- | |||
- | $credentials = array( | ||
- | ' | ||
- | ' | ||
- | ); | ||
- | |||
- | $utcTZ | ||
- | $utcFrom = new DateTime(' | ||
- | $utcTo | ||
- | |||
- | $valOffset = 0; | ||
- | $valCount | ||
- | $varOffset = 0; | ||
- | $varCount | ||
- | |||
- | $variablesKey = array( | ||
- | array( | ||
- | array( | ||
- | ' | ||
- | ' | ||
- | ' | ||
- | ), | ||
- | array( | ||
- | ' | ||
- | ' | ||
- | ' | ||
- | ) | ||
- | ), | ||
- | array( | ||
- | array( | ||
- | ' | ||
- | ' | ||
- | ' | ||
- | ), | ||
- | array( | ||
- | ' | ||
- | ' | ||
- | ' | ||
- | ) | ||
- | ) | ||
- | ); | ||
- | |||
- | echo " | ||
- | |||
- | $counter = 0; | ||
- | |||
- | do { | ||
- | $response | ||
- | $valOffset = 0; | ||
- | do { | ||
- | $response | ||
- | ' | ||
- | ' | ||
- | ' | ||
- | ' | ||
- | ' | ||
- | ' | ||
- | ' | ||
- | ' | ||
- | )); | ||
- | $valOffset = $response-> | ||
- | $varOffset = $response-> | ||
- | |||
- | foreach ($response-> | ||
- | foreach ($varArray-> | ||
- | { | ||
- | if ($kvp-> | ||
- | echo " | ||
- | $counter++; | ||
- | } | ||
- | } | ||
- | |||
- | if (isset($varArray-> | ||
- | foreach ($varArray-> | ||
- | echo " | ||
- | } | ||
- | } else { | ||
- | echo " | ||
- | } | ||
- | |||
- | } | ||
- | |||
- | } while ($response-> | ||
- | } while ($response-> | ||
- | } | ||
- | catch (Exception $e) { | ||
- | print_r($e); | ||
- | } | ||
- | ?> | ||
- | </ | ||
- | |||
- | \\ | ||
- | |||