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

117 lines
3.9 KiB
C#
Raw Normal View History

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;
2022-04-11 18:00:34 +05:00
namespace AsbCloudWebApi.Controllers.SAUB
{
2023-01-11 16:00:11 +05:00
#nullable enable
2022-06-16 17:37:10 +05:00
/// <summary>
/// Наработка талевого каната
/// </summary>
[ApiController]
[Authorize]
[Route("api/[controller]")]
2023-01-11 13:51:11 +05:00
public class TelemetryWirelineRunOutController : ControllerBase
{
private readonly ITelemetryService telemetryService;
private readonly IWellService wellService;
private readonly IHubContext<TelemetryHub> telemetryHubContext;
private readonly ITelemetryWirelineRunOutRepository repository;
2023-01-17 13:28:35 +05:00
private static 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>
2023-01-11 13:51:11 +05:00
/// <param name="token"></param>
/// <returns></returns>
[HttpPost]
[Route("{uid}")]
[AllowAnonymous]
public async Task<IActionResult> 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();
}
/// <summary>
/// Выдает данные по скважине
/// </summary>
/// <param name="idWell"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet("{idWell}")]
2023-01-11 13:51:11 +05:00
[Permission]
public 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();
2023-01-11 16:00:11 +05:00
var dto = await repository.GetOrDefaultAsync(idWell, token);
if (dto is null)
return NoContent();
return Ok(dto);
}
/// <summary>
/// Выдает данные по всем доступным скважинам
/// </summary>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet]
2023-01-18 10:44:40 +05:00
[Permission]
2023-01-11 13:51:11 +05:00
public async Task<ActionResult<IEnumerable<TelemetryWirelineRunOutDto>>> GetAllAsync(CancellationToken token)
{
int? idCompany = User.GetCompanyId();
if (idCompany is null)
return Forbid();
2023-01-11 13:51:11 +05:00
var dtos = await repository.GetAllAsync((int)idCompany, token);
return Ok(dtos);
}
}
2023-01-11 16:00:11 +05:00
#nullable disable
}