DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/WITS/WitsControllerAbstract.cs

129 lines
5.4 KiB
C#
Raw Normal View History

2022-04-08 13:10:06 +05:00
using AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudWebApi.SignalR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
2022-04-08 13:10:06 +05:00
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudWebApi.Controllers.WITS
{
[Route("api/[controller]")]
[ApiController]
public abstract class WitsControllerAbstract<TDto> : ControllerBase
where TDto : ITelemetryData
{
private readonly ITelemetryService telemetryService;
private readonly IHubContext<TelemetryHub> telemetryHubContext;
protected abstract string SirnalRMethodGetDataName { get; }
public WitsControllerAbstract(
ITelemetryService telemetryService,
IHubContext<TelemetryHub> telemetryHubContext)
{
this.telemetryService = telemetryService;
this.telemetryHubContext = telemetryHubContext;
}
2022-04-14 13:46:51 +05:00
/// <summary>
/// Метод для получения WITS записи от панели оператора.
/// Созраняет в БД.
/// Ретранслирует заинтересованным через SignalR.
/// </summary>
/// <param name="uid">уникальный идентификатор телеметрии</param>
/// <param name="dtos">WITS запись</param>
/// <param name="witsRecordRepository"></param>
/// <param name="token"></param>
/// <returns></returns>
2022-04-08 13:10:06 +05:00
[HttpPost("{uid}")]
[AllowAnonymous]
public async virtual Task<IActionResult> PostDataAsync(
string uid,
[FromBody] IEnumerable<TDto> dtos,
[FromServices] IWitsRecordRepository<TDto> witsRecordRepository,
CancellationToken token = default)
{
var idTelemetry = telemetryService.GetOrCreateTelemetryIdByUid(uid);
await witsRecordRepository.SaveDataAsync(idTelemetry, dtos, 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();
}
/// <summary>
/// Получение последней пришедшей WITS записи.
/// </summary>
/// <param name="idWell">id скважины</param>
/// <param name="witsRecordRepository"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet("{idWell}/last")]
public async virtual Task<ActionResult<IEnumerable<TDto>>> GetLastDataAsync(
int idWell,
[FromServices] IWitsRecordRepository<TDto> witsRecordRepository,
CancellationToken token)
{
var idTelemetry = telemetryService.GetIdTelemetryByIdWell(idWell);
if (idTelemetry is null)
return NoContent();
var dtos = await witsRecordRepository.GetLastAsync((int)idTelemetry, token);
return Ok(dtos);
}
2022-04-14 13:46:51 +05:00
/// <summary>
/// Получение списка архивных WITS записей за период.
/// </summary>
/// <param name="idWell">id скважины</param>
/// <param name="begin">начало диапазона</param>
/// <param name="end">конец диапазона</param>
/// <param name="witsRecordRepository"></param>
/// <param name="token"></param>
/// <returns></returns>
2022-04-08 13:10:06 +05:00
[HttpGet("{idWell}")]
2022-04-14 13:46:51 +05:00
public async virtual Task<ActionResult<IEnumerable<TDto>>> GetDataAsync(
2022-04-08 13:10:06 +05:00
int idWell,
[Required] DateTime begin,
[Required] DateTime end,
2022-04-08 13:10:06 +05:00
[FromServices] IWitsRecordRepository<TDto> witsRecordRepository,
CancellationToken token)
{
var idTelemetry = telemetryService.GetIdTelemetryByIdWell(idWell);
if (idTelemetry is null)
return NoContent();
var dtos = await witsRecordRepository.GetAsync((int)idTelemetry, begin, end, token);
return Ok(dtos);
}
2022-04-14 13:46:51 +05:00
/// <summary>
/// Получение статистики по WITS записи.
/// Диапазон дат и общее количество записей.
/// </summary>
/// <param name="idWell">id скважины</param>
/// <param name="witsRecordRepository"></param>
/// <param name="token"></param>
/// <returns></returns>
2022-04-08 13:10:06 +05:00
[HttpGet("{idWell}/datesRange")]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof((DateTime begin, DateTime end, int count)?))]
public async virtual Task<IActionResult> GetDatesRangeAsync(
int idWell,
[FromServices] IWitsRecordRepository<TDto> witsRecordRepository,
CancellationToken token)
{
var idTelemetry = telemetryService.GetIdTelemetryByIdWell(idWell);
if (idTelemetry is null)
return NoContent();
var dtos = await witsRecordRepository.GetStatAsync((int)idTelemetry, token);
return Ok(dtos);
}
}
}