using AsbCloudApp.Data; using AsbCloudApp.Services; using AsbCloudWebApi.SignalR; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.SignalR; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace AsbCloudWebApi.Controllers.WITS { [Route("api/[controller]")] [ApiController] public abstract class WitsControllerAbstract : ControllerBase where TDto : ITelemetryData { private readonly ITelemetryService telemetryService; private readonly IHubContext telemetryHubContext; protected abstract string SirnalRMethodGetDataName { get; } public WitsControllerAbstract( ITelemetryService telemetryService, IHubContext telemetryHubContext) { this.telemetryService = telemetryService; this.telemetryHubContext = telemetryHubContext; } /// /// Метод для получения WITS записи от панели оператора. /// Созраняет в БД. /// Ретранслирует заинтересованным через SignalR. /// /// уникальный идентификатор телеметрии /// WITS запись /// /// /// [HttpPost("{uid}")] [AllowAnonymous] public async virtual Task PostDataAsync( string uid, [FromBody] IEnumerable dtos, [FromServices] IWitsRecordRepository witsRecordRepository, CancellationToken token) { var telemetry = telemetryService.GetOrCreateTelemetryByUid(uid); await witsRecordRepository.SaveDataAsync(telemetry.Id, dtos, token).ConfigureAwait(false); var idWell = telemetryService.GetIdWellByTelemetryUid(uid); if (idWell != null && dtos.Any()) _ = Task.Run(() => telemetryHubContext.Clients.Group($"well_{idWell}_wits") .SendAsync(SirnalRMethodGetDataName, dtos), CancellationToken.None); return Ok(); } /// /// Получение последней пришедшей WITS записи. /// /// id скважины /// /// [HttpGet("{idWell}/last")] public virtual ActionResult> GetLastData( int idWell, [FromServices] IWitsRecordRepository witsRecordRepository) { var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(idWell); if (telemetry is null) return NoContent(); var dto = witsRecordRepository.GetLastOrDefault(telemetry.Id); return Ok(new[] { dto }); } /// /// Получение списка архивных WITS записей за период. /// /// id скважины /// начало диапазона /// конец диапазона /// /// /// [HttpGet("{idWell}")] public async virtual Task>> GetDataAsync( int idWell, [Required] DateTime begin, [Required] DateTime end, [FromServices] IWitsRecordRepository witsRecordRepository, CancellationToken token) { var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(idWell); if (telemetry is null) return NoContent(); var dtos = await witsRecordRepository.GetAsync(telemetry.Id, begin, end, token); return Ok(dtos); } /// /// Получение статистики по WITS записи. /// Диапазон дат и общее количество записей. /// /// id скважины /// /// /// [HttpGet("{idWell}/datesRange")] [ProducesResponseType(StatusCodes.Status200OK, Type = typeof((DateTime begin, DateTime end, int count)?))] public async virtual Task GetDatesRangeAsync( int idWell, [FromServices] IWitsRecordRepository witsRecordRepository, CancellationToken token) { var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(idWell); if (telemetry is null) return NoContent(); var dtos = await witsRecordRepository.GetStatAsync(telemetry.Id, token); return Ok(dtos); } } }