From 7bbcdf84831f2250280c47033127d048494da483 Mon Sep 17 00:00:00 2001 From: KharchenkoVV Date: Wed, 12 May 2021 17:25:52 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20=D0=BA?= =?UTF-8?q?=20=D0=BF=D1=80=D0=B5=D0=B4=D1=8B=D0=B4=D1=83=D1=89=D0=B5=D0=BC?= =?UTF-8?q?=D1=83=20=D0=BA=D0=BE=D0=BC=D0=BC=D0=B8=D1=82=D1=83=20=D0=BF?= =?UTF-8?q?=D0=BE=20=D0=B7=D0=B0=D0=B4=D0=B0=D1=87=D0=B5=20CS2-6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudApp/Services/ITelemetryTracker.cs | 1 - .../Services/TelemetryTracker.cs | 15 +++++++++------ AsbCloudInfrastructure/Services/WellService.cs | 10 +++++++--- AsbCloudWebApi/Controllers/TelemetryController.cs | 13 ++++++++++--- AsbCloudWebApi/Controllers/WellController.cs | 2 +- 5 files changed, 27 insertions(+), 14 deletions(-) diff --git a/AsbCloudApp/Services/ITelemetryTracker.cs b/AsbCloudApp/Services/ITelemetryTracker.cs index e8baf619..ac582a52 100644 --- a/AsbCloudApp/Services/ITelemetryTracker.cs +++ b/AsbCloudApp/Services/ITelemetryTracker.cs @@ -5,7 +5,6 @@ namespace AsbCloudApp.Services public interface ITelemetryTracker { void SaveRequest(string id); - void DeleteOldRequests(int minutes); IEnumerable GetRequests(); } } diff --git a/AsbCloudInfrastructure/Services/TelemetryTracker.cs b/AsbCloudInfrastructure/Services/TelemetryTracker.cs index c0d2d031..f6c87ad5 100644 --- a/AsbCloudInfrastructure/Services/TelemetryTracker.cs +++ b/AsbCloudInfrastructure/Services/TelemetryTracker.cs @@ -8,19 +8,22 @@ namespace AsbCloudInfrastructure.Services public class TelemetryTracker : ITelemetryTracker { private IDictionary _requests = new Dictionary(); + private readonly TimeSpan _timeout = TimeSpan.FromMinutes(6); + + private void _deleteOldRequests() + { + _requests = _requests.Where(dValue => (DateTime.Now - dValue.Value) < _timeout).ToDictionary(dValue => dValue.Key, dValue => dValue.Value); + } + 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); + _deleteOldRequests(); } public IEnumerable GetRequests() { - DeleteOldRequests(); + _deleteOldRequests(); return _requests.Keys; } } diff --git a/AsbCloudInfrastructure/Services/WellService.cs b/AsbCloudInfrastructure/Services/WellService.cs index 338c735e..2557d23f 100644 --- a/AsbCloudInfrastructure/Services/WellService.cs +++ b/AsbCloudInfrastructure/Services/WellService.cs @@ -20,10 +20,14 @@ namespace AsbCloudInfrastructure.Services public IEnumerable GetTransmittingWells(int idCustomer) { + var wells = new List(); IEnumerable activeTelemetriesUids = telemetryTracker.GetRequests(); - var wells = db.GetWellsByCustomer(idCustomer) - .Where(w => activeTelemetriesUids.Contains(w.Telemetry.RemoteUid)) - .ToList(); + if (activeTelemetriesUids.Count() > 0) + { + wells = db.GetWellsByCustomer(idCustomer) + .Where(w => activeTelemetriesUids.Contains(w.Telemetry.RemoteUid)) + .ToList(); + } return wells.Select(w => From(w)); } diff --git a/AsbCloudWebApi/Controllers/TelemetryController.cs b/AsbCloudWebApi/Controllers/TelemetryController.cs index 5acf1b0a..05d52c22 100644 --- a/AsbCloudWebApi/Controllers/TelemetryController.cs +++ b/AsbCloudWebApi/Controllers/TelemetryController.cs @@ -42,9 +42,16 @@ namespace AsbCloudWebApi.Controllers this.telemetryHubContext = telemetryHubContext; this.telemetryTracker = telemetryTracker; - Request.Query.TryGetValue("uid", out Microsoft.Extensions.Primitives.StringValues pId); - string panelId = pId; - telemetryTracker.SaveRequest(panelId); + _registerRequest(); + } + + private void _registerRequest() + { + if (Request.Query.TryGetValue("uid", out Microsoft.Extensions.Primitives.StringValues pId) && pId.Count > 0) + { + string panelId = pId.First(); + telemetryTracker.SaveRequest(panelId); + } } /// diff --git a/AsbCloudWebApi/Controllers/WellController.cs b/AsbCloudWebApi/Controllers/WellController.cs index 57d3ab47..9530eceb 100644 --- a/AsbCloudWebApi/Controllers/WellController.cs +++ b/AsbCloudWebApi/Controllers/WellController.cs @@ -45,7 +45,7 @@ namespace AsbCloudWebApi.Controllers return NoContent(); } - IEnumerable transmittingWells = wellService.GetTransmittingWells((int)idCustomer); + var transmittingWells = wellService.GetTransmittingWells((int)idCustomer); return Ok(transmittingWells);