148 lines
6.4 KiB
C#
148 lines
6.4 KiB
C#
using DbAPI.Connections;
|
|
using DTS.Common.Interface.Database;
|
|
using DTS.Common.Interface.DataRecorders;
|
|
using DTS.Common.Interface.TestSetups.TestSetupsList;
|
|
|
|
namespace DTS.Core.DbAPIWrapper
|
|
{
|
|
public class DbApiWrapper
|
|
{
|
|
private readonly ConnectionDetails _connectionDetail;
|
|
private ApiInfo? _apiInfo;
|
|
public DbApiWrapper(string dbServer, string dbName, string dbUsername,
|
|
string dbPassword, bool adAuthentication, bool useCentralizedDb, int? clientDbVersion=null)
|
|
{
|
|
|
|
_connectionDetail = new ConnectionDetails
|
|
{
|
|
DbName = dbName,
|
|
DbServer = dbServer,
|
|
DbUser = dbUsername,
|
|
DbUserPassword = dbPassword,
|
|
UseNTLMAuthentication = adAuthentication,
|
|
UsingCentralizedDb = useCentralizedDb,
|
|
ClientDbVersion = clientDbVersion.HasValue ? clientDbVersion.Value : 0
|
|
};
|
|
}
|
|
|
|
private void Connect()
|
|
{
|
|
var res = DbAPI.DbAPI.Connections.ConnectToDb(_connectionDetail);
|
|
if (res != 0)
|
|
{
|
|
throw new Exception("Failed to connect to database");
|
|
}
|
|
}
|
|
|
|
public void AuthenticateUser(string username, string password)
|
|
{
|
|
Connect();
|
|
var hr = DbAPI.DbAPI.Connections.LoginUser(_connectionDetail, username, password, out var userRecord);
|
|
if (hr == 0)
|
|
_ = userRecord;
|
|
else
|
|
throw new UnauthorizedAccessException("Failed to Authenticate the user");
|
|
|
|
var loggedInUsers = DbAPI.DbAPI.Connections.GetLoggedInUsers();
|
|
|
|
if (loggedInUsers != null && loggedInUsers.Any())
|
|
{
|
|
ApiInfo apiInfo = new ApiInfo { ConnectionDetails = loggedInUsers[0].Item2, UserDbRecord = loggedInUsers[0].Item1 };
|
|
_apiInfo = apiInfo;
|
|
return;
|
|
}
|
|
|
|
throw new UnauthorizedAccessException("Failed to Authenticate the user");
|
|
}
|
|
|
|
public Tuple<ITestSetupRecord[], string[], ulong> TestSetupsGet(int? testSetupId = null, string? testSetupName = null, double defaultROIStart = double.NaN, double defaultROIEnd =double.NaN, bool defaultIgnoreShortedStart = false, bool defaultIgnoreShortedTrigger = false)
|
|
{
|
|
try
|
|
{
|
|
return GetTestSetups(testSetupId, testSetupName, defaultROIStart, defaultROIEnd, defaultIgnoreShortedStart, defaultIgnoreShortedTrigger);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception($"TestSetupsGet failed. {ex.Message}");
|
|
}
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="testSetupId"></param>
|
|
/// <returns>Returns Tuple<ITestSetupRecord[], string[]> with arrays of ITestSetupRecord as Item1 and array of errors as Item2 and error code as Item3</returns>
|
|
/// <exception cref="Exception"></exception>
|
|
public async Task<Tuple<ITestSetupRecord[], string[], ulong>> TestSetupsGetAsync(int? testSetupId = null, string? testSetupName = null, double defaultROIStart = double.NaN, double defaultROIEnd = double.NaN, bool defaultIgnoreShortedStart = false, bool defaultIgnoreShortedTrigger = false)
|
|
{
|
|
try
|
|
{
|
|
var taskResult = await Task.Run(() =>
|
|
{
|
|
return GetTestSetups(testSetupId, testSetupName, defaultROIStart, defaultROIEnd, defaultIgnoreShortedStart, defaultIgnoreShortedTrigger);
|
|
});
|
|
|
|
return taskResult;
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception($"TestSetupsGet failed. {ex.Message}");
|
|
}
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="testSetupId">Optional testSetupId</param>
|
|
/// <returns>Returns Tuple<ITestSetupRecord[], string[]> with arrays of ITestSetupRecord as Item1 and array of errors as Item2 and error code as Item3</returns>
|
|
/// <exception cref="Exception"></exception>
|
|
private Tuple<ITestSetupRecord[], string[], ulong> GetTestSetups(int? testSetupId, string? testSetupName, double defaultROIStart, double defaultROIEnd, bool defaultIgnoreShortedStart, bool defaultIgnoreShortedTrigger)
|
|
{
|
|
if (_apiInfo?.UserDbRecord is null || _apiInfo.ConnectionDetails is null)
|
|
{
|
|
throw new Exception($"Authentication is required");
|
|
}
|
|
var errorCode = DbAPI.DbAPI.TestSetups.TestSetupsGet(_apiInfo.UserDbRecord, _apiInfo.ConnectionDetails, _apiInfo.ConnectionDetails.ClientDbVersion,
|
|
testSetupId, testSetupName, defaultROIStart, defaultROIEnd, defaultIgnoreShortedStart, defaultIgnoreShortedTrigger, out var records, out var errorsFromDbApi);
|
|
return Tuple.Create(records, errorsFromDbApi, errorCode);
|
|
}
|
|
|
|
public Tuple<IDASDBRecord[],ulong> DASGet(string? dasSerial = null, string? position = null)
|
|
{
|
|
try
|
|
{
|
|
return GetDAS(dasSerial, position);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception($"DASGet failed. {ex.Message}");
|
|
}
|
|
}
|
|
public async Task<Tuple<IDASDBRecord[], ulong>> DASGetAsync(string? dasSerial = null, string? position = null)
|
|
{
|
|
try
|
|
{
|
|
var taskResult = await Task.Run(() =>
|
|
{
|
|
return GetDAS(dasSerial, position);
|
|
});
|
|
|
|
return taskResult;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception($"DASGet failed. {ex.Message}");
|
|
}
|
|
}
|
|
|
|
private Tuple<IDASDBRecord[], ulong> GetDAS(string? dasSerial, string? position)
|
|
{
|
|
if (_apiInfo?.UserDbRecord is null || _apiInfo.ConnectionDetails is null)
|
|
{
|
|
throw new Exception($"Authentication is required");
|
|
}
|
|
var error = DbAPI.DbAPI.DAS.DASGet(_apiInfo.UserDbRecord, _apiInfo.ConnectionDetails, _apiInfo.ConnectionDetails.ClientDbVersion, dasSerial, position, out var records);
|
|
return Tuple.Create(records, error);
|
|
}
|
|
}
|
|
}
|