DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/SAUB/TelemetryWirelineRunOutController.cs

142 lines
4.9 KiB
C#
Raw Normal View History

using AsbCloudApp.Data.SAUB;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
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 System;
using AsbCloudApp.Repositories;
using System.Linq;
2022-04-11 18:00:34 +05:00
namespace AsbCloudWebApi.Controllers.SAUB
{
2022-06-16 17:37:10 +05:00
/// <summary>
/// Наработка талевого каната
/// </summary>
[ApiController]
[Authorize]
[Route("api/[controller]")]
public class TelemetryWirelineRunOutController : ControllerBase //TelemetryInstantDataController<TelemetryWirelineRunOutDto>
{
private readonly ITelemetryService telemetryService;
private readonly IWellService wellService;
private readonly IHubContext<TelemetryHub> telemetryHubContext;
private readonly ITelemetryWirelineRunOutRepository repository;
private string SirnalRMethodGetDataName => "ReceiveWirelineRunOut";
public TelemetryWirelineRunOutController(
2022-04-11 18:00:34 +05:00
ITelemetryService telemetryService,
IWellService wellService,
IHubContext<TelemetryHub> telemetryHubContext,
ITelemetryWirelineRunOutRepository repository)
{
this.telemetryService = telemetryService;
this.wellService = wellService;
this.telemetryHubContext = telemetryHubContext;
this.repository = repository;
}
/// <summary>
/// Принимает данные от панели
/// </summary>
/// <param name="uid"></param>
/// <param name="dto"></param>
/// <returns></returns>
[HttpPost]
[Route("{uid}")]
[AllowAnonymous]
public IActionResult PostData(string uid, [FromBody] TelemetryWirelineRunOutDto dto)
{
if (dto is null)
return BadRequest("Dto shouldn't be null");
var idTelemetry = telemetryService.GetOrCreateTelemetryIdByUid(uid);
var data = repository.AddOrUpdate(idTelemetry, dto);
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();
}
/// <summary>
/// Выдает данные по скважине
/// </summary>
/// <param name="idWell"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet("{idWell}")]
//[Permission]
public virtual async Task<ActionResult<TelemetryWirelineRunOutDto>> 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 well = await wellService.GetOrDefaultAsync(idWell, token).ConfigureAwait(false);
if (well is null)
return NoContent();
int? idTelemetry = telemetryService.GetOrDefaultIdTelemetryByIdWell(idWell);
if (idTelemetry is null)
return NoContent();
var dto = await repository.GetValueOrDefaultAsync((int)idTelemetry, well, token);
if (dto is null)
return NoContent();
return Ok(dto);
}
[HttpGet]
public virtual async Task<ActionResult<IEnumerable<TelemetryWirelineRunOutDto>>> GetAllAsync(CancellationToken token)
{
var result = new List<TelemetryWirelineRunOutDto>();
int? idCompany = User.GetCompanyId();
var wells = await wellService.GetAllAsync(token)
.ConfigureAwait(false);
if (idCompany is null)
return Forbid();
foreach (var well in wells)
{
bool isCompanyOwnsWell = await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
well.Id, token).ConfigureAwait(false);
if (!isCompanyOwnsWell)
return Forbid();
int? idTelemetry = telemetryService.GetOrDefaultIdTelemetryByIdWell(well.Id);
if (idTelemetry is null)
return NoContent();
var dto = await repository.GetValueOrDefaultAsync((int)idTelemetry, well, token);
if (dto is not null)
result.Add(dto);
}
return Ok(result);
}
}
}