Update Weather to .NET 8 and add telemetry

This commit is contained in:
2024-01-25 16:25:59 -05:00
parent 2394f613d2
commit 5d0bc2c31c
28 changed files with 856 additions and 775 deletions

View File

@@ -1,27 +1,26 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System; using System;
namespace ChrisKaczor.HomeMonitor.Weather.SerialReader.Controllers namespace ChrisKaczor.HomeMonitor.Weather.SerialReader.Controllers;
[Route("[controller]")]
[ApiController]
public class HealthController : ControllerBase
{ {
[Route("[controller]")] private readonly TimeSpan _checkTimeSpan = TimeSpan.FromSeconds(5);
[ApiController]
public class HealthController : ControllerBase [HttpGet("ready")]
public IActionResult Ready()
{ {
private readonly TimeSpan _checkTimeSpan = TimeSpan.FromSeconds(5); return SerialReader.BoardStarted ? Ok() : Conflict();
[HttpGet("ready")]
public IActionResult Ready()
{
return SerialReader.BoardStarted ? Ok() : Conflict();
}
[HttpGet("health")]
public IActionResult Health()
{
var lastReading = SerialReader.LastReading;
var timeSinceLastReading = DateTimeOffset.UtcNow - lastReading;
return SerialReader.BoardStarted && timeSinceLastReading <= _checkTimeSpan ? Ok(lastReading) : BadRequest(lastReading);
}
} }
}
[HttpGet("health")]
public IActionResult Health()
{
var lastReading = SerialReader.LastReading;
var timeSinceLastReading = DateTimeOffset.UtcNow - lastReading;
return SerialReader.BoardStarted && timeSinceLastReading <= _checkTimeSpan ? Ok(lastReading) : BadRequest(lastReading);
}
}

View File

@@ -1,8 +1,8 @@
FROM mcr.microsoft.com/dotnet/aspnet:5.0-focal-arm32v7 AS base FROM mcr.microsoft.com/dotnet/aspnet:8.0-jammy-arm32v7 AS base
WORKDIR /app WORKDIR /app
EXPOSE 80 EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:5.0-focal AS build FROM mcr.microsoft.com/dotnet/sdk:8.0-jammy AS build
WORKDIR /src WORKDIR /src
COPY ["./SerialReader.csproj", "./"] COPY ["./SerialReader.csproj", "./"]
RUN dotnet restore -r linux-arm "SerialReader.csproj" RUN dotnet restore -r linux-arm "SerialReader.csproj"

View File

@@ -1,20 +1,116 @@
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using OpenTelemetry.Exporter;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using System;
using System.IO.Compression;
using System.Reflection;
using System.Text.Json.Serialization;
namespace ChrisKaczor.HomeMonitor.Weather.SerialReader namespace ChrisKaczor.HomeMonitor.Weather.SerialReader;
public static class Program
{ {
public static class Program public static void Main(string[] args)
{ {
public static void Main(string[] args) var builder = WebApplication.CreateBuilder(args);
{
CreateHostBuilder(args).Build().Run();
}
private static IHostBuilder CreateHostBuilder(string[] args) => builder.Configuration.AddEnvironmentVariables();
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder => builder.Services.AddControllers();
// ---
var openTelemetry = builder.Services.AddOpenTelemetry();
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
var serviceName = Assembly.GetExecutingAssembly().GetName().Name;
openTelemetry.ConfigureResource(resource => resource.AddService(serviceName!));
openTelemetry.WithMetrics(meterProviderBuilder => meterProviderBuilder
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddProcessInstrumentation()
.AddMeter("Microsoft.AspNetCore.Hosting")
.AddMeter("Microsoft.AspNetCore.Server.Kestrel"));
openTelemetry.WithTracing(tracerProviderBuilder =>
{
tracerProviderBuilder.AddAspNetCoreInstrumentation(instrumentationOptions => instrumentationOptions.RecordException = true);
tracerProviderBuilder.AddHttpClientInstrumentation(instrumentationOptions => instrumentationOptions.RecordException = true);
tracerProviderBuilder.AddSqlClientInstrumentation(o =>
{
o.RecordException = true;
o.SetDbStatementForText = true;
});
tracerProviderBuilder.AddSource(nameof(SerialReader));
tracerProviderBuilder.SetErrorStatusOnException();
tracerProviderBuilder.AddOtlpExporter(exporterOptions =>
{
exporterOptions.Endpoint = new Uri(builder.Configuration["Telemetry:Endpoint"]!);
exporterOptions.Protocol = OtlpExportProtocol.Grpc;
});
});
builder.Services.AddLogging(loggingBuilder =>
{
loggingBuilder.SetMinimumLevel(LogLevel.Information);
loggingBuilder.AddOpenTelemetry(options =>
{ {
webBuilder.UseStartup<Startup>(); options.AddOtlpExporter(exporterOptions =>
}); {
exporterOptions.Endpoint = new Uri(builder.Configuration["Telemetry:Endpoint"]!);
exporterOptions.Protocol = OtlpExportProtocol.Grpc;
});
}
);
});
builder.Services.AddHostedService<SerialReader>();
builder.Services.Configure<GzipCompressionProviderOptions>(options => options.Level = CompressionLevel.Optimal);
builder.Services.AddResponseCompression(options =>
{
options.Providers.Add<GzipCompressionProvider>();
options.EnableForHttps = true;
});
builder.Services.AddCors(o => o.AddPolicy("CorsPolicy", corsPolicyBuilder => corsPolicyBuilder.AllowAnyMethod().AllowAnyHeader().AllowCredentials().WithOrigins("http://localhost:4200")));
builder.Services.AddMvc().AddJsonOptions(configure => { configure.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); });
// ---
var app = builder.Build();
if (builder.Environment.IsDevelopment())
app.UseDeveloperExceptionPage();
app.UseCors("CorsPolicy");
app.UseResponseCompression();
app.UseRouting();
app.UseAuthorization();
app.MapControllers();
app.Run();
} }
} }

View File

@@ -8,163 +8,158 @@ using System.IO.Ports;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace ChrisKaczor.HomeMonitor.Weather.SerialReader namespace ChrisKaczor.HomeMonitor.Weather.SerialReader;
public class SerialReader(IConfiguration configuration, ILogger<SerialReader> logger) : IHostedService
{ {
public class SerialReader : IHostedService public static bool BoardStarted { get; private set; }
public static DateTimeOffset LastReading { get; private set; }
private CancellationToken _cancellationToken;
public Task StartAsync(CancellationToken cancellationToken)
{ {
private readonly IConfiguration _configuration; _cancellationToken = cancellationToken;
public static bool BoardStarted { get; private set; } Task.Run(Execute, cancellationToken);
public static DateTimeOffset LastReading { get; private set; }
private CancellationToken _cancellationToken; return Task.CompletedTask;
}
public SerialReader(IConfiguration configuration) private void Execute()
{
var baudRate = configuration.GetValue<int>("Weather:Port:BaudRate");
while (!_cancellationToken.IsCancellationRequested)
{ {
_configuration = configuration; try
}
public Task StartAsync(CancellationToken cancellationToken)
{
_cancellationToken = cancellationToken;
Task.Run(Execute, cancellationToken);
return Task.CompletedTask;
}
private void Execute()
{
var baudRate = int.Parse(_configuration["Weather:Port:BaudRate"]);
while (!_cancellationToken.IsCancellationRequested)
{ {
try WriteLog("Starting main loop");
{
WriteLog("Starting main loop");
Thread.Sleep(1000);
var port = GetPort();
if (port == null)
continue;
using var serialPort = new SerialPort(port, baudRate) { NewLine = "\r\n" };
WriteLog("Opening serial port");
serialPort.Open();
BoardStarted = false;
var factory = new ConnectionFactory
{
HostName = _configuration["Weather:Queue:Host"],
UserName = _configuration["Weather:Queue:User"],
Password = _configuration["Weather:Queue:Password"]
};
WriteLog("Connecting to queue server");
using var connection = factory.CreateConnection();
using var model = connection.CreateModel();
WriteLog("Declaring queue");
model.QueueDeclare(_configuration["Weather:Queue:Name"], true, false, false, null);
WriteLog("Starting serial read loop");
ReadSerial(serialPort, model);
}
catch (Exception exception)
{
WriteLog($"Exception: {exception}");
}
}
}
private void ReadSerial(SerialPort serialPort, IModel model)
{
while (!_cancellationToken.IsCancellationRequested)
{
try
{
var message = serialPort.ReadLine();
if (!BoardStarted)
{
BoardStarted = message.Contains("Board starting");
if (BoardStarted)
WriteLog("Board started");
}
if (!BoardStarted)
{
WriteLog($"Message received but board not started: {message}");
continue;
}
LastReading = DateTimeOffset.UtcNow;
WriteLog($"Message received: {message}");
var weatherMessage = WeatherMessage.Parse(message);
var messageString = JsonConvert.SerializeObject(weatherMessage);
var body = Encoding.UTF8.GetBytes(messageString);
var properties = model.CreateBasicProperties();
properties.Persistent = true;
model.BasicPublish(string.Empty, _configuration["Weather:Queue:Name"], properties, body);
}
catch (TimeoutException)
{
WriteLog("Serial port read timeout");
}
}
}
private string GetPort()
{
var portPrefix = _configuration["Weather:Port:Prefix"];
while (!_cancellationToken.IsCancellationRequested)
{
WriteLog($"Checking for port starting with: {portPrefix}");
var ports = SerialPort.GetPortNames();
var port = Array.Find(ports, p => p.StartsWith(portPrefix));
if (port != null)
{
WriteLog($"Port found: {port}");
return port;
}
WriteLog("Port not found - waiting");
Thread.Sleep(1000); Thread.Sleep(1000);
var port = GetPort();
if (port == null)
continue;
using var serialPort = new SerialPort(port, baudRate);
serialPort.NewLine = "\r\n";
WriteLog("Opening serial port");
serialPort.Open();
BoardStarted = false;
var factory = new ConnectionFactory
{
HostName = configuration["Weather:Queue:Host"],
UserName = configuration["Weather:Queue:User"],
Password = configuration["Weather:Queue:Password"]
};
WriteLog("Connecting to queue server");
using var connection = factory.CreateConnection();
using var model = connection.CreateModel();
WriteLog("Declaring queue");
model.QueueDeclare(configuration["Weather:Queue:Name"], true, false, false, null);
WriteLog("Starting serial read loop");
ReadSerial(serialPort, model);
}
catch (Exception exception)
{
WriteLog($"Exception: {exception}");
} }
return null;
}
private static void WriteLog(string message)
{
Console.WriteLine(message);
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
} }
} }
}
private void ReadSerial(SerialPort serialPort, IModel model)
{
while (!_cancellationToken.IsCancellationRequested)
{
try
{
var message = serialPort.ReadLine();
if (!BoardStarted)
{
BoardStarted = message.Contains("Board starting");
if (BoardStarted)
WriteLog("Board started");
}
if (!BoardStarted)
{
WriteLog($"Message received but board not started: {message}");
continue;
}
LastReading = DateTimeOffset.UtcNow;
WriteLog($"Message received: {message}");
var weatherMessage = WeatherMessage.Parse(message);
var messageString = JsonConvert.SerializeObject(weatherMessage);
var body = Encoding.UTF8.GetBytes(messageString);
var properties = model.CreateBasicProperties();
properties.Persistent = true;
model.BasicPublish(string.Empty, configuration["Weather:Queue:Name"], properties, body);
}
catch (TimeoutException)
{
WriteLog("Serial port read timeout");
}
}
}
private string GetPort()
{
var portPrefix = configuration["Weather:Port:Prefix"]!;
while (!_cancellationToken.IsCancellationRequested)
{
WriteLog($"Checking for port starting with: {portPrefix}");
var ports = SerialPort.GetPortNames();
var port = Array.Find(ports, p => p.StartsWith(portPrefix));
if (port != null)
{
WriteLog($"Port found: {port}");
return port;
}
WriteLog("Port not found - waiting");
Thread.Sleep(1000);
}
return null;
}
private void WriteLog(string message)
{
logger.LogInformation(message);
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}

View File

@@ -2,8 +2,7 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<CodeAnalysisRuleSet>../../ChrisKaczor.ruleset</CodeAnalysisRuleSet>
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute> <GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
</PropertyGroup> </PropertyGroup>
@@ -13,13 +12,16 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="ChrisKaczor.HomeMonitor.Weather.Models" Version="1.1.6" /> <PackageReference Include="ChrisKaczor.HomeMonitor.Weather.Models" Version="1.1.8" />
<PackageReference Include="JetBrains.Annotations" Version="2021.1.0" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" /> <PackageReference Include="RabbitMQ.Client" Version="6.8.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="5.0.0" /> <PackageReference Include="System.IO.Ports" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" /> <PackageReference Include="OpenTelemetry.AutoInstrumentation" Version="1.3.0" />
<PackageReference Include="RabbitMQ.Client" Version="6.2.1" /> <PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.7.0" />
<PackageReference Include="System.IO.Ports" Version="5.0.1" /> <PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.7.0" />
<PackageReference Include="System.Security.Cryptography.Xml" Version="8.0.0" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -1,34 +0,0 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace ChrisKaczor.HomeMonitor.Weather.SerialReader
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddHostedService<SerialReader>();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}

View File

@@ -7,5 +7,8 @@
"Queue": { "Queue": {
"Name": "weather" "Name": "weather"
} }
},
"Telemetry": {
"Endpoint": "http://signoz-otel-collector.platform:4317/"
} }
} }

View File

@@ -97,7 +97,7 @@ spec:
livenessProbe: livenessProbe:
httpGet: httpGet:
path: health/health path: health/health
port: 80 port: 8080
env: env:
- name: Weather__Queue__Host - name: Weather__Queue__Host
value: weather-queue value: weather-queue

View File

@@ -7,78 +7,70 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace ChrisKaczor.HomeMonitor.Weather.Service.Controllers namespace ChrisKaczor.HomeMonitor.Weather.Service.Controllers;
[Route("[controller]")]
[ApiController]
public class ReadingsController(Database database) : ControllerBase
{ {
[Route("[controller]")] [HttpGet("recent")]
[ApiController] public async Task<ActionResult<WeatherReading>> GetRecent([FromQuery] string tz)
public class ReadingsController : ControllerBase
{ {
private readonly Database _database; var recentReading = await database.GetRecentReading();
public ReadingsController(Database database)
{
_database = database;
}
[HttpGet("recent")]
public async Task<ActionResult<WeatherReading>> GetRecent([FromQuery] string tz)
{
var recentReading = await _database.GetRecentReading();
if (string.IsNullOrWhiteSpace(tz))
return recentReading;
try
{
var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(tz);
recentReading.Timestamp = recentReading.Timestamp.ToOffset(timeZoneInfo.GetUtcOffset(recentReading.Timestamp));
recentReading.GpsTimestamp = recentReading.GpsTimestamp.ToOffset(timeZoneInfo.GetUtcOffset(recentReading.GpsTimestamp));
}
catch (Exception e)
{
return BadRequest(e.Message);
}
if (string.IsNullOrWhiteSpace(tz))
return recentReading; return recentReading;
}
[HttpGet("history")] try
public async Task<ActionResult<List<WeatherReading>>> GetHistory(DateTimeOffset start, DateTimeOffset end)
{ {
return (await _database.GetReadingHistory(start, end)).ToList(); var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(tz);
}
[HttpGet("aggregate")] recentReading.Timestamp = recentReading.Timestamp.ToOffset(timeZoneInfo.GetUtcOffset(recentReading.Timestamp));
public async Task<ActionResult<WeatherAggregate>> GetHistoryAggregate(DateTimeOffset start, DateTimeOffset end) recentReading.GpsTimestamp = recentReading.GpsTimestamp.ToOffset(timeZoneInfo.GetUtcOffset(recentReading.GpsTimestamp));
}
catch (Exception e)
{ {
var readings = (await _database.GetReadingHistory(start, end)).ToList(); return BadRequest(e.Message);
return readings.Any() ? new WeatherAggregate(readings) : null;
} }
[HttpGet("value-history")] return recentReading;
public async Task<ActionResult<List<WeatherValue>>> GetValueHistory(WeatherValueType weatherValueType, DateTimeOffset start, DateTimeOffset end) }
{
return (await _database.GetReadingValueHistory(weatherValueType, start, end)).ToList();
}
[HttpGet("value-history-grouped")] [HttpGet("history")]
public async Task<ActionResult<List<WeatherValueGrouped>>> GetValueHistoryGrouped(WeatherValueType weatherValueType, DateTimeOffset start, DateTimeOffset end, int bucketMinutes = 2) public async Task<ActionResult<List<WeatherReading>>> GetHistory(DateTimeOffset start, DateTimeOffset end)
{ {
return (await _database.GetReadingValueHistoryGrouped(weatherValueType, start, end, bucketMinutes)).ToList(); return (await database.GetReadingHistory(start, end)).ToList();
} }
[HttpGet("history-grouped")] [HttpGet("aggregate")]
public async Task<ActionResult<List<WeatherReadingGrouped>>> GetHistoryGrouped(DateTimeOffset start, DateTimeOffset end, int bucketMinutes = 2) public async Task<ActionResult<WeatherAggregate>> GetHistoryAggregate(DateTimeOffset start, DateTimeOffset end)
{ {
return (await _database.GetReadingHistoryGrouped(start, end, bucketMinutes)).ToList(); var readings = (await database.GetReadingHistory(start, end)).ToList();
}
[HttpGet("wind-history-grouped")] return readings.Any() ? new WeatherAggregate(readings) : null;
public async Task<ActionResult<List<WindHistoryGrouped>>> GetWindHistoryGrouped(DateTimeOffset start, DateTimeOffset end, int bucketMinutes = 60) }
{
return (await _database.GetWindHistoryGrouped(start, end, bucketMinutes)).ToList(); [HttpGet("value-history")]
} public async Task<ActionResult<List<WeatherValue>>> GetValueHistory(WeatherValueType weatherValueType, DateTimeOffset start, DateTimeOffset end)
{
return (await database.GetReadingValueHistory(weatherValueType, start, end)).ToList();
}
[HttpGet("value-history-grouped")]
public async Task<ActionResult<List<WeatherValueGrouped>>> GetValueHistoryGrouped(WeatherValueType weatherValueType, DateTimeOffset start, DateTimeOffset end, int bucketMinutes = 2)
{
return (await database.GetReadingValueHistoryGrouped(weatherValueType, start, end, bucketMinutes)).ToList();
}
[HttpGet("history-grouped")]
public async Task<ActionResult<List<WeatherReadingGrouped>>> GetHistoryGrouped(DateTimeOffset start, DateTimeOffset end, int bucketMinutes = 2)
{
return (await database.GetReadingHistoryGrouped(start, end, bucketMinutes)).ToList();
}
[HttpGet("wind-history-grouped")]
public async Task<ActionResult<List<WindHistoryGrouped>>> GetWindHistoryGrouped(DateTimeOffset start, DateTimeOffset end, int bucketMinutes = 60)
{
return (await database.GetWindHistoryGrouped(start, end, bucketMinutes)).ToList();
} }
} }

View File

@@ -7,146 +7,138 @@ using System.Collections.Generic;
using System.Data.SqlClient; using System.Data.SqlClient;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace ChrisKaczor.HomeMonitor.Weather.Service.Data namespace ChrisKaczor.HomeMonitor.Weather.Service.Data;
public class Database(IConfiguration configuration)
{ {
public class Database public void EnsureDatabase()
{ {
private readonly IConfiguration _configuration; var connectionStringBuilder = new SqlConnectionStringBuilder
public Database(IConfiguration configuration)
{ {
_configuration = configuration; DataSource = configuration["Weather:Database:Host"],
} UserID = configuration["Weather:Database:User"],
Password = configuration["Weather:Database:Password"],
InitialCatalog = "master"
};
public void EnsureDatabase() using var connection = new SqlConnection(connectionStringBuilder.ConnectionString);
var command = new SqlCommand { Connection = connection };
connection.Open();
// Check to see if the database exists
command.CommandText = $"SELECT CAST(1 as bit) from sys.databases WHERE name='{configuration["Weather:Database:Name"]}'";
var databaseExists = (bool?)command.ExecuteScalar();
// Create database if needed
if (!(databaseExists ?? false))
{ {
var connectionStringBuilder = new SqlConnectionStringBuilder command.CommandText = $"CREATE DATABASE {configuration["Weather:Database:Name"]}";
{
DataSource = _configuration["Weather:Database:Host"],
UserID = _configuration["Weather:Database:User"],
Password = _configuration["Weather:Database:Password"],
InitialCatalog = "master"
};
using var connection = new SqlConnection(connectionStringBuilder.ConnectionString);
var command = new SqlCommand { Connection = connection };
connection.Open();
// Check to see if the database exists
command.CommandText = $"SELECT CAST(1 as bit) from sys.databases WHERE name='{_configuration["Weather:Database:Name"]}'";
var databaseExists = (bool?)command.ExecuteScalar();
// Create database if needed
if (!(databaseExists ?? false))
{
command.CommandText = $"CREATE DATABASE {_configuration["Weather:Database:Name"]}";
command.ExecuteNonQuery();
}
// Switch to the database now that we're sure it exists
connection.ChangeDatabase(_configuration["Weather:Database:Name"]);
var schema = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.Schema.sql");
// Make sure the database is up to date
command.CommandText = schema;
command.ExecuteNonQuery(); command.ExecuteNonQuery();
} }
private SqlConnection CreateConnection() // Switch to the database now that we're sure it exists
connection.ChangeDatabase(configuration["Weather:Database:Name"]!);
var schema = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.Schema.sql");
// Make sure the database is up-to-date
command.CommandText = schema;
command.ExecuteNonQuery();
}
private SqlConnection CreateConnection()
{
var connectionStringBuilder = new SqlConnectionStringBuilder
{ {
var connectionStringBuilder = new SqlConnectionStringBuilder DataSource = configuration["Weather:Database:Host"],
{ UserID = configuration["Weather:Database:User"],
DataSource = _configuration["Weather:Database:Host"], Password = configuration["Weather:Database:Password"],
UserID = _configuration["Weather:Database:User"], InitialCatalog = configuration["Weather:Database:Name"]
Password = _configuration["Weather:Database:Password"], };
InitialCatalog = _configuration["Weather:Database:Name"]
};
var connection = new SqlConnection(connectionStringBuilder.ConnectionString); var connection = new SqlConnection(connectionStringBuilder.ConnectionString);
connection.Open(); connection.Open();
return connection; return connection;
} }
public void StoreWeatherData(WeatherMessage weatherMessage) public void StoreWeatherData(WeatherMessage weatherMessage)
{ {
using var connection = CreateConnection(); using var connection = CreateConnection();
var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.CreateReading.sql"); var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.CreateReading.sql");
connection.Query(query, weatherMessage); connection.Query(query, weatherMessage);
} }
public async Task<WeatherReading> GetRecentReading() public async Task<WeatherReading> GetRecentReading()
{ {
await using var connection = CreateConnection(); await using var connection = CreateConnection();
var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.GetRecentReading.sql"); var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.GetRecentReading.sql");
return await connection.QueryFirstOrDefaultAsync<WeatherReading>(query).ConfigureAwait(false); return await connection.QueryFirstOrDefaultAsync<WeatherReading>(query).ConfigureAwait(false);
} }
public async Task<IEnumerable<WeatherReading>> GetReadingHistory(DateTimeOffset start, DateTimeOffset end) public async Task<IEnumerable<WeatherReading>> GetReadingHistory(DateTimeOffset start, DateTimeOffset end)
{ {
await using var connection = CreateConnection(); await using var connection = CreateConnection();
var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.GetReadingHistory.sql"); var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.GetReadingHistory.sql");
return await connection.QueryAsync<WeatherReading>(query, new { Start = start, End = end }); return await connection.QueryAsync<WeatherReading>(query, new { Start = start, End = end });
} }
public async Task<IEnumerable<WeatherValue>> GetReadingValueHistory(WeatherValueType weatherValueType, DateTimeOffset start, DateTimeOffset end) public async Task<IEnumerable<WeatherValue>> GetReadingValueHistory(WeatherValueType weatherValueType, DateTimeOffset start, DateTimeOffset end)
{ {
await using var connection = CreateConnection(); await using var connection = CreateConnection();
var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.GetReadingValueHistory.sql"); var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.GetReadingValueHistory.sql");
query = query.Replace("@Value", weatherValueType.ToString()); query = query.Replace("@Value", weatherValueType.ToString());
return await connection.QueryAsync<WeatherValue>(query, new { Start = start, End = end }); return await connection.QueryAsync<WeatherValue>(query, new { Start = start, End = end });
} }
public async Task<decimal> GetReadingValueSum(WeatherValueType weatherValueType, DateTimeOffset start, DateTimeOffset end) public async Task<decimal> GetReadingValueSum(WeatherValueType weatherValueType, DateTimeOffset start, DateTimeOffset end)
{ {
await using var connection = CreateConnection(); await using var connection = CreateConnection();
var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.GetReadingValueSum.sql"); var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.GetReadingValueSum.sql");
query = query.Replace("@Value", weatherValueType.ToString()); query = query.Replace("@Value", weatherValueType.ToString());
return await connection.ExecuteScalarAsync<decimal>(query, new { Start = start, End = end }); return await connection.ExecuteScalarAsync<decimal>(query, new { Start = start, End = end });
} }
public async Task<IEnumerable<WeatherValueGrouped>> GetReadingValueHistoryGrouped(WeatherValueType weatherValueType, DateTimeOffset start, DateTimeOffset end, int bucketMinutes) public async Task<IEnumerable<WeatherValueGrouped>> GetReadingValueHistoryGrouped(WeatherValueType weatherValueType, DateTimeOffset start, DateTimeOffset end, int bucketMinutes)
{ {
await using var connection = CreateConnection(); await using var connection = CreateConnection();
var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.GetReadingValueHistoryGrouped.sql"); var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.GetReadingValueHistoryGrouped.sql");
query = query.Replace("@Value", weatherValueType.ToString()); query = query.Replace("@Value", weatherValueType.ToString());
return await connection.QueryAsync<WeatherValueGrouped>(query, new { Start = start, End = end, BucketMinutes = bucketMinutes }); return await connection.QueryAsync<WeatherValueGrouped>(query, new { Start = start, End = end, BucketMinutes = bucketMinutes });
} }
public async Task<IEnumerable<WeatherReadingGrouped>> GetReadingHistoryGrouped(DateTimeOffset start, DateTimeOffset end, int bucketMinutes) public async Task<IEnumerable<WeatherReadingGrouped>> GetReadingHistoryGrouped(DateTimeOffset start, DateTimeOffset end, int bucketMinutes)
{ {
await using var connection = CreateConnection(); await using var connection = CreateConnection();
var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.GetReadingHistoryGrouped.sql"); var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.GetReadingHistoryGrouped.sql");
return await connection.QueryAsync<WeatherReadingGrouped>(query, new { Start = start, End = end, BucketMinutes = bucketMinutes }); return await connection.QueryAsync<WeatherReadingGrouped>(query, new { Start = start, End = end, BucketMinutes = bucketMinutes });
} }
public async Task<IEnumerable<WindHistoryGrouped>> GetWindHistoryGrouped(DateTimeOffset start, DateTimeOffset end, int bucketMinutes) public async Task<IEnumerable<WindHistoryGrouped>> GetWindHistoryGrouped(DateTimeOffset start, DateTimeOffset end, int bucketMinutes)
{ {
await using var connection = CreateConnection(); await using var connection = CreateConnection();
var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.GetWindHistoryGrouped.sql"); var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.GetWindHistoryGrouped.sql");
return await connection.QueryAsync<WindHistoryGrouped>(query, new { Start = start, End = end, BucketMinutes = bucketMinutes }); return await connection.QueryAsync<WindHistoryGrouped>(query, new { Start = start, End = end, BucketMinutes = bucketMinutes });
}
} }
} }

View File

@@ -1,8 +1,8 @@
FROM mcr.microsoft.com/dotnet/aspnet:7.0-alpine AS base FROM mcr.microsoft.com/dotnet/aspnet:8.0-alpine AS base
WORKDIR /app WORKDIR /app
EXPOSE 80 EXPOSE 8080
FROM mcr.microsoft.com/dotnet/sdk:7.0-alpine AS build FROM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS build
WORKDIR /src WORKDIR /src
COPY ["./Service.csproj", "./"] COPY ["./Service.csproj", "./"]
RUN dotnet restore "Service.csproj" RUN dotnet restore "Service.csproj"
@@ -11,7 +11,8 @@ WORKDIR "/src"
RUN dotnet publish "Service.csproj" -c Release -o /app RUN dotnet publish "Service.csproj" -c Release -o /app
FROM base AS final FROM base AS final
RUN apk add --no-cache tzdata
WORKDIR /app WORKDIR /app
COPY --from=build /app . COPY --from=build /app .
RUN apk add --no-cache icu-libs tzdata
ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false
ENTRYPOINT ["dotnet", "ChrisKaczor.HomeMonitor.Weather.Service.dll"] ENTRYPOINT ["dotnet", "ChrisKaczor.HomeMonitor.Weather.Service.dll"]

View File

@@ -1,18 +1,17 @@
using System; using DecimalMath;
using DecimalMath; using System;
namespace ChrisKaczor.HomeMonitor.Weather.Service namespace ChrisKaczor.HomeMonitor.Weather.Service;
public static class Extensions
{ {
public static class Extensions public static bool IsBetween<T>(this T item, T start, T end) where T : IComparable
{ {
public static bool IsBetween<T>(this T item, T start, T end) where T : IComparable return item.CompareTo(start) >= 0 && item.CompareTo(end) <= 0;
{
return item.CompareTo(start) >= 0 && item.CompareTo(end) <= 0;
}
public static decimal Truncate(this decimal value, int decimalPlaces)
{
return decimal.Truncate(value * DecimalEx.Pow(10, decimalPlaces)) / DecimalEx.Pow(10, decimalPlaces);
}
} }
}
public static decimal Truncate(this decimal value, int decimalPlaces)
{
return decimal.Truncate(value * DecimalEx.Pow(10, decimalPlaces)) / DecimalEx.Pow(10, decimalPlaces);
}
}

View File

@@ -13,119 +13,109 @@ using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace ChrisKaczor.HomeMonitor.Weather.Service namespace ChrisKaczor.HomeMonitor.Weather.Service;
[UsedImplicitly]
public class MessageHandler(IConfiguration configuration, Database database) : IHostedService
{ {
[UsedImplicitly] private IConnection _queueConnection;
public class MessageHandler : IHostedService private IModel _queueModel;
private HubConnection _hubConnection;
public Task StartAsync(CancellationToken cancellationToken)
{ {
private readonly IConfiguration _configuration; var host = configuration["Weather:Queue:Host"];
private readonly Database _database;
private IConnection _queueConnection; if (string.IsNullOrEmpty(host))
private IModel _queueModel; return Task.CompletedTask;
private HubConnection _hubConnection; WriteLog("MessageHandler: Start");
public MessageHandler(IConfiguration configuration, Database database) var factory = new ConnectionFactory
{ {
_configuration = configuration; HostName = host,
_database = database; UserName = configuration["Weather:Queue:User"],
} Password = configuration["Weather:Queue:Password"]
};
public Task StartAsync(CancellationToken cancellationToken) _queueConnection = factory.CreateConnection();
_queueModel = _queueConnection.CreateModel();
_queueModel.QueueDeclare(configuration["Weather:Queue:Name"], true, false, false, null);
var consumer = new EventingBasicConsumer(_queueModel);
consumer.Received += DeviceEventHandler;
_queueModel.BasicConsume(configuration["Weather:Queue:Name"], true, consumer);
if (!string.IsNullOrEmpty(configuration["Hub:Weather"]))
_hubConnection = new HubConnectionBuilder().WithUrl(configuration["Hub:Weather"]).Build();
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
WriteLog("MessageHandler: Stop");
_hubConnection?.StopAsync(cancellationToken).Wait(cancellationToken);
_queueModel?.Close();
_queueConnection?.Close();
return Task.CompletedTask;
}
private void DeviceEventHandler(object model, BasicDeliverEventArgs eventArgs)
{
try
{ {
var host = _configuration["Weather:Queue:Host"]; var body = eventArgs.Body;
var message = Encoding.UTF8.GetString(body.ToArray());
if (string.IsNullOrEmpty(host)) WriteLog($"Message received: {message}");
return Task.CompletedTask;
WriteLog("MessageHandler: Start"); var weatherMessage = JsonConvert.DeserializeObject<WeatherMessage>(message);
var factory = new ConnectionFactory if (weatherMessage.Type == MessageType.Text)
{ {
HostName = host, WriteLog(weatherMessage.Message);
UserName = _configuration["Weather:Queue:User"],
Password = _configuration["Weather:Queue:Password"]
};
_queueConnection = factory.CreateConnection(); return;
_queueModel = _queueConnection.CreateModel(); }
_queueModel.QueueDeclare(_configuration["Weather:Queue:Name"], true, false, false, null); database.StoreWeatherData(weatherMessage);
var consumer = new EventingBasicConsumer(_queueModel); if (_hubConnection == null)
consumer.Received += DeviceEventHandler; return;
_queueModel.BasicConsume(_configuration["Weather:Queue:Name"], true, consumer); var weatherUpdate = new WeatherUpdate(weatherMessage, database);
if (!string.IsNullOrEmpty(_configuration["Hub:Weather"]))
_hubConnection = new HubConnectionBuilder().WithUrl(_configuration["Hub:Weather"]).Build();
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
WriteLog("MessageHandler: Stop");
_hubConnection?.StopAsync(cancellationToken).Wait(cancellationToken);
_queueModel?.Close();
_queueConnection?.Close();
return Task.CompletedTask;
}
private void DeviceEventHandler(object model, BasicDeliverEventArgs eventArgs)
{
try try
{ {
var body = eventArgs.Body; if (_hubConnection.State == HubConnectionState.Disconnected)
var message = Encoding.UTF8.GetString(body.ToArray()); _hubConnection.StartAsync().Wait();
WriteLog($"Message received: {message}"); var json = JsonConvert.SerializeObject(weatherUpdate);
var weatherMessage = JsonConvert.DeserializeObject<WeatherMessage>(message); _hubConnection.InvokeAsync("SendLatestReading", json).Wait();
if (weatherMessage.Type == MessageType.Text)
{
WriteLog(weatherMessage.Message);
return;
}
_database.StoreWeatherData(weatherMessage);
if (_hubConnection == null)
return;
var weatherUpdate = new WeatherUpdate(weatherMessage, _database);
try
{
if (_hubConnection.State == HubConnectionState.Disconnected)
_hubConnection.StartAsync().Wait();
var json = JsonConvert.SerializeObject(weatherUpdate);
_hubConnection.InvokeAsync("SendLatestReading", json).Wait();
}
catch (Exception exception)
{
WriteLog($"Hub exception: {exception}");
}
} }
catch (Exception exception) catch (Exception exception)
{ {
WriteLog($"Exception: {exception}"); WriteLog($"Hub exception: {exception}");
throw;
} }
} }
catch (Exception exception)
private static void WriteLog(string message)
{ {
Console.WriteLine(message); WriteLog($"Exception: {exception}");
throw;
} }
} }
}
private static void WriteLog(string message)
{
Console.WriteLine(message);
}
}

View File

@@ -1,25 +1,17 @@
using ChrisKaczor.HomeMonitor.Weather.Models;
using JetBrains.Annotations;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using ChrisKaczor.HomeMonitor.Weather.Models;
using JetBrains.Annotations;
namespace ChrisKaczor.HomeMonitor.Weather.Service.Models namespace ChrisKaczor.HomeMonitor.Weather.Service.Models;
[PublicAPI]
public class ReadingAggregate(IEnumerable<WeatherReading> readings, Func<WeatherReading, decimal> selector, int decimalPlaces)
{ {
[PublicAPI] public decimal Min { get; set; } = readings.Min(selector);
public class ReadingAggregate
{
public decimal Min { get; set; }
public decimal Max { get; set; } public decimal Max { get; set; } = readings.Max(selector);
public decimal Average { get; set; } public decimal Average { get; set; } = readings.Average(selector).Truncate(decimalPlaces);
public ReadingAggregate(IEnumerable<WeatherReading> readings, Func<WeatherReading, decimal> selector, int decimalPlaces)
{
Min = readings.Min(selector);
Max = readings.Max(selector);
Average = readings.Average(selector).Truncate(decimalPlaces);
}
}
} }

View File

@@ -1,49 +1,48 @@
using ChrisKaczor.HomeMonitor.Weather.Models;
using JetBrains.Annotations;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using ChrisKaczor.HomeMonitor.Weather.Models;
using JetBrains.Annotations;
namespace ChrisKaczor.HomeMonitor.Weather.Service.Models namespace ChrisKaczor.HomeMonitor.Weather.Service.Models;
[PublicAPI]
public class WeatherAggregate
{ {
[PublicAPI] public ReadingAggregate Humidity { get; set; }
public class WeatherAggregate
public ReadingAggregate Temperature { get; set; }
public ReadingAggregate Pressure { get; set; }
public ReadingAggregate Light { get; set; }
public ReadingAggregate WindSpeed { get; set; }
public WindDirection WindDirectionAverage { get; set; }
public decimal RainTotal { get; set; }
private static readonly List<int> WindDirectionValues = ((WindDirection[])Enum.GetValues(typeof(WindDirection))).Select(e => (int)e).ToList();
public WeatherAggregate(List<WeatherReading> readings)
{ {
public ReadingAggregate Humidity { get; set; } if (!readings.Any())
return;
public ReadingAggregate Temperature { get; set; } Humidity = new ReadingAggregate(readings, r => r.Humidity, 1);
public ReadingAggregate Pressure { get; set; } Temperature = new ReadingAggregate(readings, r => r.HumidityTemperature, 1);
public ReadingAggregate Light { get; set; } Pressure = new ReadingAggregate(readings, r => r.Pressure, 2);
public ReadingAggregate WindSpeed { get; set; } Light = new ReadingAggregate(readings, r => r.LightLevel, 2);
public WindDirection WindDirectionAverage { get; set; } WindSpeed = new ReadingAggregate(readings, r => r.WindSpeed, 1);
public decimal RainTotal { get; set; } var windDirectionAverage = readings.Average(r => (decimal)r.WindDirection);
WindDirectionAverage = (WindDirection)WindDirectionValues.Aggregate((x, y) => Math.Abs(x - windDirectionAverage) < Math.Abs(y - windDirectionAverage) ? x : y);
private static readonly List<int> WindDirectionValues = ((WindDirection[])Enum.GetValues(typeof(WindDirection))).Select(e => (int)e).ToList(); RainTotal = readings.Sum(r => r.Rain);
public WeatherAggregate(List<WeatherReading> readings)
{
if (!readings.Any())
return;
Humidity = new ReadingAggregate(readings, r => r.Humidity, 1);
Temperature = new ReadingAggregate(readings, r => r.HumidityTemperature, 1);
Pressure = new ReadingAggregate(readings, r => r.Pressure, 2);
Light = new ReadingAggregate(readings, r => r.LightLevel, 2);
WindSpeed = new ReadingAggregate(readings, r => r.WindSpeed, 1);
var windDirectionAverage = readings.Average(r => (decimal)r.WindDirection);
WindDirectionAverage = (WindDirection)WindDirectionValues.Aggregate((x, y) => Math.Abs(x - windDirectionAverage) < Math.Abs(y - windDirectionAverage) ? x : y);
RainTotal = readings.Sum(r => r.Rain);
}
} }
} }

View File

@@ -1,21 +1,20 @@
using JetBrains.Annotations; using JetBrains.Annotations;
using System; using System;
namespace ChrisKaczor.HomeMonitor.Weather.Service.Models namespace ChrisKaczor.HomeMonitor.Weather.Service.Models;
[PublicAPI]
public class WeatherReadingGrouped
{ {
[PublicAPI] public DateTimeOffset Bucket { get; set; }
public class WeatherReadingGrouped
{
public DateTimeOffset Bucket { get; set; }
public decimal AverageHumidity { get; set; } public decimal AverageHumidity { get; set; }
public decimal AverageTemperature { get; set; } public decimal AverageTemperature { get; set; }
public decimal AveragePressure { get; set; } public decimal AveragePressure { get; set; }
public decimal AverageLightLevel { get; set; } public decimal AverageLightLevel { get; set; }
public decimal RainTotal { get; set; } public decimal RainTotal { get; set; }
}
} }

View File

@@ -5,97 +5,96 @@ using JetBrains.Annotations;
using MathNet.Numerics; using MathNet.Numerics;
using System.Linq; using System.Linq;
namespace ChrisKaczor.HomeMonitor.Weather.Service.Models namespace ChrisKaczor.HomeMonitor.Weather.Service.Models;
[PublicAPI]
public class WeatherUpdate : WeatherUpdateBase
{ {
[PublicAPI] private readonly Database _database;
public class WeatherUpdate : WeatherUpdateBase
public WeatherUpdate(WeatherMessage weatherMessage, Database database)
{ {
private readonly Database _database; _database = database;
public WeatherUpdate(WeatherMessage weatherMessage, Database database) Type = weatherMessage.Type;
Message = weatherMessage.Message;
Timestamp = weatherMessage.Timestamp;
WindDirection = weatherMessage.WindDirection;
WindSpeed = weatherMessage.WindSpeed;
Humidity = weatherMessage.Humidity;
Rain = weatherMessage.Rain;
Pressure = weatherMessage.Pressure;
Temperature = weatherMessage.HumidityTemperature;
LightLevel = weatherMessage.LightLevel;
Latitude = weatherMessage.Latitude;
Longitude = weatherMessage.Longitude;
Altitude = weatherMessage.Altitude;
SatelliteCount = weatherMessage.SatelliteCount;
GpsTimestamp = weatherMessage.GpsTimestamp;
CalculateHeatIndex();
CalculateWindChill();
CalculateDewPoint();
CalculatePressureTrend();
CalculateRainLastHour();
}
private void CalculateRainLastHour()
{
RainLastHour = _database.GetReadingValueSum(WeatherValueType.Rain, Timestamp.AddHours(-1), Timestamp).Result;
}
private void CalculateWindChill()
{
var temperatureInF = Temperature;
var windSpeedInMph = WindSpeed;
if (temperatureInF.IsBetween(-45, 45) && windSpeedInMph.IsBetween(3, 60))
{ {
_database = database; WindChill = 35.74m + 0.6215m * temperatureInF - 35.75m * DecimalEx.Pow(windSpeedInMph, 0.16m) + 0.4275m * temperatureInF * DecimalEx.Pow(windSpeedInMph, 0.16m);
Type = weatherMessage.Type;
Message = weatherMessage.Message;
Timestamp = weatherMessage.Timestamp;
WindDirection = weatherMessage.WindDirection;
WindSpeed = weatherMessage.WindSpeed;
Humidity = weatherMessage.Humidity;
Rain = weatherMessage.Rain;
Pressure = weatherMessage.Pressure;
Temperature = weatherMessage.HumidityTemperature;
LightLevel = weatherMessage.LightLevel;
Latitude = weatherMessage.Latitude;
Longitude = weatherMessage.Longitude;
Altitude = weatherMessage.Altitude;
SatelliteCount = weatherMessage.SatelliteCount;
GpsTimestamp = weatherMessage.GpsTimestamp;
CalculateHeatIndex();
CalculateWindChill();
CalculateDewPoint();
CalculatePressureTrend();
CalculateRainLastHour();
}
private void CalculateRainLastHour()
{
RainLastHour = _database.GetReadingValueSum(WeatherValueType.Rain, Timestamp.AddHours(-1), Timestamp).Result;
}
private void CalculateWindChill()
{
var temperatureInF = Temperature;
var windSpeedInMph = WindSpeed;
if (temperatureInF.IsBetween(-45, 45) && windSpeedInMph.IsBetween(3, 60))
{
WindChill = 35.74m + 0.6215m * temperatureInF - 35.75m * DecimalEx.Pow(windSpeedInMph, 0.16m) + 0.4275m * temperatureInF * DecimalEx.Pow(windSpeedInMph, 0.16m);
}
}
private void CalculateDewPoint()
{
var relativeHumidity = Humidity;
var temperatureInF = Temperature;
var temperatureInC = (temperatureInF - 32.0m) * 5.0m / 9.0m;
var vaporPressure = relativeHumidity * 0.01m * 6.112m * DecimalEx.Exp(17.62m * temperatureInC / (temperatureInC + 243.12m));
var numerator = 243.12m * DecimalEx.Log(vaporPressure) - 440.1m;
var denominator = 19.43m - DecimalEx.Log(vaporPressure);
var dewPointInC = numerator / denominator;
DewPoint = dewPointInC * 9.0m / 5.0m + 32.0m;
}
private void CalculatePressureTrend()
{
var pressureData = _database
.GetReadingValueHistory(WeatherValueType.Pressure, Timestamp.AddHours(-3), Timestamp).Result.ToList();
var xData = pressureData.Select(p => (double)p.Timestamp.ToUnixTimeSeconds()).ToArray();
var yData = pressureData.Select(p => (double)p.Value / 100.0).ToArray();
var lineFunction = Fit.LineFunc(xData, yData);
PressureDifferenceThreeHour = (decimal)(lineFunction(xData.Last()) - lineFunction(xData[0]));
}
private void CalculateHeatIndex()
{
var temperature = Temperature;
var humidity = Humidity;
if (temperature.IsBetween(80, 100) && humidity.IsBetween(40, 100))
{
HeatIndex = -42.379m + 2.04901523m * temperature + 10.14333127m * humidity -
.22475541m * temperature * humidity - .00683783m * temperature * temperature -
.05481717m * humidity * humidity + .00122874m * temperature * temperature * humidity +
.00085282m * temperature * humidity * humidity -
.00000199m * temperature * temperature * humidity * humidity;
}
} }
} }
}
private void CalculateDewPoint()
{
var relativeHumidity = Humidity;
var temperatureInF = Temperature;
var temperatureInC = (temperatureInF - 32.0m) * 5.0m / 9.0m;
var vaporPressure = relativeHumidity * 0.01m * 6.112m * DecimalEx.Exp(17.62m * temperatureInC / (temperatureInC + 243.12m));
var numerator = 243.12m * DecimalEx.Log(vaporPressure) - 440.1m;
var denominator = 19.43m - DecimalEx.Log(vaporPressure);
var dewPointInC = numerator / denominator;
DewPoint = dewPointInC * 9.0m / 5.0m + 32.0m;
}
private void CalculatePressureTrend()
{
var pressureData = _database
.GetReadingValueHistory(WeatherValueType.Pressure, Timestamp.AddHours(-3), Timestamp).Result.ToList();
var xData = pressureData.Select(p => (double)p.Timestamp.ToUnixTimeSeconds()).ToArray();
var yData = pressureData.Select(p => (double)p.Value / 100.0).ToArray();
var lineFunction = Fit.LineFunc(xData, yData);
PressureDifferenceThreeHour = (decimal)(lineFunction(xData.Last()) - lineFunction(xData[0]));
}
private void CalculateHeatIndex()
{
var temperature = Temperature;
var humidity = Humidity;
if (temperature.IsBetween(80, 100) && humidity.IsBetween(40, 100))
{
HeatIndex = -42.379m + 2.04901523m * temperature + 10.14333127m * humidity -
.22475541m * temperature * humidity - .00683783m * temperature * temperature -
.05481717m * humidity * humidity + .00122874m * temperature * temperature * humidity +
.00085282m * temperature * humidity * humidity -
.00000199m * temperature * temperature * humidity * humidity;
}
}
}

View File

@@ -1,13 +1,12 @@
using JetBrains.Annotations; using JetBrains.Annotations;
using System; using System;
namespace ChrisKaczor.HomeMonitor.Weather.Service.Models namespace ChrisKaczor.HomeMonitor.Weather.Service.Models;
{
[PublicAPI]
public class WeatherValue
{
public DateTimeOffset Timestamp { get; set; }
public decimal Value { get; set; } [PublicAPI]
} public class WeatherValue
} {
public DateTimeOffset Timestamp { get; set; }
public decimal Value { get; set; }
}

View File

@@ -1,13 +1,12 @@
using JetBrains.Annotations; using JetBrains.Annotations;
using System; using System;
namespace ChrisKaczor.HomeMonitor.Weather.Service.Models namespace ChrisKaczor.HomeMonitor.Weather.Service.Models;
{
[PublicAPI]
public class WeatherValueGrouped
{
public DateTimeOffset Bucket { get; set; }
public decimal AverageValue { get; set; } [PublicAPI]
} public class WeatherValueGrouped
{
public DateTimeOffset Bucket { get; set; }
public decimal AverageValue { get; set; }
} }

View File

@@ -1,18 +1,17 @@
using JetBrains.Annotations; using JetBrains.Annotations;
namespace ChrisKaczor.HomeMonitor.Weather.Service.Models namespace ChrisKaczor.HomeMonitor.Weather.Service.Models;
[PublicAPI]
public enum WeatherValueType
{ {
[PublicAPI] WindSpeed,
public enum WeatherValueType Humidity,
{ HumidityTemperature,
WindSpeed, Rain,
Humidity, Pressure,
HumidityTemperature, PressureTemperature,
Rain, LightLevel,
Pressure, Altitude,
PressureTemperature, SatelliteCount
LightLevel, }
Altitude,
SatelliteCount
}
}

View File

@@ -1,19 +1,18 @@
using JetBrains.Annotations; using JetBrains.Annotations;
using System; using System;
namespace ChrisKaczor.HomeMonitor.Weather.Service.Models namespace ChrisKaczor.HomeMonitor.Weather.Service.Models;
[PublicAPI]
public class WindHistoryGrouped
{ {
[PublicAPI] public DateTimeOffset Bucket { get; set; }
public class WindHistoryGrouped
{
public DateTimeOffset Bucket { get; set; }
public decimal MinimumSpeed { get; set; } public decimal MinimumSpeed { get; set; }
public decimal AverageSpeed { get; set; } public decimal AverageSpeed { get; set; }
public decimal MaximumSpeed { get; set; } public decimal MaximumSpeed { get; set; }
public decimal AverageDirection { get; set; } public decimal AverageDirection { get; set; }
}
} }

View File

@@ -1,19 +1,125 @@
using Microsoft.AspNetCore; using ChrisKaczor.HomeMonitor.Weather.Service.Data;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using OpenTelemetry.Exporter;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using System;
using System.IO.Compression;
using System.Reflection;
using System.Text.Json.Serialization;
namespace ChrisKaczor.HomeMonitor.Weather.Service namespace ChrisKaczor.HomeMonitor.Weather.Service;
public static class Program
{ {
public static class Program public static void Main(string[] args)
{ {
public static void Main(string[] args) var builder = WebApplication.CreateBuilder(args);
{
CreateWebHostBuilder(args).Build().Run();
}
private static IWebHostBuilder CreateWebHostBuilder(string[] args) builder.Configuration.AddEnvironmentVariables();
builder.Services.AddControllers();
// ---
var openTelemetry = builder.Services.AddOpenTelemetry();
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
var serviceName = Assembly.GetExecutingAssembly().GetName().Name;
openTelemetry.ConfigureResource(resource => resource.AddService(serviceName!));
openTelemetry.WithMetrics(meterProviderBuilder => meterProviderBuilder
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddProcessInstrumentation()
.AddMeter("Microsoft.AspNetCore.Hosting")
.AddMeter("Microsoft.AspNetCore.Server.Kestrel"));
openTelemetry.WithTracing(tracerProviderBuilder =>
{ {
return WebHost.CreateDefaultBuilder(args).ConfigureAppConfiguration((_, config) => config.AddEnvironmentVariables()).UseStartup<Startup>(); tracerProviderBuilder.AddAspNetCoreInstrumentation(instrumentationOptions => instrumentationOptions.RecordException = true);
}
tracerProviderBuilder.AddHttpClientInstrumentation(instrumentationOptions => instrumentationOptions.RecordException = true);
tracerProviderBuilder.AddSqlClientInstrumentation(o =>
{
o.RecordException = true;
o.SetDbStatementForText = true;
});
tracerProviderBuilder.AddSource(nameof(MessageHandler));
tracerProviderBuilder.SetErrorStatusOnException();
tracerProviderBuilder.AddOtlpExporter(exporterOptions =>
{
exporterOptions.Endpoint = new Uri(builder.Configuration["Telemetry:Endpoint"]!);
exporterOptions.Protocol = OtlpExportProtocol.Grpc;
});
});
builder.Services.AddLogging(loggingBuilder =>
{
loggingBuilder.SetMinimumLevel(LogLevel.Information);
loggingBuilder.AddOpenTelemetry(options =>
{
options.AddOtlpExporter(exporterOptions =>
{
exporterOptions.Endpoint = new Uri(builder.Configuration["Telemetry:Endpoint"]!);
exporterOptions.Protocol = OtlpExportProtocol.Grpc;
});
}
);
});
builder.Services.AddTransient<Database>();
builder.Services.AddHostedService<MessageHandler>();
builder.Services.Configure<GzipCompressionProviderOptions>(options => options.Level = CompressionLevel.Optimal);
builder.Services.AddResponseCompression(options =>
{
options.Providers.Add<GzipCompressionProvider>();
options.EnableForHttps = true;
});
builder.Services.AddCors(o => o.AddPolicy("CorsPolicy", corsPolicyBuilder => corsPolicyBuilder.AllowAnyMethod().AllowAnyHeader().AllowCredentials().WithOrigins("http://localhost:4200")));
builder.Services.AddMvc().AddJsonOptions(configure =>
{
configure.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
});
// ---
var app = builder.Build();
if (builder.Environment.IsDevelopment())
app.UseDeveloperExceptionPage();
var database = app.Services.GetRequiredService<Database>();
database.EnsureDatabase();
app.UseCors("CorsPolicy");
app.UseResponseCompression();
app.UseRouting();
app.UseAuthorization();
app.MapControllers();
app.Run();
} }
} }

View File

@@ -2,21 +2,20 @@
using System.IO; using System.IO;
using System.Reflection; using System.Reflection;
namespace ChrisKaczor.HomeMonitor.Weather.Service namespace ChrisKaczor.HomeMonitor.Weather.Service;
public static class ResourceReader
{ {
public static class ResourceReader public static string GetString(string resourceName)
{ {
public static string GetString(string resourceName) var assembly = Assembly.GetExecutingAssembly();
{ using var stream = assembly.GetManifestResourceStream(resourceName);
var assembly = Assembly.GetExecutingAssembly();
using var stream = assembly.GetManifestResourceStream(resourceName); if (stream == null)
throw new Exception($"Resource {resourceName} not found in {assembly.FullName}. Valid resources are: {string.Join(", ", assembly.GetManifestResourceNames())}.");
if (stream == null) using var reader = new StreamReader(stream);
throw new Exception($"Resource {resourceName} not found in {assembly.FullName}. Valid resources are: {string.Join(", ", assembly.GetManifestResourceNames())}.");
using var reader = new StreamReader(stream); return reader.ReadToEnd();
return reader.ReadToEnd();
}
} }
} }

View File

@@ -1,15 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net7.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel> <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<AssemblyName>ChrisKaczor.HomeMonitor.Weather.Service</AssemblyName> <AssemblyName>ChrisKaczor.HomeMonitor.Weather.Service</AssemblyName>
<RootNamespace>ChrisKaczor.HomeMonitor.Weather.Service</RootNamespace> <RootNamespace>ChrisKaczor.HomeMonitor.Weather.Service</RootNamespace>
<CodeAnalysisRuleSet>../../ChrisKaczor.ruleset</CodeAnalysisRuleSet>
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute> <GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<None Remove="C:\Users\chris\.nuget\packages\opentelemetry.autoinstrumentation\1.3.0\contentFiles\any\any\instrument.cmd" />
<None Remove="C:\Users\chris\.nuget\packages\opentelemetry.autoinstrumentation\1.3.0\contentFiles\any\any\instrument.sh" />
<None Remove="Data\Resources\CreateReading.sql" /> <None Remove="Data\Resources\CreateReading.sql" />
<None Remove="Data\Resources\GetReadingHistory.sql" /> <None Remove="Data\Resources\GetReadingHistory.sql" />
<None Remove="Data\Resources\GetReadingHistoryGrouped.sql" /> <None Remove="Data\Resources\GetReadingHistoryGrouped.sql" />
@@ -35,12 +36,17 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="ChrisKaczor.HomeMonitor.Weather.Models" Version="1.1.7" /> <PackageReference Include="ChrisKaczor.HomeMonitor.Weather.Models" Version="1.1.7" />
<PackageReference Include="Dapper" Version="2.0.123" /> <PackageReference Include="Dapper" Version="2.1.28" />
<PackageReference Include="DecimalMath.DecimalEx" Version="1.0.2" /> <PackageReference Include="DecimalMath.DecimalEx" Version="1.0.2" />
<PackageReference Include="MathNet.Numerics" Version="4.15.0" /> <PackageReference Include="OpenTelemetry.AutoInstrumentation" Version="1.3.0" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="5.0.6" /> <PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.7.0" />
<PackageReference Include="RabbitMQ.Client" Version="6.2.4" /> <PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.7.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.3" /> <PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.7.0" />
<PackageReference Include="MathNet.Numerics" Version="5.0.0" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="8.0.1" />
<PackageReference Include="RabbitMQ.Client" Version="6.8.1" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.6" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -1,54 +0,0 @@
using ChrisKaczor.HomeMonitor.Weather.Service.Data;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.IO.Compression;
using System.Text.Json.Serialization;
namespace ChrisKaczor.HomeMonitor.Weather.Service
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<Database>();
services.AddHostedService<MessageHandler>();
services.Configure<GzipCompressionProviderOptions>(options => options.Level = CompressionLevel.Optimal);
services.AddResponseCompression(options =>
{
options.Providers.Add<GzipCompressionProvider>();
options.EnableForHttps = true;
});
services.AddCors(o => o.AddPolicy("CorsPolicy", builder => builder.AllowAnyMethod().AllowAnyHeader().AllowCredentials().WithOrigins("http://localhost:4200")));
services.AddMvc().AddJsonOptions(configure =>
{
configure.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
});
}
public void Configure(IApplicationBuilder applicationBuilder, IWebHostEnvironment environment)
{
if (environment.IsDevelopment())
applicationBuilder.UseDeveloperExceptionPage();
var database = applicationBuilder.ApplicationServices.GetService<Database>();
database.EnsureDatabase();
applicationBuilder.UseCors("CorsPolicy");
applicationBuilder.UseResponseCompression();
applicationBuilder.UseRouting();
applicationBuilder.UseEndpoints(endpoints => endpoints.MapDefaultControllerRoute());
}
}
}

View File

@@ -2,7 +2,6 @@
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Debug", "Default": "Debug",
"System": "Information",
"Microsoft": "Information" "Microsoft": "Information"
} }
} }

View File

@@ -1,12 +1,14 @@
{ {
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Warning" "Default": "Warning",
"Microsoft": "Information"
} }
}, },
"Weather": { "Weather": {
"Database": { "Database": {
"Name": "Weather" "Name": "Weather",
"TrustServerCertificate": true
}, },
"Queue": { "Queue": {
"Name": "weather" "Name": "weather"
@@ -14,5 +16,8 @@
}, },
"Hub": { "Hub": {
"Weather": "http://hub-server/weather" "Weather": "http://hub-server/weather"
},
"Telemetry": {
"Endpoint": "http://signoz-otel-collector.platform:4317/"
} }
} }

View File

@@ -132,7 +132,7 @@ metadata:
spec: spec:
ports: ports:
- name: client - name: client
port: 80 port: 8080
selector: selector:
app: weather-service app: weather-service
type: ClusterIP type: ClusterIP
@@ -156,7 +156,7 @@ spec:
- kind: Service - kind: Service
name: weather-service name: weather-service
namespace: home-monitor namespace: home-monitor
port: 80 port: 8080
--- ---
apiVersion: traefik.containo.us/v1alpha1 apiVersion: traefik.containo.us/v1alpha1
kind: Middleware kind: Middleware