Files
laatzen/Common/Hardware/WaterMeter/Genesis/GenesisPwd/MeterPwdHandlerDb.cs
T

117 lines
4.2 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
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>
public static Boolean GetPasswordFromDb(String pcbId, out String password)
{
var url = ServiceUrls.GenesisGetPasswordServiceUrl();
password = "";
try
{
var http = (HttpWebRequest)WebRequest.Create(url + pcbId);
var response = (HttpWebResponse)http.GetResponse();
var responseStream = response.GetResponseStream();
if (responseStream == null)
{
return false;
}
using (var sr = new StreamReader(responseStream))
{
var responseJson = sr.ReadToEnd();
password = responseJson.Trim('"');
}
}
catch (Exception )
{
return false;
}
return true;
}
/// <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 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, 20000, isRetry: true);
if (errorCode == HttpStatusCode.OK.GetHashCode())
{
passwordContainer = JsonConvert.DeserializeObject<MeterPwdDb>(requestResponse);
return true;
}
return false;
}
catch (Exception)
{
return false;
}
}
}
}