From 0e4c2c9eb35169a86c3749b7c16828a4bd99e4f8 Mon Sep 17 00:00:00 2001 From: eugeniy_ivanov Date: Tue, 20 Sep 2022 22:24:51 +0500 Subject: [PATCH] =?UTF-8?q?-=20=D0=B2=20=D0=BA=D0=BE=D0=BD=D1=82=D1=80?= =?UTF-8?q?=D0=BE=D0=BB=D0=BB=D0=B5=D1=80=20=D0=BD=D0=B0=D1=80=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=82=D0=BA=D0=B8=20=D0=BF=D0=BE=D0=B4=D1=81=D0=B8=D1=81?= =?UTF-8?q?=D1=82=D0=B5=D0=BC=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=B2=D0=B0=D0=BB=D0=B8=D0=B4=D0=B0=D1=82=D0=BE=D1=80?= =?UTF-8?q?=20=D1=81=D0=BE=D0=B3=D0=BB=D0=B0=D1=81=D0=BD=D0=BE=20=D0=BF?= =?UTF-8?q?=D0=BE=D1=81=D1=82=D0=B0=D0=BD=D0=BE=D0=B2=D0=BA=D0=B8=20=D0=B7?= =?UTF-8?q?=D0=B0=D0=B4=D0=B0=D1=87=D0=B8=20-=20=D0=B8=D0=B7=D0=BC=D0=B5?= =?UTF-8?q?=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B0=D0=BB=D0=B3=D0=BE=D1=80?= =?UTF-8?q?=D0=B8=D1=82=D0=BC=D0=B0=20=D0=BD=D0=B0=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=BA=D0=B8=20=D0=BF=D0=BE=D0=B4=D1=81=D0=B8=D1=81=D1=82?= =?UTF-8?q?=D0=B5=D0=BC=20=D0=A1=D0=90=D0=A3=D0=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...SubsystemOperationTimeBackgroundService.cs | 188 ++++++++++++++---- .../SubsystemOperationTimeController.cs | 22 ++ 2 files changed, 167 insertions(+), 43 deletions(-) diff --git a/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs b/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs index db410433..9c7e2a97 100644 --- a/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs +++ b/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs @@ -108,19 +108,63 @@ namespace AsbCloudInfrastructure.Services.Subsystems } private static async Task> 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) - return 1; - - return null; + return true; + return false; } - static bool IsSubsystemMse(short state) + static bool IsSubsystemMse(short? state) { + if (state is null) + return false; if ((state & 1) > 0) return true; return false; + } + + static List getSubsystemOperationTimes (List dataRows, Predicate satisfyCondition, int idSubsystem, int idTelemetry) + { + if (dataRows is null) + return new List(); + if (dataRows.Count < 2) + return new List(); + var listSubsystemOperationTime = new List(); + var foundSubsystem = satisfyCondition(dataRows[0]); + var dateStart = dataRows[0].Date; + var depthStart = dataRows[0].Depth; + + for (int i = 1; i(32); - - if (result.Read()) + var subsystemOperationTime = new List(); + var dataRowList = new List(); + while (result.Read()) { - var mode = result.GetFieldValue(1); - //var mseState = result.GetFieldValue(3); - var idSubsystem = GetSubsytemId(mode); - var foundSubsystem = idSubsystem.HasValue; - var dateStart = result.GetFieldValue(0); - var depthStart = result.GetFieldValue(2); - - while (result.Read()) + var dateRowItem = new DataRow() { - var dateEnd = result.GetFieldValue(0); - var depthEnd = result.GetFieldValue(2); - var currentMode = result.GetFieldValue(1); - var currentSubsystemAkb = GetSubsytemId(currentMode); - var endSubsystemAkb = !currentSubsystemAkb.HasValue; - 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; - } - } + Date = result.GetFieldValue(0), + Mode = result.GetFieldValue(1), + Depth = result.GetFieldValue(2), + State = result.GetFieldValue(3) + }; + dataRowList.Add(dateRowItem); } + + 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(1); + //var idSubsystem = GetSubsytemId(mode); + //var foundSubsystem = idSubsystem.HasValue; + //var dateStart = result.GetFieldValue(0); + //var depthStart = result.GetFieldValue(2); + ////MSE Subsystem + //short? mseState = result.GetFieldValue(3); + //var dateStartMse = result.GetFieldValue(0); + //var depthStartMse = result.GetFieldValue(2); + //var subsystemMse = IsSubsystemMse(mseState); + + //while (result.Read()) + //{ + // var dateEnd = result.GetFieldValue(0); + // var depthEnd = result.GetFieldValue(2); + // var currentMode = result.GetFieldValue(1); + // var currentSubsystemAkb = GetSubsytemId(currentMode); + // var endSubsystemAkb = !currentSubsystemAkb.HasValue; + // //MSE + // //var dateEndMse = result.GetFieldValue(0); + // //var depthEndMse = result.GetFieldValue(2); + // short? currentState = result.GetFieldValue(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; } @@ -280,5 +373,14 @@ namespace AsbCloudInfrastructure.Services.Subsystems 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 } diff --git a/AsbCloudWebApi/Controllers/Subsystems/SubsystemOperationTimeController.cs b/AsbCloudWebApi/Controllers/Subsystems/SubsystemOperationTimeController.cs index 881e738b..f6f8a8a8 100644 --- a/AsbCloudWebApi/Controllers/Subsystems/SubsystemOperationTimeController.cs +++ b/AsbCloudWebApi/Controllers/Subsystems/SubsystemOperationTimeController.cs @@ -8,6 +8,8 @@ using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; +using AsbCloudInfrastructure; +using System; namespace AsbCloudWebApi.Controllers.Subsystems { @@ -37,6 +39,8 @@ namespace AsbCloudWebApi.Controllers.Subsystems { if (!await UserHasAccesToWellAsync(request.IdWell, token)) return Forbid(); + if (!await TimeRequestValidate(request, token)) + return BadRequest(); var subsystemResult = await subsystemOperationTimeService.GetStatAsync(request, token); return Ok(subsystemResult); } @@ -67,6 +71,9 @@ namespace AsbCloudWebApi.Controllers.Subsystems { if (!await UserHasAccesToWellAsync(request.IdWell, token)) return Forbid(); + if (!await TimeRequestValidate(request, token)) + return BadRequest(); + var result = await subsystemOperationTimeService.GetOperationTimeAsync(request, token); return Ok(result); } @@ -99,5 +106,20 @@ namespace AsbCloudWebApi.Controllers.Subsystems return true; return false; } + protected async Task 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)