DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/TelemetryDataBaseController.cs
2021-11-22 11:30:08 +05:00

127 lines
5.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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);
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="isUtc">Даты в формате UTC или часового пояса скважины</param>
/// <param name="token">Токен завершения задачи</param>
/// <returns></returns>
[HttpGet("{idWell}")]
public virtual async Task<ActionResult<TDto>> GetDataAsync(int idWell, DateTime begin = default,
int intervalSec = 600, int approxPointsCount = 1024, bool isUtc = false, 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 content = await telemetryDataService.GetAsync(idWell, begin,
intervalSec, approxPointsCount, isUtc, token).ConfigureAwait(false);
return Ok(content);
}
/// <summary>
/// Возвращает диапазон дат сохраненных данных.
/// </summary>
/// <param name="idWell">id скважины</param>
/// <param name="isUtc">Смена дат с UTC формата на часовой пояс скважины</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, bool isUtc = false,
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, isUtc,
token).ConfigureAwait(false);
return Ok(dataDatesRange);
}
}
}