using AsbCloudApp.Data.ProcessMap; using AsbCloudApp.Repositories; 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.Threading; using System.Threading.Tasks; namespace AsbCloudWebApi.Controllers { /// /// РТК /// [ApiController] [Route("api/[controller]")] [Authorize] public class ProcessMapController : CrudWellRelatedController { private readonly ITelemetryService telemetryService; private readonly IHubContext telemetryHubContext; private readonly IProcessMapReportMakerService processMapReportService; private readonly IProcessMapReportService processMapService; private const string SirnalRMethodGetDataName = "ProcessMapChanged"; public ProcessMapController( IWellService wellService, IProcessMapPlanRepository repository, IProcessMapReportMakerService processMapReportService, IProcessMapReportService processMapService, ITelemetryService telemetryService, IHubContext telemetryHubContext) : base(wellService, repository) { this.telemetryService = telemetryService; this.telemetryHubContext = telemetryHubContext; this.processMapReportService = processMapReportService; this.processMapService = processMapService; } /// /// Возвращает все значения для коридоров бурения по uid панели /// /// uid панели /// Дата, с которой следует искать новые параметры /// Токен отмены задачи /// Список параметров для коридоров бурения [HttpGet] [Obsolete("use GetByUidAsync(..) instead")] [Route("/api/telemetry/{uid}/drillFlowChart")] [AllowAnonymous] [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] public IActionResult GetByTelemetry(string uid, DateTime updateFrom, CancellationToken token) { var idWell = telemetryService.GetIdWellByTelemetryUid(uid); if (idWell is null) return BadRequest($"Wrong uid {uid}"); throw new NotImplementedException(); } /// /// Возвращает РТК по uid телеметрии /// /// uid телеметрии /// Дата, с которой следует искать новые параметры /// Токен отмены задачи /// Список параметров для коридоров бурения [HttpGet] [Route("/api/telemetry/{uid}/processMap")] [AllowAnonymous] [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] public async Task GetByUidAsync(string uid, DateTime updateFrom, CancellationToken token) { var idWell = telemetryService.GetIdWellByTelemetryUid(uid); if (idWell is null) return BadRequest($"Wrong uid {uid}"); var dto = await service.GetAllAsync((int)idWell, updateFrom, token); return Ok(dto); } /// /// Выгрузка расширенной РТК /// /// /// /// /// /// [HttpGet] [Route("getReportFile/{wellId}")] [ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK)] public async Task GetReportFileAsync(int wellId, CancellationToken token) { var stream = await processMapReportService.MakeReportAsync(wellId, token); if (stream != null) { var well = await wellService.GetOrDefaultAsync(wellId, token); if (well is null) return NoContent(); else { var fileName = $"РТК по скважине {well.Caption} куст {well.Cluster}.xlsx"; return File(stream, "application/octet-stream", fileName); } } else return NoContent(); } /// /// Выгрузка режимной карты по бурению скважины /// /// /// /// [HttpGet] [Route("getDrillProcessMap/{wellId}")] [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] public async Task GetDrillProcessMap(int wellId, CancellationToken token) { var data = await processMapService.GetProcessMapReportAsync(wellId, token); return Ok(data); } /// /// Добавить запись /// /// /// /// [HttpPost] public override async Task> InsertAsync([FromBody] ProcessMapPlanDto value, CancellationToken token) { value.IdUser = User.GetUserId() ?? -1; var result = await base.InsertAsync(value, token); if (result.Value > 0) await NotifyUsersBySignalR(value.IdWell, token); return result; } /// /// Редактировать запись по id /// /// запись /// /// 1 - успешно отредактировано, 0 - нет [HttpPut] public override async Task> UpdateAsync([FromBody] ProcessMapPlanDto value, CancellationToken token) { value.IdUser = User.GetUserId() ?? -1; var result = await base.UpdateAsync(value, token); if (result.Value > 0) await NotifyUsersBySignalR(value.IdWell, token); return result; } private async Task NotifyUsersBySignalR(int idWell, CancellationToken token) { var dtos = await service.GetAllAsync(idWell, null, token); _ = Task.Run(() => telemetryHubContext.Clients.Group($"well_{idWell}") .SendAsync(SirnalRMethodGetDataName, dtos), CancellationToken.None); } } }