From a1eb94901e870a938727fed37c911d9fcce47bcd Mon Sep 17 00:00:00 2001 From: KharchenkoVV Date: Wed, 12 May 2021 16:03:14 +0500 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D1=8C=20=D0=BF=D0=BE=D0=BB=D1=83=D1=87=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D1=81=D0=BF=D0=B8=D1=81=D0=BA=D0=B0=20=D0=B0?= =?UTF-8?q?=D0=BA=D1=82=D0=B8=D0=B2=D0=BD=D0=BE=20=D0=BF=D0=B5=D1=80=D0=B5?= =?UTF-8?q?=D0=B4=D0=B0=D1=8E=D1=89=D0=B8=D1=85=20=D0=B4=D0=B0=D0=BD=D0=BD?= =?UTF-8?q?=D1=8B=D0=B5=20=D1=81=D0=BA=D0=B2=D0=B0=D0=B6=D0=B8=D0=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudApp/Services/ITelemetryTracker.cs | 11 ++++++++ AsbCloudApp/Services/IWellService.cs | 1 + AsbCloudInfrastructure/DependencyInjection.cs | 1 + .../Services/TelemetryTracker.cs | 27 +++++++++++++++++++ .../Services/WellService.cs | 17 ++++++++++-- .../Controllers/TelemetryController.cs | 10 ++++++- AsbCloudWebApi/Controllers/WellController.cs | 16 +++++++++++ AsbCloudWebApi/Extensions.cs | 19 +++++++++++++ 8 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 AsbCloudApp/Services/ITelemetryTracker.cs create mode 100644 AsbCloudInfrastructure/Services/TelemetryTracker.cs create mode 100644 AsbCloudWebApi/Extensions.cs diff --git a/AsbCloudApp/Services/ITelemetryTracker.cs b/AsbCloudApp/Services/ITelemetryTracker.cs new file mode 100644 index 00000000..e8baf619 --- /dev/null +++ b/AsbCloudApp/Services/ITelemetryTracker.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; + +namespace AsbCloudApp.Services +{ + public interface ITelemetryTracker + { + void SaveRequest(string id); + void DeleteOldRequests(int minutes); + IEnumerable GetRequests(); + } +} diff --git a/AsbCloudApp/Services/IWellService.cs b/AsbCloudApp/Services/IWellService.cs index 4f32a92c..e3d111c7 100644 --- a/AsbCloudApp/Services/IWellService.cs +++ b/AsbCloudApp/Services/IWellService.cs @@ -6,5 +6,6 @@ namespace AsbCloudApp.Services public interface IWellService { IEnumerable GetWellsByCustomer(int idCustomer); + IEnumerable GetTransmittingWells(int idCustomer); } } diff --git a/AsbCloudInfrastructure/DependencyInjection.cs b/AsbCloudInfrastructure/DependencyInjection.cs index 63982103..81aa7aae 100644 --- a/AsbCloudInfrastructure/DependencyInjection.cs +++ b/AsbCloudInfrastructure/DependencyInjection.cs @@ -21,6 +21,7 @@ namespace AsbCloudInfrastructure services.AddSingleton(new MapperConfiguration(AutoMapperConfig)); services.AddSingleton(new CacheDb()); + services.AddSingleton(); services.AddTransient(); services.AddTransient(); diff --git a/AsbCloudInfrastructure/Services/TelemetryTracker.cs b/AsbCloudInfrastructure/Services/TelemetryTracker.cs new file mode 100644 index 00000000..c0d2d031 --- /dev/null +++ b/AsbCloudInfrastructure/Services/TelemetryTracker.cs @@ -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 _requests = new Dictionary(); + 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 GetRequests() + { + DeleteOldRequests(); + return _requests.Keys; + } + } +} diff --git a/AsbCloudInfrastructure/Services/WellService.cs b/AsbCloudInfrastructure/Services/WellService.cs index 13ede118..338c735e 100644 --- a/AsbCloudInfrastructure/Services/WellService.cs +++ b/AsbCloudInfrastructure/Services/WellService.cs @@ -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 GetTransmittingWells(int idCustomer) + { + IEnumerable activeTelemetriesUids = telemetryTracker.GetRequests(); + var wells = db.GetWellsByCustomer(idCustomer) + .Where(w => activeTelemetriesUids.Contains(w.Telemetry.RemoteUid)) + .ToList(); + return wells.Select(w => From(w)); } public IEnumerable 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; } + } } diff --git a/AsbCloudWebApi/Controllers/TelemetryController.cs b/AsbCloudWebApi/Controllers/TelemetryController.cs index b066e9e1..5acf1b0a 100644 --- a/AsbCloudWebApi/Controllers/TelemetryController.cs +++ b/AsbCloudWebApi/Controllers/TelemetryController.cs @@ -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 telemetryHubContext; + private readonly ITelemetryTracker telemetryTracker; public TelemetryController( ITelemetryService telemetryService, @@ -29,7 +31,8 @@ namespace AsbCloudWebApi.Controllers IMessageService messageService, IEventService eventService, ITelemetryUserService telemetryUserService, - IHubContext telemetryHubContext) + IHubContext 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); } /// diff --git a/AsbCloudWebApi/Controllers/WellController.cs b/AsbCloudWebApi/Controllers/WellController.cs index 8f55dc49..57d3ab47 100644 --- a/AsbCloudWebApi/Controllers/WellController.cs +++ b/AsbCloudWebApi/Controllers/WellController.cs @@ -34,5 +34,21 @@ namespace AsbCloudWebApi.Controllers return Ok(wells); } + [HttpGet("transmittingWells")] + [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] + public IActionResult GetTransmittingWells() + { + int? idCustomer = User.GetCustomerId(); + + if(idCustomer is null) + { + return NoContent(); + } + + IEnumerable transmittingWells = wellService.GetTransmittingWells((int)idCustomer); + + return Ok(transmittingWells); + + } } } diff --git a/AsbCloudWebApi/Extensions.cs b/AsbCloudWebApi/Extensions.cs new file mode 100644 index 00000000..3197eb85 --- /dev/null +++ b/AsbCloudWebApi/Extensions.cs @@ -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; + } + } +}