Skip to content

Commit

Permalink
#7 display NWS temperature; removed commented code;
Browse files Browse the repository at this point in the history
almostengr committed Apr 25, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 908db11 commit 49be37c
Showing 4 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions Almostengr.ThermometerPi.Api/Clients/NwsClient.cs
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ public class NwsClient : BaseClient, INwsClient
public NwsClient(ILogger<NwsClient> logger) : base(logger)
{
_httpClient = new HttpClient();
_httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36");
}

public async Task<NwsLatestObservationDto> GetLatestWeatherObservationAsync()
3 changes: 1 addition & 2 deletions Almostengr.ThermometerPi.Api/Startup.cs
Original file line number Diff line number Diff line change
@@ -42,12 +42,11 @@ public void ConfigureServices(IServiceCollection services)
# else
services.AddScoped<ISensorService, MockSensorService>();
services.AddScoped<ILcdService, MockLcdService>();
// services.AddScoped<INwsClient, NwsClient>();
services.AddScoped<INwsClient, MockNwsClient>();
# endif

services.AddHostedService<InteriorLatestWorker>();
// services.AddHostedService<NwsLatestWorker>();
services.AddHostedService<NwsLatestWorker>();
services.AddHostedService<DbMaintenanceWorker>();
}

6 changes: 4 additions & 2 deletions Almostengr.ThermometerPi.Api/Workers/LcdDisplayWorker.cs
Original file line number Diff line number Diff line change
@@ -50,14 +50,16 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)

if (interiorTemp != null)
{
output += $"{interiorTemp.Fahrenheit.ToString()}F {interiorTemp.Celsius.ToString()}C";
output += $"In: {interiorTemp.Fahrenheit.ToString()}F ";
}

if (exteriorTemp != null)
{
output += $"E: {exteriorTemp.Fahrenheit.ToString()}F";
output += $"Out: {exteriorTemp.Fahrenheit.ToString()}F";
}

output = output.Length < 1 ? "No Data" : output;

lcd.Write(output);

lcd.SetCursorPosition(0, 1);
7 changes: 6 additions & 1 deletion Almostengr.ThermometerPi.Api/Workers/NwsLatestWorker.cs
Original file line number Diff line number Diff line change
@@ -6,16 +6,19 @@
using Almostengr.ThermometerPi.Api.Services;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace Almostengr.ThermometerPi.Api.Workers
{
public class NwsLatestWorker : BackgroundService
{
private readonly ILogger<NwsLatestWorker> _logger;
private readonly INwsClient _nwsClient;
private readonly ITemperatureReadingService _temperatureReadingService;

public NwsLatestWorker(IServiceScopeFactory factory)
public NwsLatestWorker(IServiceScopeFactory factory, ILogger<NwsLatestWorker> logger)
{
_logger = logger;
_nwsClient = factory.CreateScope().ServiceProvider.GetRequiredService<INwsClient>();
_temperatureReadingService = factory.CreateScope().ServiceProvider.GetRequiredService<ITemperatureReadingService>();
}
@@ -27,6 +30,8 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
NwsLatestObservationDto observationDto = await _nwsClient.GetLatestWeatherObservationAsync();
await _temperatureReadingService.AddReadingAsync(observationDto);

_logger.LogInformation($"Exterior Temperature: {observationDto.Properties.Temperature.Value}C");

await Task.Delay(TimeSpan.FromMinutes(30), stoppingToken);
}
}

0 comments on commit 49be37c

Please sign in to comment.