Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
mannkind committed Aug 31, 2020
2 parents 629ec81 + ae13d5d commit aa238fe
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 76 deletions.
16 changes: 5 additions & 11 deletions WSDOT/DataAccess/SourceDAO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SlugMapping, Response, Command, object>
{
/// <summary>
/// Fetch one response from the source.
/// </summary>
/// <param name="data"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<FetchResponse?> FetchOneAsync(SlugMapping data, CancellationToken cancellationToken = default);
}

/// <summary>
Expand All @@ -40,7 +34,7 @@ public SourceDAO(ILogger<SourceDAO> logger, IHttpClientFactory httpClientFactory
}

/// <inheritdoc />
public async Task<FetchResponse?> FetchOneAsync(SlugMapping data,
public async Task<Response?> FetchOneAsync(SlugMapping data,
CancellationToken cancellationToken = default)
{
try
Expand Down Expand Up @@ -78,7 +72,7 @@ public SourceDAO(ILogger<SourceDAO> logger, IHttpClientFactory httpClientFactory
/// <param name="timeTravelId"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
private async Task<FetchResponse?> FetchAsync(long timeTravelId,
private async Task<Response?> FetchAsync(long timeTravelId,
CancellationToken cancellationToken = default)
{
this.Logger.LogDebug($"Started finding {timeTravelId} from WSDOT");
Expand All @@ -87,7 +81,7 @@ public SourceDAO(ILogger<SourceDAO> logger, IHttpClientFactory httpClientFactory
var resp = await this.Client.GetAsync($"{baseUrl}?{query}", cancellationToken);
resp.EnsureSuccessStatusCode();
var content = await resp.Content.ReadAsStringAsync();
var obj = JsonConvert.DeserializeObject<FetchResponse>(content);
var obj = JsonConvert.DeserializeObject<Response>(content);
this.Logger.LogDebug("Finished finding {ttid} from WSDOT", timeTravelId);

return obj;
Expand Down
30 changes: 8 additions & 22 deletions WSDOT/Liasons/MQTTLiason.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,28 @@
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;
using WSDOT.Models.Shared;

namespace WSDOT.Liasons
{
/// <inheritdoc />
public class MQTTLiason : IMQTTLiason<Resource, Command>
/// <summary>
/// An class representing a managed way to interact with MQTT.
/// </summary>
public class MQTTLiason : MQTTLiasonBase<Resource, Command, SlugMapping, SharedOpts>, IMQTTLiason<Resource, Command>
{
/// <summary>
/// Initializes a new instance of the MQTTLiason class.
///
/// </summary>
/// <param name="logger"></param>
/// <param name="generator"></param>
/// <param name="sharedOpts"></param>
public MQTTLiason(ILogger<MQTTLiason> logger, IMQTTGenerator generator, IOptions<SharedOpts> sharedOpts)
public MQTTLiason(ILogger<MQTTLiason> logger, IMQTTGenerator generator, IOptions<SharedOpts> sharedOpts) :
base(logger, generator, sharedOpts)
{
this.Logger = logger;
this.Generator = generator;
this.Questions = sharedOpts.Value.Resources;
}

/// <inheritdoc />
Expand Down Expand Up @@ -78,20 +79,5 @@ public MQTTLiason(ILogger<MQTTLiason> logger, IMQTTGenerator generator, IOptions

return discoveries;
}

/// <summary>
/// The logger used internally.
/// </summary>
private readonly ILogger<MQTTLiason> Logger;

/// <summary>
/// The questions to ask the source (typically some kind of key/slug pairing).
/// </summary>
private readonly List<SlugMapping> Questions;

/// <summary>
/// The MQTT generator used for things such as availability topic, state topic, command topic, etc.
/// </summary>
private readonly IMQTTGenerator Generator;
}
}
43 changes: 11 additions & 32 deletions WSDOT/Liasons/SourceLiason.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -13,15 +14,12 @@ namespace WSDOT.Liasons
/// <summary>
/// A class representing a managed way to interact with a source.
/// </summary>
public class SourceLiason : ISourceLiason<Resource, Command>
public class SourceLiason : SourceLiasonBase<Resource, Command, SlugMapping, ISourceDAO, SharedOpts>, ISourceLiason<Resource, Command>
{
public SourceLiason(ILogger<SourceLiason> logger, ISourceDAO sourceDAO,
IOptions<Models.Options.SourceOpts> opts, IOptions<Models.Options.SharedOpts> sharedOpts)
IOptions<SourceOpts> opts, IOptions<SharedOpts> 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" +
Expand All @@ -34,38 +32,19 @@ public SourceLiason(ILogger<SourceLiason> logger, ISourceDAO sourceDAO,
}

/// <inheritdoc />
public async IAsyncEnumerable<Resource?> FetchAllAsync([EnumeratorCancellation] CancellationToken cancellationToken = default)
protected override async Task<Resource?> 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;
}

/// <summary>
/// The logger used internally.
/// </summary>
private readonly ILogger<SourceLiason> Logger;

/// <summary>
/// The dao used to interact with the source.
/// </summary>
private readonly ISourceDAO SourceDAO;

/// <summary>
/// The questions to ask the source (typically some kind of key/slug pairing).
/// </summary>
private readonly List<SlugMapping> Questions;

/// <summary>
/// Map the source response to a shared response representation.
/// </summary>
/// <param name="src"></param>
/// <returns></returns>
private Resource MapData(FetchResponse src) =>
private Resource MapData(Response src) =>
new Resource
{
TravelTimeID = src.TravelTimeID,
Expand Down
3 changes: 2 additions & 1 deletion WSDOT/Models/Options/SharedOpts.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System.Collections.Generic;
using TwoMQTT.Core.Interfaces;
using WSDOT.Models.Shared;

namespace WSDOT.Models.Options
{
/// <summary>
/// The shared options across the application
/// </summary>
public class SharedOpts
public class SharedOpts : ISharedOpts<SlugMapping>
{
public const string Section = "WSDOT";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace WSDOT.Models.Source
/// <summary>
/// The response from the source
/// </summary>
public class FetchResponse
public class Response
{
/// <summary>
///
Expand Down
25 changes: 23 additions & 2 deletions WSDOT/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,12 +50,33 @@ protected override IServiceCollection ConfigureServices(HostBuilderContext hostC
.AddSingleton<IThrottleManager, ThrottleManager>(x =>
{
var opts = x.GetService<IOptions<Models.Options.SourceOpts>>();
if (opts == null)
{
throw new ArgumentException($"{nameof(opts.Value.PollingInterval)} is required for {nameof(ThrottleManager)}.");
}

return new ThrottleManager(opts.Value.PollingInterval);
})
.AddSingleton<ISourceDAO, SourceDAO>(x =>
{
var logger = x.GetService<ILogger<SourceDAO>>();
var httpClientFactory = x.GetService<IHttpClientFactory>();
var opts = x.GetService<IOptions<Models.Options.SourceOpts>>();
return new SourceDAO(x.GetService<ILogger<SourceDAO>>(), x.GetService<IHttpClientFactory>(), 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);
});
}
}
Expand Down
6 changes: 3 additions & 3 deletions WSDOT/WSDOT.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<VersionPrefix>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))))</VersionPrefix>
<VersionPrefix>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))))</VersionPrefix>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Nullable>enable</Nullable>
<RestoreSources>$(RestoreSources);https://api.nuget.org/v3/index.json</RestoreSources>
<RestoreSources>$(RestoreSources);../vendor;https://api.nuget.org/v3/index.json</RestoreSources>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="twomqtt" Version="0.7.*" />
<PackageReference Include="twomqtt" Version="0.8.*" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion WSDOTTest/Liasons/SourceLiasonTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public async Task FetchAllAsyncTest()
});

sourceDAO.Setup(x => x.FetchOneAsync(test.Q, It.IsAny<CancellationToken>()))
.ReturnsAsync(new WSDOT.Models.Source.FetchResponse
.ReturnsAsync(new WSDOT.Models.Source.Response
{
TravelTimeID = test.Expected.TravelTimeID,
CurrentTime = test.Expected.CurrentTime,
Expand Down
6 changes: 3 additions & 3 deletions WSDOTTest/WSDOTTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
<RestoreSources>$(RestoreSources);https://api.nuget.org/v3/index.json</RestoreSources>
<RestoreSources>$(RestoreSources);../vendor;https://api.nuget.org/v3/index.json</RestoreSources>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="Moq" Version="4.14.5" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.0" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.2" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.2" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
</ItemGroup>

Expand Down
Empty file added vendor/.retain
Empty file.

0 comments on commit aa238fe

Please sign in to comment.