diff --git a/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeCalcWorkFactory.cs b/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeCalcWorkFactory.cs index 74d19967..35216280 100644 --- a/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeCalcWorkFactory.cs +++ b/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeCalcWorkFactory.cs @@ -23,9 +23,8 @@ namespace AsbCloudInfrastructure.Services.Subsystems private const int idSubsytemTorqueMaster = 65537; private const int idSubsytemSpinMaster = 65536; - private const int idSubsytemAkb = 1; - private const int idSubsytemAkbRototor = 11; - private const int idSubsytemAkbSlide = 12; + private const int idSubsystemAPDRotor = 11; + private const int idSubsystemAPDSlide = 12; private const int idSubsytemMse = 2; public static WorkPeriodic MakeWork() @@ -103,32 +102,11 @@ namespace AsbCloudInfrastructure.Services.Subsystems private static async Task> OperationTimeSaubAsync(int idTelemetry, DateTimeOffset begin, IAsbCloudDbContext db, CancellationToken token) { - static bool isSubsytemAkbRotor(short? mode) - { - if (mode is null) - return false; - if (mode == 1) - return true; - return false; - } + static bool isSubsytemAkbRotor(short? mode) => mode == 1; - static bool isSubsytemAkbSlide(short? mode) - { - if (mode is null) - return false; - if (mode == 3) - return true; - return false; - } + static bool isSubsytemAkbSlide(short? mode) => mode == 3; - static bool IsSubsystemMse(short? state) - { - if (state is null) - return false; - if ((state & 1) > 0) - return true; - return false; - } + static bool IsSubsystemMse(short? state) => (state & 1) > 0; var query = $"select tt.date, tt.mode, tt.well_depth, tt.mse_state " + @@ -149,10 +127,9 @@ namespace AsbCloudInfrastructure.Services.Subsystems using var result = await ExecuteReaderAsync(db, query, token); var subsystemsOperationTimes = new List(); - - (bool isEnable, DateTimeOffset date, float depth) akbPreRotor = default; - (bool isEnable, DateTimeOffset date, float depth) akbPreSlide = default; - (bool isEnable, DateTimeOffset date, float depth) msePre = default; + var detectorRotor = new SubsystemDetector(idTelemetry, idSubsystemAPDRotor, isSubsytemAkbRotor, IsValid); + var detectorSlide = new SubsystemDetector(idTelemetry, idSubsystemAPDSlide, isSubsytemAkbSlide, IsValid); + var detectorMse = new SubsystemDetector(idTelemetry, idSubsytemMse, IsSubsystemMse, IsValid); while (result.Read()) { @@ -165,66 +142,17 @@ namespace AsbCloudInfrastructure.Services.Subsystems var date = result.GetFieldValue(0); var depth = result.GetFieldValue(2); - if (!akbPreRotor.isEnable && isAkbRotorEnable) - { - akbPreRotor = (true, date, depth); - } - else if (akbPreRotor.isEnable && !isAkbRotorEnable) - { - var subsystemOperationTime = new SubsystemOperationTime - { - IdTelemetry = idTelemetry, - IdSubsystem = idSubsytemAkbRototor, - DateStart = akbPreRotor.date, - DateEnd = date, - DepthStart = akbPreRotor.depth, - DepthEnd = depth, - }; - if (IsValid(subsystemOperationTime)) - subsystemsOperationTimes.Add(subsystemOperationTime); - akbPreRotor.isEnable = false; - } + var rotorSubsystemOperationTime = detectorRotor.Detect(mode, date, depth); + if (rotorSubsystemOperationTime != null) + subsystemsOperationTimes.Add(rotorSubsystemOperationTime); - if (!akbPreSlide.isEnable && isAkbSlideEnable) - { - akbPreSlide = (true, date, depth); - } - else if (akbPreSlide.isEnable && !isAkbSlideEnable) - { - var subsystemOperationTime = new SubsystemOperationTime - { - IdTelemetry = idTelemetry, - IdSubsystem = idSubsytemAkbSlide, - DateStart = akbPreSlide.date, - DateEnd = date, - DepthStart = akbPreSlide.depth, - DepthEnd = depth, - }; - if (IsValid(subsystemOperationTime)) - subsystemsOperationTimes.Add(subsystemOperationTime); - akbPreSlide.isEnable = false; - } + var slideSubsystemOperationTime = detectorSlide.Detect(mode, date, depth); + if (slideSubsystemOperationTime != null) + subsystemsOperationTimes.Add(slideSubsystemOperationTime); - if (!msePre.isEnable && isMseEnable) - { - msePre = (true, date, depth); - } - else if (msePre.isEnable && !isMseEnable) - { - var subsystemOperationTime = new SubsystemOperationTime - { - IdTelemetry = idTelemetry, - IdSubsystem = idSubsytemMse, - DateStart = akbPreRotor.date < akbPreSlide.date ? akbPreRotor.date : akbPreSlide.date, - DateEnd = date, - DepthStart = akbPreRotor.depth < akbPreSlide.depth ? akbPreRotor.depth : akbPreSlide.depth, - DepthEnd = depth, - }; - if (IsValid(subsystemOperationTime)) - subsystemsOperationTimes.Add(subsystemOperationTime); - - msePre.isEnable = false; - } + var mseSubsystemOperationTime = detectorMse.Detect(mode, date, depth); + if (mseSubsystemOperationTime != null) + subsystemsOperationTimes.Add(mseSubsystemOperationTime); } return subsystemsOperationTimes; diff --git a/AsbCloudInfrastructure/Services/Subsystems/SybsystemDetector.cs b/AsbCloudInfrastructure/Services/Subsystems/SybsystemDetector.cs new file mode 100644 index 00000000..3a2aed50 --- /dev/null +++ b/AsbCloudInfrastructure/Services/Subsystems/SybsystemDetector.cs @@ -0,0 +1,55 @@ +using AsbCloudDb.Model.Subsystems; +using System; + +namespace AsbCloudInfrastructure.Services.Subsystems +{ + public class SubsystemDetector + { + private readonly int idTelemetry; + private readonly int idSubsystem; + private readonly Func isEnable; + private readonly Func isValid; + (bool isEnable, DateTimeOffset date, float depth) pre = default; + + public SubsystemDetector( + int idTelemetry, + int idSubsystem, + Func isEnable, + Func isValid) + { + this.idTelemetry = idTelemetry; + this.idSubsystem = idSubsystem; + this.isEnable = isEnable; + this.isValid = isValid; + } + + public SubsystemOperationTime? Detect(short? mode, DateTimeOffset date, float depth) + { + var isEnable = this.isEnable(mode); + + if (!pre.isEnable && isEnable) + { + pre = (true, date, depth); + } + else if (pre.isEnable && !isEnable) + { + var subsystemOperationTime = new SubsystemOperationTime + { + IdTelemetry = idTelemetry, + IdSubsystem = idSubsystem, + DateStart = pre.date, + DateEnd = date, + DepthStart = pre.depth, + DepthEnd = depth, + }; + pre.isEnable = false; + if (!isValid(subsystemOperationTime)) + return null; + + return subsystemOperationTime; + + } + return null; + } + } +} diff --git a/AsbCloudWebApi/Controllers/Subsystems/SubsystemOperationTimeController.cs b/AsbCloudWebApi/Controllers/Subsystems/SubsystemOperationTimeController.cs index 879d61b7..1c7279b7 100644 --- a/AsbCloudWebApi/Controllers/Subsystems/SubsystemOperationTimeController.cs +++ b/AsbCloudWebApi/Controllers/Subsystems/SubsystemOperationTimeController.cs @@ -1,18 +1,19 @@ -using AsbCloudApp.Data.Subsystems; +using AsbCloudApp.Data; +using AsbCloudApp.Data.Subsystems; using AsbCloudApp.Requests; using AsbCloudApp.Services; using AsbCloudApp.Services.Subsystems; +using AsbCloudDb.Model; +using AsbCloudInfrastructure; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; -using AsbCloudInfrastructure; -using System; -using AsbCloudApp.Data; namespace AsbCloudWebApi.Controllers.Subsystems -{ +{ /// /// Наработка подсистем /// @@ -21,7 +22,8 @@ namespace AsbCloudWebApi.Controllers.Subsystems [Authorize] public class SubsystemOperationTimeController : ControllerBase { - private readonly ISubsystemOperationTimeService subsystemOperationTimeService; + private readonly ISubsystemOperationTimeService subsystemOperationTimeService; + private readonly ITelemetryDataSaubService telemetryDataSaubService; private readonly IWellService wellService; private readonly ISubsystemService subsystemService; @@ -32,11 +34,16 @@ namespace AsbCloudWebApi.Controllers.Subsystems { 65536, "Spin Master" } }; - public SubsystemOperationTimeController(ISubsystemOperationTimeService subsystemOperationTimeService, IWellService wellService, ISubsystemService subsystemService) + public SubsystemOperationTimeController( + ISubsystemOperationTimeService subsystemOperationTimeService, + IWellService wellService, + ISubsystemService subsystemService, + ITelemetryDataSaubService telemetryDataSaubService) { this.subsystemOperationTimeService = subsystemOperationTimeService; this.wellService = wellService; this.subsystemService = subsystemService; + this.telemetryDataSaubService = telemetryDataSaubService; } /// /// получить статистику @@ -54,18 +61,17 @@ namespace AsbCloudWebApi.Controllers.Subsystems } /// - /// получить период, за который будет расчитываться статистика + /// получить период, за который будет рассчитываться статистика /// - [HttpGet("dateRange")] - [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] - public async Task GetStatDateRangeAsync([FromQuery] SubsystemOperationTimeRequest request, CancellationToken token) + [HttpGet("operationsPeriod/{idWell}")] + [ProducesResponseType(typeof(DatesRangeDto), (int)System.Net.HttpStatusCode.OK)] + public async Task GetStatDateRangeAsync([FromRoute] int idWell, CancellationToken token) { - if (!await UserHasAccesToWellAsync(request.IdWell, token)) + if (!await UserHasAccesToWellAsync(idWell, token)) return Forbid(); - if (!await IsValidRequest(request, token)) - return BadRequest("Запрашиваемый диапазон должен заканчиваться за 2 часа до текущего времени(после приведения к UTC)."); - - var dateRange = await subsystemOperationTimeService.GetStatDateRangeAsync(request, token); + + var dateRange = await telemetryDataSaubService.GetRangeAsync(idWell, DateTimeOffset.MinValue, DateTimeOffset.MaxValue, token); + return Ok(dateRange); } @@ -94,10 +100,10 @@ namespace AsbCloudWebApi.Controllers.Subsystems [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] public async Task GetSubsystemAsync([FromQuery] int? idWell, CancellationToken token) { - if (idWell.HasValue) + if (idWell.HasValue) if (!await UserHasAccesToWellAsync(idWell.Value, token)) - return Forbid(); - var result = await subsystemService.GetSubsystemAsync(idWell, token); + return Forbid(); + var result = await subsystemService.GetSubsystemAsync(idWell, token); return Ok(result); } @@ -109,7 +115,7 @@ namespace AsbCloudWebApi.Controllers.Subsystems public async Task GetDateRangeOperationTimeAsync([FromQuery] SubsystemOperationTimeRequest request, CancellationToken token) { if (!await UserHasAccesToWellAsync(request.IdWell, token)) - return Forbid(); + return Forbid(); var result = await subsystemOperationTimeService.GetDateRangeOperationTimeAsync(request, token); return Ok(result); } @@ -119,7 +125,7 @@ namespace AsbCloudWebApi.Controllers.Subsystems /// [HttpGet("operationTime")] [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] - + public async Task GetOperationTimeAsync( [FromQuery] SubsystemOperationTimeRequest request, CancellationToken token) @@ -151,7 +157,7 @@ namespace AsbCloudWebApi.Controllers.Subsystems if (!await IsValidRequest(request, token)) return BadRequest("Запрашиваемый диапазон должен заканчиваться за 2 часа до текущего времени(после приведения к UTC)."); var result = await subsystemOperationTimeService.DeleteAsync(request, token); - return Ok(result); + return Ok(result); } /// @@ -161,7 +167,7 @@ namespace AsbCloudWebApi.Controllers.Subsystems [HttpGet("names")] [ProducesResponseType(typeof(Dictionary), (int)System.Net.HttpStatusCode.OK)] public IActionResult GetSubsystemsNames() - { + { return Ok(subsystemNames); } @@ -181,11 +187,11 @@ namespace AsbCloudWebApi.Controllers.Subsystems { var ltDate = (DateTimeOffset)request.LtDate; var utcDateRequest = ltDate.ToRemoteDateTime(well.Timezone.Hours); - + if (utcDateRequest.AddHours(2) > DateTime.UtcNow) { return false; - } + } } return true; }