DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/SAUB/DrillTestController.cs

70 lines
2.6 KiB
C#

using AsbCloudApp.Data.SAUB;
using AsbCloudApp.Repositories;
using AsbCloudApp.Services;
using AsbCloudWebApi.SignalR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudWebApi.Controllers.SAUB
{
[Route("api/telemetry")]
[ApiController]
public class DrillTestController : ControllerBase
{
protected readonly IWellService wellService;
private readonly ITelemetryService telemetryService;
private readonly IDrillTestRepository drillTestRepository;
private readonly IHubContext<TelemetryHub> telemetryHubContext;
public string SignalRMethodGetDataName { get; protected set; } = "ReceiveDrilltestData";
public DrillTestController(
ITelemetryService telemetryService,
IDrillTestRepository drillTestRepository,
IWellService wellService,
IHubContext<TelemetryHub> telemetryHubContext)
{
this.telemetryService = telemetryService;
this.drillTestRepository = drillTestRepository;
this.wellService = wellService;
this.telemetryHubContext = telemetryHubContext;
}
/// <summary>
/// Метод получения данных drill_test и drill_test_params от панели оператора.
/// Сохраняет в БД.
/// </summary>
/// <param name="uid">уникальный идентификатор записи drill_test</param>
/// <param name="dto">запись drill test</param>
/// <param name="token"></param>
/// <returns></returns>
[HttpPost("{uid}/[controller]")]
public async Task<IActionResult> PostDataAsync(
string uid,
[FromBody] DrillTestDto dto,
CancellationToken token)
{
var telemetry = telemetryService.GetOrCreateTelemetryByUid(uid);
if (telemetry is null)
throw new Exception($"Telemetry with RemoteUid: {uid} does not exist.");
await drillTestRepository.SaveDataAsync(telemetry.Id, dto, token);
var idWell = telemetryService.GetIdWellByTelemetryUid(uid);
if (idWell is not null)
_ = Task.Run(async () =>
{
var clients = telemetryHubContext.Clients.Group($"well_{idWell}");
await clients.SendAsync(SignalRMethodGetDataName, dto);
}, CancellationToken.None);
return Ok();
}
}
}