common/Hardware/WaterMeter/Genesis/GenesisPwd/MeterPwdHandlerDb.cs
2026-04-23 17:50:07 +02:00

121 lines
4.5 KiB
C#

using System;
using System.Linq;
using System.Net;
using System.Text;
using Newtonsoft.Json;
using Xylem.Common.CommonCore.Configuration;
using Xylem.Common.Logic.SoftwareAccessHelper;
namespace Xylem.Common.Hardware.WaterMeter.Genesis.GenesisPwd
{
/// <summary>
/// Class for meter password handler DB interfaces.
/// </summary>
[Serializable]
public static class MeterPwdHandlerDb
{
/// <summary>
/// Acquires the actual password from DB. Initially, this is the Skeleton-Key, if the password file is
/// written it is the Lvl8Pwd.
/// </summary>
/// <param name="pcbId">input the PcbId</param>
/// <param name="password">output of password</param>
/// <returns>true if successfully executed</returns>
/// <remarks date="2023-Feb-23" author="Roland Drabesch/Thomas Wiedebusch">
/// - Initial.
/// </remarks>
/// <remarks date="2023-Feb-28" author="Roland Drabesch/Thomas Wiedebusch">
/// - Used JsonConvert instead of StreamReader.
/// </remarks>
public static Boolean GetPasswordFromDb(String pcbId, out String password)
{
password = "";
if (string.IsNullOrEmpty(pcbId))
{
return false;
}
try
{
var url = ServiceUrls.GenesisGetPasswordServiceUrl();
var response = LocalWebRequest.GetRequest(url + pcbId, 20000);
password = JsonConvert.DeserializeObject<String>(response);
if (string.IsNullOrEmpty(password))
{
password = "";
return false;
}
}
catch (Exception )
{
password = "";
return false;
}
return true;
}
/// <summary>
/// Overwrites the (production) password used for production in DB.
/// Initially this (production) password is set to the skeletonKey as the password file is not installed.
/// After installation of the password file the (production) password has to be set to the level 8 password
/// being able to access the meter with the production tools and the GTB.
/// </summary>
/// <param name="pcbId">input the PcbId</param>
/// <param name="passwordContainer">output of passwordContainer</param>
/// <returns>true if successfully executed</returns>
/// <remarks date="2023-Feb-23" author="Roland Drabesch/Thomas Wiedebusch">
/// - Initial.
/// </remarks>
public static Boolean SetPwdFromSkeletonToLvl8inDd(String pcbId, MeterPwdDb passwordContainer)
{
try
{
var newPwd = Encoding.UTF8.GetString(passwordContainer.ListOfPasswords.Last());
var passwordSetServiceUrl = ServiceUrls.GenesisSetPasswordServiceUrl();
var responseJson = LocalWebRequest.GetRequest(passwordSetServiceUrl + pcbId + "&Password=" + newPwd, 8000);
if (responseJson.ToLower().Contains("store"))
{
return true;
}
return false;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// Collect order number, radio address, skeleton key, password hashes and passwords from DB.
/// </summary>
/// <param name="pcbId">input the PcbId</param>
/// <param name="passwordContainer">output of passwordContainer</param>
/// <returns>true if successfully executed</returns>
/// <remarks date="2023-Feb-23" author="Roland Drabesch/Thomas Wiedebusch">
/// - Initial.
/// </remarks>
public static Boolean RequestPwdFileFromDb(String pcbId, out MeterPwdDb passwordContainer)
{
passwordContainer = new MeterPwdDb();
try
{
var url = ServiceUrls.GenesisGetPasswordContainerServiceUrl();
var requestResponse = LocalWebRequest.GetRequestWithError(url + pcbId, out var errorCode, 80000, isRetry: true);
if (errorCode == HttpStatusCode.OK.GetHashCode())
{
passwordContainer = JsonConvert.DeserializeObject<MeterPwdDb>(requestResponse);
return true;
}
return false;
}
catch (Exception)
{
return false;
}
}
}
}