DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/TelemetryController.cs

128 lines
5.3 KiB
C#
Raw Normal View History

2021-04-07 18:01:56 +05:00
using AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudWebApi.SignalR;
2021-04-07 18:01:56 +05:00
using Microsoft.AspNetCore.Mvc;
2021-04-08 17:54:02 +05:00
using Microsoft.AspNetCore.SignalR;
using System;
2021-04-07 18:01:56 +05:00
using System.Collections.Generic;
using System.Linq;
using System.Threading;
2021-04-07 18:01:56 +05:00
using System.Threading.Tasks;
namespace AsbCloudWebApi.Controllers
{
/// <summary>
/// Контроллер сбора данных от буровых
/// </summary>
[Route("api/telemetry")]
[ApiController]
public class TelemetryController : ControllerBase
{
private readonly ITelemetryService telemetryService;
2021-04-23 10:21:25 +05:00
private readonly IMessageService messageService;
private readonly IEventService eventService;
private readonly ITelemetryUserService telemetryUserService;
2021-04-08 17:54:02 +05:00
private readonly IHubContext<TelemetryHub> telemetryHubContext;
2021-04-07 18:01:56 +05:00
2021-04-23 10:21:25 +05:00
public TelemetryController(
ITelemetryService telemetryService,
IMessageService messageService,
IEventService eventService,
ITelemetryUserService telemetryUserService,
IHubContext<TelemetryHub> telemetryHubContext)
2021-04-07 18:01:56 +05:00
{
this.telemetryService = telemetryService;
2021-04-23 10:21:25 +05:00
this.messageService = messageService;
this.eventService = eventService;
this.telemetryUserService = telemetryUserService;
2021-04-08 17:54:02 +05:00
this.telemetryHubContext = telemetryHubContext;
}
2021-07-21 15:29:19 +05:00
2021-04-07 18:01:56 +05:00
/// <summary>
/// Принимает общую информацию по скважине
/// </summary>
2021-04-08 17:54:02 +05:00
/// <param name="uid">Уникальный идентификатор отправителя</param>
/// <param name="info">Информация об отправителе</param>
/// <param name="token">Токен отмены задачи</param>
2021-04-07 18:01:56 +05:00
/// <returns></returns>
[HttpPost]
[Route("{uid}/info")]
public async Task<IActionResult> PostInfoAsync(string uid, [FromBody] TelemetryInfoDto info,
CancellationToken token = default)
2021-04-07 18:01:56 +05:00
{
2021-11-22 11:30:08 +05:00
await telemetryService.UpdateInfoAsync(uid, info, token).ConfigureAwait(false);
return Ok();
}
/// <summary>
/// Обновляет часовой пояс скважины
/// </summary>
/// <param name="uid">Уникальный идентификатор отправителя</param>
/// <param name="timeZoneInfo">Информация о часовом поясе</param>
/// <param name="token">Токен отмены задачи</param>
/// <returns></returns>
[HttpPost]
[Route("{uid}/timeZone")]
public async Task<IActionResult> UpdateTimeZoneAsync(string uid, TelemetryTimeZoneDto timeZoneInfo,
CancellationToken token = default)
{
2021-11-22 11:30:08 +05:00
await telemetryService.UpdateTimeZoneAsync(uid, timeZoneInfo, token)
.ConfigureAwait(false);
2021-04-07 18:01:56 +05:00
return Ok();
}
2021-04-23 10:21:25 +05:00
/// <summary>
/// Принимает список новых сообщений от телеметрии
/// </summary>
/// <param name="uid">Уникальный идентификатор отправителя</param>
/// <param name="dtos">сообщения</param>
/// <param name="token">Токен для отмены задачи</param>
2021-04-23 10:21:25 +05:00
/// <returns></returns>
[HttpPost]
[Route("{uid}/message")]
public async Task<IActionResult> PostMessagesAsync(string uid, [FromBody] IEnumerable<TelemetryMessageDto> dtos,
CancellationToken token = default)
2021-04-23 10:21:25 +05:00
{
var idWell = telemetryService.GetIdWellByTelemetryUid(uid);
await messageService.InsertAsync(uid, dtos, token).ConfigureAwait(false);
2021-04-23 10:21:25 +05:00
if (dtos.Any())
await Task.Run(() => telemetryHubContext.Clients.Group($"well_{idWell}")
.SendAsync("ReceiveMessages", dtos), token).ConfigureAwait(false);
2021-04-23 10:21:25 +05:00
return Ok();
}
/// <summary>
/// Принимает справочник событий
/// </summary>
/// <param name="uid">Уникальный идентификатор отправителя</param>
/// <param name="events">справочник событий</param>
/// <param name="token">Токен для отмены задачи</param>
2021-04-23 10:21:25 +05:00
/// <returns></returns>
[HttpPost]
[Route("{uid}/event")]
public async Task<IActionResult> PostEventsAsync(string uid, [FromBody] List<EventDto> events,
CancellationToken token = default)
2021-04-23 10:21:25 +05:00
{
await eventService.UpsertAsync(uid, events, token)
.ConfigureAwait(false);
2021-04-23 10:21:25 +05:00
return Ok();
}
/// <summary>
/// Принимает справочник пользователей телеметрии
/// </summary>
/// <param name="uid">Уникальный идентификатор отправителя</param>
/// <param name="users">справочник пользователей телеметрии</param>
/// <returns></returns>
[HttpPost]
[Route("{uid}/user")]
public IActionResult PostUsers(string uid, [FromBody] List<TelemetryUserDto> users)
2021-04-23 10:21:25 +05:00
{
telemetryUserService.Upsert(uid, users);
return Ok();
}
2021-04-07 18:01:56 +05:00
}
}