Добавлена возможность получения списка активно передающих данные скважин

This commit is contained in:
KharchenkoVV 2021-05-12 16:03:14 +05:00
parent e1a72db780
commit a1eb94901e
8 changed files with 99 additions and 3 deletions

View File

@ -0,0 +1,11 @@
using System.Collections.Generic;
namespace AsbCloudApp.Services
{
public interface ITelemetryTracker
{
void SaveRequest(string id);
void DeleteOldRequests(int minutes);
IEnumerable<string> GetRequests();
}
}

View File

@ -6,5 +6,6 @@ namespace AsbCloudApp.Services
public interface IWellService public interface IWellService
{ {
IEnumerable<WellDto> GetWellsByCustomer(int idCustomer); IEnumerable<WellDto> GetWellsByCustomer(int idCustomer);
IEnumerable<WellDto> GetTransmittingWells(int idCustomer);
} }
} }

View File

@ -21,6 +21,7 @@ namespace AsbCloudInfrastructure
services.AddSingleton(new MapperConfiguration(AutoMapperConfig)); services.AddSingleton(new MapperConfiguration(AutoMapperConfig));
services.AddSingleton(new CacheDb()); services.AddSingleton(new CacheDb());
services.AddSingleton<ITelemetryTracker, TelemetryTracker>();
services.AddTransient<IAuthService, AuthService>(); services.AddTransient<IAuthService, AuthService>();
services.AddTransient<IWellService, WellService>(); services.AddTransient<IWellService, WellService>();

View File

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using AsbCloudApp.Services;
namespace AsbCloudInfrastructure.Services
{
public class TelemetryTracker : ITelemetryTracker
{
private IDictionary<string, DateTime> _requests = new Dictionary<string, DateTime>();
public void SaveRequest(string id)
{
_requests[id] = DateTime.Now;
DeleteOldRequests();
}
public void DeleteOldRequests(int minutes = 6)
{
_requests = _requests.Where(dValue => (DateTime.Now - dValue.Value).Minutes < minutes).ToDictionary(dValue => dValue.Key, dValue => dValue.Value);
}
public IEnumerable<string> GetRequests()
{
DeleteOldRequests();
return _requests.Keys;
}
}
}

View File

@ -3,16 +3,28 @@ using AsbCloudApp.Services;
using AsbCloudDb.Model; using AsbCloudDb.Model;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System;
namespace AsbCloudInfrastructure.Services namespace AsbCloudInfrastructure.Services
{ {
public class WellService : IWellService public class WellService : IWellService
{ {
private readonly IAsbCloudDbContext db; private readonly IAsbCloudDbContext db;
private readonly ITelemetryTracker telemetryTracker;
public WellService(IAsbCloudDbContext db) public WellService(IAsbCloudDbContext db, ITelemetryTracker telemetryTracker)
{ {
this.db = db; this.db = db;
this.telemetryTracker = telemetryTracker;
}
public IEnumerable<WellDto> GetTransmittingWells(int idCustomer)
{
IEnumerable<string> activeTelemetriesUids = telemetryTracker.GetRequests();
var wells = db.GetWellsByCustomer(idCustomer)
.Where(w => activeTelemetriesUids.Contains(w.Telemetry.RemoteUid))
.ToList();
return wells.Select(w => From(w));
} }
public IEnumerable<WellDto> GetWellsByCustomer(int idCustomer) public IEnumerable<WellDto> GetWellsByCustomer(int idCustomer)
@ -21,7 +33,7 @@ namespace AsbCloudInfrastructure.Services
return wells.Select(w => From(w)); return wells.Select(w => From(w));
} }
private WellDto From(Well well) private static WellDto From(Well well)
{ {
var wellDto = new WellDto var wellDto = new WellDto
{ {
@ -33,5 +45,6 @@ namespace AsbCloudInfrastructure.Services
return wellDto; return wellDto;
} }
} }
} }

View File

@ -3,6 +3,7 @@ using AsbCloudApp.Services;
using AsbCloudWebApi.SignalR; using AsbCloudWebApi.SignalR;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -22,6 +23,7 @@ namespace AsbCloudWebApi.Controllers
private readonly IEventService eventService; private readonly IEventService eventService;
private readonly ITelemetryUserService telemetryUserService; private readonly ITelemetryUserService telemetryUserService;
private readonly IHubContext<TelemetryHub> telemetryHubContext; private readonly IHubContext<TelemetryHub> telemetryHubContext;
private readonly ITelemetryTracker telemetryTracker;
public TelemetryController( public TelemetryController(
ITelemetryService telemetryService, ITelemetryService telemetryService,
@ -29,7 +31,8 @@ namespace AsbCloudWebApi.Controllers
IMessageService messageService, IMessageService messageService,
IEventService eventService, IEventService eventService,
ITelemetryUserService telemetryUserService, ITelemetryUserService telemetryUserService,
IHubContext<TelemetryHub> telemetryHubContext) IHubContext<TelemetryHub> telemetryHubContext,
ITelemetryTracker telemetryTracker)
{ {
this.DataService = DataService; this.DataService = DataService;
this.telemetryService = telemetryService; this.telemetryService = telemetryService;
@ -37,6 +40,11 @@ namespace AsbCloudWebApi.Controllers
this.eventService = eventService; this.eventService = eventService;
this.telemetryUserService = telemetryUserService; this.telemetryUserService = telemetryUserService;
this.telemetryHubContext = telemetryHubContext; this.telemetryHubContext = telemetryHubContext;
this.telemetryTracker = telemetryTracker;
Request.Query.TryGetValue("uid", out Microsoft.Extensions.Primitives.StringValues pId);
string panelId = pId;
telemetryTracker.SaveRequest(panelId);
} }
/// <summary> /// <summary>

View File

@ -34,5 +34,21 @@ namespace AsbCloudWebApi.Controllers
return Ok(wells); return Ok(wells);
} }
[HttpGet("transmittingWells")]
[ProducesResponseType(typeof(IEnumerable<WellDto>), (int)System.Net.HttpStatusCode.OK)]
public IActionResult GetTransmittingWells()
{
int? idCustomer = User.GetCustomerId();
if(idCustomer is null)
{
return NoContent();
}
IEnumerable<WellDto> transmittingWells = wellService.GetTransmittingWells((int)idCustomer);
return Ok(transmittingWells);
}
} }
} }

View File

@ -0,0 +1,19 @@
using AsbCloudDb.Model;
using System.Security.Claims;
namespace AsbCloudWebApi
{
public static class Extensions
{
public static int? GetCustomerId(this ClaimsPrincipal user)
{
var claimIdCustomer = user.FindFirst(nameof(User.IdCustomer));
if (claimIdCustomer is null)
return null;
return int.TryParse(claimIdCustomer.Value, out int uid)
? uid
: null;
}
}
}