DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/WITS/WitsControllerAbstract.cs
2024-08-19 10:01:07 +05:00

126 lines
4.9 KiB
C#

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;
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;
}
/// <summary>
/// Метод для получения WITS записи от панели оператора.
/// Созраняет в БД.
/// Ретранслирует заинтересованным через SignalR.
/// </summary>
/// <param name="uid">уникальный идентификатор телеметрии</param>
/// <param name="dtos">WITS запись</param>
/// <param name="witsRecordRepository"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpPost("{uid}")]
[AllowAnonymous]
public async virtual Task<IActionResult> PostDataAsync(
string uid,
[FromBody] IEnumerable<TDto> dtos,
[FromServices] IWitsRecordRepository<TDto> witsRecordRepository,
CancellationToken token)
{
var telemetry = telemetryService.GetOrCreateTelemetryByUid(uid);
await witsRecordRepository.SaveDataAsync(telemetry.Id, 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>
/// <returns></returns>
[HttpGet("{idWell}/last")]
public virtual ActionResult<IEnumerable<TDto>> GetLastData(
int idWell,
[FromServices] IWitsRecordRepository<TDto> witsRecordRepository)
{
var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(idWell);
if (telemetry is null)
return NoContent();
var dto = witsRecordRepository.GetLastOrDefault(telemetry.Id);
return Ok(new[] { dto });
}
/// <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>
[HttpGet("{idWell}")]
public async virtual Task<ActionResult<IEnumerable<TDto>>> GetDataAsync(
int idWell,
[Required] DateTime begin,
[Required] DateTime end,
[FromServices] IWitsRecordRepository<TDto> witsRecordRepository,
CancellationToken token)
{
var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(idWell);
if (telemetry is null)
return NoContent();
var dtos = await witsRecordRepository.GetAsync(telemetry.Id, begin, end, token);
return Ok(dtos);
}
/// <summary>
/// Получение статистики по WITS записи.
/// Диапазон дат и общее количество записей.
/// </summary>
/// <param name="idWell">id скважины</param>
/// <param name="witsRecordRepository"></param>
/// <param name="token"></param>
/// <returns></returns>
[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 telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(idWell);
if (telemetry is null)
return NoContent();
var dtos = await witsRecordRepository.GetStatAsync(telemetry.Id, token);
return Ok(dtos);
}
}