forked from ddrilling/AsbCloudServer
182 lines
6.5 KiB
C#
182 lines
6.5 KiB
C#
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.Exceptions;
|
|
using AsbCloudApp.Requests;
|
|
using Microsoft.AspNetCore.Http;
|
|
using AsbCloudApp.Data;
|
|
using System;
|
|
using Org.BouncyCastle.Asn1.Ocsp;
|
|
|
|
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<TelemetryHub> telemetryHubContext;
|
|
|
|
public string SignalRMethodGetDataName { get; protected set; } = "ReceiveData";
|
|
|
|
public GtrWitsController(
|
|
ITelemetryService telemetryService,
|
|
IGtrRepository gtrRepository,
|
|
IWellService wellService,
|
|
IHubContext<TelemetryHub> telemetryHubContext)
|
|
{
|
|
this.telemetryService = telemetryService;
|
|
this.gtrRepository = gtrRepository;
|
|
this.wellService = wellService;
|
|
this.telemetryHubContext = telemetryHubContext;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Получить значение от ГТИ
|
|
/// </summary>
|
|
/// <param name="idWell"></param>
|
|
/// <param name="request"></param>
|
|
/// <param name="token"></param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
[Permission]
|
|
[ProducesResponseType(typeof(IEnumerable<GtrWitsDto>), StatusCodes.Status200OK)]
|
|
public async Task<IActionResult> GetAllAsync([Required] int idWell, [FromQuery] GtrRequest request, CancellationToken token)
|
|
{
|
|
await AssertUserHasAccessToWellAsync(idWell, token);
|
|
|
|
var dtos = await gtrRepository.GetAsync(idWell, request, token);
|
|
|
|
return Ok(dtos);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Возвращает диапазон дат за которые есть телеметрия за период времени
|
|
/// </summary>
|
|
/// <param name="idWell"></param>
|
|
/// <param name="geDate"></param>
|
|
/// <param name="leDate"></param>
|
|
/// <param name="token"></param>
|
|
/// <returns></returns>
|
|
[HttpGet("{idWell}/dateRange")]
|
|
[ProducesResponseType(typeof(DatesRangeDto), (int)System.Net.HttpStatusCode.OK)]
|
|
[ProducesResponseType((int)System.Net.HttpStatusCode.NotFound)]
|
|
[ProducesResponseType((int)System.Net.HttpStatusCode.NoContent)]
|
|
public virtual async Task<ActionResult<DatesRangeDto?>> GetRangeAsync(
|
|
[FromRoute] int idWell,
|
|
DateTimeOffset? geDate,
|
|
DateTimeOffset? leDate,
|
|
CancellationToken token)
|
|
{
|
|
await AssertUserHasAccessToWellAsync(idWell, token);
|
|
|
|
var range = await gtrRepository.GetRangeAsync(idWell, geDate, leDate, token);
|
|
|
|
return Ok(range);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Получить загруженные данные ГТИ по скважине
|
|
/// </summary>
|
|
/// <param name="idWell">Id скважины</param>
|
|
/// <param name="request">Параметры запроса</param>
|
|
/// <param name = "token" >Токен завершения задачи</param>
|
|
/// <returns></returns>
|
|
[HttpGet("{idWell}")]
|
|
public async Task<ActionResult<IEnumerable<WitsRecordDto>>> GetDataAsync([Required] int idWell,
|
|
[FromQuery] GtrRequest 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?.DateTime,
|
|
request.IntervalSec, request.ApproxPointsCount, token).ConfigureAwait(false);
|
|
|
|
return Ok(content);
|
|
}
|
|
|
|
/// <summary>
|
|
/// получение последних данных ГТИ по ключу record
|
|
/// </summary>
|
|
/// <param name="idWell">id скважины</param>
|
|
/// <param name="idRecord">id record</param>
|
|
/// <param name="token"></param>
|
|
/// <returns></returns>
|
|
[HttpGet("{idWell}/{idRecord}")]
|
|
public async Task<ActionResult<IEnumerable<WitsItemRecordDto>>> 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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Метод для получения WITS записи от панели оператора.
|
|
/// Сохраняет в БД.
|
|
/// </summary>
|
|
/// <param name="uid">уникальный идентификатор телеметрии</param>
|
|
/// <param name="dtos">WITS запись</param>
|
|
/// <param name="token"></param>
|
|
/// <returns></returns>
|
|
[HttpPost("{uid}")]
|
|
public async Task<IActionResult> PostDataAsync(
|
|
string uid,
|
|
[FromBody] IEnumerable<WitsRecordDto> 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();
|
|
}
|
|
|
|
private async Task AssertUserHasAccessToWellAsync(int idWell, CancellationToken token)
|
|
{
|
|
var idUser = User.GetUserId();
|
|
var idCompany = User.GetCompanyId();
|
|
|
|
if (!idUser.HasValue)
|
|
throw new ForbidException("Нет доступа к скважине");
|
|
|
|
if (!idCompany.HasValue)
|
|
throw new ForbidException("Нет доступа к скважине");
|
|
|
|
if (!await wellService.IsCompanyInvolvedInWellAsync(idCompany.Value, idWell, token))
|
|
throw new ForbidException("Нет доступа к скважине");
|
|
}
|
|
}
|