DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/TelemetryDataBaseController.cs

130 lines
5.4 KiB
C#
Raw Normal View History

using AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudWebApi.SignalR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudWebApi.Controllers
{
[ApiController]
[Authorize]
[Route("api/[controller]")]
public abstract class TelemetryDataBaseController<TDto> : ControllerBase
where TDto: ITelemetryData
{
private readonly ITelemetryService telemetryService;
private readonly ITelemetryDataService<TDto> telemetryDataService;
private readonly IWellService wellService;
private readonly IHubContext<TelemetryHub> telemetryHubContext;
public string SirnalRMethodGetDataName { get; protected set; } = "ReceiveData";
public TelemetryDataBaseController(
ITelemetryService telemetryService,
ITelemetryDataService<TDto> telemetryDataService,
IWellService wellService,
IHubContext<TelemetryHub> telemetryHubContext)
{
this.telemetryService = telemetryService;
this.telemetryDataService = telemetryDataService;
this.wellService = wellService;
this.telemetryHubContext = telemetryHubContext;
}
/// <summary>
/// Принимает данные от разных систем по скважине
/// </summary>
/// <param name="uid">Уникальный идентификатор отправителя</param>
/// <param name="dtos">Данные</param>
/// <param name="token">Токен для отмены задачи</param>
/// <returns></returns>
[HttpPost]
[Route("{uid}")]
[AllowAnonymous]
public virtual async Task<IActionResult> PostDataAsync(string uid, [FromBody] IEnumerable<TDto> dtos,
CancellationToken token = default)
{
await telemetryDataService.UpdateDataAsync(uid, dtos, token).ConfigureAwait(false);
var idWell = telemetryService.GetidWellByTelemetryUid(uid);
if (idWell != null && dtos.Any())
await Task.Run(() => telemetryHubContext.Clients.Group($"well_{idWell}")
.SendAsync(SirnalRMethodGetDataName, dtos), token).ConfigureAwait(false);
telemetryService.SaveRequestDate(uid);
return Ok();
}
/// <summary>
/// Возвращает данные САУБ по скважине.
/// По умолчанию за последние 10 минут.
/// </summary>
/// <param name="idWell">id скважины</param>
/// <param name="begin">дата начала выборки. По умолчанию: текущее время - intervalSec</param>
/// <param name="intervalSec">интервал времени даты начала выборки, секунды</param>
/// <param name="approxPointsCount">желаемое количество точек. Если в выборке точек будет больше, то выборка будет прорежена.</param>
/// <param name="token">Токен завершения задачи</param>
/// <returns></returns>
[HttpGet("{idWell}")]
//[ProducesResponseType(typeof(IEnumerable<Tdto>), (int)System.Net.HttpStatusCode.OK)]
public virtual async Task<IActionResult> GetDataAsync(int idWell, DateTime begin = default,
int intervalSec = 600, int approxPointsCount = 1024, CancellationToken token = default)
{
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();
if (begin == default)
begin = DateTime.Now.AddSeconds(-intervalSec);
var content = await telemetryDataService.GetAsync(idWell, begin,
intervalSec, approxPointsCount, token).ConfigureAwait(false);
return Ok(content);
}
/// <summary>
/// Возвращает диапазон дат сохраненных данных.
/// </summary>
/// <param name="idWell">id скважины</param>
/// <param name="token">Токен завершения задачи</param>
/// <returns></returns>
[HttpGet]
[Route("{idWell}/datesRange")]
[ProducesResponseType(typeof(DatesRangeDto), (int)System.Net.HttpStatusCode.OK)]
public virtual async Task<IActionResult> GetDataDatesRangeAsync(int idWell,
CancellationToken token = default)
{
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 dataDatesRange = await telemetryDataService.GetDataDatesRangeAsync(idWell,
token).ConfigureAwait(false);
return Ok(dataDatesRange);
}
}
}