ReSHarper: -SoftwareAccessHelper
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
namespace Xylem.Common.Hardware.WaterMeter.Genesis.Registers.DataTypes
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
internal class st_radio_dewa
|
||||
{
|
||||
//todo find out struct definition and implement it
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
namespace Xylem.Common.Hardware.WaterMeter.Genesis.Registers.DataTypes
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
internal class st_radio_tfx
|
||||
{
|
||||
//todo find out struct definition and implement it
|
||||
|
||||
@@ -88,6 +88,7 @@ namespace Xylem.Common.Hardware.WaterMeter.Genesis.Registers
|
||||
public static class Powermon
|
||||
{
|
||||
public static readonly String BatteryVoltage = "POWERMON_BatteryVoltage";
|
||||
public static readonly String BatteryQuantity = "POWERMON_BatteryQuantity";
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -356,7 +356,7 @@ namespace Xylem.Common.Service.MeterProcessState.Controllers
|
||||
try
|
||||
{
|
||||
|
||||
var responseJson = LocalWebRequest.GetRequest(Url: Url, Header: header, TimeoutMS: 20000);
|
||||
var responseJson = LocalWebRequest.GetRequest(url: Url, header: header, timeoutMs: 20000);
|
||||
responseJson = responseJson.Trim('"');
|
||||
return MeterPw.FromJson(responseJson);
|
||||
|
||||
@@ -512,7 +512,7 @@ namespace Xylem.Common.Service.MeterProcessState.Controllers
|
||||
var helpArray = new List<MapKeySetItem>() { postData };
|
||||
|
||||
|
||||
return LocalWebRequest.PostRequestAsyncAndGetContent(Url: Url, Header: header, json: helpArray.ToArray(), TimeoutMS: 20000);
|
||||
return LocalWebRequest.PostRequestAsyncAndGetContent(url: Url, header: header, json: helpArray.ToArray(), timeoutMs: 20000);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
@@ -4,16 +4,38 @@ using Xylem.Common.CommonCore.Configuration;
|
||||
|
||||
namespace Xylem.Common.Logic.SoftwareAccessHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Access right and permission handling for software assemblies. The production data base contains the assembly names and
|
||||
/// validation dates. This avoids usage of not permitted software. New software releases can invalid older versions.
|
||||
/// </summary>
|
||||
public static class Access
|
||||
{
|
||||
/// <summary>
|
||||
/// Check this assembly access rights/permissions.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static Boolean HasAccess()
|
||||
{
|
||||
return HasAccess(Assembly.GetExecutingAssembly().GetName());
|
||||
}
|
||||
/// <summary>
|
||||
/// Check the assembly name and version.
|
||||
/// </summary>
|
||||
/// <param name="assemblyName"></param>
|
||||
/// <returns></returns>
|
||||
public static Boolean HasAccess(AssemblyName assemblyName)
|
||||
{
|
||||
return HasAccess(assemblyName.Name, assemblyName.Version.Major, assemblyName.Version.Minor, assemblyName.Version.Build);
|
||||
return HasAccess(assemblyName.Name, assemblyName.Version.Major, assemblyName.Version.Minor,
|
||||
assemblyName.Version.Build);
|
||||
}
|
||||
/// <summary>
|
||||
/// check the program name and version.
|
||||
/// </summary>
|
||||
/// <param name="program"></param>
|
||||
/// <param name="major"></param>
|
||||
/// <param name="minor"></param>
|
||||
/// <param name="build"></param>
|
||||
/// <returns></returns>
|
||||
public static Boolean HasAccess(String program, Int32 major, Int32 minor, Int32 build)
|
||||
{
|
||||
if (System.Diagnostics.Debugger.IsAttached)
|
||||
|
||||
@@ -3,21 +3,29 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.NetworkInformation;
|
||||
|
||||
namespace Xylem.Common.Logic.SoftwareAccessHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Web request
|
||||
/// </summary>
|
||||
public static class LocalWebRequest
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
public static bool PostRequestAsync(string Url, ref int responseCode, ref Exception ex, int? TimeoutMS = null, object json = null, Dictionary<string, string> Header = null)
|
||||
/// <summary>
|
||||
/// Web request with asynchronous operation and response code or exception response, json object input and/or dictionary
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="responseCode"></param>
|
||||
/// <param name="ex"></param>
|
||||
/// <param name="timeoutMs"></param>
|
||||
/// <param name="json"></param>
|
||||
/// <param name="header"></param>
|
||||
/// <returns></returns>
|
||||
public static Boolean PostRequestAsync(String url, ref Int32 responseCode, ref Exception ex, Int32? timeoutMs = null, Object json = null, Dictionary<String, String> header = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var r = PostRequestAndGetResponseAsync(Url, TimeoutMS, json, Header);
|
||||
var r = PostRequestAndGetResponseAsync(url, timeoutMs, json, header);
|
||||
responseCode = r.StatusCode.GetHashCode();
|
||||
if (r.StatusCode == HttpStatusCode.OK)
|
||||
{
|
||||
@@ -37,36 +45,52 @@ namespace Xylem.Common.Logic.SoftwareAccessHelper
|
||||
|
||||
}
|
||||
|
||||
public static bool PostRequestAsync(string Url, int? TimeoutMS = null, object json = null, Dictionary<string, string> Header = null)
|
||||
/// <summary>
|
||||
/// Web request with asynchronous operation and true/false return, json object input and/or dictionary
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="timeoutMs"></param>
|
||||
/// <param name="json"></param>
|
||||
/// <param name="header"></param>
|
||||
/// <returns></returns>
|
||||
public static Boolean PostRequestAsync(String url, Int32? timeoutMs = null, Object json = null, Dictionary<String, String> header = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var r = PostRequestAndGetResponseAsync(Url, TimeoutMS, json, Header);
|
||||
var r = PostRequestAndGetResponseAsync(url, timeoutMs, json, header);
|
||||
if (r.StatusCode == HttpStatusCode.OK)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception error)
|
||||
catch (Exception)
|
||||
{
|
||||
// Log
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
public static string PostRequestAsyncAndGetContent(string Url, int? TimeoutMS = null, object json = null, Dictionary<string, string> Header = null)
|
||||
/// <summary>
|
||||
/// Web request with asynchronous operation with string return, json object input and/or dictionary
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="timeoutMs"></param>
|
||||
/// <param name="json"></param>
|
||||
/// <param name="header"></param>
|
||||
/// <returns></returns>
|
||||
public static String PostRequestAsyncAndGetContent(String url, Int32? timeoutMs = null, Object json = null, Dictionary<String, String> header = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var r = PostRequestAndGetResponseAsync(Url, TimeoutMS, json, Header);
|
||||
var r = PostRequestAndGetResponseAsync(url, timeoutMs, json, header);
|
||||
if (r.StatusCode == HttpStatusCode.OK)
|
||||
{
|
||||
return r.Content;
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception error)
|
||||
catch (Exception)
|
||||
{
|
||||
// Log
|
||||
}
|
||||
@@ -74,12 +98,21 @@ namespace Xylem.Common.Logic.SoftwareAccessHelper
|
||||
|
||||
}
|
||||
|
||||
public static bool PostBinaryFileRequestAsync(string Url, string FileName, byte[] Content, out string response, int? TimeoutMS = null)
|
||||
/// <summary>
|
||||
/// Web request with asynchronous operation, binary file byte array input, response string output
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="fileName"></param>
|
||||
/// <param name="content"></param>
|
||||
/// <param name="response"></param>
|
||||
/// <param name="timeoutMs"></param>
|
||||
/// <returns></returns>
|
||||
public static Boolean PostBinaryFileRequestAsync(String url, String fileName, Byte[] content, out String response, Int32? timeoutMs = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
var client = new RestClient(Url);
|
||||
var client = new RestClient(url);
|
||||
//WebRequest.DefaultWebProxy = null;
|
||||
var request = new RestRequest(Method.POST);
|
||||
|
||||
@@ -93,13 +126,13 @@ namespace Xylem.Common.Logic.SoftwareAccessHelper
|
||||
{
|
||||
Name = "file",
|
||||
Writer = (s) => {
|
||||
var stream = new MemoryStream(Content);
|
||||
var stream = new MemoryStream(content);
|
||||
stream.CopyTo(s);
|
||||
stream.Dispose();
|
||||
},
|
||||
FileName = FileName,
|
||||
FileName = fileName,
|
||||
ContentType = "multipart/form-data",
|
||||
ContentLength = Content.Length
|
||||
ContentLength = content.Length
|
||||
});
|
||||
|
||||
|
||||
@@ -109,7 +142,7 @@ namespace Xylem.Common.Logic.SoftwareAccessHelper
|
||||
|
||||
|
||||
}
|
||||
catch (Exception error)
|
||||
catch (Exception)
|
||||
{
|
||||
response = string.Empty;
|
||||
return false;
|
||||
@@ -118,12 +151,20 @@ namespace Xylem.Common.Logic.SoftwareAccessHelper
|
||||
|
||||
}
|
||||
|
||||
public static bool PostBinaryFileRequestAsync(string Url, string FileName, byte[] Content, int? TimeoutMS = null)
|
||||
/// <summary>
|
||||
/// Web request with asynchronous operation, binary file input
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="fileName"></param>
|
||||
/// <param name="content"></param>
|
||||
/// <param name="timeoutMs"></param>
|
||||
/// <returns></returns>
|
||||
public static Boolean PostBinaryFileRequestAsync(String url, String fileName, Byte[] content, Int32? timeoutMs = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
var client = new RestClient(Url);
|
||||
var client = new RestClient(url);
|
||||
// WebRequest.DefaultWebProxy = null;
|
||||
var request = new RestRequest(Method.POST);
|
||||
|
||||
@@ -137,13 +178,13 @@ namespace Xylem.Common.Logic.SoftwareAccessHelper
|
||||
{
|
||||
Name = "file",
|
||||
Writer = (s) => {
|
||||
var stream = new MemoryStream(Content);
|
||||
var stream = new MemoryStream(content);
|
||||
stream.CopyTo(s);
|
||||
stream.Dispose();
|
||||
},
|
||||
FileName = FileName,
|
||||
FileName = fileName,
|
||||
ContentType = "multipart/form-data",
|
||||
ContentLength = Content.Length
|
||||
ContentLength = content.Length
|
||||
});
|
||||
|
||||
|
||||
@@ -152,7 +193,7 @@ namespace Xylem.Common.Logic.SoftwareAccessHelper
|
||||
|
||||
|
||||
}
|
||||
catch (Exception error)
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
|
||||
@@ -161,20 +202,29 @@ namespace Xylem.Common.Logic.SoftwareAccessHelper
|
||||
}
|
||||
|
||||
|
||||
public static IRestResponse PostBinaryRequestAndGetResponseAsync(string Url, int? TimeoutMS = null, Dictionary<string, string> Files = null, Dictionary<string, string> Header = null)
|
||||
/// <summary>
|
||||
/// Web request with asynchronous operation
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="timeoutMs"></param>
|
||||
/// <param name="files"></param>
|
||||
/// <param name="header"></param>
|
||||
/// <returns></returns>
|
||||
public static IRestResponse PostBinaryRequestAndGetResponseAsync(String url, Int32? timeoutMs = null,
|
||||
Dictionary<String, String> files = null, Dictionary<String, String> header = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
var client = new RestClient(Url);
|
||||
var client = new RestClient(url);
|
||||
//WebRequest.DefaultWebProxy = null;
|
||||
var request = new RestRequest(Method.POST);
|
||||
|
||||
ServicePointManager.Expect100Continue = false;
|
||||
|
||||
if (Header != null)
|
||||
if (header != null)
|
||||
{
|
||||
foreach (var item in Header)
|
||||
foreach (var item in header)
|
||||
{
|
||||
if (item.Key != "Content-Type" && item.Key != "Accept")
|
||||
{
|
||||
@@ -187,13 +237,15 @@ namespace Xylem.Common.Logic.SoftwareAccessHelper
|
||||
|
||||
request.AddHeader("Content-Type", "multipart/form-data");
|
||||
|
||||
foreach (var item in Files)
|
||||
if (files != null)
|
||||
{
|
||||
request.AddFile(item.Key, item.Value);
|
||||
foreach (var item in files)
|
||||
{
|
||||
request.AddFile(item.Key, item.Value);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
var r = client.ExecuteAsPost(request, Method.POST.ToString());
|
||||
return r;
|
||||
|
||||
@@ -211,20 +263,29 @@ namespace Xylem.Common.Logic.SoftwareAccessHelper
|
||||
|
||||
}
|
||||
|
||||
public static IRestResponse PostRequestAndGetResponseAsync(string Url, int? TimeoutMS = null, object json = null, Dictionary<string, string> Header = null)
|
||||
/// <summary>
|
||||
/// Web request with asynchronous operation
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="timeoutMs"></param>
|
||||
/// <param name="json"></param>
|
||||
/// <param name="header"></param>
|
||||
/// <returns></returns>
|
||||
public static IRestResponse PostRequestAndGetResponseAsync(String url, Int32? timeoutMs = null, Object json = null,
|
||||
Dictionary<String, String> header = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
var client = new RestClient(Url);
|
||||
var client = new RestClient(url);
|
||||
//WebRequest.DefaultWebProxy = null;
|
||||
ServicePointManager.Expect100Continue = false;
|
||||
var request = new RestRequest(Method.POST);
|
||||
|
||||
|
||||
if (Header != null)
|
||||
if (header != null)
|
||||
{
|
||||
foreach (var item in Header)
|
||||
foreach (var item in header)
|
||||
{
|
||||
if (item.Key != "Content-Type" && item.Key != "Accept")
|
||||
{
|
||||
@@ -260,35 +321,52 @@ namespace Xylem.Common.Logic.SoftwareAccessHelper
|
||||
}
|
||||
|
||||
}
|
||||
public static string GetRequest(string Url, int? TimeoutMS = null, Dictionary<string, string> Header = null, bool isRetry = false)
|
||||
|
||||
/// <summary>
|
||||
/// Web request
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="timeoutMs"></param>
|
||||
/// <param name="header"></param>
|
||||
/// <param name="isRetry"></param>
|
||||
/// <returns></returns>
|
||||
public static String GetRequest(String url, Int32? timeoutMs = null, Dictionary<String, String> header = null, Boolean isRetry = false)
|
||||
{
|
||||
var tmp = 0;
|
||||
var ret = GetRequestWithError(Url,out tmp, TimeoutMS ,Header , isRetry);
|
||||
if (tmp != 200)
|
||||
Int32 temp;
|
||||
var ret = GetRequestWithError(url, out temp, timeoutMs, header, isRetry);
|
||||
if (temp != 200)
|
||||
{
|
||||
throw new ApplicationException($"WebRequest Status {tmp}");
|
||||
throw new ApplicationException($"WebRequest Status {temp}");
|
||||
}
|
||||
return ret;
|
||||
|
||||
|
||||
}
|
||||
public static string GetRequestWithError(string Url, out int errorCode, int? TimeoutMS = null, Dictionary<string, string> Header = null, bool isRetry = false)
|
||||
|
||||
/// <summary>
|
||||
/// Web request with asynchronous operation, error code response
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="errorCode"></param>
|
||||
/// <param name="timeoutMs"></param>
|
||||
/// <param name="header"></param>
|
||||
/// <param name="isRetry"></param>
|
||||
/// <returns></returns>
|
||||
public static String GetRequestWithError(String url, out Int32 errorCode, Int32? timeoutMs = null, Dictionary<String, String> header = null, Boolean isRetry = false)
|
||||
{
|
||||
|
||||
var client = new RestClient(Url);
|
||||
var client = new RestClient(url);
|
||||
//WebRequest.DefaultWebProxy = null;
|
||||
var request = new RestRequest(Method.GET);
|
||||
ServicePointManager.Expect100Continue = false;
|
||||
if (!TimeoutMS.HasValue)
|
||||
if (!timeoutMs.HasValue)
|
||||
{
|
||||
TimeoutMS = 800;
|
||||
timeoutMs = 800;
|
||||
}
|
||||
|
||||
request.Timeout = TimeoutMS.Value;
|
||||
request.Timeout = timeoutMs.Value;
|
||||
|
||||
if (Header != null)
|
||||
if (header != null)
|
||||
{
|
||||
foreach (var item in Header)
|
||||
foreach (var item in header)
|
||||
{
|
||||
if (item.Key != "Accept")
|
||||
{
|
||||
@@ -309,12 +387,11 @@ namespace Xylem.Common.Logic.SoftwareAccessHelper
|
||||
|
||||
if (r.StatusCode != 0) return r.Content;
|
||||
|
||||
|
||||
// Changed logic as it seems to be an endless loop THW 25-Mar-2021
|
||||
//if (presult.Status == IPStatus.Success || !isRetry)
|
||||
if (!isRetry)
|
||||
{
|
||||
return GetRequest(Url, TimeoutMS, Header, true);
|
||||
return GetRequest(url, timeoutMs, header, true);
|
||||
}
|
||||
// Removed throwing of exception as the caller cannot evaluate the result THW 25-Mar-2021
|
||||
//throw new ApplicationException($"Timeout { r.StatusDescription} {Environment.NewLine} PingState: {presult.Status} {Environment.NewLine} RoundtripTime: {presult.RoundtripTime} ");
|
||||
|
||||
Reference in New Issue
Block a user