- в контроллер наработки подсистем добавлен валидатор согласно постановки задачи

- изменения алгоритма наработки подсистем САУБ
This commit is contained in:
eugeniy_ivanov 2022-09-20 22:24:51 +05:00
parent c6a49056bd
commit 0e4c2c9eb3
2 changed files with 167 additions and 43 deletions

View File

@ -108,19 +108,63 @@ namespace AsbCloudInfrastructure.Services.Subsystems
} }
private static async Task<IEnumerable<SubsystemOperationTime>> OperationTimeSaubAsync(int idTelemetry, DateTimeOffset begin, IAsbCloudDbContext db, CancellationToken token) private static async Task<IEnumerable<SubsystemOperationTime>> OperationTimeSaubAsync(int idTelemetry, DateTimeOffset begin, IAsbCloudDbContext db, CancellationToken token)
{ {
static int? GetSubsytemId(short mode) static bool isSubsytemAkb(short? mode)
{ {
if (mode is null)
return false;
if (mode == 1 | mode == 3) if (mode == 1 | mode == 3)
return 1; return true;
return false;
return null;
} }
static bool IsSubsystemMse(short state) static bool IsSubsystemMse(short? state)
{ {
if (state is null)
return false;
if ((state & 1) > 0) if ((state & 1) > 0)
return true; return true;
return false; return false;
}
static List<SubsystemOperationTime> getSubsystemOperationTimes (List<DataRow> dataRows, Predicate<DataRow> satisfyCondition, int idSubsystem, int idTelemetry)
{
if (dataRows is null)
return new List<SubsystemOperationTime>();
if (dataRows.Count < 2)
return new List<SubsystemOperationTime>();
var listSubsystemOperationTime = new List<SubsystemOperationTime>();
var foundSubsystem = satisfyCondition(dataRows[0]);
var dateStart = dataRows[0].Date;
var depthStart = dataRows[0].Depth;
for (int i = 1; i<dataRows.Count; i++)
{
var dateEnd = dataRows[i].Date;
var depthEnd = dataRows[i].Depth;
var currentSatisfy = satisfyCondition(dataRows[i]);
var endSubsystem = !currentSatisfy;
if (foundSubsystem && endSubsystem)
{
var operationTimeItem = new SubsystemOperationTime()
{
IdTelemetry = idTelemetry,
IdSubsystem = idSubsystem,
DateStart = dateStart,
DateEnd = dateEnd,
DepthStart = depthStart,
DepthEnd = depthEnd
};
listSubsystemOperationTime.Add(operationTimeItem);
foundSubsystem = false;
}
if (currentSatisfy && !foundSubsystem)
{
dateStart = dateEnd;
foundSubsystem = true;
depthStart = depthEnd;
}
}
return listSubsystemOperationTime;
} }
var query = var query =
@ -149,47 +193,96 @@ namespace AsbCloudInfrastructure.Services.Subsystems
using var result = await command.ExecuteReaderAsync(token); using var result = await command.ExecuteReaderAsync(token);
var subsystemOperationTime = new List<SubsystemOperationTime>(32); var subsystemOperationTime = new List<SubsystemOperationTime>();
var dataRowList = new List<DataRow>();
if (result.Read()) while (result.Read())
{ {
var mode = result.GetFieldValue<short>(1); var dateRowItem = new DataRow()
//var mseState = result.GetFieldValue<short>(3);
var idSubsystem = GetSubsytemId(mode);
var foundSubsystem = idSubsystem.HasValue;
var dateStart = result.GetFieldValue<DateTimeOffset>(0);
var depthStart = result.GetFieldValue<float>(2);
while (result.Read())
{ {
var dateEnd = result.GetFieldValue<DateTimeOffset>(0); Date = result.GetFieldValue<DateTimeOffset>(0),
var depthEnd = result.GetFieldValue<float>(2); Mode = result.GetFieldValue<short?>(1),
var currentMode = result.GetFieldValue<short>(1); Depth = result.GetFieldValue<float?>(2),
var currentSubsystemAkb = GetSubsytemId(currentMode); State = result.GetFieldValue<short?>(3)
var endSubsystemAkb = !currentSubsystemAkb.HasValue; };
if (foundSubsystem && endSubsystemAkb) dataRowList.Add(dateRowItem);
{
var operationTimeItem = new SubsystemOperationTime()
{
IdTelemetry = idTelemetry,
IdSubsystem = idSubsystem.Value,
DateStart = dateStart,
DateEnd = dateEnd,
DepthStart = depthStart,
DepthEnd = depthEnd
};
subsystemOperationTime.Add(operationTimeItem);
foundSubsystem = false;
}
if (currentSubsystemAkb.HasValue && !foundSubsystem)
{
idSubsystem = currentSubsystemAkb;
dateStart = dateEnd;
foundSubsystem = true;
depthStart = depthEnd;
}
}
} }
var akbOperationTimes = getSubsystemOperationTimes(dataRowList, d => isSubsytemAkb(d.Mode), idSubsytemAkb, idTelemetry);
var mseOperationTimes = getSubsystemOperationTimes(dataRowList, d => IsSubsystemMse(d.State), idSubsytemMse, idTelemetry);
subsystemOperationTime.AddRange(akbOperationTimes);
subsystemOperationTime.AddRange(mseOperationTimes);
//if (result.Read())
//{
//short? mode = result.GetFieldValue<short?>(1);
//var idSubsystem = GetSubsytemId(mode);
//var foundSubsystem = idSubsystem.HasValue;
//var dateStart = result.GetFieldValue<DateTimeOffset>(0);
//var depthStart = result.GetFieldValue<float>(2);
////MSE Subsystem
//short? mseState = result.GetFieldValue<short?>(3);
//var dateStartMse = result.GetFieldValue<DateTimeOffset>(0);
//var depthStartMse = result.GetFieldValue<float>(2);
//var subsystemMse = IsSubsystemMse(mseState);
//while (result.Read())
//{
// var dateEnd = result.GetFieldValue<DateTimeOffset>(0);
// var depthEnd = result.GetFieldValue<float>(2);
// var currentMode = result.GetFieldValue<short?>(1);
// var currentSubsystemAkb = GetSubsytemId(currentMode);
// var endSubsystemAkb = !currentSubsystemAkb.HasValue;
// //MSE
// //var dateEndMse = result.GetFieldValue<DateTimeOffset>(0);
// //var depthEndMse = result.GetFieldValue<float>(2);
// short? currentState = result.GetFieldValue<short?>(3);
// var endSubsystemMse = !IsSubsystemMse(currentState);
// if (foundSubsystem && endSubsystemAkb)
// {
// var operationTimeItem = new SubsystemOperationTime()
// {
// IdTelemetry = idTelemetry,
// IdSubsystem = idSubsystem.Value,
// DateStart = dateStart,
// DateEnd = dateEnd,
// DepthStart = depthStart,
// DepthEnd = depthEnd
// };
// subsystemOperationTime.Add(operationTimeItem);
// foundSubsystem = false;
// }
// if (currentSubsystemAkb.HasValue && !foundSubsystem)
// {
// idSubsystem = currentSubsystemAkb;
// dateStart = dateEnd;
// foundSubsystem = true;
// depthStart = depthEnd;
// }
// if (subsystemMse && endSubsystemMse)
// {
// var operationTimeItemMse = new SubsystemOperationTime()
// {
// IdTelemetry = idTelemetry,
// IdSubsystem = 2,
// DateStart = dateStartMse,
// DateEnd = dateEnd,
// DepthStart = depthStartMse,
// DepthEnd = depthEnd
// };
// subsystemOperationTime.Add(operationTimeItemMse);
// subsystemMse = false;
// }
// if (currentState.HasValue && !subsystemMse)
// {
// dateStartMse = dateEnd;
// subsystemMse = true;
// depthStartMse = depthEnd;
// }
//}
//}
return subsystemOperationTime; return subsystemOperationTime;
} }
@ -280,5 +373,14 @@ namespace AsbCloudInfrastructure.Services.Subsystems
return subsystemOperationTime; return subsystemOperationTime;
} }
} }
internal class DataRow
{
public DateTimeOffset Date { get; set; }
public short? Mode { get; set; }
public float? Depth { get; set; }
public short? State { get; set; }
}
#nullable disable #nullable disable
} }

View File

@ -8,6 +8,8 @@ using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using AsbCloudInfrastructure;
using System;
namespace AsbCloudWebApi.Controllers.Subsystems namespace AsbCloudWebApi.Controllers.Subsystems
{ {
@ -37,6 +39,8 @@ namespace AsbCloudWebApi.Controllers.Subsystems
{ {
if (!await UserHasAccesToWellAsync(request.IdWell, token)) if (!await UserHasAccesToWellAsync(request.IdWell, token))
return Forbid(); return Forbid();
if (!await TimeRequestValidate(request, token))
return BadRequest();
var subsystemResult = await subsystemOperationTimeService.GetStatAsync(request, token); var subsystemResult = await subsystemOperationTimeService.GetStatAsync(request, token);
return Ok(subsystemResult); return Ok(subsystemResult);
} }
@ -67,6 +71,9 @@ namespace AsbCloudWebApi.Controllers.Subsystems
{ {
if (!await UserHasAccesToWellAsync(request.IdWell, token)) if (!await UserHasAccesToWellAsync(request.IdWell, token))
return Forbid(); return Forbid();
if (!await TimeRequestValidate(request, token))
return BadRequest();
var result = await subsystemOperationTimeService.GetOperationTimeAsync(request, token); var result = await subsystemOperationTimeService.GetOperationTimeAsync(request, token);
return Ok(result); return Ok(result);
} }
@ -99,5 +106,20 @@ namespace AsbCloudWebApi.Controllers.Subsystems
return true; return true;
return false; return false;
} }
protected async Task<bool> TimeRequestValidate(SubsystemOperationTimeRequest request, CancellationToken token)
{
var well = await wellService.GetOrDefaultAsync(request.IdWell, token);
if (well is not null)
{
var ltDate = (DateTimeOffset)request.LtDate;
var utcDateRequest = ltDate.ToRemoteDateTime(well.Timezone.Hours);
var DateNow = ((DateTimeOffset)DateTime.Now).ToRemoteDateTime(well.Timezone.Hours);
if (utcDateRequest.AddHours(2)<DateNow)
{
return true;
}
}
return false;
}
} }
} }