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; + } + } +}