From ae13d5d52b06568a11ea39782120353caf814596 Mon Sep 17 00:00:00 2001 From: Dustin Brewer Date: Mon, 31 Aug 2020 00:07:55 +0000 Subject: [PATCH] Utilize newer version of TwoMQTT, base-Liasons, etc. --- WSDOT/DataAccess/SourceDAO.cs | 16 +++---- WSDOT/Liasons/MQTTLiason.cs | 30 ++++--------- WSDOT/Liasons/SourceLiason.cs | 43 +++++-------------- WSDOT/Models/Options/SharedOpts.cs | 3 +- .../Source/{FetchResponse.cs => Response.cs} | 2 +- WSDOT/Program.cs | 25 ++++++++++- WSDOT/WSDOT.csproj | 6 +-- WSDOTTest/Liasons/SourceLiasonTest.cs | 2 +- WSDOTTest/WSDOTTest.csproj | 6 +-- vendor/.retain | 0 10 files changed, 57 insertions(+), 76 deletions(-) rename WSDOT/Models/Source/{FetchResponse.cs => Response.cs} (96%) create mode 100644 vendor/.retain diff --git a/WSDOT/DataAccess/SourceDAO.cs b/WSDOT/DataAccess/SourceDAO.cs index 41424d7..f8bb198 100644 --- a/WSDOT/DataAccess/SourceDAO.cs +++ b/WSDOT/DataAccess/SourceDAO.cs @@ -4,20 +4,14 @@ using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Newtonsoft.Json; +using TwoMQTT.Core.Interfaces; using WSDOT.Models.Shared; using WSDOT.Models.Source; namespace WSDOT.DataAccess { - public interface ISourceDAO + public interface ISourceDAO : ISourceDAO { - /// - /// Fetch one response from the source. - /// - /// - /// - /// - Task FetchOneAsync(SlugMapping data, CancellationToken cancellationToken = default); } /// @@ -40,7 +34,7 @@ public SourceDAO(ILogger logger, IHttpClientFactory httpClientFactory } /// - public async Task FetchOneAsync(SlugMapping data, + public async Task FetchOneAsync(SlugMapping data, CancellationToken cancellationToken = default) { try @@ -78,7 +72,7 @@ public SourceDAO(ILogger logger, IHttpClientFactory httpClientFactory /// /// /// - private async Task FetchAsync(long timeTravelId, + private async Task FetchAsync(long timeTravelId, CancellationToken cancellationToken = default) { this.Logger.LogDebug($"Started finding {timeTravelId} from WSDOT"); @@ -87,7 +81,7 @@ public SourceDAO(ILogger logger, IHttpClientFactory httpClientFactory var resp = await this.Client.GetAsync($"{baseUrl}?{query}", cancellationToken); resp.EnsureSuccessStatusCode(); var content = await resp.Content.ReadAsStringAsync(); - var obj = JsonConvert.DeserializeObject(content); + var obj = JsonConvert.DeserializeObject(content); this.Logger.LogDebug("Finished finding {ttid} from WSDOT", timeTravelId); return obj; diff --git a/WSDOT/Liasons/MQTTLiason.cs b/WSDOT/Liasons/MQTTLiason.cs index 5458bac..1a85416 100644 --- a/WSDOT/Liasons/MQTTLiason.cs +++ b/WSDOT/Liasons/MQTTLiason.cs @@ -5,6 +5,7 @@ using Microsoft.Extensions.Options; using TwoMQTT.Core; using TwoMQTT.Core.Interfaces; +using TwoMQTT.Core.Liasons; using TwoMQTT.Core.Models; using TwoMQTT.Core.Utils; using WSDOT.Models.Options; @@ -12,20 +13,20 @@ namespace WSDOT.Liasons { - /// - public class MQTTLiason : IMQTTLiason + /// + /// An class representing a managed way to interact with MQTT. + /// + public class MQTTLiason : MQTTLiasonBase, IMQTTLiason { /// - /// Initializes a new instance of the MQTTLiason class. + /// /// /// /// /// - public MQTTLiason(ILogger logger, IMQTTGenerator generator, IOptions sharedOpts) + public MQTTLiason(ILogger logger, IMQTTGenerator generator, IOptions sharedOpts) : + base(logger, generator, sharedOpts) { - this.Logger = logger; - this.Generator = generator; - this.Questions = sharedOpts.Value.Resources; } /// @@ -78,20 +79,5 @@ public MQTTLiason(ILogger logger, IMQTTGenerator generator, IOptions return discoveries; } - - /// - /// The logger used internally. - /// - private readonly ILogger Logger; - - /// - /// The questions to ask the source (typically some kind of key/slug pairing). - /// - private readonly List Questions; - - /// - /// The MQTT generator used for things such as availability topic, state topic, command topic, etc. - /// - private readonly IMQTTGenerator Generator; } } \ No newline at end of file diff --git a/WSDOT/Liasons/SourceLiason.cs b/WSDOT/Liasons/SourceLiason.cs index 4e76d17..380d419 100644 --- a/WSDOT/Liasons/SourceLiason.cs +++ b/WSDOT/Liasons/SourceLiason.cs @@ -1,10 +1,11 @@ -using System.Collections.Generic; -using System.Runtime.CompilerServices; using System.Threading; +using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using TwoMQTT.Core.Interfaces; +using TwoMQTT.Core.Liasons; using WSDOT.DataAccess; +using WSDOT.Models.Options; using WSDOT.Models.Shared; using WSDOT.Models.Source; @@ -13,15 +14,12 @@ namespace WSDOT.Liasons /// /// A class representing a managed way to interact with a source. /// - public class SourceLiason : ISourceLiason + public class SourceLiason : SourceLiasonBase, ISourceLiason { public SourceLiason(ILogger logger, ISourceDAO sourceDAO, - IOptions opts, IOptions sharedOpts) + IOptions opts, IOptions sharedOpts) : + base(logger, sourceDAO, sharedOpts) { - this.Logger = logger; - this.SourceDAO = sourceDAO; - this.Questions = sharedOpts.Value.Resources; - this.Logger.LogInformation( "ApiKey: {apiKey}\n" + "PollingInterval: {pollingInterval}\n" + @@ -34,38 +32,19 @@ public SourceLiason(ILogger logger, ISourceDAO sourceDAO, } /// - public async IAsyncEnumerable FetchAllAsync([EnumeratorCancellation] CancellationToken cancellationToken = default) + protected override async Task FetchOneAsync(SlugMapping key, CancellationToken cancellationToken) { - foreach (var key in this.Questions) - { - this.Logger.LogDebug("Looking up {key}", key); - var result = await this.SourceDAO.FetchOneAsync(key, cancellationToken); - var resp = result != null ? this.MapData(result) : null; - yield return resp; - } + var result = await this.SourceDAO.FetchOneAsync(key, cancellationToken); + var resp = result != null ? this.MapData(result) : null; + return resp; } - /// - /// The logger used internally. - /// - private readonly ILogger Logger; - - /// - /// The dao used to interact with the source. - /// - private readonly ISourceDAO SourceDAO; - - /// - /// The questions to ask the source (typically some kind of key/slug pairing). - /// - private readonly List Questions; - /// /// Map the source response to a shared response representation. /// /// /// - private Resource MapData(FetchResponse src) => + private Resource MapData(Response src) => new Resource { TravelTimeID = src.TravelTimeID, diff --git a/WSDOT/Models/Options/SharedOpts.cs b/WSDOT/Models/Options/SharedOpts.cs index c627c4d..fea94c6 100644 --- a/WSDOT/Models/Options/SharedOpts.cs +++ b/WSDOT/Models/Options/SharedOpts.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using TwoMQTT.Core.Interfaces; using WSDOT.Models.Shared; namespace WSDOT.Models.Options @@ -6,7 +7,7 @@ namespace WSDOT.Models.Options /// /// The shared options across the application /// - public class SharedOpts + public class SharedOpts : ISharedOpts { public const string Section = "WSDOT"; diff --git a/WSDOT/Models/Source/FetchResponse.cs b/WSDOT/Models/Source/Response.cs similarity index 96% rename from WSDOT/Models/Source/FetchResponse.cs rename to WSDOT/Models/Source/Response.cs index 977a5e7..4caeac2 100644 --- a/WSDOT/Models/Source/FetchResponse.cs +++ b/WSDOT/Models/Source/Response.cs @@ -3,7 +3,7 @@ namespace WSDOT.Models.Source /// /// The response from the source /// - public class FetchResponse + public class Response { /// /// diff --git a/WSDOT/Program.cs b/WSDOT/Program.cs index ba2dd77..ca0ec3a 100644 --- a/WSDOT/Program.cs +++ b/WSDOT/Program.cs @@ -9,7 +9,7 @@ using TwoMQTT.Core; using TwoMQTT.Core.Extensions; using TwoMQTT.Core.Interfaces; -using TwoMQTT.Core.Utils; +using TwoMQTT.Core.Managers; using WSDOT.DataAccess; using WSDOT.Liasons; using WSDOT.Models.Shared; @@ -50,12 +50,33 @@ protected override IServiceCollection ConfigureServices(HostBuilderContext hostC .AddSingleton(x => { var opts = x.GetService>(); + if (opts == null) + { + throw new ArgumentException($"{nameof(opts.Value.PollingInterval)} is required for {nameof(ThrottleManager)}."); + } + return new ThrottleManager(opts.Value.PollingInterval); }) .AddSingleton(x => { + var logger = x.GetService>(); + var httpClientFactory = x.GetService(); var opts = x.GetService>(); - return new SourceDAO(x.GetService>(), x.GetService(), opts.Value.ApiKey); + + if (logger == null) + { + throw new ArgumentException($"{nameof(logger)} is required for {nameof(SourceDAO)}."); + } + if (httpClientFactory == null) + { + throw new ArgumentException($"{nameof(httpClientFactory)} is required for {nameof(SourceDAO)}."); + } + if (opts == null) + { + throw new ArgumentException($"{nameof(opts.Value.ApiKey)} are required for {nameof(SourceDAO)}."); + } + + return new SourceDAO(logger, httpClientFactory, opts.Value.ApiKey); }); } } diff --git a/WSDOT/WSDOT.csproj b/WSDOT/WSDOT.csproj index 78d6e86..72c12ac 100644 --- a/WSDOT/WSDOT.csproj +++ b/WSDOT/WSDOT.csproj @@ -1,15 +1,15 @@ - 0.7.$([System.DateTime]::UtcNow.ToString(yy))$([System.DateTime]::UtcNow.DayOfYear.ToString(000)).$([System.DateTime]::UtcNow.ToString(HHmm))$([System.Math]::Floor($([MSBuild]::Divide($([System.DateTime]::UtcNow.Second), 6)))) + 0.8.$([System.DateTime]::UtcNow.ToString(yy))$([System.DateTime]::UtcNow.DayOfYear.ToString(000)).$([System.DateTime]::UtcNow.ToString(HHmm))$([System.Math]::Floor($([MSBuild]::Divide($([System.DateTime]::UtcNow.Second), 6)))) Exe netcoreapp3.1 enable - $(RestoreSources);https://api.nuget.org/v3/index.json + $(RestoreSources);../vendor;https://api.nuget.org/v3/index.json - + diff --git a/WSDOTTest/Liasons/SourceLiasonTest.cs b/WSDOTTest/Liasons/SourceLiasonTest.cs index 298f36a..eee0893 100644 --- a/WSDOTTest/Liasons/SourceLiasonTest.cs +++ b/WSDOTTest/Liasons/SourceLiasonTest.cs @@ -39,7 +39,7 @@ public async Task FetchAllAsyncTest() }); sourceDAO.Setup(x => x.FetchOneAsync(test.Q, It.IsAny())) - .ReturnsAsync(new WSDOT.Models.Source.FetchResponse + .ReturnsAsync(new WSDOT.Models.Source.Response { TravelTimeID = test.Expected.TravelTimeID, CurrentTime = test.Expected.CurrentTime, diff --git a/WSDOTTest/WSDOTTest.csproj b/WSDOTTest/WSDOTTest.csproj index 848986e..216eb15 100644 --- a/WSDOTTest/WSDOTTest.csproj +++ b/WSDOTTest/WSDOTTest.csproj @@ -4,14 +4,14 @@ netcoreapp3.1 false - $(RestoreSources);https://api.nuget.org/v3/index.json + $(RestoreSources);../vendor;https://api.nuget.org/v3/index.json - - + + diff --git a/vendor/.retain b/vendor/.retain new file mode 100644 index 0000000..e69de29