2533 lines
115 KiB
C#
2533 lines
115 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Text;
|
|
using DTS.Common.Utilities.Logging;
|
|
using System.Threading;
|
|
using System.Linq;
|
|
// ReSharper disable ConditionIsAlwaysTrueOrFalse
|
|
using System.Data.SqlClient;
|
|
using DTS.Common.Interface.Channels;
|
|
using DTS.Common.Interface.Groups;
|
|
using DTS.Common.Classes.Groups.ChannelSettings;
|
|
using Prism.Ioc;
|
|
using DTS.Common.Events;
|
|
using System.IO.Compression;
|
|
using System.IO;
|
|
using DTS.Common.Interface.Database;
|
|
using DTS.Common.Interface.DataRecorders;
|
|
using DTS.Common.Interface.Sensors;
|
|
using DTS.Common.Interface.Graphs;
|
|
using DTS.Common.Interface.TestSetups;
|
|
using DTS.Common.Interface.TestSetups.TestSetupsList;
|
|
using DTS.Common.Interface.Tags;
|
|
using DTS.Common.Interface.TestMetaData;
|
|
using DTS.Common.Classes.CustomerDetails;
|
|
using DTS.Common.Classes.LabratoryDetails;
|
|
using DTS.Common.Classes.TestEngineerDetails;
|
|
using DTS.Common.Interface.Channels.ChannelCodes;
|
|
using DTS.Common.Enums.Channels;
|
|
using DTS.Common.Classes.Groups;
|
|
using DTS.Common.Classes;
|
|
using DTS.Common.Interface.RegionOfInterest;
|
|
using DTS.Common.Interface.Sensors.AnalogDiagnostics;
|
|
using Prism.Events;
|
|
using DbAPI.Errors;
|
|
|
|
namespace DTS.Common.Storage
|
|
{
|
|
public partial class DbOperations
|
|
{
|
|
public static void LogDBCaching(string message)
|
|
{
|
|
DbAPI.DbAPI.LogDBCaching(message);
|
|
}
|
|
/// <summary>
|
|
/// returns the client db version (software/code version)
|
|
/// </summary>
|
|
public static int GetClientDbVersion()
|
|
{
|
|
var connection = DbAPI.DbAPI.Connections.GetActiveConnections().FirstOrDefault();
|
|
if (null == connection) { return MINIMUM_LTS_DB_VERSION; }
|
|
return connection.ClientDbVersion;
|
|
}
|
|
/// <summary>
|
|
/// returns the version of the connected database (caches value)
|
|
/// </summary>
|
|
public static int GetSQLVersion()
|
|
{
|
|
var sqlVersion = 0;
|
|
|
|
var connection = DbAPI.DbAPI.Connections.GetActiveConnections().FirstOrDefault();
|
|
if (connection != null)
|
|
{
|
|
sqlVersion = DbAPI.DbAPI.Database.GetSQLVersion(connection);
|
|
}
|
|
return sqlVersion;
|
|
}
|
|
/// <summary>
|
|
/// returns the version of the connected database (caches value)
|
|
/// </summary>
|
|
public static int GetConnectionDbVersion()
|
|
{
|
|
var connection = DbAPI.DbAPI.Connections.GetActiveConnections().FirstOrDefault();
|
|
if (null == connection) { return MINIMUM_LTS_DB_VERSION; }
|
|
|
|
if (0 == connection.ConnectionDbVersion)
|
|
{
|
|
var user = CurrentUserDbRecord;
|
|
_ = DbAPI.DbAPI.Database.GetDatabaseVersion(user, connection, out var iVersion);
|
|
connection.ConnectionDbVersion = iVersion;
|
|
}
|
|
|
|
return 0 >= connection.ConnectionDbVersion ? MINIMUM_LTS_DB_VERSION : connection.ConnectionDbVersion;
|
|
}
|
|
public static ulong GetStoredProcedureToUse(string storedProcedure, out int storedProcedureVersionToUse)
|
|
{
|
|
var connection = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var ret = DbAPI.DbAPI.GetStoredProcedureToUse(connection, storedProcedure, CURRENT_DB_VERSION, out storedProcedureVersionToUse);
|
|
return ret;
|
|
}
|
|
/// <summary>
|
|
/// returns all EID (or squib article id) entries in the database
|
|
/// </summary>
|
|
public static HashSet<string> GetAllExistingSensorIds()
|
|
{
|
|
var hash = new HashSet<string>();
|
|
|
|
try
|
|
{
|
|
using (var cmd = GetSQLCommand(true))
|
|
{
|
|
try
|
|
{
|
|
cmd.CommandText = "SELECT eId FROM [SensorsAnalog]";
|
|
var reader = cmd.ExecuteReader();
|
|
while (reader.Read())
|
|
{
|
|
var eid = Utility.GetString(reader, "eId");
|
|
if (string.IsNullOrWhiteSpace(eid)) { continue; }
|
|
hash.Add(eid);
|
|
}
|
|
reader.Close();
|
|
cmd.CommandText = "SELECT ArticleId FROM [SensorsSquib]";
|
|
reader = cmd.ExecuteReader();
|
|
while (reader.Read())
|
|
{
|
|
var articleId = Utility.GetString(reader, "ArticleId");
|
|
if (string.IsNullOrWhiteSpace(articleId)) { continue; }
|
|
hash.Add(articleId);
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
cmd.Connection.Dispose();
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex) { APILogger.Log(ex); }
|
|
|
|
return hash;
|
|
}
|
|
public static ulong SensorsAnalogRunsGet(long? Id, int? testId, string testName, out IDiagnosticRun[] records)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Sensors.SensorsAnalogDiagnosticRunGet(user, con, Id, testId, testName, out records);
|
|
}
|
|
public static ulong SensorsAnalogDiagnosticsGet(long? Id, long? diagnosticRunId, int? sensorId, string sensorSerialNumber, out IDiagnosticEntry[] records)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Sensors.SensorsAnalogDiagnosticsGet(user, con, Id, diagnosticRunId, sensorId, sensorSerialNumber, out records);
|
|
}
|
|
public static ulong SensorsAnalogDiagnosticRunUpdateInsert(
|
|
IDiagnosticRun run)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Sensors.SensorsAnalogDiagnosticRunUpdateInsert(user, con, ref run);
|
|
}
|
|
public static ulong SensorsAnalogDiagnosticUpdateInsert(
|
|
IDiagnosticEntry entry)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Sensors.SensorsAnalogDiagnosticUpdateInsert(user, con, ref entry);
|
|
}
|
|
public static ulong SensorsStreamOutputUpdateInsert(
|
|
ref IStreamOutputRecord record)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Sensors.SensorsOutputStreamUpdateInsert(user, con, ref record);
|
|
}
|
|
public static ulong SensorsStreamOutputGet(
|
|
int? Id,
|
|
string SerialNumber,
|
|
out IStreamOutputRecord[] records)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Sensors.SensorsOutputStreamGet(user, con, Id, SerialNumber, out records);
|
|
}
|
|
public static ulong SensorsStreamInputUpdateInsert(
|
|
ref IStreamInputRecord record)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Sensors.SensorsInputStreamUpdateInsert(user, con, ref record);
|
|
}
|
|
public static ulong SensorsStreamInputGet(
|
|
int? Id,
|
|
string SerialNumber,
|
|
out IStreamInputRecord[] records)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Sensors.SensorsInputStreamGet(user, con, Id, SerialNumber, out records);
|
|
}
|
|
public static ulong SensorsThermocouplerGet(
|
|
int? Id,
|
|
string SerialNumber,
|
|
out IThermocouplerRecord[] records)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
if (con.ConnectionDbVersion < Constants.SLICE_TC_DB_VERSION)
|
|
{
|
|
//The database doesn't have this Stored Procedure so don't try to call it.
|
|
records = null;
|
|
return ErrorCodes.ERROR_UNKNOWN;
|
|
}
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Sensors.SensorsThermocouplerGet(user, con, CURRENT_DB_VERSION, Id, SerialNumber, out records);
|
|
}
|
|
public static ulong SensorsUARTUpdateInsert(ref IUARTRecord record)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Sensors.SensorsUARTUpdateInsert(user, con, ref record);
|
|
}
|
|
public static ulong SensorsUARTGet(
|
|
int? Id,
|
|
string SerialNumber,
|
|
out IUARTRecord[] records)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Sensors.SensorsUARTGet(user, con, Id, SerialNumber, out records);
|
|
}
|
|
public static ulong SensorsCANGet(
|
|
int? Id,
|
|
string SerialNumber,
|
|
out ICANRecord[] records)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Sensors.SensorsCanGet(user, con, Id, SerialNumber, out records);
|
|
}
|
|
public static ulong SensorsCanUpdateInsert(ref ICANRecord record)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Sensors.SensorsCanUpdateInsert(user, con, ref record);
|
|
}
|
|
/// <summary>
|
|
/// deletes matching test setup hardware records
|
|
/// Either Id, dasId, or testSetupId must be specified
|
|
/// </summary>
|
|
/// <param name="Id">test setup harware id (use null for all)</param>
|
|
/// <param name="dasId">das database id (use null for all)</param>
|
|
/// <param name="testSetupId">test setup database id (use null for all)</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong TestSetupHardwareDelete(
|
|
int? Id,
|
|
int? dasId,
|
|
int? testSetupId
|
|
)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.TestSetups.TestSetupHardwareDelete(user, con, Id, dasId, testSetupId);
|
|
}
|
|
/// <summary>
|
|
/// updates a test setup hardware record
|
|
/// </summary>
|
|
/// <param name="record">record to update</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong TestSetupHardwareUpdate(ITestSetupHardwareRecord record)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.TestSetups.TestSetupHardwareUpdate(user, con, record);
|
|
}
|
|
/// <summary>
|
|
/// inserts a new test setup hardware record
|
|
/// </summary>
|
|
/// <param name="record">record to insert</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong TestSetupHardwareInsert(ITestSetupHardwareRecord record)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.TestSetups.TestSetupHardwareInsert(user, con, record);
|
|
}
|
|
/// <summary>
|
|
/// retrieves hardware links for test setup
|
|
/// </summary>
|
|
/// <param name="testSetupId">test setup id (use null for all)</param>
|
|
/// <param name="records">all matching records</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong TestSetupHardwareGet(int? testSetupId, out ITestSetupHardwareRecord[] records)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.TestSetups.TestSetupHardwareGet(user, con, con.ClientDbVersion, testSetupId, out records);
|
|
}
|
|
/// <summary>
|
|
/// inserts a new group record into test setup
|
|
/// </summary>
|
|
/// <param name="record">record to insert</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong TestSetupGroupsInsert(ITestSetupGroupRecord record)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.TestSetups.TestSetupGroupsInsert(user, con, record);
|
|
}
|
|
/// <summary>
|
|
/// Updates a group record for test setup
|
|
/// </summary>
|
|
/// <param name="record">record to update</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong TestSetupGroupsUpdate(ITestSetupGroupRecord record)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.TestSetups.TestSetupGroupsUpdate(user, con, record);
|
|
}
|
|
/// <summary>
|
|
/// returns all matching records to search criteria
|
|
/// </summary>
|
|
/// <param name="groupId">matching group ids (use null for all)</param>
|
|
/// <param name="testSetupId">matching test setup id (use null for all)</param>
|
|
/// <param name="testSetupName">matching test setup name (use null for all)</param>
|
|
/// <param name="records">matching records</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong TestSetupGroupsGet(int? groupId, int? testSetupId, string testSetupName,
|
|
out ITestSetupGroupRecord[] records)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.TestSetups.TestSetupGroupsGet(user,
|
|
con, groupId, testSetupId, testSetupName, out records);
|
|
}
|
|
/// <summary>
|
|
/// Inserts a new channel code into the database
|
|
/// </summary>
|
|
/// <param name="lookup">lookup of string channel code type to short</param>
|
|
/// <param name="channelCode">channel code to insert</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong ChannelCodesInsert(IReadOnlyDictionary<ChannelEnumsAndConstants.ChannelCodeType, short> lookup,
|
|
IChannelCode channelCode, out int id)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Channels.ChannelCodesInsert(user, con, lookup, channelCode, out id);
|
|
}
|
|
/// <summary>
|
|
/// Updates a channel code database record
|
|
/// </summary>
|
|
/// <param name="lookup">lookup of string channel code type to short</param>
|
|
/// <param name="channelCode">channel code to update</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong ChannelCodesUpdate(IReadOnlyDictionary<ChannelEnumsAndConstants.ChannelCodeType, short> lookup,
|
|
IChannelCode channelCode)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Channels.ChannelCodesUpdate(user, con, lookup, channelCode);
|
|
}
|
|
/// <summary>
|
|
/// deletes matching channel codes
|
|
/// </summary>
|
|
/// <param name="user">user making deletes</param>
|
|
/// <param name="connection">connection to delete on</param>
|
|
/// <param name="id">id of channel code</param>
|
|
/// <param name="code">code of matching channel codes (can be null)</param>
|
|
/// <param name="name">name of matching channel codes (can be null)</param>
|
|
/// <param name="codeType">code type of matching channel codes (can be null)</param>
|
|
/// <returns>0 (ERROR_SUCCESS) on success, all other values are error codes</returns>
|
|
public static ulong ChannelCodesDelete(
|
|
int? id,
|
|
string code,
|
|
string name,
|
|
int? codeType)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Channels.ChannelCodesDelete(user, con,
|
|
id, code, name, codeType);
|
|
}
|
|
/// <summary>
|
|
/// retrieves all matching channel code types
|
|
/// </summary>
|
|
/// <param name="id">id, use null for all</param>
|
|
/// <param name="codeType">code type, use null for all</param>
|
|
/// <param name="records">matching records</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong ChannelCodeTypesGet(
|
|
short? id,
|
|
string codeType,
|
|
out Tuple<short, string>[] records)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Channels.ChannelCodeTypesGet(user,
|
|
con, id, codeType, out records);
|
|
}
|
|
/// <summary>
|
|
/// retrieves all matching channel codes
|
|
/// </summary>
|
|
/// <param name="Id"></param>
|
|
/// <param name="code"></param>
|
|
/// <param name="name"></param>
|
|
/// <param name="codeType"></param>
|
|
/// <param name="channelTypeLookup"></param>
|
|
/// <param name="records"></param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong ChannelCodesGet(
|
|
int? Id,
|
|
string code,
|
|
string name,
|
|
ChannelEnumsAndConstants.ChannelCodeType? codeType,
|
|
IReadOnlyDictionary<short, string> channelTypeLookup,
|
|
out IChannelCode[] records
|
|
)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Channels.ChannelCodesGet(user, con, Id, code, name, codeType,
|
|
channelTypeLookup, out records);
|
|
}
|
|
/// <summary>
|
|
/// inserts a tag assignment into the database
|
|
/// </summary>
|
|
/// <param name="tagAssignment">assignment to insert</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong TagAssignmentsInsert(ITagAssignment tagAssignment)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Tags.TagAssignmentsInsert(user, con, tagAssignment);
|
|
}
|
|
/// <summary>
|
|
/// deletes all tag assignments which match search criteria
|
|
/// </summary>
|
|
/// <param name="objectType">object type tag is assigned to</param>
|
|
/// <param name="objectId">object id of object tags are assigned to</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong TagAssignmentsDelete(TagTypes objectType, int objectId)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Tags.TagAssignmentsDelete(user, con, objectId, objectType);
|
|
}
|
|
/// <summary>
|
|
/// retrieves all tag assignments which match search criteria
|
|
/// </summary>
|
|
/// <param name="tagType">tag type for which to retrieve assignments (use null for all)</param>
|
|
/// <param name="records">matching records</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong TagAssignmentsGet(TagTypes? tagType, out ITagAssignment[] records)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Tags.TagAssignmentsGet(user, con, tagType, out records);
|
|
}
|
|
/// <summary>
|
|
/// inserts a tag into the database
|
|
/// modifies tag by updating id after insert
|
|
/// </summary>
|
|
/// <param name="tag">tag to be inserted</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong TagsInsert(ref ITag tag)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Tags.TagsInsert(user, con, ref tag);
|
|
}
|
|
/// <summary>
|
|
/// retrieves a tag id associated with a tag text
|
|
/// </summary>
|
|
/// <param name="text">text to search for</param>
|
|
/// <param name="id">id (or null if not found)</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong TagsGetId(string text, out int? id)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Tags.TagsGetId(user, con, text, out id);
|
|
}
|
|
/// <summary>
|
|
/// deletes requested tag
|
|
/// </summary>
|
|
/// <param name="tagId">database id of tag to delete</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong TagsDelete(int tagId)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Tags.TagsDelete(user, con, tagId);
|
|
}
|
|
/// <summary>
|
|
/// returns all tags matching given search criteria
|
|
/// </summary>
|
|
/// <param name="tagId">tag id to search for (use null for all)</param>
|
|
/// <param name="records">all matching records</param>
|
|
/// <returns>0 on success, all other values are errors</returns>
|
|
public static ulong TagsGet(int? tagId, out ITag[] records)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Tags.TagsGet(user, con, tagId, out records);
|
|
}
|
|
/// <summary>
|
|
/// updates channel setting
|
|
/// </summary>
|
|
/// <param name="settingId">setting id to update</param>
|
|
/// <param name="defaultValue">new default value for setting</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong ChannelSettingsUpdate(int settingId, string defaultValue)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Channels.ChannelSettingsUpdate(user, con, settingId, defaultValue);
|
|
}
|
|
/// <summary>
|
|
/// retrieves all channel settings matching search criteria
|
|
/// </summary>
|
|
/// <param name="settingId">channel id to search for (allows NULL)</param>
|
|
/// <param name="settingName">setting name to search for (allows null/empty)</param>
|
|
/// <param name="records">matching records</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong ChannelSettingsGet(int? settingId, string settingName, out IChannelSettingRecord[] records)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Channels.ChannelSettingsGet(user, con, settingId, settingName, out records);
|
|
}
|
|
/// <summary>
|
|
/// deletes channel setting(s) from db associated with channel
|
|
/// </summary>
|
|
/// <param name="channelId">channel settings are associated</param>
|
|
/// <param name="settingId">setting to delete (use null to delete all)</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong GroupChannelSettingsDelete(long channelId, int? settingId)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Channels.GroupChannelSettingsDelete(user, con,
|
|
channelId, settingId);
|
|
}
|
|
/// <summary>
|
|
/// inserts a group channel setting into the db
|
|
/// </summary>
|
|
/// <param name="channelid">channel setting is associated with</param>
|
|
/// <param name="record">setting to insert</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong GroupChannelSettingsInsert(long channelid, IGroupChannelSettingRecord record)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Channels.GroupChannelSettingsInsert(user, con, CURRENT_DB_VERSION, channelid, record);
|
|
}
|
|
/// <summary>
|
|
/// returns all channel settings matching input channel id
|
|
/// </summary>
|
|
/// <param name="channelId">channel id to query for</param>
|
|
/// <param name="records">all matching channel settings</param>
|
|
/// <param name="errors">any errors encountered while retrieving group channel settings</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong GroupChannelSettingsGet(List<long> channelIdList, out IGroupChannelSettingRecord[] records, out string[] errors)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Channels.GroupChannelSettingsGet(user, con, CURRENT_DB_VERSION, channelIdList, out records, out errors);
|
|
}
|
|
/// <summary>
|
|
/// marks a test setup as dirty or complete in the db
|
|
/// a test setup is dirty if readiness to run or completeness has not been calculated yet or not
|
|
/// </summary>
|
|
/// <param name="name">name of test setup to mark</param>
|
|
/// <param name="dirty">whether the test setup has completeness and readiness has been calculated yet or not</param>
|
|
/// <param name="complete">whether the test setup is complete and ready to run</param>
|
|
/// <param name="error">any errors or warnings associated with test setup</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong TestSetupsMarkIsCompleteIsDirty(string name, bool dirty, bool complete, string error)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.TestSetups.TestSetupsMarkIsDirtyIsComplete(user,
|
|
con, name, dirty, complete, error);
|
|
}
|
|
public static ulong TestSetupsDeleteByDate(DateTime date)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.TestSetups.TestSetupsDeleteByDate(user, con, date);
|
|
}
|
|
/// <summary>
|
|
/// deletes all test setups and groups
|
|
/// </summary>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong TestSetupsAndGroupsDeleteAll()
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.TestSetups.TestSetupsAndGroupsDeleteAll(user, con);
|
|
}
|
|
/// <summary>
|
|
/// deletes test setups matching criteria
|
|
/// </summary>
|
|
/// <param name="ids">ids of test setups that should be deleted</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong TestSetupsDeleteById(int[] ids)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.TestSetups.TestSetupsDeleteById(user, con, ids);
|
|
}
|
|
/// <summary>
|
|
/// deletes test setups matching criteria
|
|
/// </summary>
|
|
/// <param name="names">names of test setups that should be deleted</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong TestSetupsDeleteByName(string[] names)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.TestSetups.TestSetupsDeleteByName(user, con, names);
|
|
}
|
|
/// <summary>
|
|
/// updates or inserts a test setup record into the database
|
|
/// test setup is modified with database id if an insert is completed
|
|
/// </summary>
|
|
/// <param name="record">record to update or insert</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong TestSetupsUpdateInsert(ref ITestSetupRecord record)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.TestSetups.TestSetupsUpdateInsert(user, con, CURRENT_DB_VERSION, ref record);
|
|
}
|
|
/// <summary>
|
|
/// returns all test setup records matching search criteria
|
|
/// </summary>
|
|
/// <param name="testSetupId">database id of test setup (can be null)</param>
|
|
/// <param name="testSetupName">test setup name (can be empty or null)</param>
|
|
/// <param name="defaultROIStart">default ROI start time (used for old test setups which may not have ROI start/stop specified for all events)</param>
|
|
/// <param name="defaultROIEnd">default ROI end time (used for old test setups which may not have ROI start/stop specified for all events)</param>
|
|
/// <param name="records">all matching records</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong TestSetupsGet(int? testSetupId,
|
|
string testSetupName,
|
|
double defaultROIStart,
|
|
double defaultROIEnd,
|
|
bool defaultIgnoreShortedStart,
|
|
bool defaultIgnoreShortedTrigger,
|
|
out ITestSetupRecord[] records,
|
|
out string[] errors)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.TestSetups.TestSetupsGet(user, con, CURRENT_DB_VERSION, testSetupId, testSetupName,
|
|
defaultROIStart, defaultROIEnd, defaultIgnoreShortedStart, defaultIgnoreShortedTrigger, out records,
|
|
out errors);
|
|
}
|
|
/// <summary>
|
|
/// deletes a calculated channel record from db
|
|
/// </summary>
|
|
/// <param name="calculatedChannelId"></param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong CalculatedChannelsDelete(int calculatedChannelId)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.CalculatedChannels.CalculatedChannelsDelete(user, con, calculatedChannelId);
|
|
}
|
|
/// <summary>
|
|
/// inserts a calculated channel record into the db
|
|
/// </summary>
|
|
/// <param name="record">record to insert</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong CalculatedChannelsInsert(ICalculatedChannelRecord record)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.CalculatedChannels.CalculatedChannelsInsert(user, con, ref record);
|
|
}
|
|
/// <summary>
|
|
/// updates a calculated channel record in the db
|
|
/// </summary>
|
|
/// <param name="record">record to update</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong CalculatedChannelsUpdate(ICalculatedChannelRecord record)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.CalculatedChannels.CalculatedChannelsUpdate(user, con, record);
|
|
}
|
|
/// <summary>
|
|
/// retrieves all calculated channels matching search criteria
|
|
/// </summary>
|
|
/// <param name="ccId">database id of calculated channel (can be null)</param>
|
|
/// <param name="testSetupName">test setup calculated channels belong to</param>
|
|
/// <param name="records">all matching records</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong CalculatedChannelsGet(int? ccId, string testSetupName,
|
|
out ICalculatedChannelRecord[] records)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.CalculatedChannels.CalculatedChannelsGet(user, con, ccId, testSetupName, out records);
|
|
}
|
|
/// <summary>
|
|
/// Deletes records from the TestSetupROIs and ROIPeriodChannels tables
|
|
/// </summary>
|
|
/// <param name="testSetupId"></param>
|
|
/// <returns></returns>
|
|
public static ulong RegionsOfInterestDelete(int testSetupId)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.RegionsOfInterest.RegionsOfInterestDelete(user, con, testSetupId);
|
|
}
|
|
/// <summary>
|
|
/// Inserts records into the TestSetupROIs and ROIPeriodChannels tables
|
|
/// </summary>
|
|
/// <param name="testSetupId"></param>
|
|
/// <param name="regionOfInterest"></param>
|
|
/// <returns></returns>
|
|
public static ulong RegionsOfInterestInsert(int testSetupId, IRegionOfInterest regionOfInterest)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.RegionsOfInterest.RegionsOfInterestInsert(user, con, CURRENT_DB_VERSION, testSetupId, regionOfInterest);
|
|
}
|
|
/// <summary>
|
|
/// Retrieves records from the TestSetupROIs and ROIPeriodChannels tables
|
|
/// </summary>
|
|
/// <param name="testSetupId"></param>
|
|
/// <param name="records"></param>
|
|
/// <returns></returns>
|
|
public static ulong RegionsOfInterestGet(int testSetupId, out IRegionOfInterest[] records)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.RegionsOfInterest.RegionsOfInterestGet(user, con, CURRENT_DB_VERSION, testSetupId, out records);
|
|
}
|
|
public static ulong TestGraphsDelete(int graphId)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Graphs.GraphsDelete(user, con, graphId);
|
|
}
|
|
public static ulong TestGraphsInsert(IGraphRecord record)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Graphs.GraphsInsert(user, con, ref record);
|
|
}
|
|
public static ulong TestGraphsUpdate(IGraphRecord record)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Graphs.GraphsUpdate(user, con, record);
|
|
}
|
|
public static ulong TestGraphsGet(int? graphId, int? testSetupId, out IGraphRecord[] records)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Graphs.GraphsGet(user, con, graphId, testSetupId, out records);
|
|
}
|
|
public static ulong SensorsDigitalOutUpdateInsert(IDigitalOutDbRecord record)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Sensors.SensorsDigitalOutUpdateInsert(user, con, record);
|
|
}
|
|
/// <summary>
|
|
/// returns all db digital output records matching search criteria
|
|
/// </summary>
|
|
/// <param name="id">database id of setting (can be null)</param>
|
|
/// <param name="serialNumber">serial number/name of sensor (can be null)</param>
|
|
/// <param name="records">matching records</param>
|
|
/// <returns>0 (ERROR_SUCCESS) on success, all other values are error codes</returns>
|
|
public static ulong SensorsDigitalOutGet(int? id,
|
|
string serialNumber,
|
|
out IDigitalOutDbRecord[] records)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Sensors.SensorsDigitalOutGet(user, con, id, serialNumber, out records);
|
|
}
|
|
/// <summary>
|
|
/// updates or inserts a digital input setting into the db
|
|
/// modifies record on the case of insert with new database id
|
|
/// </summary>
|
|
/// <param name="record">record to commit</param>
|
|
/// <returns>0 (ERROR_SUCCESS) on success, all other values are error codes</returns>
|
|
public static ulong SensorsDigitalInUpdateInsert(IDigitalInDbRecord record)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Sensors.SensorsDigitalInUpdateInsert(user, con, record);
|
|
}
|
|
/// <summary>
|
|
/// retrieves all digital input settings matching search criteria
|
|
/// </summary>
|
|
/// <param name="id">database id of setting (can be null)</param>
|
|
/// <param name="serialNumber">serial number/name of setting (can be null)</param>
|
|
/// <param name="eId">electronic id of setting (can be null)</param>
|
|
/// <param name="records">matching records</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong SensorsDigitalInGet(int? id, string serialNumber, string eId, out IDigitalInDbRecord[] records)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Sensors.SensorsDigitalInGet(user, con, id, serialNumber, eId, out records);
|
|
}
|
|
/// <summary>
|
|
/// commits a squib to the database
|
|
/// </summary>
|
|
/// <param name="record">record to commit</param>
|
|
/// <returns>0 on success, all other values are errors</returns>
|
|
public static ulong SensorsSquibUpdateInsert(ISquibDbRecord record)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Sensors.SensorsSquibUpdateInsert(user, con, record);
|
|
}
|
|
/// <summary>
|
|
/// retrieves any squibs matching search criteria
|
|
/// </summary>
|
|
/// <param name="id">Database id of squib (can be null)</param>
|
|
/// <param name="serialNumber">Setting name or serial number of squib (can be null)</param>
|
|
/// <param name="eId">Electronic Id of squib (can be null)</param>
|
|
/// <param name="records">any matching records</param>
|
|
/// <returns>0 on success, all other values are errors</returns>
|
|
public static ulong SensorsSquibGet(int? id, string serialNumber, string eId, out ISquibDbRecord[] records)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Sensors.SensorsSquibGet(user, con, id, serialNumber, eId, out records);
|
|
}
|
|
/// <summary>
|
|
/// Deletes all sensors (but test specific template sensors)
|
|
/// </summary>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong SensorsDeleteAll()
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Sensors.SensorsDeleteAll(user, con);
|
|
}
|
|
/// <summary>
|
|
/// retrieves bridge resistance for sensor in db
|
|
/// </summary>
|
|
/// <param name="serialNumber">serial number of sensor</param>
|
|
/// <param name="bridgeResistance">bridge resistance in db</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong SensorsAnalogBridgeResistanceGet(
|
|
string serialNumber,
|
|
out double bridgeResistance)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Sensors.SensorsAnalogBridgeResistanceGet(user, con, serialNumber, out bridgeResistance);
|
|
}
|
|
/// <summary>
|
|
/// deletes matching sensors
|
|
/// </summary>
|
|
/// <param name="sensorId">database id of sensor to be deleted</param>
|
|
/// <param name="sensorType">type of sensor
|
|
/// Squib = 3
|
|
/// DigitalIn = 1
|
|
/// DigitalOut = 2
|
|
/// UART = 4
|
|
/// Analog = 0
|
|
/// </param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong SensorsDelete(int sensorId, int sensorType)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Sensors.SensorsDelete(user, con, sensorId, sensorType);
|
|
}
|
|
/// <summary>
|
|
/// deletes any matching sensor calibrations
|
|
/// </summary>
|
|
/// <param name="sensorSerialNumber">serial number</param>
|
|
/// <param name="calibrationDate">calibration date (can be null)</param>
|
|
/// <param name="modifyDate">modify date (can be null)</param>
|
|
/// <returns></returns>
|
|
public static ulong SensorCalibrationsDelete(string sensorSerialNumber, DateTime? calibrationDate, DateTime? modifyDate)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Sensors.SensorCalibrationsDelete(user, con, sensorSerialNumber, calibrationDate, modifyDate);
|
|
}
|
|
/// <summary>
|
|
/// inserts a sensor calibration into the db
|
|
/// </summary>
|
|
/// <param name="cal">calibration record to insert</param>
|
|
/// <param name="sensorType">sensor type
|
|
/// Squib = 3
|
|
/// DigitalIn = 1
|
|
/// DigitalOut = 2
|
|
/// UART = 4
|
|
/// Analog = 0
|
|
/// </param>
|
|
/// <param name="setCalibrationId"></param>
|
|
/// <returns></returns>
|
|
public static ulong SensorCalibrationsInsert(
|
|
ISensorCalibration cal,
|
|
int sensorType,
|
|
bool setCalibrationId)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Sensors.SensorCalibrationsInsert(user, con, cal, sensorType, setCalibrationId);
|
|
}
|
|
/// <summary>
|
|
/// inserts or updates sensor in database
|
|
/// if inserted will be modified with database id after insert
|
|
/// </summary>
|
|
/// <param name="record">record to commit</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong SensorsAnalogInsertUpdate(IAnalogDbRecord record)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Sensors.SensorsAnalogUpdateInsert(user, con, record);
|
|
}
|
|
/// <summary>
|
|
/// retrieves all analog sensors matching criteria
|
|
/// </summary>
|
|
/// <param name="sensorId">database id of sensor (can be null)</param>
|
|
/// <param name="serialNumber">serial number of sensor (can be null)</param>
|
|
/// <param name="eId">Electronic Id (can be null)</param>
|
|
/// <param name="records">all records matching criteria</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong SensorsAnalogGet(int? sensorId, string serialNumber, string eId, out IAnalogDbRecord[] records)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Sensors.SensorsAnalogGet(user, con, sensorId, serialNumber, eId, out records);
|
|
}
|
|
|
|
/// <summary>
|
|
/// retrieves all sensors matching criteria
|
|
/// </summary>
|
|
/// <param name="serialNumber">serial number of sensor (can be null)</param>
|
|
/// <param name="records">all records matching criteria</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong SensorsGet(string serialNumber, out ISensorDbRecord[] records)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Sensors.SensorsGet(user, con, serialNumber, out records);
|
|
}
|
|
/// <summary>
|
|
/// Updates the Sensor Usage count for all sensors in an Assembly
|
|
/// </summary>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong UpdateAssemblySensorUsageCount(string assemblyName, int newUsageCount)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Sensors.UpdateAssemblySensorUsageCount(user, con, assemblyName, newUsageCount);
|
|
}
|
|
/// <summary>
|
|
/// Updates the Sensor Usage count for a sensor in database
|
|
/// </summary>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong UpdateSensorUsageCount(ISensorData sd)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Sensors.UpdateSensorUsageCount(user, con, sd);
|
|
}
|
|
/// <summary>
|
|
/// returns all calibrations matching criteria
|
|
/// </summary>
|
|
/// <param name="sensorId">database id of sensor (can be null)</param>
|
|
/// <param name="serialNumber">serial number of sensor (can be null)</param>
|
|
/// <param name="records">records matching criteria</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong SensorCalibrationsGet(int? sensorId, string serialNumber, out ISensorCalDbRecord[] records)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Sensors.SensorCalibrationsGet(user, con, sensorId, serialNumber, out records);
|
|
}
|
|
/// <summary>
|
|
/// Updates the Sensor Calibration Usage count for a sensor in database
|
|
/// </summary>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong UpdateSensorCalibrationUsageCount(int sensorId, int sensorCalibrationId, int newUsageCount)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Sensors.UpdateSensorCalibrationUsageCount(user, con, sensorId, sensorCalibrationId, newUsageCount);
|
|
}
|
|
/// <summary>
|
|
/// initializes loggers for DbAPI (path, size, verbosity)
|
|
/// </summary>
|
|
/// <param name="logSize"></param>
|
|
/// <param name="path"></param>
|
|
/// <param name="logTypes">Bitmask for log types (based on TraceEventTypes)
|
|
/// Critical - Bit 0
|
|
/// Error - Bit 1
|
|
/// Warning - Bit 2
|
|
/// Information - Bit 3
|
|
/// Verbose - Bit 4
|
|
/// Start - Bit 8
|
|
/// Stop - Bit 9
|
|
/// Suspend - Bit 10
|
|
/// Resume - Bit 11
|
|
/// Transfer - Bit 12
|
|
/// </param>
|
|
public static void InitializeDbAPI(int logSize, string path, int logTypes)
|
|
{
|
|
DbAPI.DbAPI.InitializeLogger(logSize, path, logTypes);
|
|
}
|
|
/// <summary>
|
|
/// Deletes channels belong to a DAS
|
|
/// </summary>
|
|
/// <param name="hardwareId">string indicating hardware (serial_dastype)</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong DASChannelsDelete(string hardwareId)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.DAS.DASChannelsDelete(user, con, hardwareId);
|
|
}
|
|
/// <summary>
|
|
/// gets any children associated with a das (S6 connected to a S6 distributor for instance)
|
|
/// </summary>
|
|
/// <param name="serial">serial number of parent das</param>
|
|
/// <param name="childrenSerialNumbers">output of serial numbers for any associated children</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong DASChildrenGet(string serial, out string[] childrenSerialNumbers)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.DAS.DASChildrenGet(user, con, serial, out childrenSerialNumbers);
|
|
}
|
|
/// <summary>
|
|
/// Deletes a DAS
|
|
/// will remove record from any test setups
|
|
/// will remove from channel assignments (if embedded, otherwise will remove channels as well)
|
|
/// will remove das channels and das meta data
|
|
/// </summary>
|
|
/// <param name="DASId"></param>
|
|
/// <param name="serial"></param>
|
|
/// <param name="embedded">whether das is embedded (only exists in a given test or group)</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong DASDelete(int DASId, string serial, bool embedded)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.DAS.DASDelete(user, con, DASId, serial, embedded);
|
|
}
|
|
/// <summary>
|
|
/// deletes all DAS
|
|
/// </summary>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong ClearNonPrototypeDas()
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
var ret = DbAPI.DbAPI.DAS.DASGet(user, con, con.ClientDbVersion, null, null, out var allDAS);
|
|
foreach (var d in allDAS)
|
|
{
|
|
ret = DbAPI.DbAPI.DAS.DASDelete(user, con, d.DASId, d.SerialNumber, false);
|
|
if (ret != 0) break;
|
|
}
|
|
return ret;
|
|
}
|
|
/// <summary>
|
|
/// retrieves all das matching input criteria
|
|
/// </summary>
|
|
/// <param name="DASId">specific das id to look for (or null)</param>
|
|
/// <param name="dasSerial">specific serial to look for (or null)</param>
|
|
/// <param name="position">position to look for (or null) use Prototype to find prototype hardware</param>
|
|
/// <param name="das">matching DAS</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong DASGet(string dasSerial, string position, out IDASDBRecord[] das)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.DAS.DASGet(user, con, con.ClientDbVersion, dasSerial, position, out das);
|
|
}
|
|
/// <summary>
|
|
/// inserts a new das record into the DB
|
|
/// DAS Id will be updated on insert
|
|
/// </summary>
|
|
/// <param name="das">DAS to insert</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong DASInsert(IDASDBRecord das)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.DAS.DASInsert(user, con, das);
|
|
}
|
|
/// <summary>
|
|
/// update a DAS record in the db
|
|
/// </summary>
|
|
/// <param name="das">DAS to update</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong DASUpdate(IDASDBRecord das)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.DAS.DASUpdate(user, con, das);
|
|
}
|
|
/// <summary>
|
|
/// returns all channels matching input criteria
|
|
/// </summary>
|
|
/// <param name="hardwareId">string indentifying hardware (serial_dastype)</param>
|
|
/// <param name="channels">output all matching channels</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong DASChannelsGet(string hardwareId, out IDASChannelDBRecord[] channels)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.DAS.DASChannelsGet(user, con, hardwareId, out channels);
|
|
}
|
|
/// <summary>
|
|
/// inserts DAS channels into database
|
|
/// </summary>
|
|
/// <param name="channel">channels to insert</param>
|
|
/// <param name="hardwareId">string indentifying hardware (serial_dastype)</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong DASChannelsInsert(IDASChannelDBRecord channel, string hardwareId)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.DAS.DASChannelsInsert(user, con, hardwareId, ref channel);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Inserts a new record in the Channels table
|
|
/// </summary>
|
|
/// <param name="channel">The new values for the record in the Channels table</param>
|
|
/// <returns></returns>
|
|
public static ulong ChannelsInsert(ref IChannelDbRecord channel)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Channels.ChannelsInsert(user, con, ref channel);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates an existing record in the Channels table
|
|
/// </summary>
|
|
/// <param name="channel">The new values for the record in the Channels table</param>
|
|
/// <returns></returns>
|
|
public static ulong ChannelsUpdate(IChannelDbRecord channel)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Channels.ChannelsUpdate(user, con, channel);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns all channels matching input criteria
|
|
/// </summary>
|
|
/// <param name="channelId">Id in the Channels table</param>
|
|
/// <param name="groupId">GroupId in the Channels and TestSetupGroups tables</param>
|
|
/// <param name="dasId">DASId in the Channels table</param>
|
|
/// <param name="sensorId">SensorId in the Channels table</param>
|
|
/// <param name="testSetupId">TestSetupId in the TestSetupGroups and TestSetups tables</param>
|
|
/// <param name="testSetupName">TestSetupName in the TestSetups table</param>
|
|
/// <param name="channels">output all matching channels</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong ChannelsGet(int? channelId, int? groupId, int? dasId, int? sensorId, int? testSetupId, string testSetupName, out IChannelDbRecord[] channels)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Channels.ChannelsGet(user, con, con.ClientDbVersion, channelId, groupId, dasId, sensorId, testSetupId, testSetupName, out channels);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes an entry in the Channels table
|
|
/// </summary>
|
|
/// <param name="id">Id in the Channels table</param>
|
|
/// <param name="errorMessage">Error string returned, possibly from sp_ChannelsDelete</param>
|
|
/// <returns></returns>
|
|
public static ulong ChannelsDelete(long id, out string errorMessage)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Channels.ChannelsDelete(user, con, id, out errorMessage);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Inserts a new record in the GroupHardware table
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static ulong GroupHardwareInsert(GroupHardwareDbRecord groupHardwareDbRecord, out int newId, out string errorMessage)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
newId = -1;
|
|
errorMessage = string.Empty;
|
|
return DbAPI.DbAPI.GroupHardware.GroupHardwareInsert(user, con, groupHardwareDbRecord, out newId, out errorMessage);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets one or more record from the GroupHardware table
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static ulong GroupHardwareGet(int? groupId, string serialNumber, bool? embedded, out GroupHardwareDbRecord[] groupHardwareDbRecords)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.GroupHardware.GroupHardwareGet(user, con, groupId, serialNumber, embedded, out groupHardwareDbRecords);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes a record in the GroupHardware table
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static ulong GroupHardwareDelete(int? groupId, int? dasId, out string errorMessage)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.GroupHardware.GroupHardwareDelete(user, con, groupId, dasId, out errorMessage);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Inserts a new record in the Groups table
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static ulong GroupsInsert(ref IGroupDbRecord group)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Groups.GroupsInsert(user, con, ref group);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates a record in the Groups table
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static ulong GroupsUpdate(IGroupDbRecord group)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Groups.GroupsUpdate(user, con, group);
|
|
}
|
|
public static ulong GroupsGet(int? id, string serialNumber, string displayName, bool? embedded, int? staticGroupId, out IGroupDbRecord[] groups)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Groups.GroupsGet(user, con, id, serialNumber, displayName, embedded, staticGroupId, out groups);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes a record in the Groups table
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static ulong GroupsDelete(long id, out string errorMessage)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.Groups.GroupsDelete(user, con, id, out errorMessage);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Inserts a new record in the CustomerDetails table
|
|
/// </summary>
|
|
/// <param name="customerDetailsDbRecord">The new values for the record in the CustomerDetails table</param>
|
|
/// <param name="newIdLong">The Id of the new record in the CustomerDetails table</param>
|
|
/// <param name="errorMessage">Error string returned, possibly from sp_CustomerDetailsInsert</param>
|
|
/// <returns></returns>
|
|
public static ulong CustomerDetailsInsert(CustomerDetailsDbRecord customerDetailsDbRecord, out int newId, out string errorMessage)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.CustomerDetails.CustomerDetailsInsert(user, con, customerDetailsDbRecord, out newId, out errorMessage);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates an existing record in the CustomerDetails table
|
|
/// </summary>
|
|
/// <param name="channel">The new values for the record in the CustomerDetails table</param>
|
|
/// <param name="errorMessage">Error string returned, possibly from sp_CustomerDetailsUpdate</param>
|
|
/// <returns></returns>
|
|
public static ulong CustomerDetailsUpdate(CustomerDetailsDbRecord customerDetailsDbRecord, out string errorMessage)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.CustomerDetails.CustomerDetailsUpdate(user, con, customerDetailsDbRecord, out errorMessage);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns all channels matching input criteria
|
|
/// </summary>
|
|
/// <param name="name">Name in the CustomerDetails table</param>
|
|
/// <param name="customerDetailsDbRecords">output all matching records</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong CustomerDetailsGet(string name, out ICustomerDetailsDbRecord[] customerDetailsDbRecords)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.CustomerDetails.CustomerDetailsGet(user, con, name, out customerDetailsDbRecords);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes an entry in the CustomerDetails table
|
|
/// </summary>
|
|
/// <param name="id">Id in the CustomerDetails table</param>
|
|
/// <param name="errorMessage">Error string returned, possibly from sp_CustomerDetailsDelete</param>
|
|
/// <returns></returns>
|
|
public static ulong CustomerDetailsDelete(string name, out string errorMessage)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.CustomerDetails.CustomerDetailsDelete(user, con, name, out errorMessage);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Inserts a new record in the LabratoryDetails table
|
|
/// </summary>
|
|
/// <param name="labratoryDetailsDbRecord">The new values for the record in the LabratoryDetails table</param>
|
|
/// <param name="newIdLong">The Id of the new record in the LabratoryDetails table</param>
|
|
/// <param name="errorMessage">Error string returned, possibly from sp_LabratoryDetailsInsert</param>
|
|
/// <returns></returns>
|
|
public static ulong LabratoryDetailsInsert(LabratoryDetailsDbRecord labratoryDetailsDbRecord, out int newId, out string errorMessage)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.LabratoryDetails.LabratoryDetailsInsert(user, con, labratoryDetailsDbRecord, out newId, out errorMessage);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates an existing record in the LabratoryDetails table
|
|
/// </summary>
|
|
/// <param name="channel">The new values for the record in the LabratoryDetails table</param>
|
|
/// <param name="errorMessage">Error string returned, possibly from sp_LabratoryDetailsUpdate</param>
|
|
/// <returns></returns>
|
|
public static ulong LabratoryDetailsUpdate(LabratoryDetailsDbRecord labratoryDetailsDbRecord, out string errorMessage)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.LabratoryDetails.LabratoryDetailsUpdate(user, con, labratoryDetailsDbRecord, out errorMessage);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates an existing record or Inserts a new record in the LabratoryDetails table
|
|
/// </summary>
|
|
/// <param name="channel">The new values for the record in the LabratoryDetails table</param>
|
|
/// <param name="errorMessage">Error string returned, possibly from sp_LabratoryDetailsUpdate</param>
|
|
/// <returns></returns>
|
|
public static ulong LabratoryDetailsUpdateInsert(LabratoryDetailsDbRecord labratoryDetailsDbRecord, out string errorMessage)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.LabratoryDetails.LabratoryDetailsUpdateInsert(user, con, labratoryDetailsDbRecord, out errorMessage);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns all channels matching input criteria
|
|
/// </summary>
|
|
/// <param name="name">Name in the LabratoryDetails table</param>
|
|
/// <param name="labratoryDetailsDbRecords">output all matching records</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong LabratoryDetailsGet(string name, out ILabratoryDetailsDbRecord[] labratoryDetailsDbRecords)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.LabratoryDetails.LabratoryDetailsGet(user, con, name, out labratoryDetailsDbRecords);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes an entry in the LabratoryDetails table
|
|
/// </summary>
|
|
/// <param name="id">Id in the LabratoryDetails table</param>
|
|
/// <param name="errorMessage">Error string returned, possibly from sp_LabratoryDetailsDelete</param>
|
|
/// <returns></returns>
|
|
public static ulong LabratoryDetailsDelete(string name, out string errorMessage)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.LabratoryDetails.LabratoryDetailsDelete(user, con, name, out errorMessage);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Inserts a new record in the TestEngineerDetails table
|
|
/// </summary>
|
|
/// <param name="testEngineerDetailsDbRecord">The new values for the record in the TestEngineerDetails table</param>
|
|
/// <param name="newIdLong">The Id of the new record in the TestEngineerDetails table</param>
|
|
/// <param name="errorMessage">Error string returned, possibly from sp_TestEngineerDetailsInsert</param>
|
|
/// <returns></returns>
|
|
public static ulong TestEngineerDetailsInsert(TestEngineerDetailsDbRecord testEngineerDetailsDbRecord, out int newId, out string errorMessage)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.TestEngineerDetails.TestEngineerDetailsInsert(user, con, testEngineerDetailsDbRecord, out newId, out errorMessage);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates an existing record in the TestEngineerDetails table
|
|
/// </summary>
|
|
/// <param name="channel">The new values for the record in the TestEngineerDetails table</param>
|
|
/// <param name="errorMessage">Error string returned, possibly from sp_TestEngineerDetailsUpdate</param>
|
|
/// <returns></returns>
|
|
public static ulong TestEngineerDetailsUpdate(TestEngineerDetailsDbRecord testEngineerDetailsDbRecord, out string errorMessage)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.TestEngineerDetails.TestEngineerDetailsUpdate(user, con, testEngineerDetailsDbRecord, out errorMessage);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates an existing record or Inserts a new record in the TestEngineerDetails table
|
|
/// </summary>
|
|
/// <param name="channel">The new values for the record in the TestEngineerDetails table</param>
|
|
/// <param name="errorMessage">Error string returned, possibly from sp_TestEngineerDetailsUpdate</param>
|
|
/// <returns></returns>
|
|
public static ulong TestEngineerDetailsUpdateInsert(TestEngineerDetailsDbRecord testEngineerDetailsDbRecord, out string errorMessage)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.TestEngineerDetails.TestEngineerDetailsUpdateInsert(user, con, testEngineerDetailsDbRecord, out errorMessage);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns all channels matching input criteria
|
|
/// </summary>
|
|
/// <param name="name">Name in the LabratoryDetails table</param>
|
|
/// <param name="testEngineerDetailsDbRecords">output all matching records</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public static ulong TestEngineerDetailsGet(string name, out ITestEngineerDetailsDbRecord[] testEngineerDetailsDbRecords)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.TestEngineerDetails.TestEngineerDetailsGet(user, con, name, out testEngineerDetailsDbRecords);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes an entry in the TestEngineerDetails table
|
|
/// </summary>
|
|
/// <param name="id">Id in the TestEngineerDetails table</param>
|
|
/// <param name="errorMessage">Error string returned, possibly from sp_TestEngineerDetailsDelete</param>
|
|
/// <returns></returns>
|
|
public static ulong TestEngineerDetailsDelete(string name, out string errorMessage)
|
|
{
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var user = CurrentUserDbRecord;
|
|
return DbAPI.DbAPI.TestEngineerDetails.TestEngineerDetailsDelete(user, con, name, out errorMessage);
|
|
}
|
|
|
|
/// <summary>
|
|
/// updated whenever current user is changed, this is the recorder of the user in terms of the database information on
|
|
/// the user, used for database calls
|
|
/// </summary>
|
|
public static IUserDbRecord CurrentUserDbRecord { get; set; } = null;
|
|
public static bool LoginUserHash(string user, string hash, out IUserDbRecord iUser)
|
|
{
|
|
iUser = null;
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var hResult = DbAPI.DbAPI.Connections.LoginUserHash(con, user, hash, out var userObject);
|
|
if (0 == hResult)
|
|
{
|
|
iUser = userObject;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
public static bool LoginUser(string user, string password, out IUserDbRecord iUser)
|
|
{
|
|
iUser = null;
|
|
var con = DbAPI.DbAPI.Connections.GetActiveConnections()[0];
|
|
var hResult = DbAPI.DbAPI.Connections.LoginUser(con, user, password, out var userObject);
|
|
if (0 == hResult)
|
|
{
|
|
iUser = userObject;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
public static string BEGIN_STATEMENT => _usingMSSQL ? "BEGIN TRAN;" : "BEGIN;";
|
|
|
|
public static string COMMIT_STATEMENT => _usingMSSQL ? "COMMIT TRAN;" : "COMMIT;";
|
|
|
|
public int DbVersion { get; set; } = -1;
|
|
|
|
public class NoDBAccessException : Exception
|
|
{
|
|
public NoDBAccessException(Exception ex)
|
|
: base(ex.Message, ex)
|
|
{
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// inserts a sensor history record into the db
|
|
/// </summary>
|
|
/// <param name="testHistoryId"></param>
|
|
/// <param name="sensorId"></param>
|
|
/// <param name="serialNumber"></param>
|
|
/// <param name="capacity"></param>
|
|
/// <param name="range"></param>
|
|
/// <param name="calibrationDate"></param>
|
|
/// <param name="hardwareChannelName"></param>
|
|
/// <param name="isoChannelName"></param>
|
|
/// <param name="isoCode"></param>
|
|
/// <param name="userChannelName"></param>
|
|
/// <param name="userCode"></param>
|
|
/// <param name="sensitivity"></param>
|
|
/// <param name="filterclass"></param>
|
|
/// <param name="isProportional"></param>
|
|
/// <param name="linearizationFormula"></param>
|
|
/// <param name="eid"></param>
|
|
/// <param name="measuredExcitation"></param>
|
|
/// <param name="measurementUnit"></param>
|
|
/// <param name="samplesPerSecond"></param>
|
|
/// <param name="aaf"></param>
|
|
/// <returns></returns>
|
|
public static int InsertArmEventSensorRecord(
|
|
long testHistoryId,
|
|
int sensorId,
|
|
string serialNumber,
|
|
double? capacity,
|
|
double? range,
|
|
DateTime? calibrationDate,
|
|
string hardwareChannelName,
|
|
string isoChannelName,
|
|
string isoCode,
|
|
string userChannelName,
|
|
string userCode,
|
|
string sensitivity,
|
|
string filterclass,
|
|
bool isProportional,
|
|
string linearizationFormula,
|
|
string eid,
|
|
double? measuredExcitation,
|
|
string measurementUnit,
|
|
int samplesPerSecond,
|
|
int aaf)
|
|
{
|
|
using (var cmd = GetSQLCommand(true))
|
|
{
|
|
try
|
|
{
|
|
cmd.CommandType = CommandType.StoredProcedure;
|
|
cmd.CommandText = "sp_SensorTestHistoryUpdateInsert";
|
|
cmd.Parameters.Add(new SqlParameter("@SensorTestHistoryId", SqlDbType.BigInt) { Value = DBNull.Value });
|
|
cmd.Parameters.Add(new SqlParameter("@TestHistoryId", SqlDbType.BigInt) { Value = testHistoryId });
|
|
cmd.Parameters.Add(new SqlParameter("@SensorId", SqlDbType.Int) { Value = sensorId });
|
|
cmd.Parameters.Add(new SqlParameter("@SerialNumber", SqlDbType.NVarChar) { Value = serialNumber });
|
|
if (null != capacity)
|
|
{
|
|
cmd.Parameters.Add(new SqlParameter("@Capacity", SqlDbType.Float) { Value = (double)capacity });
|
|
}
|
|
|
|
if (null != range)
|
|
{
|
|
cmd.Parameters.Add(new SqlParameter("@Range", SqlDbType.Float) { Value = (double)range });
|
|
}
|
|
|
|
if (null != calibrationDate)
|
|
{
|
|
cmd.Parameters.Add(new SqlParameter("@CalibrationDate", SqlDbType.DateTime)
|
|
{ Value = (DateTime)calibrationDate });
|
|
}
|
|
|
|
cmd.Parameters.Add(new SqlParameter("@HardwareChannelName", SqlDbType.NVarChar)
|
|
{ Value = hardwareChannelName });
|
|
cmd.Parameters.Add(new SqlParameter("@ISOChannelName", SqlDbType.NVarChar)
|
|
{ Value = isoChannelName });
|
|
cmd.Parameters.Add(new SqlParameter("@ISOCode", SqlDbType.NVarChar, 50) { Value = isoCode });
|
|
cmd.Parameters.Add(new SqlParameter("@UserChannelName", SqlDbType.NVarChar)
|
|
{ Value = userChannelName });
|
|
cmd.Parameters.Add(new SqlParameter("@UserCode", SqlDbType.NVarChar, 50) { Value = userCode });
|
|
cmd.Parameters.Add(new SqlParameter("@Sensitivity", SqlDbType.NVarChar) { Value = sensitivity });
|
|
cmd.Parameters.Add(new SqlParameter("@FilterClass", SqlDbType.NVarChar, 50) { Value = filterclass });
|
|
cmd.Parameters.Add(new SqlParameter("@IsProportional", SqlDbType.Bit) { Value = isProportional });
|
|
cmd.Parameters.Add(new SqlParameter("@LinearizationFormula", SqlDbType.NVarChar)
|
|
{ Value = linearizationFormula });
|
|
cmd.Parameters.Add(new SqlParameter("@EID", SqlDbType.NVarChar, 50) { Value = eid });
|
|
if (null != measuredExcitation)
|
|
{
|
|
cmd.Parameters.Add(new SqlParameter("@MeasuredExcitation", SqlDbType.Decimal)
|
|
{ Value = (double)measuredExcitation });
|
|
}
|
|
|
|
cmd.Parameters.Add(new SqlParameter("@MeasurementUnit", SqlDbType.NVarChar)
|
|
{ Value = measurementUnit });
|
|
cmd.Parameters.Add(new SqlParameter("@SamplesPerSecond", SqlDbType.Int) { Value = samplesPerSecond });
|
|
cmd.Parameters.Add(new SqlParameter("@AAF", SqlDbType.Int) { Value = aaf });
|
|
|
|
var newId = new SqlParameter("@new_id", SqlDbType.Int) { Direction = ParameterDirection.Output };
|
|
cmd.Parameters.Add(newId);
|
|
|
|
var errorNumber = new SqlParameter("@errorNumber", SqlDbType.Int) { Direction = ParameterDirection.Output };
|
|
cmd.Parameters.Add(errorNumber);
|
|
|
|
var errorMessage = new SqlParameter("@errorMessage", SqlDbType.NVarChar, 250)
|
|
{ Direction = ParameterDirection.Output };
|
|
cmd.Parameters.Add(errorMessage);
|
|
|
|
cmd.ExecuteNonQuery();
|
|
if (!DBNull.Value.Equals(errorNumber.Value))
|
|
{
|
|
var error = Convert.ToInt32(errorNumber.Value);
|
|
if (0 != error)
|
|
{
|
|
throw new Exception(
|
|
$"Failed to insert sensor record - errorcode: {error} - msg: {(string)errorNumber.Value}");
|
|
}
|
|
}
|
|
return Convert.ToInt32(newId.Value);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
APILogger.Log(ex);
|
|
return -1;
|
|
}
|
|
finally
|
|
{
|
|
cmd.Connection.Dispose();
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Inserts a test history record into the db
|
|
/// </summary>
|
|
/// <param name="testSetupId"></param>
|
|
/// <param name="testName"></param>
|
|
/// <param name="testDescription"></param>
|
|
/// <param name="testId"></param>
|
|
/// <param name="destructive"></param>
|
|
/// <param name="armTime"></param>
|
|
/// <param name="testSetupXml"></param>
|
|
/// <param name="storeTestSetup">whether to store the test setup in a compressed form in the db</param>
|
|
/// <returns></returns>
|
|
public static int InsertArmEvent(
|
|
int testSetupId,
|
|
string testName,
|
|
string testDescription,
|
|
string testId,
|
|
bool destructive,
|
|
DateTime armTime,
|
|
string testSetupXml,
|
|
bool storeTestSetup
|
|
)
|
|
{
|
|
using (var cmd = GetSQLCommand(true))
|
|
{
|
|
try
|
|
{
|
|
cmd.CommandType = CommandType.StoredProcedure;
|
|
cmd.CommandText = "sp_TestHistoryUpdateInsert";
|
|
cmd.Parameters.Add(new SqlParameter("@TestHistoryId", SqlDbType.BigInt) { Value = DBNull.Value });
|
|
cmd.Parameters.Add(new SqlParameter("@TestSetupId", SqlDbType.Int) { Value = testSetupId });
|
|
cmd.Parameters.Add(new SqlParameter("@TestSetupName", SqlDbType.NVarChar, 50) { Value = testName });
|
|
cmd.Parameters.Add(new SqlParameter("@TestSetupDescription", SqlDbType.NVarChar, 50)
|
|
{ Value = testDescription });
|
|
cmd.Parameters.Add(new SqlParameter("@TestId", SqlDbType.NVarChar) { Value = testId });
|
|
cmd.Parameters.Add(new SqlParameter("@Destructive", SqlDbType.Bit) { Value = destructive });
|
|
cmd.Parameters.Add(new SqlParameter("@ArmTime", SqlDbType.DateTime) { Value = armTime });
|
|
if (string.IsNullOrWhiteSpace(testSetupXml) || !storeTestSetup)
|
|
{
|
|
cmd.Parameters.Add(new SqlParameter("@TestSetup", SqlDbType.Binary) { Value = DBNull.Value });
|
|
}
|
|
else
|
|
{
|
|
cmd.Parameters.Add(new SqlParameter("@TestSetup", SqlDbType.Binary)
|
|
{ Value = GetSetupBytes(testSetupXml) });
|
|
}
|
|
|
|
var newId = new SqlParameter("@new_id", SqlDbType.Int) { Direction = ParameterDirection.Output };
|
|
cmd.Parameters.Add(newId);
|
|
|
|
var errorNumber = new SqlParameter("@errorNumber", SqlDbType.Int)
|
|
{ Direction = ParameterDirection.Output };
|
|
cmd.Parameters.Add(errorNumber);
|
|
|
|
var errorMessage = new SqlParameter("@errorMessage", SqlDbType.NVarChar, 250) { Direction = ParameterDirection.Output };
|
|
cmd.Parameters.Add(errorMessage);
|
|
|
|
cmd.ExecuteNonQuery();
|
|
if (!DBNull.Value.Equals(errorNumber.Value))
|
|
{
|
|
var error = Convert.ToInt32(errorNumber.Value);
|
|
if (0 != error)
|
|
{
|
|
throw new Exception($"InsertArmEvent failed - errorcode: {error} msg: {errorMessage.Value}");
|
|
}
|
|
}
|
|
return Convert.ToInt32(newId.Value);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
APILogger.Log("Failed to InsertArmEvent", ex);
|
|
var eventAggregator = ContainerLocator.Container.Resolve<IEventAggregator>();
|
|
eventAggregator.GetEvent<PageErrorEvent>()
|
|
.Publish(new PageErrorArg(new[] { DTS.Common.Strings.Strings.Warning_FailedEventInsert }, null));
|
|
return -1;
|
|
}
|
|
finally
|
|
{
|
|
cmd.Connection.Dispose();
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// compresses a string using gzip and utf-8
|
|
/// </summary>
|
|
/// <param name="xml"></param>
|
|
/// <returns></returns>
|
|
private static byte[] GetSetupBytes(string xml)
|
|
{
|
|
var bytes = new byte[0];
|
|
using (var s = new MemoryStream())
|
|
{
|
|
using (var gs = new GZipStream(s, CompressionMode.Compress, false))
|
|
{
|
|
var localbytes = Encoding.UTF8.GetBytes(xml);
|
|
gs.Write(localbytes, 0, localbytes.Length);
|
|
}
|
|
s.Flush();
|
|
bytes = s.ToArray();
|
|
}
|
|
return bytes;
|
|
}
|
|
|
|
/// <summary>
|
|
/// current db version in this code base
|
|
/// </summary>
|
|
public const int CURRENT_DB_VERSION = 100;
|
|
public const int MINIMUM_LTS_DB_VERSION = 91;
|
|
|
|
/// <summary>
|
|
/// inserts all known ui items into the UIItems table, as part of migration step 52 to 53
|
|
/// </summary>
|
|
/// <param name="items"></param>
|
|
private void InsertIUIItems(string[] items)
|
|
{
|
|
using (var cmd = GetCommand())
|
|
{
|
|
var sb = new StringBuilder();
|
|
sb.Append(BEGIN_STATEMENT);
|
|
|
|
for (var i = 0; i < items.Length; i++)
|
|
{
|
|
sb.AppendFormat("INSERT INTO UIItems ([NAME]) VALUES (@{0}_1);", i);
|
|
CreateParam(cmd, $"@{i}_1", SqlDbType.NVarChar, items[i]);
|
|
}
|
|
|
|
sb.Append(COMMIT_STATEMENT);
|
|
cmd.CommandText = sb.ToString();
|
|
//Connection.ExecuteCommand(cmd);
|
|
if (_usingMSSQL)
|
|
{
|
|
ExecuteCommand(cmd);
|
|
}
|
|
else
|
|
{
|
|
ExecuteSQLiteCommand(cmd);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// looks up all the UIitems in the db, used in migration 52 to 53
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private static Dictionary<string, long> GetPermissionsLookup()
|
|
{
|
|
var lookup = new Dictionary<string, long>();
|
|
using (var cmd = GetCommand())
|
|
{
|
|
cmd.CommandText = "SELECT [ID],[NAME] FROM [UIItems]";
|
|
using (var ds = Connection.QueryDataSet(cmd))
|
|
{
|
|
foreach (DataRow row in ds.Tables[0].Rows)
|
|
{
|
|
var id = Convert.ToInt64(row["ID"]);
|
|
var name = Convert.ToString(row["NAME"]);
|
|
lookup[name] = id;
|
|
}
|
|
}
|
|
}
|
|
|
|
return lookup;
|
|
}
|
|
|
|
private enum SensUnits
|
|
{
|
|
NONE,
|
|
mV,
|
|
mVperV,
|
|
mVperVperEU,
|
|
mVperEU
|
|
}
|
|
|
|
private void UpdateCalibrationRecords()
|
|
{
|
|
using (var cmdQRY = GetSQLCommand())
|
|
{
|
|
//Get all Calibrations
|
|
cmdQRY.CommandText = "SELECT * from tblSensorCalibrations";
|
|
using (var ds = QueryDataSet(cmdQRY))
|
|
{
|
|
foreach (DataRow dr in ds.Tables[0].Rows)
|
|
{
|
|
var serialNumber =
|
|
Convert.ToString(dr[SensorDB.SensorCalibrationFields.SerialNumber.ToString()]);
|
|
var calDate =
|
|
Convert.ToDateTime(dr[SensorDB.SensorCalibrationFields.CalibrationDate.ToString()]);
|
|
var modifyDate = Convert.ToDateTime(dr[SensorDB.SensorCalibrationFields.ModifyDate.ToString()]);
|
|
var proportionalToExcitation =
|
|
Convert.ToBoolean(dr[SensorDB.SensorCalibrationFields.IsProportional.ToString()]);
|
|
var nonLinear = Convert.ToBoolean(dr[SensorDB.SensorCalibrationFields.NonLinear.ToString()]);
|
|
var cr = Convert.ToString(dr[SensorDB.SensorCalibrationFields.CalibrationRecords.ToString()]);
|
|
|
|
var excitationRecords = cr.Split(new string[] { "__x__" }, StringSplitOptions.None);
|
|
for (var i = 0; i < excitationRecords.Length; i++)
|
|
{
|
|
// split record into fields
|
|
var calRecordFields = excitationRecords[i].Split(',');
|
|
|
|
//Set AtCapacity to False
|
|
calRecordFields[(int)SensorDB.SensorCalibrationRecordFields.AtCapacity] = "False";
|
|
|
|
//Stuff back in record string
|
|
excitationRecords[i] = string.Join(",", calRecordFields);
|
|
|
|
// Build SensitivityUnits string
|
|
var sbSensitivityUnits = new StringBuilder();
|
|
sbSensitivityUnits.Append(",");
|
|
|
|
if (nonLinear)
|
|
{
|
|
// If we are nonlinear leave units blank
|
|
sbSensitivityUnits.Append(SensUnits.NONE);
|
|
}
|
|
else
|
|
{
|
|
// Always Append per EU because we have no "AtCapacity" Sensors, yet.
|
|
sbSensitivityUnits.Append(proportionalToExcitation
|
|
? SensUnits.mVperVperEU
|
|
: SensUnits.mVperEU);
|
|
}
|
|
|
|
//Add new SensitivityUnits CalibrationRecord Setting
|
|
excitationRecords[i] = string.Concat(excitationRecords[i], sbSensitivityUnits.ToString());
|
|
}
|
|
|
|
cr = string.Join("__x__", excitationRecords);
|
|
// execute change
|
|
using (var cmdEX = GetSQLCommand())
|
|
{
|
|
cmdEX.CommandText =
|
|
"UPDATE tblSensorCalibrations SET CalibrationRecords=@CalibrationRecords where SerialNumber=@SerialNumber AND CalibrationDate=@CalibrationDate AND ModifyDate=@ModifyDate";
|
|
CreateParam(cmdEX, "@SerialNumber", SqlDbType.NVarChar, serialNumber);
|
|
CreateParam(cmdEX, "@CalibrationDate", SqlDbType.DateTime, calDate);
|
|
CreateParam(cmdEX, "@ModifyDate", SqlDbType.DateTime, modifyDate);
|
|
CreateParam(cmdEX, "@CalibrationRecords", SqlDbType.NVarChar, cr);
|
|
ExecuteCommand(cmdEX);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void InsertIntoSqlVersionsTable(SqlCommand cmd, int version, int step)
|
|
{
|
|
try
|
|
{
|
|
cmd.CommandType = CommandType.StoredProcedure;
|
|
cmd.CommandText = DbOperationsEnum.StoredProcedure.sp_DbVersionInsert.ToString();
|
|
|
|
#region params
|
|
|
|
cmd.Parameters.Add(new SqlParameter("@Version", SqlDbType.Int) { Value = version });
|
|
cmd.Parameters.Add(new SqlParameter("@Step", SqlDbType.Int) { Value = step });
|
|
cmd.Parameters.Add(new SqlParameter("@Date", SqlDbType.DateTime) { Value = DateTime.Now });
|
|
cmd.Parameters.Add(new SqlParameter("@Remarks", SqlDbType.NVarChar, 255) { Value = string.Empty });
|
|
cmd.Parameters.Add(new SqlParameter("@UserField", SqlDbType.NVarChar, 255) { Value = "SYSTEM" });
|
|
|
|
cmd.Parameters.Add(new SqlParameter("@new_id", SqlDbType.Int) { Direction = ParameterDirection.Output });
|
|
cmd.Parameters.Add(
|
|
new SqlParameter("@errorNumber", SqlDbType.Int) { Direction = ParameterDirection.Output });
|
|
cmd.Parameters.Add(
|
|
new SqlParameter("@errorMessage", SqlDbType.NVarChar, 250) { Direction = ParameterDirection.Output });
|
|
|
|
#endregion params
|
|
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception(ex.Message, ex);
|
|
}
|
|
}
|
|
|
|
public class DbTypeAttr : Attribute
|
|
{
|
|
public string DbType { get; }
|
|
|
|
internal DbTypeAttr(string attr)
|
|
{
|
|
DbType = attr;
|
|
}
|
|
|
|
public static string GetDbType(object o)
|
|
{
|
|
var mi = o?.GetType().GetMember(o.ToString());
|
|
if (mi != null && mi.Length > 0)
|
|
{
|
|
if (GetCustomAttribute(mi[0], typeof(DbTypeAttr)) is DbTypeAttr attr)
|
|
{
|
|
return attr.DbType;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static bool
|
|
_usingCentralizedDB =
|
|
true; //True means using a remote, centralized server; False means using a local SqlLocalDb
|
|
|
|
public static bool _usingMSSQL = true;
|
|
public static bool _usingNTLMAuthentication = false;
|
|
public static string _previousDir = string.Empty;
|
|
|
|
|
|
|
|
public static /*System.Data.IDbDataParameter*/
|
|
void CreateParam(IDbCommand icmd, string name, SqlDbType type, object value)
|
|
{
|
|
switch (type)
|
|
{
|
|
case SqlDbType.DateTime:
|
|
if (null == value)
|
|
{
|
|
value = (DateTime)System.Data.SqlTypes.SqlDateTime.MinValue;
|
|
}
|
|
else if (value is DateTime)
|
|
{
|
|
if (((DateTime)value) < (DateTime)System.Data.SqlTypes.SqlDateTime.MinValue)
|
|
{
|
|
value = (DateTime)System.Data.SqlTypes.SqlDateTime.MinValue;
|
|
}
|
|
|
|
//value = string.Format("{0:yyyy-MM-dd}", ((DateTime)value)) + " " + ((DateTime)value).ToLongTimeString();
|
|
value = $"{(DateTime)value:yyyy-MM-dd} {(DateTime)value:HH:mm:ss}";
|
|
}
|
|
|
|
break;
|
|
case SqlDbType.NVarChar:
|
|
|
|
break;
|
|
}
|
|
|
|
if (_usingMSSQL)
|
|
{
|
|
var param = new SqlParameter(name, type);
|
|
param.Value = value;
|
|
icmd.Parameters.Add(param);
|
|
}
|
|
else
|
|
{
|
|
var cmd = icmd as System.Data.SQLite.SQLiteCommand;
|
|
cmd.Parameters.AddWithValue(name, value);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// returns a DbCommand interface to the DASFactory db
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static IDbCommand GetDASFactoryCommand()
|
|
{
|
|
var cmd = new SqlCommand();
|
|
cmd.Connection = new SqlConnection(Connection.GetLocalDASFactoryConnectionString());
|
|
cmd.Connection.Open();
|
|
|
|
return cmd;
|
|
}
|
|
public static IDbCommand GetISOCommand()
|
|
{
|
|
var cmd = new SqlCommand();
|
|
|
|
cmd.Connection = new SqlConnection(Connection.GetLocalISOConnectionString());
|
|
cmd.Connection.Open();
|
|
|
|
return cmd;
|
|
}
|
|
|
|
public static IDbCommand GetCommand()
|
|
{
|
|
if (_usingMSSQL) { return GetSQLCommand(); }
|
|
return new System.Data.SQLite.SQLiteCommand();
|
|
}
|
|
|
|
public static IDbCommand GetSQLiteCommand()
|
|
{
|
|
return new System.Data.SQLite.SQLiteCommand();
|
|
}
|
|
public static SqlCommand _cmd { get; set; } = null;
|
|
|
|
public static SqlCommand GetSQLCommand()
|
|
{
|
|
return GetSQLCommand(true);
|
|
}
|
|
|
|
public static SqlCommand GetSQLCommand(bool newCommand)
|
|
{
|
|
if (_cmd == null) { _cmd = new SqlCommand(); }
|
|
var currentCmd = _cmd;
|
|
if (newCommand) { currentCmd = new SqlCommand(); }
|
|
|
|
if (currentCmd.Connection == null || currentCmd.Connection.State != ConnectionState.Open)
|
|
{
|
|
currentCmd.Connection = new SqlConnection(Connection.GetLocalConnectionString());
|
|
try
|
|
{
|
|
//FB16165: don't crash when SQL connection drops
|
|
currentCmd.Connection.Open();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
APILogger.Log("Could not open db connection", ex);
|
|
}
|
|
}
|
|
|
|
currentCmd.Parameters.Clear();
|
|
return currentCmd;
|
|
}
|
|
public static IDbCommand GetSQLOnlyCommand()
|
|
{
|
|
return new SqlCommand();
|
|
}
|
|
private string _localConnection = null;
|
|
|
|
public void ResetLocalConnectionString()
|
|
{
|
|
lock (dbLock)
|
|
{
|
|
_localConnection = null;
|
|
//http://manuscript.dts.local/f/cases/39203/Local-mode-interacts-with-central-db
|
|
//we are switching from local to remote or vice versa, clear any existing connections
|
|
//we want to make sure we dont' use them
|
|
DbAPI.DbAPI.Connections.ClearConnections();
|
|
Server = "";
|
|
}
|
|
}
|
|
public void ResetLocalConnectionStringDb(string dbName)
|
|
{
|
|
lock (dbLock)
|
|
{
|
|
_localConnection = null;
|
|
DBName = dbName;
|
|
}
|
|
}
|
|
private string _localDASFactoryConnection = null;
|
|
|
|
public string GetLocalDASFactoryConnectionString()
|
|
{
|
|
lock (dbLock)
|
|
{
|
|
if (null != _localDASFactoryConnection) return _localDASFactoryConnection;
|
|
_localDASFactoryConnection = _usingNTLMAuthentication
|
|
? $"Server={Server};Database=DASFactory;Trusted_Connection=TRUE;"
|
|
: $"Server={Server};Database=DASFactory;User Id={Username};Password={Password};";
|
|
}
|
|
return _localDASFactoryConnection;
|
|
}
|
|
private string _localISOConnection = null;
|
|
public string GetLocalISOConnectionString()
|
|
{
|
|
lock (dbLock)
|
|
{
|
|
if (null != _localISOConnection) return _localISOConnection;
|
|
_localISOConnection = _usingNTLMAuthentication
|
|
? $"Server={Server};Database=ISO;Trusted_Connection=TRUE;"
|
|
: $"Server={Server};Database=ISO;User Id={Username};Password={Password};";
|
|
}
|
|
return _localISOConnection;
|
|
}
|
|
public string GetLocalConnectionString()
|
|
{
|
|
lock (dbLock)
|
|
{
|
|
if (null != _localConnection) return _localConnection;
|
|
if (null == Server && !_bCS3) { throw new Exception("db connection not initialized"); }
|
|
if (_bCS3)
|
|
{
|
|
_localConnection = CS3UsingNTLMAuthentication ? $"Server={CS3Host};Database={CS3Name};Trusted_Connection=TRUE;"
|
|
: $"Server={CS3Host};Database={CS3Name};User Id={CS3User};Password={CS3Password}";
|
|
}
|
|
else
|
|
{
|
|
_localConnection = _usingNTLMAuthentication
|
|
? $"Server={Server};Database={DBName};Trusted_Connection=TRUE;"
|
|
: $"Server={Server};Database={DBName};User Id={Username};Password={Password};";
|
|
}
|
|
}
|
|
return _localConnection;
|
|
}
|
|
|
|
#region CS3 STUFF
|
|
public bool CS3UsingNTLMAuthentication { get; set; } = false;
|
|
|
|
public string CS3Host { get; set; } = null;
|
|
|
|
public string CS3Name { get; set; } = null;
|
|
|
|
public string CS3User { get; set; } = null;
|
|
|
|
public string CS3Password { get; set; } = null;
|
|
#endregion CS3 STUFF
|
|
|
|
public string Server { get; set; } = null;
|
|
|
|
public string DBName { get; set; } = null;
|
|
|
|
public string Username { get; set; } = "";
|
|
|
|
public string Password { get; set; }
|
|
|
|
private static bool _lastConnectionStatus = false;
|
|
public static bool LastConnectionStatus
|
|
{
|
|
get { lock (dbLock) { return _lastConnectionStatus; } }
|
|
set
|
|
{
|
|
lock (dbLock) { _lastConnectionStatus = value; }
|
|
ConnectionStatusChanged?.Invoke(value);
|
|
}
|
|
}
|
|
|
|
public delegate void ConnectionStatusChangedDelegate(bool connected);
|
|
|
|
private static ConnectionStatusChangedDelegate _ConnectionStatusChange;
|
|
public static ConnectionStatusChangedDelegate ConnectionStatusChanged
|
|
{
|
|
get { lock (dbLock) { return _ConnectionStatusChange; } }
|
|
set { lock (dbLock) { _ConnectionStatusChange = value; } }
|
|
}
|
|
|
|
private static DbOperations _dbOperations = null;
|
|
private static DbOperations _dbOperationsCS3 = null;
|
|
private static readonly object dbLock = new object();
|
|
public static DbOperations Connection
|
|
{
|
|
get
|
|
{
|
|
lock (dbLock)
|
|
{
|
|
if (null == _dbOperations) { _dbOperations = new DbOperations(); }
|
|
}
|
|
return _dbOperations;
|
|
}
|
|
}
|
|
|
|
public static DbOperations Cs3Connection
|
|
{
|
|
get
|
|
{
|
|
lock (dbLock)
|
|
{
|
|
if (null == _dbOperationsCS3) { _dbOperationsCS3 = new DbOperations(true); }
|
|
}
|
|
return _dbOperationsCS3;
|
|
}
|
|
}
|
|
private readonly bool _bCS3 = false;
|
|
protected DbOperations(bool bCs3)
|
|
{
|
|
_bCS3 = true;
|
|
}
|
|
protected DbOperations() { }
|
|
|
|
public void StartSynchronizationThread()
|
|
{
|
|
if (null != _syncThread)
|
|
{
|
|
StopSynchronizationThread();
|
|
}
|
|
_stopHandle = new ManualResetEvent(false);
|
|
_syncThread = new Thread(new ThreadStart(SyncThread));
|
|
_syncThread.Start();
|
|
}
|
|
private ManualResetEvent _stopHandle;
|
|
private Thread _syncThread = null;
|
|
private readonly int msWaitTime = 100;
|
|
private readonly int waitsBetweenSync = 6000;
|
|
|
|
private void SyncThread()
|
|
{
|
|
var currentWait = waitsBetweenSync;
|
|
while (!_stopHandle.WaitOne(msWaitTime, false))
|
|
{
|
|
|
|
if (++currentWait >= waitsBetweenSync)
|
|
{
|
|
try
|
|
{
|
|
//Synchronize();
|
|
}
|
|
catch (Exception ex) { APILogger.Log(ex); }
|
|
|
|
currentWait = 0;
|
|
}
|
|
}
|
|
_syncThread = null;
|
|
}
|
|
public void StopSynchronizationThread()
|
|
{
|
|
if (null != _syncThread)
|
|
{
|
|
while (null != _syncThread)
|
|
{
|
|
_stopHandle.Set();
|
|
Thread.Sleep(100);
|
|
}
|
|
_stopHandle = null;
|
|
}
|
|
}
|
|
public const string NoConnection = "NoConnection";
|
|
|
|
|
|
public DataSet QueryDataSet(SqlCommand icmd)
|
|
{
|
|
try { Log(icmd); }
|
|
catch (Exception ex) { APILogger.Log(ex); }
|
|
|
|
if (icmd == null) return null;
|
|
using (var adapter = new SqlDataAdapter(icmd))
|
|
{
|
|
var ds = new DataSet();
|
|
try
|
|
{
|
|
adapter.Fill(ds);
|
|
return ds;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
Log(icmd, true);
|
|
throw;
|
|
}
|
|
}
|
|
// }
|
|
}
|
|
public DataSet QueryDataSet(IDbCommand icmd)
|
|
{
|
|
try { Log(icmd); }
|
|
catch (Exception ex) { APILogger.Log(ex); }
|
|
|
|
if (_usingMSSQL)
|
|
{
|
|
var cmd = icmd as SqlCommand;
|
|
|
|
if (icmd == null) return null;
|
|
using (var adapter = new SqlDataAdapter(cmd))
|
|
{
|
|
var ds = new DataSet();
|
|
try
|
|
{
|
|
adapter.Fill(ds);
|
|
return ds;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
Log(icmd, true);
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
var fullDbConnectionPath = System.IO.Path.Combine(_previousDir, "db", "datapro.db");
|
|
using (var c = new System.Data.SQLite.SQLiteConnection($"Data Source={fullDbConnectionPath}"))
|
|
{
|
|
if (c.State == ConnectionState.Closed)
|
|
{
|
|
try
|
|
{
|
|
c.Open();
|
|
LastConnectionStatus = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LastConnectionStatus = false;
|
|
throw new SystemException(NoConnection, ex);
|
|
}
|
|
}
|
|
|
|
var cmd = icmd as System.Data.SQLite.SQLiteCommand;
|
|
cmd.Connection = c;
|
|
using (var adapter = new System.Data.SQLite.SQLiteDataAdapter(cmd))
|
|
{
|
|
var ds = new DataSet();
|
|
try
|
|
{
|
|
adapter.Fill(ds);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log(icmd, true);
|
|
APILogger.Log("Failed to read db", ex);
|
|
}
|
|
return ds;
|
|
}
|
|
}
|
|
}
|
|
private string _dbConnection = System.IO.Path.Combine("db", "datapro.db");
|
|
/// <summary>
|
|
/// Used by DBTypeChoice in the InstallerCustomActions to migrate old SQLite databases
|
|
/// </summary>
|
|
/// <param name="icmd"></param>
|
|
/// <param name="previousDir"></param>
|
|
/// <param name="standAlone"></param>
|
|
/// <param name="dbName"></param>
|
|
/// <returns></returns>
|
|
public DataSet QueryDataSet(IDbCommand icmd, string previousDir, bool standAlone, string dbName)
|
|
{
|
|
if (standAlone)
|
|
{
|
|
_dbConnection = dbName;
|
|
}
|
|
var fullDbConnectionPath = System.IO.Path.Combine(previousDir, _dbConnection);
|
|
using (var c = new System.Data.SQLite.SQLiteConnection($"Data Source={fullDbConnectionPath}"))
|
|
{
|
|
if (c.State == ConnectionState.Closed)
|
|
{
|
|
try
|
|
{
|
|
c.Open();
|
|
LastConnectionStatus = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LastConnectionStatus = false;
|
|
throw new SystemException(NoConnection, ex);
|
|
}
|
|
}
|
|
|
|
var cmd = icmd as System.Data.SQLite.SQLiteCommand;
|
|
cmd.Connection = c;
|
|
using (var adapter = new System.Data.SQLite.SQLiteDataAdapter(cmd))
|
|
{
|
|
var ds = new DataSet();
|
|
try
|
|
{
|
|
adapter.Fill(ds);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log(icmd, true);
|
|
APILogger.Log("Failed to read db", ex);
|
|
}
|
|
return ds;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public int ExecuteSQLiteCommand(IDbCommand icmd)
|
|
{
|
|
var fullDbConnectionPath = System.IO.Path.Combine(_previousDir, _dbConnection);
|
|
using (var c = new System.Data.SQLite.SQLiteConnection($"Data Source={fullDbConnectionPath}"))
|
|
{
|
|
if (c.State == ConnectionState.Closed)
|
|
{
|
|
try
|
|
{
|
|
c.Open();
|
|
LastConnectionStatus = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LastConnectionStatus = false;
|
|
throw new SystemException(NoConnection, ex);
|
|
}
|
|
}
|
|
|
|
var cmd = icmd as System.Data.SQLite.SQLiteCommand;
|
|
cmd.Connection = c;
|
|
return cmd.ExecuteNonQuery();
|
|
}
|
|
}
|
|
public int ExecuteCommand(IDbCommand icmd)
|
|
{
|
|
try { Log(icmd); }
|
|
catch (Exception ex) { APILogger.Log(ex); }
|
|
|
|
var cmd = icmd as SqlCommand;
|
|
using (var MsSqlConnection = new SqlConnection(GetLocalConnectionString()))
|
|
{
|
|
try
|
|
{
|
|
MsSqlConnection.Open();
|
|
cmd.Connection = MsSqlConnection;
|
|
LastConnectionStatus = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LastConnectionStatus = false;
|
|
throw new SystemException(NoConnection, ex);
|
|
}
|
|
if (cmd != null)
|
|
{
|
|
cmd.Connection = MsSqlConnection;
|
|
}
|
|
try
|
|
{
|
|
return cmd.ExecuteNonQuery();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
Log(cmd, true);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
private volatile bool _bLoggingOn = false;
|
|
public void SetTransactionLogging(bool bLog)
|
|
{
|
|
lock (LogLock)
|
|
{
|
|
LogName = $"SQL_{DateTime.Now.Year:0000}_{DateTime.Now.Month:00}_{DateTime.Now.Day:00}.log";
|
|
}
|
|
_bLoggingOn = bLog;
|
|
}
|
|
|
|
private static readonly object LogLock = new object();
|
|
private string LogName;
|
|
private void Log(IDbCommand cmd, bool exception = false)
|
|
{
|
|
if (!_bLoggingOn && !exception) { return; }
|
|
|
|
var sb = new StringBuilder();
|
|
sb.AppendFormat("{1:u} COMMAND_TEXT: {0}\r\n", cmd.CommandText, DateTime.Now);
|
|
|
|
foreach (IDbDataParameter p in cmd.Parameters)
|
|
{
|
|
sb.AppendFormat("Param: {0}\r\n", p.ParameterName);
|
|
sb.AppendFormat("Value: {0}\r\n", p.Value?.ToString() ?? string.Empty);
|
|
}
|
|
if (exception)
|
|
{
|
|
try
|
|
{
|
|
APILogger.Log("SQLException:\r\n", sb.ToString());
|
|
}
|
|
catch (Exception ex) { APILogger.Log(ex); }
|
|
}
|
|
else
|
|
{
|
|
lock (LogLock)
|
|
{
|
|
System.IO.File.AppendAllText(LogName, sb.ToString());
|
|
}
|
|
}
|
|
}
|
|
public static void SaveChannelSettingDefaults(IChannelSetting[] channelDefaults)
|
|
{
|
|
foreach (var setting in channelDefaults)
|
|
{
|
|
_ = ChannelSettingsUpdate(setting.SettingTypeId, setting.Value);
|
|
}
|
|
}
|
|
public static IChannelSetting[] GetChannelSettingDefaults()
|
|
{
|
|
var dictionary = new Dictionary<int, Tuple<string, string>>();
|
|
var hr = ChannelSettingsGet(null, null, out var records);
|
|
if (0 == hr && null != records && records.Any())
|
|
{
|
|
foreach (var record in records)
|
|
{
|
|
dictionary[record.Id] = new Tuple<string, string>(record.SettingName, record.DefaultValue);
|
|
}
|
|
}
|
|
var list = new List<IChannelSetting>();
|
|
using (var enumSettings = dictionary.GetEnumerator())
|
|
{
|
|
while (enumSettings.MoveNext())
|
|
{
|
|
list.Add(new ChannelSettingBase(enumSettings.Current.Key, enumSettings.Current.Value.Item1, enumSettings.Current.Value.Item2));
|
|
}
|
|
}
|
|
return list.ToArray();
|
|
}
|
|
public static Dictionary<string, Tuple<int, string, string>> GetSettingsLookup()
|
|
{
|
|
var dictionary = new Dictionary<string, Tuple<int, string, string>>();
|
|
var hr = DbOperations.ChannelSettingsGet(null, null, out var records);
|
|
if (0 == hr && null != records && records.Any())
|
|
{
|
|
foreach (var record in records)
|
|
{
|
|
dictionary[record.SettingName] = new Tuple<int, string, string>(record.Id,
|
|
record.SettingName, record.DefaultValue);
|
|
}
|
|
}
|
|
return dictionary;
|
|
}
|
|
public static Dictionary<int, Tuple<string, string>> GetSettingsLookup2()
|
|
{
|
|
var dictionary = new Dictionary<int, Tuple<string, string>>();
|
|
var hr = ChannelSettingsGet(null, null, out var records);
|
|
if (0 == hr && null != records && records.Any())
|
|
{
|
|
foreach (var record in records)
|
|
{
|
|
dictionary[record.Id] = new Tuple<string, string>(
|
|
record.SettingName, record.DefaultValue);
|
|
}
|
|
}
|
|
return dictionary;
|
|
}
|
|
}
|
|
#region CustomChannelFieldSizeExtensions
|
|
public class CustomChannelFieldSizeAttribute : Attribute
|
|
{
|
|
internal CustomChannelFieldSizeAttribute(int size)
|
|
{
|
|
Size = size;
|
|
}
|
|
public int Size { get; }
|
|
}
|
|
public static class CustomChannelFieldSizeExtensions
|
|
{
|
|
public static int GetFieldSize(DbOperations.MMETables.MMEPossibleChannelsFields field)
|
|
{
|
|
return (field.GetAttribute<CustomChannelFieldSizeAttribute>()).Size;
|
|
}
|
|
|
|
}
|
|
public static class EnumExtensions
|
|
{
|
|
public static TAttribute GetAttribute<TAttribute>(this Enum value)
|
|
where TAttribute : Attribute
|
|
{
|
|
return (value.GetType()).GetField(Enum.GetName(value.GetType(), value)).GetCustomAttributes(false).OfType<TAttribute>().SingleOrDefault();
|
|
}
|
|
}
|
|
#endregion CustomChannelFieldSizeExtensions
|
|
|
|
}
|