using AsbCloudApp.Data.SAUB; using AsbCloudApp.Services; using AsbCloudWebApi.SignalR; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.SignalR; using System.Collections.Generic; using System.Threading.Tasks; using System.Threading; using AsbCloudApp.Repositories; namespace AsbCloudWebApi.Controllers.SAUB { /// /// Наработка талевого каната /// [ApiController] [Authorize] [Route("api/[controller]")] public class TelemetryWirelineRunOutController : ControllerBase { private readonly ITelemetryService telemetryService; private readonly IWellService wellService; private readonly IHubContext telemetryHubContext; private readonly ITelemetryWirelineRunOutRepository repository; private static string SirnalRMethodGetDataName => "ReceiveWirelineRunOut"; public TelemetryWirelineRunOutController( ITelemetryService telemetryService, IWellService wellService, IHubContext telemetryHubContext, ITelemetryWirelineRunOutRepository repository) { this.telemetryService = telemetryService; this.wellService = wellService; this.telemetryHubContext = telemetryHubContext; this.repository = repository; } /// /// Принимает данные от панели /// /// /// /// /// [HttpPost("{uid}")] [AllowAnonymous] public async Task PostDataAsync(string uid, [FromBody] TelemetryWirelineRunOutBaseDto dto, CancellationToken token) { if (dto is null) return BadRequest("Dto shouldn't be null"); var data = await repository.AddOrUpdateAsync(uid, dto, token); var idWell = telemetryService.GetIdWellByTelemetryUid(uid); if (idWell is not null) _ = Task.Run(async () => { var clients = telemetryHubContext.Clients.Group($"well_{idWell}"); await clients.SendAsync(SirnalRMethodGetDataName, dto); }, CancellationToken.None); return Ok(); } /// /// Выдает данные по скважине /// /// /// /// [HttpGet("{idWell}")] [Permission] public async Task> GetDataAsync(int idWell, 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 dto = await repository.GetOrDefaultAsync(idWell, token); if (dto is null) return NoContent(); return Ok(dto); } /// /// Выдает данные по всем доступным скважинам /// /// /// [HttpGet] [Permission] public async Task>> GetAllAsync(CancellationToken token) { int? idCompany = User.GetCompanyId(); if (idCompany is null) return Forbid(); var dtos = await repository.GetAllAsync((int)idCompany, token); return Ok(dtos); } } }