доработка фонового сервиса

This commit is contained in:
eugeniy_ivanov 2022-08-18 15:48:20 +05:00
parent babd1076a4
commit 118ed4e701
2 changed files with 51 additions and 26 deletions

View File

@ -35,6 +35,7 @@
<PackageReference Include="Mapster" Version="7.3.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.18.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.3" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.18.0" />
</ItemGroup>

View File

@ -6,6 +6,7 @@ using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Threading;
@ -15,7 +16,7 @@ namespace AsbCloudInfrastructure.Services.Subsystems
{
#nullable enable
internal class SubsystemOperationTimeBackgroundService : BackgroundService
{
{
private readonly string connectionString;
private readonly TimeSpan period = TimeSpan.FromHours(1);
private const int idSubsytemTorqueMaster = 65537;
@ -102,43 +103,62 @@ namespace AsbCloudInfrastructure.Services.Subsystems
return affected;
}
private static async Task<IEnumerable<SubsystemOperationTime>?> OperationTimeSaubAsync(int idTelemetry, DateTimeOffset begin, IAsbCloudDbContext db, CancellationToken token)
{
var dateStart = db.TelemetryDataSaub
.AsNoTracking()
.Where(d => (d.IdTelemetry == idTelemetry) & (d.DateTime >= begin))
.Min(d => d.DateTime);
var depthStart = db.TelemetryDataSaub
.AsNoTracking()
.Where(d => (d.IdTelemetry == idTelemetry) & (d.DateTime >= begin))
.Min(d => d.WellDepth);
var operationTimeSpinWithDepth =
$"select tt.date, tt.mode, tt.mode_prev, tt.well_depth" +
{
var operationTimeSaub =
$"select tt.date, tt.mode, tt.well_depth" +
$" from (select date, mode, well_depth, lag(mode,1) over (order by date) as mode_prev" +
$" from t_telemetry_data_saub where id_telemetry = {idTelemetry}) as tt" +
$" where tt.mode != tt.mode_prev and tt.date >='{begin}' order by tt.date;";
$" from t_telemetry_data_saub where id_telemetry = @idTelemetry) as tt" +
$" where tt.mode != tt.mode_prev and tt.date >=@begin order by tt.date;";
using var command = db.Database.GetDbConnection().CreateCommand();
command.CommandText = operationTimeSpinWithDepth;
command.
command.CommandText = operationTimeSaub;
SqlParameter telemetry = new SqlParameter("@idTelemetry", idTelemetry);
command.Parameters.Add(telemetry);
SqlParameter dateStart = new SqlParameter("@begin", begin);
command.Parameters.Add(dateStart);
db.Database.OpenConnection();
using var result = await command.ExecuteReaderAsync(token);
var resultSubsystemOperationTime = new List<SubsystemOperationTime>();
var telemetryOpearationSaubSubsystems = new List<SubsystemsSpinWithDepth>();
if (result.HasRows)
{
while (result.Read())
{
var itemEntity = new SubsystemOperationTime()
var itemEntity = new SubsystemsSpinWithDepth()
{
Date = result.GetFieldValue<DateTimeOffset>(0),
IdSubsystem = result.GetFieldValue<short>(1)+1,
Depth = result.GetFieldValue<float>(2)
};
telemetryOpearationSaubSubsystems.Add(itemEntity);
}
}
var resultSubsystemOperationTime = new List<SubsystemOperationTime>();
var firstItem = telemetryOpearationSaubSubsystems.FirstOrDefault();
if (firstItem is null)
return null;
int idSubsystem = firstItem.IdSubsystem;
DateTimeOffset dateBegin = firstItem.Date;
float? depthStart = firstItem.Depth;
for (int i = 1; i < telemetryOpearationSaubSubsystems.Count; i++)
{
if (telemetryOpearationSaubSubsystems[i].IdSubsystem != idSubsystem)
{
var operationTimeItem = new SubsystemOperationTime()
{
IdTelemetry = idTelemetry,
DateStart = dateStart,
DateEnd = result.GetFieldValue<DateTimeOffset>(0),
DateStart = dateBegin,
IdSubsystem = telemetryOpearationSaubSubsystems[i - 1].IdSubsystem,
DateEnd = telemetryOpearationSaubSubsystems[i].Date,
DepthStart = depthStart,
DepthEnd = result.GetFieldValue<float>(3),
IdSubsystem = result.GetFieldValue<short>(2) + 1
DepthEnd = telemetryOpearationSaubSubsystems[i].Depth
};
dateStart = itemEntity.DateEnd;
depthStart = itemEntity.DepthStart;
resultSubsystemOperationTime.Add(itemEntity);
dateBegin = telemetryOpearationSaubSubsystems[i].Date;
depthStart = telemetryOpearationSaubSubsystems[i].Depth;
idSubsystem = telemetryOpearationSaubSubsystems[i].IdSubsystem;
if (telemetryOpearationSaubSubsystems[i - 1].IdSubsystem != 0)
{
resultSubsystemOperationTime.Add(operationTimeItem);
}
}
}
return resultSubsystemOperationTime;
@ -157,6 +177,10 @@ namespace AsbCloudInfrastructure.Services.Subsystems
$"where mode!=mode_pre or state != state_pre order by \"date\";";
using var command = db.Database.GetDbConnection().CreateCommand();
command.CommandText = operationTimeSpinWithDepth;
SqlParameter telemetry = new SqlParameter("@idTelemetry", idTelemetry);
command.Parameters.Add(telemetry);
SqlParameter dateStart = new SqlParameter("@begin", begin);
command.Parameters.Add(dateStart);
db.Database.OpenConnection();
using var result = await command.ExecuteReaderAsync(token);
var telemetryOpearationSpinSubsystems = new List<SubsystemsSpinWithDepth>();