forked from ddrilling/AsbCloudServer
Добавлена возможность получения списка активно передающих данные скважин
This commit is contained in:
parent
e1a72db780
commit
a1eb94901e
11
AsbCloudApp/Services/ITelemetryTracker.cs
Normal file
11
AsbCloudApp/Services/ITelemetryTracker.cs
Normal 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();
|
||||
}
|
||||
}
|
@ -6,5 +6,6 @@ namespace AsbCloudApp.Services
|
||||
public interface IWellService
|
||||
{
|
||||
IEnumerable<WellDto> GetWellsByCustomer(int idCustomer);
|
||||
IEnumerable<WellDto> GetTransmittingWells(int idCustomer);
|
||||
}
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ namespace AsbCloudInfrastructure
|
||||
|
||||
services.AddSingleton(new MapperConfiguration(AutoMapperConfig));
|
||||
services.AddSingleton(new CacheDb());
|
||||
services.AddSingleton<ITelemetryTracker, TelemetryTracker>();
|
||||
|
||||
services.AddTransient<IAuthService, AuthService>();
|
||||
services.AddTransient<IWellService, WellService>();
|
||||
|
27
AsbCloudInfrastructure/Services/TelemetryTracker.cs
Normal file
27
AsbCloudInfrastructure/Services/TelemetryTracker.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -3,16 +3,28 @@ using AsbCloudApp.Services;
|
||||
using AsbCloudDb.Model;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System;
|
||||
|
||||
namespace AsbCloudInfrastructure.Services
|
||||
{
|
||||
public class WellService : IWellService
|
||||
{
|
||||
private readonly IAsbCloudDbContext db;
|
||||
private readonly ITelemetryTracker telemetryTracker;
|
||||
|
||||
public WellService(IAsbCloudDbContext db)
|
||||
public WellService(IAsbCloudDbContext db, ITelemetryTracker telemetryTracker)
|
||||
{
|
||||
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)
|
||||
@ -21,7 +33,7 @@ namespace AsbCloudInfrastructure.Services
|
||||
return wells.Select(w => From(w));
|
||||
}
|
||||
|
||||
private WellDto From(Well well)
|
||||
private static WellDto From(Well well)
|
||||
{
|
||||
var wellDto = new WellDto
|
||||
{
|
||||
@ -33,5 +45,6 @@ namespace AsbCloudInfrastructure.Services
|
||||
|
||||
return wellDto;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ using AsbCloudApp.Services;
|
||||
using AsbCloudWebApi.SignalR;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
@ -22,6 +23,7 @@ namespace AsbCloudWebApi.Controllers
|
||||
private readonly IEventService eventService;
|
||||
private readonly ITelemetryUserService telemetryUserService;
|
||||
private readonly IHubContext<TelemetryHub> telemetryHubContext;
|
||||
private readonly ITelemetryTracker telemetryTracker;
|
||||
|
||||
public TelemetryController(
|
||||
ITelemetryService telemetryService,
|
||||
@ -29,7 +31,8 @@ namespace AsbCloudWebApi.Controllers
|
||||
IMessageService messageService,
|
||||
IEventService eventService,
|
||||
ITelemetryUserService telemetryUserService,
|
||||
IHubContext<TelemetryHub> telemetryHubContext)
|
||||
IHubContext<TelemetryHub> telemetryHubContext,
|
||||
ITelemetryTracker telemetryTracker)
|
||||
{
|
||||
this.DataService = DataService;
|
||||
this.telemetryService = telemetryService;
|
||||
@ -37,6 +40,11 @@ namespace AsbCloudWebApi.Controllers
|
||||
this.eventService = eventService;
|
||||
this.telemetryUserService = telemetryUserService;
|
||||
this.telemetryHubContext = telemetryHubContext;
|
||||
this.telemetryTracker = telemetryTracker;
|
||||
|
||||
Request.Query.TryGetValue("uid", out Microsoft.Extensions.Primitives.StringValues pId);
|
||||
string panelId = pId;
|
||||
telemetryTracker.SaveRequest(panelId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -34,5 +34,21 @@ namespace AsbCloudWebApi.Controllers
|
||||
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);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
19
AsbCloudWebApi/Extensions.cs
Normal file
19
AsbCloudWebApi/Extensions.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user