using AsbCloudApp.Data.GTR; using AsbCloudApp.Repositories; using AsbCloudApp.Services; using AsbCloudDb.Model; using AsbCloudWebApi.SignalR; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.SignalR; using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; namespace AsbCloudWebApi.Controllers.GTR { [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 SirnalRMethodGetDataName { 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 скважины /// дата начала выборки.По умолчанию: текущее время - intervalSec /// интервал времени даты начала выборки, секунды /// желаемое количество точек. Если в выборке точек будет больше, то выборка будет прорежена. /// Токен завершения задачи /// [HttpGet("{idWell}")] //[Permission] public async Task>> GetDataAsync(int idWell, DateTime begin = default, int intervalSec = 600, int approxPointsCount = 1024, CancellationToken token = default) { //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, begin, intervalSec, approxPointsCount, token).ConfigureAwait(false); return Ok(content); } [HttpPost("{uid}")] public async Task PostDataAsync( string uid, [FromBody] WitsRecordDto dto, [FromServices] IGtrRepository gtrRepository, CancellationToken token = default) { var idTelemetry = telemetryService.GetOrCreateTelemetryIdByUid(uid); await gtrRepository.SaveDataAsync(idTelemetry, dto, 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(); } } }