Merge branch 'cordonel_pressure_sensor_test'

This commit is contained in:
Stoyan Zlatev
2023-05-22 14:33:13 +02:00
8 changed files with 323 additions and 12 deletions
@@ -97,7 +97,7 @@ namespace Xylem.Common.Service.MeterProcessState
// those comments into the generated docs and UI. You can enable this by providing the path to one or
// more Xml comment files.
//
//c.IncludeXmlComments(GetXmlCommentsPath());
// c.IncludeXmlComments(GetXmlCommentsPath());
// Swashbuckle makes a best attempt at generating Swagger compliant JSON schemas for the various types
// exposed in your API. However, there may be occasions when more control of the output is needed.
@@ -1,8 +1,8 @@
using System.Web.Http;
using Microsoft.Owin.Security.OAuth;
namespace Service.MeterProcessState
namespace Service.MeterProcessState
{
using System.Web.Http;
using Microsoft.Owin.Security.OAuth;
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
@@ -0,0 +1,12 @@
namespace Xylem.Common.Service.MeterProcessState
{
using System.Configuration;
public class Appsettings
{
public class ConnectionStrings
{
public static string Default => ConfigurationManager.ConnectionStrings[nameof(Default)].ConnectionString;
}
}
}
@@ -0,0 +1,261 @@
namespace Xylem.Common.Service.MeterProcessState.Controllers
{
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Http;
using Xylem.Common.Service.MeterProcessState.Models;
[RoutePrefix("api/CordonelPressureSensorTest")]
public class CordonelPressureSensorTestController : ApiController
{
[HttpGet]
[Route(nameof(GetTestSpec))]
public async Task<IHttpActionResult> GetTestSpec()
{
var actionResult = default(IHttpActionResult);
var testSpec = default(CordonelTestSpec);
using (var sqlConnection = new SqlConnection(Appsettings.ConnectionStrings.Default))
{
sqlConnection.FireInfoMessageEventOnUserErrors = true;
sqlConnection.InfoMessage += (sender, args) =>
{
var messages = args
.Errors
.Cast<SqlError>()
.Select(x => x.Message);
// TODO: Switch to expected error messages list.
actionResult = this.Json(args.Errors);
};
await sqlConnection.OpenAsync();
using (var sqlCommand = sqlConnection.CreateCommand())
{
sqlCommand.CommandText = $@"
SELECT ...
, ...
FROM [x]";
using (var sqlDataReader = await sqlCommand.ExecuteReaderAsync())
{
while (await sqlDataReader.ReadAsync())
{
testSpec = new CordonelTestSpec
{
NumberOfMeasurements = await sqlDataReader.GetIntAsync(),
TimeMeasurementsInS = await sqlDataReader.GetIntAsync(),
MaxDeviationInBar = await sqlDataReader.GetFloatAsync(),
};
}
if (actionResult is null)
{
actionResult = this.Json(testSpec);
}
}
}
}
return actionResult;
}
/// <summary>
/// Nach dem Test wird für jede PcbID diese Funktion aufgerufen und die daten in die Datenbank übermittelt
/// </summary>
/// <returns>TODO: return value</returns>
[HttpPost]
[Route(nameof(PostTestResults))]
public async Task<IHttpActionResult> PostTestResults([FromBody] CordonelTestResult cordonelTestResult)
{
if (!this.ModelState.IsValid)
{
return this.BadRequest(this.ModelState);
}
var actionResult = default(IHttpActionResult);
var rowsAffected = -1;
using (var sqlConnection = new SqlConnection(Appsettings.ConnectionStrings.Default))
{
sqlConnection.FireInfoMessageEventOnUserErrors = true;
sqlConnection.InfoMessage += (sender, args) =>
{
var messages = args
.Errors
.Cast<SqlError>()
.Select(x => x.Message);
// TODO: Switch to expected error messages list.
actionResult = this.Json(args.Errors);
};
await sqlConnection.OpenAsync();
using (var sqlCommand = sqlConnection.CreateCommand())
{
sqlCommand.CommandText = $@"
INSERT INTO [x] (
...
, ...)
VALUES (@{nameof(cordonelTestResult.PcbId)}
, @{nameof(cordonelTestResult.TestDate)}
, @{nameof(cordonelTestResult.AvgPressureInBar)}
, @{nameof(cordonelTestResult.TestPassed)})";
sqlCommand.Parameters.AddWithValue(nameof(cordonelTestResult.PcbId), cordonelTestResult.PcbId);
sqlCommand.Parameters.AddWithValue(nameof(cordonelTestResult.TestDate), cordonelTestResult.TestDate);
sqlCommand.Parameters.AddWithValue(nameof(cordonelTestResult.AvgPressureInBar), cordonelTestResult.AvgPressureInBar);
sqlCommand.Parameters.AddWithValue(nameof(cordonelTestResult.TestPassed), cordonelTestResult.TestPassed);
rowsAffected = await sqlCommand.ExecuteNonQueryAsync();
}
}
if (rowsAffected > 0)
{
return this.Ok();
}
return this.BadRequest();
}
/// <summary>
/// Nach dem Test wird für jede PcbID diese Funktion aufgerufen und die daten in die Datenbank übermittelt
/// </summary>
/// <returns>TODO: return value</returns>
[HttpGet]
[Route(nameof(GetTestResults) + "/{pcbId}")]
public async Task<IHttpActionResult> GetTestResults(int pcbId)
{
var actionResult = default(IHttpActionResult);
var testResults = new List<CordonelTestResult>();
using (var sqlConnection = new SqlConnection(Appsettings.ConnectionStrings.Default))
{
sqlConnection.FireInfoMessageEventOnUserErrors = true;
sqlConnection.InfoMessage += (sender, args) =>
{
var messages = args
.Errors
.Cast<SqlError>()
.Select(x => x.Message);
// TODO: Switch to expected error messages list.
actionResult = this.Json(args.Errors);
};
await sqlConnection.OpenAsync();
using (var sqlCommand = sqlConnection.CreateCommand())
{
sqlCommand.CommandText = $@"
SELECT ...
, ...
FROM [x]
WHERE [PcbId] = {nameof(pcbId)}";
sqlCommand.Parameters.AddWithValue(nameof(pcbId), pcbId);
using (var sqlDataReader = await sqlCommand.ExecuteReaderAsync())
{
if (await sqlDataReader.ReadAsync())
{
testResults.Add(new CordonelTestResult
{
PcbId = await sqlDataReader.GetIntAsync(),
TestDate = await sqlDataReader.GetDateTimeAsync(),
AvgPressureInBar = await sqlDataReader.GetFloatAsync(),
TestPassed = await sqlDataReader.GetBoolAsync(),
});
}
if (actionResult is null)
{
actionResult = this.Json(testResults);
}
}
}
}
return actionResult;
}
}
public static class SqlDataReaderExtensions
{
private static readonly ConcurrentDictionary<int, int> indexes = new ConcurrentDictionary<int, int>();
public static async Task<int> GetIntAsync(this SqlDataReader sqlDataReader)
{
var hashCode = sqlDataReader.GetHashCode();
var index = indexes.GetOrAdd(hashCode, 0);
var value = default(int);
if (!await sqlDataReader.IsDBNullAsync(index))
{
value = sqlDataReader.GetInt32(index);
}
indexes.AddOrUpdate(hashCode, index, (_1, _2) => index + 1);
return value;
}
public static async Task<DateTime> GetDateTimeAsync(this SqlDataReader sqlDataReader)
{
var index = sqlDataReader
.GetHashCode()
.GetIndex();
var value = default(DateTime);
if (!await sqlDataReader.IsDBNullAsync(index))
{
value = sqlDataReader.GetDateTime(index);
}
return value;
}
public static async Task<float> GetFloatAsync(this SqlDataReader sqlDataReader)
{
var index = sqlDataReader
.GetHashCode()
.GetIndex();
var value = default(float);
if (!await sqlDataReader.IsDBNullAsync(index))
{
value = sqlDataReader.GetFloat(index);
}
return value;
}
public static async Task<bool> GetBoolAsync(this SqlDataReader sqlDataReader)
{
var index = sqlDataReader
.GetHashCode()
.GetIndex();
var value = default(bool);
if (!await sqlDataReader.IsDBNullAsync(index))
{
value = sqlDataReader.GetBoolean(index);
}
return value;
}
private static int GetIndex(this int hashCode)
{
var index = indexes.GetOrAdd(hashCode, 0);
indexes.AddOrUpdate(hashCode, index, (_1, _2) => index + 1);
return index;
}
}
}
@@ -1,11 +1,12 @@
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
namespace Service.MeterProcessState
namespace Service.MeterProcessState
{
public class WebApiApplication : System.Web.HttpApplication
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
public class WebApiApplication : HttpApplication
{
protected void Application_Start()
{
@@ -37,6 +37,7 @@
<WarningLevel>4</WarningLevel>
<LangVersion>7</LangVersion>
<DocumentationFile>bin\Xylem.Common.Service.MeterProcessState.xml</DocumentationFile>
<NoWarn>1591</NoWarn>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
@@ -185,6 +186,7 @@
<Compile Include="..\..\.shared\SharedAssemblyInfo.cs">
<Link>Properties\SharedAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Appsettings.cs" />
<Compile Include="App_Start\BundleConfig.cs" />
<Compile Include="App_Start\FilterConfig.cs" />
<Compile Include="App_Start\IdentityConfig.cs" />
@@ -220,6 +222,7 @@
<Compile Include="Areas\HelpPage\SampleGeneration\SampleDirection.cs" />
<Compile Include="Areas\HelpPage\SampleGeneration\TextSample.cs" />
<Compile Include="Areas\HelpPage\XmlDocumentationProvider.cs" />
<Compile Include="Controllers\CordonelPressureSensorTestController.cs" />
<Compile Include="Controllers\PushControler.cs" />
<Compile Include="Controllers\FinalCheckController.cs" />
<Compile Include="Controllers\FileUpToDateController.cs" />
@@ -277,6 +280,7 @@
</Compile>
<Compile Include="Models\AccountBindingModels.cs" />
<Compile Include="Models\AccountViewModels.cs" />
<Compile Include="Models\Cordonel.cs" />
<Compile Include="Models\IdentityModels.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Providers\ApplicationOAuthProvider.cs" />
@@ -343,6 +347,7 @@
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\" />
<Folder Include="Views\CordonelPressureSensorTest\" />
</ItemGroup>
<ItemGroup>
<Content Include="fonts\glyphicons-halflings-regular.woff" />
@@ -10,6 +10,14 @@
<IISExpressWindowsAuthentication />
<IISExpressUseClassicPipelineMode />
<UseGlobalApplicationHostFile />
<Controller_SelectedScaffolderID>MvcControllerEmptyScaffolder</Controller_SelectedScaffolderID>
<Controller_SelectedScaffolderCategoryPath>root/Controller</Controller_SelectedScaffolderCategoryPath>
<WebStackScaffolding_ControllerDialogWidth>600</WebStackScaffolding_ControllerDialogWidth>
<WebStackScaffolding_IsLayoutPageSelected>True</WebStackScaffolding_IsLayoutPageSelected>
<WebStackScaffolding_IsPartialViewSelected>False</WebStackScaffolding_IsPartialViewSelected>
<WebStackScaffolding_IsReferencingScriptLibrariesSelected>True</WebStackScaffolding_IsReferencingScriptLibrariesSelected>
<WebStackScaffolding_LayoutPageFile />
<WebStackScaffolding_IsAsyncSelected>False</WebStackScaffolding_IsAsyncSelected>
</PropertyGroup>
<ProjectExtensions>
<VisualStudio>
@@ -0,0 +1,24 @@
namespace Xylem.Common.Service.MeterProcessState.Models
{
using System;
public class CordonelTestSpec
{
public int NumberOfMeasurements { get; set; }
public int TimeMeasurementsInS { get; set; }
public float MaxDeviationInBar { get; set; }
}
public class CordonelTestResult
{
public int PcbId { get; set; }
public DateTime TestDate { get; set; }
public float AvgPressureInBar { get; set; }
public bool TestPassed { get; set; }
}
}