using AsbCloudApp.Data.GTR; using AsbCloudApp.Repositories; using AsbCloudApp.Services; using AsbCloudWebApi.SignalR; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.SignalR; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Threading; using System.Threading.Tasks; using AsbCloudApp.Requests; namespace AsbCloudWebApi.Controllers.SAUB { [Route("api/[controller]")] [ApiController] public class GtrWitsController : ControllerBase { protected readonly IWellService wellService; private readonly ITelemetryService telemetryService; private readonly IGtrRepository gtrRepository; private readonly IHubContext telemetryHubContext; public string SignalRMethodGetDataName { get; protected set; } = "ReceiveData"; public GtrWitsController( ITelemetryService telemetryService, IGtrRepository gtrRepository, IWellService wellService, IHubContext telemetryHubContext) { this.telemetryService = telemetryService; this.gtrRepository = gtrRepository; this.wellService = wellService; this.telemetryHubContext = telemetryHubContext; } /// /// Получить загруженные данные ГТИ по скважине /// /// Id скважины /// Параметры запроса /// Токен завершения задачи /// [HttpGet("{idWell}")] [Permission] public async Task>> GetDataAsync([Required] int idWell, [FromQuery] GtrWithGetDataRequest request, CancellationToken token) { int? idCompany = User.GetCompanyId(); if (idCompany is null) return Forbid(); bool isCompanyOwnsWell = await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, idWell, token).ConfigureAwait(false); if (!isCompanyOwnsWell) return Forbid(); var content = await gtrRepository.GetAsync(idWell, request.Begin, request.IntervalSec, request.ApproxPointsCount, token).ConfigureAwait(false); return Ok(content); } /// /// получение последних данных ГТИ по ключу record /// /// id скважины /// id record /// /// [HttpGet("{idWell}/{idRecord}")] [Permission] public async Task>> GetLastDataByRecordIdAsync(int idWell, int idRecord, CancellationToken token) { int? idCompany = User.GetCompanyId(); if (idCompany is null) return Forbid(); bool isCompanyOwnsWell = await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, idWell, token).ConfigureAwait(false); if (!isCompanyOwnsWell) return Forbid(); var content = gtrRepository.GetLastDataByRecordId(idWell, idRecord); return Ok(content); } /// /// Метод для получения WITS записи от панели оператора. /// Сохраняет в БД. /// /// уникальный идентификатор телеметрии /// WITS запись /// /// [HttpPost("{uid}")] public async Task PostDataAsync( string uid, [FromBody] IEnumerable dtos, CancellationToken token) { var telemetry = telemetryService.GetOrCreateTelemetryByUid(uid); await gtrRepository.SaveDataAsync(telemetry.Id, dtos, token).ConfigureAwait(false); var idWell = telemetryService.GetIdWellByTelemetryUid(uid); if (idWell is not null && dtos is not null) _ = Task.Run(() => telemetryHubContext.Clients.Group($"well_{idWell}_gtr") .SendAsync(SignalRMethodGetDataName, dtos), CancellationToken.None); return Ok(); } } }