2021-09-14 17:17:33 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
2021-09-17 16:24:01 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using AsbCloudWebApi.SignalR;
|
2021-09-14 17:17:33 +05:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2021-09-17 16:24:01 +05:00
|
|
|
|
using Microsoft.AspNetCore.SignalR;
|
2021-09-14 17:17:33 +05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2021-09-17 16:24:01 +05:00
|
|
|
|
using System.Linq;
|
2021-09-14 17:17:33 +05:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2022-04-11 18:00:34 +05:00
|
|
|
|
namespace AsbCloudWebApi.Controllers.SAUB
|
2021-09-14 17:17:33 +05:00
|
|
|
|
{
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Authorize]
|
|
|
|
|
[Route("api/[controller]")]
|
2021-09-17 16:24:01 +05:00
|
|
|
|
public abstract class TelemetryDataBaseController<TDto> : ControllerBase
|
2022-04-11 18:00:34 +05:00
|
|
|
|
where TDto : ITelemetryData
|
2021-09-14 17:17:33 +05:00
|
|
|
|
{
|
2023-03-09 12:32:09 +05:00
|
|
|
|
protected readonly IWellService wellService;
|
2021-09-17 16:24:01 +05:00
|
|
|
|
private readonly ITelemetryService telemetryService;
|
|
|
|
|
private readonly ITelemetryDataService<TDto> telemetryDataService;
|
|
|
|
|
private readonly IHubContext<TelemetryHub> telemetryHubContext;
|
|
|
|
|
|
|
|
|
|
public string SirnalRMethodGetDataName { get; protected set; } = "ReceiveData";
|
|
|
|
|
|
|
|
|
|
public TelemetryDataBaseController(
|
2022-04-11 18:00:34 +05:00
|
|
|
|
ITelemetryService telemetryService,
|
2021-09-17 16:24:01 +05:00
|
|
|
|
ITelemetryDataService<TDto> telemetryDataService,
|
|
|
|
|
IWellService wellService,
|
|
|
|
|
IHubContext<TelemetryHub> telemetryHubContext)
|
|
|
|
|
{
|
|
|
|
|
this.telemetryService = telemetryService;
|
|
|
|
|
this.telemetryDataService = telemetryDataService;
|
|
|
|
|
this.wellService = wellService;
|
|
|
|
|
this.telemetryHubContext = telemetryHubContext;
|
|
|
|
|
}
|
2021-09-14 17:17:33 +05:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Принимает данные от разных систем по скважине
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="uid">Уникальный идентификатор отправителя</param>
|
|
|
|
|
/// <param name="dtos">Данные</param>
|
|
|
|
|
/// <param name="token">Токен для отмены задачи</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpPost]
|
|
|
|
|
[Route("{uid}")]
|
2021-09-17 16:24:01 +05:00
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
public virtual async Task<IActionResult> PostDataAsync(string uid, [FromBody] IEnumerable<TDto> dtos,
|
2021-09-14 17:17:33 +05:00
|
|
|
|
CancellationToken token = default)
|
|
|
|
|
{
|
2021-09-17 16:24:01 +05:00
|
|
|
|
await telemetryDataService.UpdateDataAsync(uid, dtos, token).ConfigureAwait(false);
|
2021-09-14 17:17:33 +05:00
|
|
|
|
|
2021-12-07 18:27:52 +05:00
|
|
|
|
var idWell = telemetryService.GetIdWellByTelemetryUid(uid);
|
2022-04-11 17:22:52 +05:00
|
|
|
|
if (idWell is not null && dtos.Any())
|
2021-12-24 12:59:17 +05:00
|
|
|
|
_ = Task.Run(() => telemetryHubContext.Clients.Group($"well_{idWell}")
|
|
|
|
|
.SendAsync(SirnalRMethodGetDataName, dtos), CancellationToken.None);
|
2022-04-11 18:00:34 +05:00
|
|
|
|
|
2021-09-14 17:17:33 +05:00
|
|
|
|
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}")]
|
2022-01-19 11:42:26 +05:00
|
|
|
|
[Permission]
|
2023-01-27 11:59:51 +05:00
|
|
|
|
public virtual async Task<ActionResult<IEnumerable<TDto>>> GetDataAsync(int idWell, DateTime begin = default,
|
2022-01-05 17:50:45 +05:00
|
|
|
|
int intervalSec = 600, int approxPointsCount = 1024, CancellationToken token = default)
|
2021-09-14 17:17:33 +05:00
|
|
|
|
{
|
2021-09-29 17:05:27 +05:00
|
|
|
|
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();
|
2022-04-11 18:00:34 +05:00
|
|
|
|
|
2023-03-27 09:40:51 +05:00
|
|
|
|
var content = await telemetryDataService.GetAsync(idWell, begin,
|
2022-01-05 17:50:45 +05:00
|
|
|
|
intervalSec, approxPointsCount, token).ConfigureAwait(false);
|
2021-09-14 17:17:33 +05:00
|
|
|
|
|
2021-09-29 17:05:27 +05:00
|
|
|
|
return Ok(content);
|
2021-09-14 17:17:33 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Возвращает диапазон дат сохраненных данных.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell">id скважины</param>
|
|
|
|
|
/// <param name="token">Токен завершения задачи</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("{idWell}/datesRange")]
|
2022-01-19 11:42:26 +05:00
|
|
|
|
[Permission]
|
2021-09-14 17:17:33 +05:00
|
|
|
|
[ProducesResponseType(typeof(DatesRangeDto), (int)System.Net.HttpStatusCode.OK)]
|
2023-01-27 11:59:51 +05:00
|
|
|
|
public virtual async Task<ActionResult<DatesRangeDto>> GetDataDatesRangeAsync(int idWell,
|
2021-09-14 17:17:33 +05:00
|
|
|
|
CancellationToken token = default)
|
|
|
|
|
{
|
2021-09-17 16:24:01 +05:00
|
|
|
|
int? idCompany = User.GetCompanyId();
|
2021-09-14 17:17:33 +05:00
|
|
|
|
|
2021-09-17 16:24:01 +05:00
|
|
|
|
if (idCompany is null)
|
|
|
|
|
return Forbid();
|
2021-09-14 17:17:33 +05:00
|
|
|
|
|
2021-09-17 16:24:01 +05:00
|
|
|
|
bool isCompanyOwnsWell = await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
|
|
|
|
|
idWell, token).ConfigureAwait(false);
|
2021-09-14 17:17:33 +05:00
|
|
|
|
|
2021-09-17 16:24:01 +05:00
|
|
|
|
if (!isCompanyOwnsWell)
|
|
|
|
|
return Forbid();
|
2022-11-21 16:58:37 +05:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var dataDatesRange = wellService.GetDatesRange(idWell);
|
|
|
|
|
return Ok(dataDatesRange);
|
|
|
|
|
}
|
|
|
|
|
catch(KeyNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
2021-09-14 17:17:33 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|