forked from ddrilling/AsbCloudServer
87 lines
3.5 KiB
C#
87 lines
3.5 KiB
C#
using AsbCloudApp.Data;
|
||
using AsbCloudApp.Services;
|
||
using Microsoft.AspNetCore.Authorization;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace AsbCloudWebApi.Controllers
|
||
{
|
||
/// <summary>
|
||
/// Контроллер сбора данных от буровых
|
||
/// </summary>
|
||
[Route("api/well")]
|
||
[ApiController]
|
||
[Authorize]
|
||
public class DataController : ControllerBase
|
||
{
|
||
private readonly IDataService telemetryDataService;
|
||
private readonly IWellService wellService;
|
||
|
||
public DataController(IDataService telemetryDataService, IWellService wellService)
|
||
{
|
||
this.telemetryDataService = telemetryDataService;
|
||
this.wellService = wellService;
|
||
}
|
||
|
||
/// <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]
|
||
[Route("{idWell}/data")]
|
||
[ProducesResponseType(typeof(IEnumerable<DataSaubBaseDto>), (int)System.Net.HttpStatusCode.OK)]
|
||
public async Task<IActionResult> GetDataAsync(int idWell, DateTime begin = default,
|
||
int intervalSec = 600, int approxPointsCount = 1024, CancellationToken token = default)
|
||
{
|
||
if (begin == default)
|
||
begin = DateTime.Now.AddSeconds(-intervalSec);
|
||
var content = await telemetryDataService.GetAsync(idWell, begin,
|
||
intervalSec, approxPointsCount, token).ConfigureAwait(false);
|
||
|
||
if (content is null || !content.Any())
|
||
return NoContent();
|
||
|
||
return Ok(content);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Возвращает диапазон дат сохраненных данных.
|
||
/// </summary>
|
||
/// <param name="idWell">id скважины</param>
|
||
/// <param name="token">Токен завершения задачи</param>
|
||
/// <returns></returns>
|
||
[HttpGet]
|
||
[Route("{idWell}/dataDatesRange")]
|
||
[ProducesResponseType(typeof(DatesRangeDto), (int)System.Net.HttpStatusCode.OK)]
|
||
public 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();
|
||
|
||
DatesRangeDto dataDatesRange = await telemetryDataService.GetDataDatesRangeAsync(idWell,
|
||
token).ConfigureAwait(false);
|
||
|
||
return Ok(dataDatesRange);
|
||
}
|
||
}
|
||
}
|