laatzen/ServiceFwUpdate/Common/FwUpdateDb/FwUpdateDb.cs

1555 lines
64 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Logic.ProductionToProductMapper.Cordonel;
using Logic.ProductionToProductMapper.Files.Fw;
using Newtonsoft.Json;
using Xylem.Common.CommonCore.Configuration;
using Xylem.Common.Cryptology.Security;
using Xylem.Common.Hardware.WaterMeter.Genesis.GenesisPwd;
using Xylem.Common.Logic.SoftwareAccessHelper;
using Xylem.Common.Utils.DateTimeServer;
using Xylem.ServiceFwUpdate.Common.FwUpdateSafe;
namespace Xylem.ServiceFwUpdate.Common.FwUpdateDb
{
/// <summary>
/// Class for DB interfaces.
/// </summary>
[Serializable]
public class FwUpdateDb
{
/// <summary>
/// DB connection cannot be established.
/// </summary>
public Boolean DbIsConnected { get; private set; }
/// <summary>
/// Complete information of all builder operators registered in DB.
/// </summary>
public List<UserInformation> DbAllBuilderOperators { get; private set; }
/// <summary>
/// Complete information of all update operators registered in DB.
/// </summary>
public List<UserInformation> DbAllUpdateOperators { get; private set; }
/// <summary>
/// Cordonel SW package.
/// </summary>
public List<FilePart> DbFwUpdateSwPackage { get; private set; }
/// <summary>
/// Cordonel configuration files.
/// </summary>
public List<FilePart> DbFwUpdateConfigFiles { get; private set; }
/// <summary>
/// Cordonel FW packages.
/// </summary>
public List<ClusteredFile> DbCordonelFwPackages { get; private set; }
/// <summary>
/// Cordonel safes.
/// </summary>
public List<ClusteredFile> DbCordonelSafes { get; private set; }
/// <summary>
/// Complete information of all searched Cordonel customers.
/// </summary>
public List<CordonelCustomerInfosDb> DbSearchedCordonelCustomers { get; private set; }
/// <summary>
/// All production orders of Cordonels by a specific customer.
/// </summary>
public List<CordonelCustomerOrderDb> DbCordonelCustomerOrders { get; private set; }
/// <summary>
/// All serial numbers for a specific Cordonel (Customer serial number, Sensus serial number and PCB ID).
/// </summary>
public List<CordonelSerialNumbersDb> DbCordonelSerialNumbers { get; private set; }
/// <summary>
/// Complete information of fully qualified users registered in DB.
/// Only these users can be used for en- and decryption as the Pwd Hash and Hw Id
/// shall be used for this operation.
/// </summary>
public List<UserInformation> DbFullQualifiedUpdateOperators { get; private set; }
/// <summary>
/// Get the FW-Update SW validation from DB.
/// </summary>
/// <param name="programName"></param>
/// <param name="swLicense"></param>
/// <remarks date="2021-Mar-22" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
/// <remarks date="2021-Apr-06" author="Thomas Wiedebusch">
/// - timeout to 1200 ms.
/// </remarks>
/// <remarks date="2021-Apr-07" author="Thomas Wiedebusch">
/// - License assigned in DB access.
/// </remarks>
public Boolean GetSwLicenseFromDb(String programName, out SoftwareLicense swLicense)
{
swLicense = new SoftwareLicense();
try
{
var url = ServiceUrls.GetSoftwareLicenseUrl();
var requestResponse = LocalWebRequest.GetRequest(url + programName, 1200);
if (string.IsNullOrEmpty(requestResponse))
{
DbIsConnected = false;
return false;
}
DbIsConnected = true;
swLicense = JsonConvert.DeserializeObject<SoftwareLicense>(requestResponse);
return true;
}
catch (Exception)
{
DbIsConnected = false;
return false;
}
}
/// <summary>
/// Get the FW-Update SW validation from DB.
/// </summary>
/// <param name="swLicense"></param>
/// <remarks date="2019-Jun-19" author="Roland Drabesch">
/// - Initial.
/// </remarks>
/// <remarks date="2021-Apr-01" author="Thomas Wiedebusch">
/// - Modified.
/// </remarks>
/// <remarks date="2021-Apr-06" author="Thomas Wiedebusch">
/// - timeout to 1200 ms.
/// </remarks>
public Boolean CheckSwLicenseIsValidFromDb(SoftwareLicense swLicense)
{
if (swLicense == null) return false;
try
{
var url = ServiceUrls.GetSoftwareStartUpAccessUrl();
url += $"?Program={swLicense.Program}" +
$"&Major={swLicense.Major}" +
$"&Minor={swLicense.Minor}" +
$"&Build={swLicense.Build}";
var requestResponse = LocalWebRequest.GetRequest(url, 1200);
var ret = bool.Parse(requestResponse.Trim('"'));
if (string.IsNullOrEmpty(requestResponse))
{
DbIsConnected = false;
return false;
}
DbIsConnected = true;
return ret;
}
catch (Exception)
{
DbIsConnected = false;
return false;
}
}
/// <summary>
/// Set the FW-Update SW validation to DB
/// </summary>
/// <param name="swLicense"></param>
/// <remarks date="2021-Mar-22" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
/// <remarks date="2021-Mar-25" author="Roland Drabesch">
/// - Modified url string.
/// </remarks>
/// <remarks date="2021-Mar-25" author="Thomas Wiedebusch">
/// - Set DB connection state.
/// </remarks>
/// <remarks date="2021-Apr-06" author="Thomas Wiedebusch">
/// - timeout to 1200 ms.
/// </remarks>
public Boolean SetSwLicenseToDb(SoftwareLicense swLicense)
{
if (swLicense == null) return false;
try
{
var url = ServiceUrls.SetSoftwareLicenseUrl();
url += $"{swLicense.Program}&Major={swLicense.Major}&Minor={swLicense.Minor}" +
$"&Build={swLicense.Build}&ValidTo={swLicense.ValidTo:yyyy-MM-ddTHH:mm:ss}" +
$"&Description={swLicense.Description ?? "null"}&ProgramNameIsUnique={swLicense.ProgramNameIsUnique}";
var requestResponse = LocalWebRequest.PostRequestAsyncAndGetContent(url, 1200);
if (string.IsNullOrEmpty(requestResponse) && !JsonConvert.DeserializeObject<Boolean>(requestResponse))
{
DbIsConnected = false;
return false;
}
DbIsConnected = true;
return true;
}
catch (Exception)
{
DbIsConnected = false;
return false;
}
}
/// <summary>
/// Collect order number, radio address, skeleton key, password hashes and passwords from DB.
/// </summary>
/// <param name="pcbId"></param>
/// <param name="passwordContainer"></param>
/// <returns>password container</returns>
/// <remarks date="2021-Feb-01" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
/// <remarks date="2021-Feb-02" author="Thomas Wiedebusch">
/// - Returns bool.
/// </remarks>
/// <remarks date="2021-Feb-03" author="Thomas Wiedebusch">
/// - Check content of password container.
/// </remarks>
/// <remarks date="2021-Feb-04" author="Thomas Wiedebusch">
/// - Using software access helper class.
/// </remarks>
/// <remarks date="2021-Mar-22" author="Thomas Wiedebusch">
/// - Additional timeout as the password request will redirected from DB Laatzen to DB Ludwigshafen.
/// </remarks>
/// <remarks date="2021-Mar-25" author="Thomas Wiedebusch">
/// - Set DB connection state.
/// </remarks>
public Boolean DownloadCordonelPwdFromDb(String pcbId, out PasswordContainer passwordContainer)
{
passwordContainer = new PasswordContainer();
try
{
var url = ServiceUrls.GenesisGetPasswordContainerServiceUrl();
var requestResponse = LocalWebRequest.GetRequest(url + pcbId, 5000);
if (string.IsNullOrEmpty(requestResponse))
{
DbIsConnected = false;
return false;
}
DbIsConnected = true;
var meterPwdDb = JsonConvert.DeserializeObject<MeterPwdDb>(requestResponse);
passwordContainer.PcbId = pcbId;
passwordContainer.SkeletonKey =
Encoding.UTF8.GetString(meterPwdDb.ListOfPasswords[MeterPwdDb.SkeletonKeyIdx]);
passwordContainer.PasswordLvl8 =
Encoding.UTF8.GetString(meterPwdDb.ListOfPasswords[MeterPwdDb.PwdLevel8Idx]);
passwordContainer.EncryptedPasswordFile = new Byte[MeterPwdDb.PwdFileLength];
var hashList = new List<Byte>();
foreach (var hash in meterPwdDb.ListOfHashes)
{
hashList.AddRange(hash);
}
passwordContainer.EncryptedPasswordFile = hashList.ToArray();
return !string.IsNullOrEmpty(passwordContainer.SkeletonKey) &&
!string.IsNullOrEmpty(passwordContainer.PasswordLvl8) &&
passwordContainer.EncryptedPasswordFile.Length >= MeterPwdDb.PwdFileLength;
}
catch (Exception)
{
DbIsConnected = false;
return false;
}
}
/// <summary>
/// Get the update operators information from DB needed to generate the primary key for encryption.
/// This is a direct DB access. The read information from DB may be incomplete as the password hash and
/// hardware id are optional.
/// </summary>
/// <returns>true if successful</returns>
/// <remarks date="2021-Feb-04" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
/// <remarks date="2021-Feb-11" author="Thomas Wiedebusch">
/// - Refresh all DbAllUsersInfos and DbFullQualifiedUsersInfos lists.
/// </remarks>
/// <remarks date="2021-Mar-25" author="Thomas Wiedebusch">
/// - Set DB connection state.
/// </remarks>
public Boolean GetAllUpdateOperatorsFromDb()
{
if (DbAllUpdateOperators == null) DbAllUpdateOperators = new List<UserInformation>();
if (DbFullQualifiedUpdateOperators == null) DbFullQualifiedUpdateOperators = new List<UserInformation>();
DbAllUpdateOperators.Clear();
DbFullQualifiedUpdateOperators.Clear();
try
{
var url = ServiceUrls.ListAllFwUpdateUsersServiceUrl();
var requestResponse = LocalWebRequest.GetRequest(url, 1200);
if (string.IsNullOrEmpty(requestResponse))
{
DbIsConnected = false;
return false;
}
DbIsConnected = true;
DbAllUpdateOperators = JsonConvert.DeserializeObject<List<UserInformation>>(requestResponse);
if (DbAllUpdateOperators == null || DbAllUpdateOperators.Count == 0 || DbFullQualifiedUpdateOperators == null)
{
return false;
}
// If the user is fully qualified it will be put to the DbFullQualifiedUsersInfos list.
foreach (var user in DbAllUpdateOperators.Where(CheckIfUpdateOperatorIsFullyQualified))
{
// Avoid doubling of names if these are already listed
if (DbFullQualifiedUpdateOperators.Any(u => u.FullName == user.FullName)) continue;
DbFullQualifiedUpdateOperators.Add(user);
}
return true;
}
catch (Exception)
{
DbIsConnected = false;
return false;
}
}
/// <summary>
/// Get the Builder Operator information from DB for user validation check.
/// </summary>
/// <returns>true if successful</returns>
/// <remarks date="2021-Apr-14" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
public Boolean GetAllBuilderOperatorsFromDb()
{
if (DbAllBuilderOperators == null) DbAllBuilderOperators = new List<UserInformation>();
DbAllBuilderOperators.Clear();
try
{
var url = ServiceUrls.ListAllFwUpdateBuilderOperatorsServiceUrl();
var requestResponse = LocalWebRequest.GetRequest(url, 1200);
if (string.IsNullOrEmpty(requestResponse))
{
DbIsConnected = false;
return false;
}
DbIsConnected = true;
DbAllBuilderOperators = JsonConvert.DeserializeObject<List<UserInformation>>(requestResponse);
return DbAllBuilderOperators != null && DbAllBuilderOperators.Count != 0;
}
catch (Exception)
{
DbIsConnected = false;
return false;
}
}
/// <summary>
/// Get all Cordonel FW packages info files from DB.
/// </summary>
/// <returns>true if successful</returns>
/// <remarks date="2021-Apr-08" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
public Boolean GetAllCordonelFwInfoFilesFromDb()
{
if (DbCordonelFwPackages == null) DbCordonelFwPackages = new List<ClusteredFile>();
DbCordonelFwPackages.Clear();
try
{
var url = ServiceUrls.ListAllFwPackages();
var requestResponse = LocalWebRequest.GetRequest(url, 10000);
if (string.IsNullOrEmpty(requestResponse))
{
DbIsConnected = false;
return false;
}
DbIsConnected = true;
DbCordonelFwPackages = JsonConvert.DeserializeObject<List<ClusteredFile>>(requestResponse);
return DbCordonelFwPackages != null && DbCordonelFwPackages.Count != 0;
}
catch (Exception)
{
DbIsConnected = false;
return false;
}
}
/// <summary>
/// Load the latest FW update SW package from DB.
/// </summary>
/// <returns>true if successful</returns>
/// <remarks date="2021-Oct-26" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
public Boolean DownloadFwUpdateSwContainerFromDb()
{
try
{
var url = ServiceUrls.ListAllSwPackages();
var requestResponse = LocalWebRequest.GetRequest(url, 10000);
if (string.IsNullOrEmpty(requestResponse))
{
DbIsConnected = false;
return false;
}
DbIsConnected = true;
// get a list of all stored FW update SW packages
var dbCordonelSwPackages = JsonConvert.DeserializeObject<List<ClusteredFile>>(requestResponse);
if (dbCordonelSwPackages == null || dbCordonelSwPackages.Count == 0)
{
return false;
}
// get latest version
var fileId = dbCordonelSwPackages.Select(package => package.FileId).Prepend(0).Max();
if (fileId == 0)
{
return false;
}
if (DbFwUpdateSwPackage == null) DbFwUpdateSwPackage = new List<FilePart>();
DbFwUpdateSwPackage.Clear();
// download content
url = ServiceUrls.DownloadFileParts();
url += $"{fileId}&readContent=true";
requestResponse = LocalWebRequest.GetRequest(url, 15000);
if (string.IsNullOrEmpty(requestResponse))
{
DbIsConnected = false;
return false;
}
DbIsConnected = true;
DbFwUpdateSwPackage = JsonConvert.DeserializeObject<List<FilePart>>(requestResponse);
return DbFwUpdateSwPackage != null && DbFwUpdateSwPackage.Count > 0;
}
catch (Exception)
{
DbIsConnected = false;
return false;
}
}
/// <summary>
/// Load the latest FW update configuration files from DB.
/// </summary>
/// <returns>true if successful</returns>
/// <remarks date="2021-Oct-26" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
public Boolean DownloadFwUpdateConfigurationFilesFromDb()
{
try
{
var url = ServiceUrls.ListAllConfigurationFilePackages();
var requestResponse = LocalWebRequest.GetRequest(url, 10000);
if (string.IsNullOrEmpty(requestResponse))
{
DbIsConnected = false;
return false;
}
DbIsConnected = true;
// get a list of all stored FW update config file packages
var dbConfigFilePackages = JsonConvert.DeserializeObject<List<ClusteredFile>>(requestResponse);
if (dbConfigFilePackages == null || dbConfigFilePackages.Count == 0)
{
return false;
}
// get latest version
var fileId = dbConfigFilePackages.Select(package => package.FileId).Prepend(0).Max();
if (fileId == 0)
{
return false;
}
if (DbFwUpdateConfigFiles == null) DbFwUpdateConfigFiles = new List<FilePart>();
DbFwUpdateConfigFiles.Clear();
// download content
url = ServiceUrls.DownloadFileParts();
url += $"{fileId}&readContent=true";
requestResponse = LocalWebRequest.GetRequest(url, 10000);
if (string.IsNullOrEmpty(requestResponse))
{
DbIsConnected = false;
return false;
}
DbIsConnected = true;
DbFwUpdateConfigFiles = JsonConvert.DeserializeObject<List<FilePart>>(requestResponse);
return DbFwUpdateConfigFiles != null && DbFwUpdateConfigFiles.Count > 0;
}
catch (Exception)
{
DbIsConnected = false;
return false;
}
}
/// <summary>
/// Get all Cordonel safe info files from DB.
/// </summary>
/// <returns>true if successful</returns>
/// <remarks date="2021-Apr-27" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
public Boolean GetAllCordonelSafeInfoFilesFromDb()
{
if (DbCordonelSafes == null) DbCordonelSafes = new List<ClusteredFile>();
DbCordonelSafes.Clear();
try
{
var url = ServiceUrls.ListAllFwUpdateSafes();
var requestResponse = LocalWebRequest.GetRequest(url, 5000);
if (string.IsNullOrEmpty(requestResponse))
{
DbIsConnected = false;
return false;
}
DbIsConnected = true;
DbCordonelSafes = JsonConvert.DeserializeObject<List<ClusteredFile>>(requestResponse);
return DbCordonelSafes != null && DbCordonelSafes.Count != 0;
}
catch (Exception)
{
DbIsConnected = false;
return false;
}
}
/// <summary>
/// Getting all FW-Update Safes information by user id. The download has to be triggered with
/// the DownloadFileContentFromDB with FilePartId as reference
/// </summary>
/// <param name="userId"></param>
/// <param name="fwUpdSafeContainers"></param>
/// <returns>true if containers exists</returns>
/// <remarks date="2021-Mar-25" author="Roland Drabesch">
/// - Initial.
/// </remarks>
/// <remarks date="2021-Apr-06" author="Thomas Wiedebusch">
/// - timeout to 1200 ms.
/// </remarks>
/// <remarks date="2021-Jun-01" author="Thomas Wiedebusch">
/// - timeout to 3000 ms.
/// </remarks>
public Boolean ListAllFwUpdateSafesOfUserFromDb(Int32 userId, out List<FwUpdateSafeDb> fwUpdSafeContainers)
{
fwUpdSafeContainers = new List<FwUpdateSafeDb>();
try
{
var url = ServiceUrls.ListAllFwUpdateSafesForUserServiceUrl();
url += $"{userId}";
var requestResponse = LocalWebRequest.GetRequest(url, 3000);
if (string.IsNullOrEmpty(requestResponse))
{
DbIsConnected = false;
return false;
}
DbIsConnected = true;
fwUpdSafeContainers = JsonConvert.DeserializeObject<List<FwUpdateSafeDb>>(requestResponse);
return fwUpdSafeContainers != null && fwUpdSafeContainers.Count > 0;
}
catch (Exception)
{
DbIsConnected = false;
return false;
}
}
/// <summary>
/// Get a single Cordonel safe from DB.
/// </summary>
/// <param name="fileId">the file id previously read with <see cref="GetAllCordonelSafeInfoFilesFromDb"/></param>
/// <param name="safe">the safe</param>
/// <param name="readContent">reading content if true, else only the file information</param>
/// <returns>true if successful</returns>
/// <remarks date="2021-Apr-27" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
/// <remarks date="2021-Jun-01" author="Thomas Wiedebusch">
/// - timeout to 5000 ms.
/// </remarks>
public Boolean GetCordonelSafeFromDb(Int32 fileId, out FilePart safe, Boolean readContent = true)
{
safe = new FilePart();
try
{
var url = ServiceUrls.DownloadFileParts();
url += $"{fileId}&readContent={readContent}";
var requestResponse = LocalWebRequest.GetRequest(url, 5000);
if (string.IsNullOrEmpty(requestResponse))
{
DbIsConnected = false;
return false;
}
DbIsConnected = true;
safe = JsonConvert.DeserializeObject<FilePart>(requestResponse);
return safe != null;
}
catch (Exception)
{
DbIsConnected = false;
return false;
}
}
/// <summary>
/// Get a single Cordonel FW package from DB.
/// </summary>
/// <param name="fileId">the file id previously read with <see cref="GetAllCordonelFwInfoFilesFromDb"/></param>
/// <param name="fwPackage">the FW as package</param>
/// <param name="readContent">reading content if true, else only the file information</param>
/// <returns>true if successful</returns>
/// <remarks date="2021-Apr-09" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
/// <remarks date="2021-Jun-01" author="Thomas Wiedebusch">
/// - Timeout increased to 5000 ms.
/// </remarks>
public Boolean GetCordonelFwPackageFromDb(Int32 fileId, out List<FilePart> fwPackage, Boolean readContent = true)
{
fwPackage = new List<FilePart>();
try
{
var url = ServiceUrls.DownloadFileParts();
url += $"{fileId}&readContent={readContent}";
var requestResponse = LocalWebRequest.GetRequest(url, 5000);
if (string.IsNullOrEmpty(requestResponse))
{
DbIsConnected = false;
return false;
}
DbIsConnected = true;
fwPackage = JsonConvert.DeserializeObject<List<FilePart>>(requestResponse);
return fwPackage != null && fwPackage.Count > 0;
}
catch (Exception)
{
DbIsConnected = false;
return false;
}
}
/// <summary>
/// Get all Cordonel customers from DB. The return can be used to select a specific customer and get
/// the customer number which is the base for the order search.
/// </summary>
/// <param name="searchPattern">specific search pattern for customer name</param>
/// <returns>true if successful</returns>
/// <remarks date="2021-Mar-31" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
public Boolean GetAllCordonelCustomersFromDb(String searchPattern = "*")
{
// for the DB interface a specific search pattern will be used, so replace it from the user interface
// as there is a "*" the placeholder for "all".
searchPattern = searchPattern.Replace("*", "%");
if (DbSearchedCordonelCustomers == null)
DbSearchedCordonelCustomers = new List<CordonelCustomerInfosDb>();
DbSearchedCordonelCustomers.Clear();
try
{
var url = ServiceUrls.ListAllCordonelCustomersWithFilterServiceUrl();
url += searchPattern;
var requestResponse = LocalWebRequest.GetRequest(url, 1200);
if (string.IsNullOrEmpty(requestResponse))
{
DbIsConnected = false;
return false;
}
DbIsConnected = true;
DbSearchedCordonelCustomers =
JsonConvert.DeserializeObject<List<CordonelCustomerInfosDb>>(requestResponse);
return DbSearchedCordonelCustomers != null && DbSearchedCordonelCustomers.Count != 0;
}
catch (Exception)
{
DbIsConnected = false;
return false;
}
}
/// <summary>
/// Get all Cordonel customers orders from DB. The return can be used to select a specific order number
/// and position which is the base for the Cordonel serial number search.
/// </summary>
/// <param name="customerNumber"></param>
/// <returns>true if successful</returns>
/// <remarks date="2021-Mar-31" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
public Boolean GetAllCordonelCustomerOrdersFromDb(Int64 customerNumber)
{
if (DbCordonelCustomerOrders == null)
DbCordonelCustomerOrders = new List<CordonelCustomerOrderDb>();
DbCordonelCustomerOrders.Clear();
try
{
var url = ServiceUrls.ListAllCordonelCustomerOrdersServiceUrl();
url += $@"{customerNumber}";
var requestResponse = LocalWebRequest.GetRequest(url, 1200);
if (string.IsNullOrEmpty(requestResponse))
{
DbIsConnected = false;
return false;
}
DbIsConnected = true;
DbCordonelCustomerOrders =
JsonConvert.DeserializeObject<List<CordonelCustomerOrderDb>>(requestResponse);
return DbCordonelCustomerOrders != null && DbCordonelCustomerOrders.Count != 0;
}
catch (Exception)
{
DbIsConnected = false;
return false;
}
}
/// <summary>
/// Get all Cordonel serial numbers of a specific order.
/// </summary>
/// <param name="orderNumber"></param>
/// <param name="orderPos"></param>
/// <returns>true if successful</returns>
/// <remarks date="2021-Mar-31" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
public Boolean GetAllCordonelSerialNumbersFromDb(Int64 orderNumber, Int64 orderPos)
{
if (DbCordonelSerialNumbers == null)
DbCordonelSerialNumbers = new List<CordonelSerialNumbersDb>();
DbCordonelSerialNumbers.Clear();
try
{
var url = ServiceUrls.ListAllOrderCordonelsServiceUrl();
url += $@"{orderNumber}&CustomerOrderPos={orderPos}";
var requestResponse = LocalWebRequest.GetRequest(url, 1200);
if (string.IsNullOrEmpty(requestResponse))
{
DbIsConnected = false;
return false;
}
DbIsConnected = true;
DbCordonelSerialNumbers =
JsonConvert.DeserializeObject<List<CordonelSerialNumbersDb>>(requestResponse);
return DbCordonelSerialNumbers != null && DbCordonelSerialNumbers.Count != 0;
}
catch (Exception)
{
DbIsConnected = false;
return false;
}
}
/// <summary>
/// Simple command to access the DB and check therefore if it is available.
/// </summary>
/// <returns>true if successful</returns>
/// <remarks date="2021-Feb-04" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
/// <remarks date="2021-Apr-06" author="Thomas Wiedebusch">
/// - Timeout extended to 1200 ms.
/// </remarks>
public Boolean CheckDbConnection()
{
try
{
var url = ServiceUrls.GetFwUpdateUserByIdServiceUrl();
url += "0";
var requestResponse = LocalWebRequest.GetRequest(url, 1200);
if (string.IsNullOrEmpty(requestResponse))
{
DbIsConnected = false;
return false;
}
DbIsConnected = true;
return true;
}
catch (Exception)
{
DbIsConnected = false;
return false;
}
}
/// <summary>
/// Check if user is registered to DB with all contents including Hw Id and pwd hash.
/// The DbAllUsersInfos has to be loaded or updated in advance, which will be done in the Ctor or on every
/// registration of a new user or update of Hw Id or User Pwd Hash <see cref="GetAllUpdateOperatorsFromDb"/>.
/// The first fully specified user will be returned by its user id. This routine can be used to create
/// the Db
/// </summary>
/// <param name="dbCheckFullSpecUpdateOperator">
/// - user to search for registration in DB with full specification,
/// - the search parameter is the FullName,
/// - the first found full qualified DB user will set the user id</param>
/// <returns>true if user is registered in DB</returns>
/// <remarks date="2021-Feb-10" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
/// <remarks date="2021-Feb-11" author="Thomas Wiedebusch">
/// - Input full specified user info.
/// </remarks>
private Boolean CheckIfUpdateOperatorIsFullyQualified(UserInformation dbCheckFullSpecUpdateOperator)
{
// The fully specified user has to be defined by FullName, LogInName, Domain, Hw Id and User Pwd Hash.
// The user id mustn't be set as this will be returned by the DB search.
if (dbCheckFullSpecUpdateOperator == null || DbAllUpdateOperators == null ||
DbAllUpdateOperators.Count == 0 ||
string.IsNullOrEmpty(dbCheckFullSpecUpdateOperator.FullName) ||
string.IsNullOrEmpty(dbCheckFullSpecUpdateOperator.LogInName) ||
string.IsNullOrEmpty(dbCheckFullSpecUpdateOperator.Domain) ||
string.IsNullOrEmpty(dbCheckFullSpecUpdateOperator.HardwareId) ||
string.IsNullOrEmpty(dbCheckFullSpecUpdateOperator.PasswordHash))
return false;
foreach (var user in DbAllUpdateOperators.Where(user =>
user.FullName == dbCheckFullSpecUpdateOperator.FullName &&
user.LogInName == dbCheckFullSpecUpdateOperator.LogInName &&
user.Domain == dbCheckFullSpecUpdateOperator.Domain &&
user.HardwareId == dbCheckFullSpecUpdateOperator.HardwareId &&
user.PasswordHash == dbCheckFullSpecUpdateOperator.PasswordHash))
{
dbCheckFullSpecUpdateOperator.Id = user.Id;
return true;
}
return false;
}
/// <summary>
/// Register user to DB. This is a direct DB access. The user hardware id and and password hash are optional.
/// The dbNewUser will get his user id on registration.
/// </summary>
/// <param name="dbNewUser">new user to register to DB</param>
/// <returns>true if successful and all fields set except pwd hash and hw id</returns>
/// <remarks date="2021-Feb-08" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
/// <remarks date="2021-Feb-11" author="Thomas Wiedebusch">
/// - Extended to full registration, optional hw id and pwd hash,
/// - Refresh all DbAllUsersInfos and DbFullQualifiedUsersInfos lists.
/// </remarks>
/// <remarks date="2021-Mar-12" author="Thomas Wiedebusch">
/// - Set initial validation date.
/// </remarks>
/// <remarks date="2021-Mar-25" author="Thomas Wiedebusch">
/// - Set DB connection state.
/// </remarks>
/// <remarks date="2021-Mar-30" author="Thomas Wiedebusch">
/// - Added initial valid date.
/// </remarks>
/// <remarks date="2021-Apr-06" author="Thomas Wiedebusch">
/// - timeout to 1200 ms.
/// </remarks>
private Boolean RegisterUpdateOperatorToDbAndRefreshLists(UserInformation dbNewUser)
{
// The new user has to be defined by FullName, LogInName and Domain
if (dbNewUser == null || string.IsNullOrEmpty(dbNewUser.FullName) ||
string.IsNullOrEmpty(dbNewUser.LogInName) || string.IsNullOrEmpty(dbNewUser.Domain)) return false;
// Hw Id and Pwd Hash are optional on initial registration
if (dbNewUser.HardwareId == null) dbNewUser.HardwareId = "";
if (dbNewUser.PasswordHash == null) dbNewUser.PasswordHash = "";
var validDateTime = DateTimeServer.SetValidationDate(FwUpdateConfig.Consts.FwUpdateConfig.ValidDays);
try
{
var url = ServiceUrls.CreateNewFwUpdateUserServiceUrl();
url += $"FullName={dbNewUser.FullName}" +
$"&LoginName={dbNewUser.LogInName}" +
$"&Domain={dbNewUser.Domain}" +
$"&HardwareId={dbNewUser.HardwareId}" +
$"&PasswordHash={dbNewUser.PasswordHash} +" +
$"&ValidDate={validDateTime:yyyy-MM-ddTHH:mm:ss}&AccountActive=true";
var requestResponse = LocalWebRequest.GetRequest(url, 1200);
if (string.IsNullOrEmpty(requestResponse))
{
DbIsConnected = false;
return false;
}
DbIsConnected = true;
// During registration the user id will be set. This is needed to access later the hw id and pwd hash.
dbNewUser.Id = JsonConvert.DeserializeObject<Int32>(requestResponse);
// Set validation date and refresh all DbAllUsersInfos and DbFullQualifiedUsersInfos lists
return RefreshValidDateToDbAndRefreshUserLists(dbNewUser.Id);
}
catch (Exception)
{
DbIsConnected = false;
return false;
}
}
/// <summary>
/// Set user pwd hash to DB. This is a direct DB access. The user has to be referenced by id.
/// On every refresh the DbAllUsersInfos and DbFullQualifiedUsersInfos lists will be generated
/// <see cref="GetAllUpdateOperatorsFromDb"/>.
/// </summary>
/// <param name="dbUserId">user id as reference</param>
/// <param name="dbUserPwdHash">the password hash as string</param>
/// <remarks date="2021-Feb-04" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
/// <remarks date="2021-Feb-11" author="Thomas Wiedebusch">
/// - Input is user Id and pwd hash as string.
/// - Refresh all DbAllUsersInfos and DbFullQualifiedUsersInfos lists.
/// </remarks>
/// <remarks date="2021-Mar-12" author="Thomas Wiedebusch">
/// - Refresh validation date.
/// </remarks>
/// <remarks date="2021-Mar-25" author="Thomas Wiedebusch">
/// - Set DB connection state.
/// </remarks>
/// <remarks date="2021-Apr-06" author="Thomas Wiedebusch">
/// - timeout to 1200 ms.
/// </remarks>
private void RefreshPwdHashToDbAndRefreshUserLists(Int32 dbUserId, String dbUserPwdHash)
{
if (dbUserId < 0 || string.IsNullOrEmpty(dbUserPwdHash)) return;
try
{
var url = ServiceUrls.RefreshFwUpdateUsersPasswordHashServiceUrl();
url += $"{dbUserId}&PasswordHash={dbUserPwdHash}";
var requestResponse = LocalWebRequest.GetRequest(url, 1200);
if (string.IsNullOrEmpty(requestResponse))
{
DbIsConnected = false;
return;
}
DbIsConnected = true;
if (!JsonConvert.DeserializeObject<Boolean>(requestResponse)) return;
// Set validation date and refresh all DbAllUsersInfos and DbFullQualifiedUsersInfos lists
RefreshValidDateToDbAndRefreshUserLists(dbUserId);
}
catch (Exception)
{
DbIsConnected = false;
}
}
/// <summary>
/// Set user validation to DB. This is a direct DB access. The user has to be referenced by id.
/// On every refresh the DbAllUsersInfos and DbFullQualifiedUsersInfos lists will be generated.
/// <see cref="GetAllUpdateOperatorsFromDb"/>.
/// </summary>
/// <param name="dbUserId">user id as reference</param>
/// <remarks date="2021-Mar-12" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
/// <remarks date="2021-Mar-15" author="Thomas Wiedebusch">
/// - Valid date as string.
/// </remarks>
/// <remarks date="2021-Mar-25" author="Thomas Wiedebusch">
/// - Set DB connection state.
/// </remarks>
/// <remarks date="2021-Apr-06" author="Thomas Wiedebusch">
/// - timeout to 1200 ms.
/// </remarks>
private Boolean RefreshValidDateToDbAndRefreshUserLists(Int32 dbUserId)
{
if (dbUserId < 0) return false;
try
{
var validDateTime = DateTimeServer.SetValidationDate(FwUpdateConfig.Consts.FwUpdateConfig.ValidDays);
var url = ServiceUrls.EditFwUpdateUserValidityServiceUrl();
url += $"{dbUserId}&ValidDate={validDateTime:yyyy-MM-ddTHH:mm:ss}&AccountActive=true";
var requestResponse = LocalWebRequest.GetRequest(url, 1200);
if (string.IsNullOrEmpty(requestResponse))
{
DbIsConnected = false;
return false;
}
DbIsConnected = true;
// Refresh all DbAllUsersInfos and DbFullQualifiedUsersInfos lists
return JsonConvert.DeserializeObject<Boolean>(requestResponse) && GetAllUpdateOperatorsFromDb();
}
catch (Exception)
{
DbIsConnected = false;
return false;
}
}
/// <summary>
/// Set user hw id to DB. This is a direct DB access. The user has to be referenced by the user Id.
/// On every refresh the DbAllUsersInfos and DbFullQualifiedUsersInfos lists will be generated
/// <see cref="GetAllUpdateOperatorsFromDb"/>.
/// </summary>
/// <param name="dbUserId">user id as reference</param>
/// <param name="dbHwId">the hardware identifier as string</param>
/// <remarks date="2021-Feb-04" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
/// <remarks date="2021-Feb-11" author="Thomas Wiedebusch">
/// - Input is user Id and Hw Id as string. Refresh
/// - Refresh all DbAllUsersInfos and DbFullQualifiedUsersInfos lists.
/// </remarks>
/// <remarks date="2021-Mar-12" author="Thomas Wiedebusch">
/// - Refresh validation date.
/// </remarks>
/// <remarks date="2021-Mar-25" author="Thomas Wiedebusch">
/// - Set DB connection state.
/// </remarks>
/// <remarks date="2021-Apr-06" author="Thomas Wiedebusch">
/// - timeout to 1200 ms.
/// </remarks>
private void RefreshHwIdToDbAndRefreshUserLists(Int32 dbUserId, String dbHwId)
{
if (dbUserId < 0 || string.IsNullOrEmpty(dbHwId)) return;
try
{
var url = ServiceUrls.RefreshFwUpdateUsersHardwareIdServiceUrl();
url += $"{dbUserId}&HardwareId={dbHwId}";
var requestResponse = LocalWebRequest.GetRequest(url, 1200);
if (string.IsNullOrEmpty(requestResponse))
{
DbIsConnected = false;
return;
}
DbIsConnected = true;
if (!JsonConvert.DeserializeObject<Boolean>(requestResponse)) return;
// Set validation date and refresh all DbAllUsersInfos and DbFullQualifiedUsersInfos lists
RefreshValidDateToDbAndRefreshUserLists(dbUserId);
}
catch (Exception)
{
DbIsConnected = false;
}
}
/// <summary>
/// Get all user infos by full name. The user information needed to be fully registered.
/// All user infos will be returned. The first user will be taken where the FullName, Domain, LogInName
/// HW Id and pwd hash matches and the Id is set. The DbFullQualifiedUsersInfos will be set in advance in
/// the <see cref="GetAllUpdateOperatorsFromDb"/> and has been verified to be fully qualified.
/// </summary>
/// <param name="userName">user name to search for</param>
/// <param name="dbFullQualifiedUser">user to search for registration in DB with full name only, all contents
/// will be filled with the information out of DbFullQualifiedUsersInfos</param>
/// <returns>
/// - true if user is registered in DB and sets all other informational fields,
/// - false if the essential fields are missed.
/// </returns>
/// <remarks date="2021-Feb-10" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
/// <remarks date="2021-Feb-11" author="Thomas Wiedebusch">
/// - Input user name to get at all infos of fully qualified user.
/// </remarks>
/// <remarks date="2021-MAr-08" author="Thomas Wiedebusch">
/// - Input user name to search for,
/// - UserInformation object will be build here to avoid preset requirements of name.
/// </remarks>
public Boolean GetFullQualifiedUserByFullName(String userName, out UserInformation dbFullQualifiedUser)
{
// The user has to be defined by FullName only. It will be searched in DbFullQualifiedUsersInfos as this
// list is already checked for fully qualification.
dbFullQualifiedUser = new UserInformation();
if (dbFullQualifiedUser == null || DbFullQualifiedUpdateOperators == null ||
DbFullQualifiedUpdateOperators.Count == 0 || string.IsNullOrEmpty(userName)) return false;
foreach (var user in DbFullQualifiedUpdateOperators.Where(user => user.FullName == userName))
{
return CloneUser(dbFullQualifiedUser, user);
}
return false;
}
/// <summary>
/// Get all user infos by full name. User needn't to be be fully registered, but domain, logInName
/// and FullName are essential. All user infos will be returned. The first user will be taken where
/// the FullName, Domain and LogInName matches and the Id is set.
/// </summary>
/// <param name="dbAllRegUserByFullName">user to search for registration in DB with full name only</param>
/// <returns>
/// - true if user is registered in DB and sets all other informational fields,
/// - false if the essential fields are missed.
/// </returns>
/// <remarks date="2021-Feb-10" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
/// <remarks date="2021-Feb-11" author="Thomas Wiedebusch">
/// - Input user name to get at least the user id, but all other infos will be returned as well.
/// </remarks>
public Boolean GetUserFromAllRegUsersByFullName(UserInformation dbAllRegUserByFullName)
{
// The user has to be defined by FullName only. It will be searched in DbAllUsersInfos
if (dbAllRegUserByFullName == null || DbAllUpdateOperators == null || DbAllUpdateOperators.Count == 0 ||
string.IsNullOrEmpty(dbAllRegUserByFullName.FullName)) return false;
foreach (var user in DbAllUpdateOperators.Where(user => user.FullName == dbAllRegUserByFullName.FullName))
{
// Skip all users which do not have set the essential needed elements: Domain, LogInName and FullName.
if (string.IsNullOrEmpty(user.Domain) || string.IsNullOrEmpty(user.LogInName)) continue;
return CloneUser(dbAllRegUserByFullName, user);
}
return false;
}
/// <summary>
/// Clone user with all contents
/// </summary>
/// <param name="destinationUser"></param>
/// <param name="sourceUser"></param>
/// <returns>true if users are identical</returns>
/// <remarks date="2021-Feb-12" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
public static Boolean CloneUser(UserInformation destinationUser, UserInformation sourceUser)
{
if (destinationUser == null || sourceUser == null) return false;
destinationUser.Id = sourceUser.Id;
destinationUser.FullName = sourceUser.FullName;
destinationUser.Domain = sourceUser.Domain;
destinationUser.LogInName = sourceUser.LogInName;
destinationUser.PasswordHash = sourceUser.PasswordHash;
destinationUser.HardwareId = sourceUser.HardwareId;
destinationUser.RegisterDate = sourceUser.RegisterDate;
destinationUser.ValidDate = sourceUser.ValidDate;
destinationUser.AccountActive = sourceUser.AccountActive;
return true;
}
/// <summary>
/// Compare two users with all contents.
/// </summary>
/// <param name="user1"></param>
/// <param name="user2"></param>
/// <returns>true if users are identical</returns>
/// <remarks date="2021-Feb-12" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
public static Boolean UsersAreIdentical(UserInformation user1, UserInformation user2)
{
return user1.Id == user2.Id &&
user1.FullName == user2.FullName &&
user1.Domain == user2.Domain &&
user1.LogInName == user2.LogInName &&
user1.PasswordHash == user2.PasswordHash &&
user1.HardwareId == user2.HardwareId &&
user1.RegisterDate == user2.RegisterDate &&
user1.ValidDate == user2.ValidDate &&
user1.AccountActive == user2.AccountActive;
}
/// <summary>
/// Register new user or refresh password hash and Hw Id.
/// </summary>
/// <param name="newUser">user to search for registration in DB</param>
/// <returns>true if user is registered in DB</returns>
/// <remarks date="2021-Feb-11" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
/// <remarks date="2021-Feb-12" author="Thomas Wiedebusch">
/// - Backup newUserInfo before trying to GetPartialRegisteredUser.. as this overwrites all elements
/// and therefor the NEW pwd hash and/or NEW hw id will be lost and couldn't be changed.
/// </remarks>
public Boolean UserRegisterOrRefresh(UserInformation newUser)
{
if (newUser == null) return false;
// Use a new dbUser for the search to avoid overwriting of newUserInfo. Copy the minimal elements which are
// needed for a registered users: Domain, LogInName and FullName.
var dbUser = new UserInformation
{
FullName = newUser.FullName,
Domain = newUser.Domain,
LogInName = newUser.LogInName
};
// This routine returns at least the needed user id for refresh of pwd hash and hw id.
// This user can be in both lists, fully qualified or partial in all users list.
if (GetUserFromAllRegUsersByFullName(dbUser))
{
// The dbUser has feedback the user id and the newUserInfo contains the info to change.
newUser.Id = dbUser.Id;
if (!string.IsNullOrEmpty(newUser.PasswordHash) && dbUser.PasswordHash != newUser.PasswordHash)
{
// The DbAllUsersInfos and DbFullQualifiedUsersInfos will be refreshed.
RefreshPwdHashToDbAndRefreshUserLists(newUser.Id, newUser.PasswordHash);
}
if (!string.IsNullOrEmpty(newUser.HardwareId) && dbUser.HardwareId != newUser.HardwareId)
{
// The DbAllUsersInfos and DbFullQualifiedUsersInfos will be refreshed.
RefreshHwIdToDbAndRefreshUserLists(newUser.Id, newUser.HardwareId);
}
}
// Check if the user is fully qualified!
if (CheckIfUpdateOperatorIsFullyQualified(newUser))
{
// Set validation date and refresh all DbAllUsersInfos and DbFullQualifiedUsersInfos lists
RefreshValidDateToDbAndRefreshUserLists(newUser.Id);
return true;
}
// Or if the user is at least initially set without Hw Id and/ or Pwd Hash
if (GetUserFromAllRegUsersByFullName(newUser)) return true;
// If the user is unknown, it shall be initially registered. The fields pwd hash and hw id are optional,
// but only fully registered users can be used for en- and decryption. This will also refresh the
// DbAllUsersInfos and DbFullQualifiedUsersInfos lists.
return RegisterUpdateOperatorToDbAndRefreshLists(newUser);
}
/// <summary>
/// Refresh actual validation date if FW-Update Safes are existing or create new password hash if not
/// to invalidate the old FW-Update Safes.
/// </summary>
/// <param name="regUser"></param>
/// <param name="keepActualPwdHash"></param>
/// <returns>true if refresh succeeded</returns>
/// <remarks date="2021-Mar-29" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
public Boolean RefreshUserValidation(UserInformation regUser, Boolean keepActualPwdHash)
{
if (regUser == null || string.IsNullOrEmpty(regUser.FullName)) return false;
// All user information shall be set as the user needs to be fully
// qualified. This form adds exclusively the FullName of the user,
// so that the "FW-Update Builder" operator can select a real user name!
var newUser = new UserInformation
{
FullName = regUser.FullName
};
// Keep the password hash as an encrypted container exists
if (keepActualPwdHash)
{
return UserRegisterOrRefresh(regUser);
}
// create a new password hash to invalidate all previously created password safes
CryptInformation.GetSysUser(newUser);
return UserRegisterOrRefresh(newUser);
}
/// <summary>
/// Download FW update safe referenced by container id
/// </summary>
/// <param name="containerId"></param>
/// <returns>true if file content exists</returns>
/// <remarks date="2021-Apr-28" author="Roland Drabesch">
/// - Initial.
/// </remarks>
public List<FwUpdateSafeDb> DownloadFwUpdateSafeFromDb(Int32 containerId)
{
var fwUpdateSafes = new List<FwUpdateSafeDb>();
try
{
var url = ServiceUrls.DownloadFwUpdateSafeServiceUrl();
url += $"{containerId}";
var requestResponse = LocalWebRequest.GetRequest(url, 10000);
if (string.IsNullOrEmpty(requestResponse))
{
DbIsConnected = false;
return fwUpdateSafes;
}
DbIsConnected = true;
fwUpdateSafes = JsonConvert.DeserializeObject<List<FwUpdateSafeDb>>(requestResponse);
return fwUpdateSafes;
}
catch (Exception)
{
DbIsConnected = false;
return fwUpdateSafes;
}
}
/// <summary>
/// Getting file contents referenced by file part id.
/// -http://localhost:56011/GetFileContent?FileId=3831
/// </summary>
/// <param name="filePartId"></param>
/// <param name="fileContent"></param>
/// <returns>true if file content exists</returns>
/// <remarks date="2021-Mar-25" author="Roland Drabesch">
/// - Initial.
/// </remarks>
public Boolean DownloadFileContentFromDb(Int32 filePartId, out Byte[] fileContent)
{
fileContent = new Byte[] { };
try
{
var url = ServiceUrls.DownloadFileContentServiceUrl();
url += "{filePartId}";
var requestResponse = LocalWebRequest.GetRequest(url, 10000);
if (string.IsNullOrEmpty(requestResponse))
{
DbIsConnected = false;
return false;
}
DbIsConnected = true;
fileContent = JsonConvert.DeserializeObject<Byte[]>(requestResponse);
return fileContent != null && fileContent.Length > 0;
}
catch (Exception)
{
DbIsConnected = false;
return false;
}
}
/// <summary>
/// Get reports from DB.
/// It can be searched for:
/// PCB ID,
/// User ID or
/// Order Number.
/// Leaving all empty will search all reports.
/// </summary>
/// <param name="fwUpdRpt">list of reports</param>
/// <param name="pcbId">optional pcbId</param>
/// <param name="userId">optional userId</param>
/// <param name="orderNr">optional order number</param>
/// <returns>true if app list could be loaded</returns>
/// <remarks date="2021-Apr-30" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
public Boolean DownloadFwUpdateReportsFromDb(out List<FwUpdateReportDb> fwUpdRpt, Int64 pcbId = 0,
Int32 userId = 0, Int64 orderNr = 0)
{
fwUpdRpt = new List<FwUpdateReportDb>();
try
{
var url = ServiceUrls.DownloadFwUpdateReportServiceUrl();
if (pcbId > 0)
{
url += $"PcbId={pcbId}";
if (userId > 0) url += $"&UserId={userId}";
if (orderNr > 0) url += $"&OrderNr={orderNr}";
}
else if (userId > 0)
{
url += $"UserId={userId}";
if (orderNr > 0) url += $"&OrderNr={orderNr}";
}
else if (orderNr > 0)
{
url += $"OrderNr={orderNr}";
}
var requestResponse = LocalWebRequest.GetRequest(url, 10000);
if (string.IsNullOrEmpty(requestResponse))
{
DbIsConnected = false;
return false;
}
DbIsConnected = true;
fwUpdRpt = JsonConvert.DeserializeObject<List<FwUpdateReportDb>>(requestResponse);
return fwUpdRpt != null && fwUpdRpt.Count != 0;
}
catch (Exception)
{
DbIsConnected = false;
return false;
}
}
/// <summary>
/// Upload the report file to DB.
/// </summary>
/// <param name="fwUpdRpt"></param>
/// <returns>true if file could be uploaded</returns>
/// <remarks date="2021-Mar-25" author="Roland Drabesch">
/// - Initial.
/// </remarks>
/// <remarks date="2021-Mar-29" author="Roland Drabesch">
/// - Changed to FwUpdateReportDb.
/// </remarks>
/// <remarks date="2021-Apr-30" author="Roland Drabesch">
/// - DB interface restructured.
/// </remarks>
public Boolean UploadFwUpdateReportToDb(FwUpdateReportDb fwUpdRpt)
{
try
{
var url = ServiceUrls.UploadFwUpdateReportServiceUrl();
if (LocalWebRequest.PostRequestAsync(url, 10000, fwUpdRpt))
{
DbIsConnected = true;
return true;
}
DbIsConnected = false;
return false;
}
catch (Exception)
{
DbIsConnected = false;
return false;
}
}
/// <summary>
/// Upload the FW-Update Safe as container to DB.
/// </summary>
/// <param name="fwUpdateSafeDb"></param>
/// <param name="pcbIds"></param>
/// <returns>true if file could be uploaded</returns>
/// <remarks date="2021-Mar-25" author="Roland Drabesch">
/// - Initial.
/// </remarks>
/// <remarks date="2021-Apr-27" author="Roland Drabesch">
/// - Set container Id, needed to assign PCB IDs.
/// </remarks>
public Boolean UploadFwUpdateSafeToDb(FwUpdateSafeDb fwUpdateSafeDb, List<String> pcbIds)
{
if (fwUpdateSafeDb == null) return false;
fwUpdateSafeDb.ContainerId = 0;
try
{
var url = ServiceUrls.UploadFwUpdateSafeContainerServiceUrl();
url += $"?PUserId={fwUpdateSafeDb.UserId}";
url += $"&PFileName={fwUpdateSafeDb.Name}";
url += $"&PValidDate={fwUpdateSafeDb.ValidDate:yyyy-MM-ddTHH:mm:ss}";
url += $"&PBuilderOperatorName={fwUpdateSafeDb.OperatorNameBuilder}";
var success = LocalWebRequest.PostBinaryFileRequestAsync(url, fwUpdateSafeDb.Name, fwUpdateSafeDb.Content, out var requestResponse);
var containerId = JsonConvert.DeserializeObject<Int32>(requestResponse);
if (success && !string.IsNullOrEmpty(requestResponse) && containerId > 0)
{
fwUpdateSafeDb.ContainerId = containerId;
url = ServiceUrls.AddPcbIdsToSafeServiceUrl();
url += containerId;
if (LocalWebRequest.PostRequestAsync(url, 1200, pcbIds))
{
DbIsConnected = true;
return true;
}
}
DbIsConnected = false;
return false;
}
catch (Exception)
{
DbIsConnected = false;
return false;
}
}
/// <summary>
/// Upload installed FW versions and core revision to DB.
/// </summary>
/// <param name="pcbId"></param>
/// <param name="apps"></param>
/// <returns>true if apps could be uploaded</returns>
/// <remarks date="2021-Feb-12" author="Roland Drabesch">
/// - Initial
/// </remarks>
/// <remarks date="2021-Mar-10" author="Thomas Wiedebusch">
/// - Changed version from Uint32 to String
/// </remarks>
/// <remarks date="2021-Apr-06" author="Thomas Wiedebusch">
/// - timeout to 1200 ms.
/// </remarks>
/// <remarks date="2021-Apr-07" author="Thomas Wiedebusch">
/// - changed service url.
/// </remarks>
public Boolean UploadCordonelAppVersionsToDb(Int64 pcbId, List<CordonelAppVersion> apps)
{
try
{
var url = ServiceUrls.UploadCordonelAppVersionServiceUrl();
url += $"{pcbId}&ProgressName={Assembly.GetExecutingAssembly().GetName().Name}";
url += $"&version={Assembly.GetExecutingAssembly().GetName().Version}";
if (LocalWebRequest.PostRequestAsync(url, 1200, apps))
{
DbIsConnected = true;
return true;
}
DbIsConnected = false;
return false;
}
catch (Exception)
{
DbIsConnected = false;
return false;
}
}
/// <summary>
/// Get all installed application versions from DB.
/// </summary>
/// <param name="pcbId"></param>
/// <param name="apps"></param>
/// <returns>true if app list could be loaded</returns>
/// <remarks date="2021-Apr-07" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
/// <remarks date="2021-Apr-15" author="Thomas Wiedebusch">
/// - Add request for radio version.
/// </remarks>
public Boolean DownloadCordonelAppVersionsFromDb(String pcbId, out List<CordonelAppVersion> apps)
{
apps = new List<CordonelAppVersion>();
try
{
var url = ServiceUrls.DownloadCordonelAppVersionServiceUrl();
url += $"{pcbId}&returnRadioFRQ=true";
var requestResponse = LocalWebRequest.GetRequest(url, 10000);
if (string.IsNullOrEmpty(requestResponse))
{
DbIsConnected = false;
return false;
}
DbIsConnected = true;
apps = JsonConvert.DeserializeObject<List<CordonelAppVersion>>(requestResponse);
return apps != null && apps.Count != 0;
}
catch (Exception)
{
DbIsConnected = false;
return false;
}
}
}
}