using AsbCloudApp.Data; using AsbCloudApp.Data.GTR; using AsbCloudApp.Data.SAUB; using AsbCloudApp.Repositories; using AsbCloudApp.Services; using AsbCloudInfrastructure.Repository; using AsbCloudWebApi.SignalR; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.SignalR; using System; using System.Collections.Generic; 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 telemetryHubContext; public string SignalRMethodGetDataName { get; protected set; } = "ReceiveDrilltestData"; public DrillTestController( ITelemetryService telemetryService, IDrillTestRepository drillTestRepository, IWellService wellService, IHubContext telemetryHubContext) { this.telemetryService = telemetryService; this.drillTestRepository = drillTestRepository; this.wellService = wellService; this.telemetryHubContext = telemetryHubContext; } /// /// Метод получения данных drill_test и drill_test_params от панели оператора. /// Сохраняет в БД. /// /// уникальный идентификатор записи drill_test /// запись drill test /// /// [HttpPost("{uid}/[controller]")] public async Task 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(); } } }