mirror of
https://github.com/ckaczor/HomeMonitor.git
synced 2026-02-16 18:47:40 -05:00
Update Weather to .NET 8 and add telemetry
This commit is contained in:
@@ -1,12 +1,12 @@
|
|||||||
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]")]
|
|
||||||
[ApiController]
|
|
||||||
public class HealthController : ControllerBase
|
|
||||||
{
|
|
||||||
private readonly TimeSpan _checkTimeSpan = TimeSpan.FromSeconds(5);
|
private readonly TimeSpan _checkTimeSpan = TimeSpan.FromSeconds(5);
|
||||||
|
|
||||||
[HttpGet("ready")]
|
[HttpGet("ready")]
|
||||||
@@ -23,5 +23,4 @@ namespace ChrisKaczor.HomeMonitor.Weather.SerialReader.Controllers
|
|||||||
|
|
||||||
return SerialReader.BoardStarted && timeSinceLastReading <= _checkTimeSpan ? Ok(lastReading) : BadRequest(lastReading);
|
return SerialReader.BoardStarted && timeSinceLastReading <= _checkTimeSpan ? Ok(lastReading) : BadRequest(lastReading);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -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"
|
||||||
|
|||||||
@@ -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)
|
||||||
{
|
{
|
||||||
CreateHostBuilder(args).Build().Run();
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
}
|
|
||||||
|
|
||||||
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 =>
|
||||||
{
|
{
|
||||||
webBuilder.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(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 =>
|
||||||
|
{
|
||||||
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -8,23 +8,17 @@ 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
|
|
||||||
{
|
|
||||||
private readonly IConfiguration _configuration;
|
|
||||||
|
|
||||||
public static bool BoardStarted { get; private set; }
|
public static bool BoardStarted { get; private set; }
|
||||||
public static DateTimeOffset LastReading { get; private set; }
|
public static DateTimeOffset LastReading { get; private set; }
|
||||||
|
|
||||||
private CancellationToken _cancellationToken;
|
private CancellationToken _cancellationToken;
|
||||||
|
|
||||||
public SerialReader(IConfiguration configuration)
|
|
||||||
{
|
|
||||||
_configuration = configuration;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Task StartAsync(CancellationToken cancellationToken)
|
public Task StartAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
_cancellationToken = cancellationToken;
|
_cancellationToken = cancellationToken;
|
||||||
@@ -36,7 +30,7 @@ namespace ChrisKaczor.HomeMonitor.Weather.SerialReader
|
|||||||
|
|
||||||
private void Execute()
|
private void Execute()
|
||||||
{
|
{
|
||||||
var baudRate = int.Parse(_configuration["Weather:Port:BaudRate"]);
|
var baudRate = configuration.GetValue<int>("Weather:Port:BaudRate");
|
||||||
|
|
||||||
while (!_cancellationToken.IsCancellationRequested)
|
while (!_cancellationToken.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
@@ -51,7 +45,9 @@ namespace ChrisKaczor.HomeMonitor.Weather.SerialReader
|
|||||||
if (port == null)
|
if (port == null)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
using var serialPort = new SerialPort(port, baudRate) { NewLine = "\r\n" };
|
using var serialPort = new SerialPort(port, baudRate);
|
||||||
|
|
||||||
|
serialPort.NewLine = "\r\n";
|
||||||
|
|
||||||
WriteLog("Opening serial port");
|
WriteLog("Opening serial port");
|
||||||
|
|
||||||
@@ -61,9 +57,9 @@ namespace ChrisKaczor.HomeMonitor.Weather.SerialReader
|
|||||||
|
|
||||||
var factory = new ConnectionFactory
|
var factory = new ConnectionFactory
|
||||||
{
|
{
|
||||||
HostName = _configuration["Weather:Queue:Host"],
|
HostName = configuration["Weather:Queue:Host"],
|
||||||
UserName = _configuration["Weather:Queue:User"],
|
UserName = configuration["Weather:Queue:User"],
|
||||||
Password = _configuration["Weather:Queue:Password"]
|
Password = configuration["Weather:Queue:Password"]
|
||||||
};
|
};
|
||||||
|
|
||||||
WriteLog("Connecting to queue server");
|
WriteLog("Connecting to queue server");
|
||||||
@@ -73,7 +69,7 @@ namespace ChrisKaczor.HomeMonitor.Weather.SerialReader
|
|||||||
|
|
||||||
WriteLog("Declaring queue");
|
WriteLog("Declaring queue");
|
||||||
|
|
||||||
model.QueueDeclare(_configuration["Weather:Queue:Name"], true, false, false, null);
|
model.QueueDeclare(configuration["Weather:Queue:Name"], true, false, false, null);
|
||||||
|
|
||||||
WriteLog("Starting serial read loop");
|
WriteLog("Starting serial read loop");
|
||||||
|
|
||||||
@@ -122,7 +118,7 @@ namespace ChrisKaczor.HomeMonitor.Weather.SerialReader
|
|||||||
|
|
||||||
properties.Persistent = true;
|
properties.Persistent = true;
|
||||||
|
|
||||||
model.BasicPublish(string.Empty, _configuration["Weather:Queue:Name"], properties, body);
|
model.BasicPublish(string.Empty, configuration["Weather:Queue:Name"], properties, body);
|
||||||
}
|
}
|
||||||
catch (TimeoutException)
|
catch (TimeoutException)
|
||||||
{
|
{
|
||||||
@@ -133,7 +129,7 @@ namespace ChrisKaczor.HomeMonitor.Weather.SerialReader
|
|||||||
|
|
||||||
private string GetPort()
|
private string GetPort()
|
||||||
{
|
{
|
||||||
var portPrefix = _configuration["Weather:Port:Prefix"];
|
var portPrefix = configuration["Weather:Port:Prefix"]!;
|
||||||
|
|
||||||
while (!_cancellationToken.IsCancellationRequested)
|
while (!_cancellationToken.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
@@ -157,14 +153,13 @@ namespace ChrisKaczor.HomeMonitor.Weather.SerialReader
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void WriteLog(string message)
|
private void WriteLog(string message)
|
||||||
{
|
{
|
||||||
Console.WriteLine(message);
|
logger.LogInformation(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task StopAsync(CancellationToken cancellationToken)
|
public Task StopAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -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>
|
||||||
@@ -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();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -7,5 +7,8 @@
|
|||||||
"Queue": {
|
"Queue": {
|
||||||
"Name": "weather"
|
"Name": "weather"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"Telemetry": {
|
||||||
|
"Endpoint": "http://signoz-otel-collector.platform:4317/"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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
|
||||||
|
|||||||
@@ -7,23 +7,16 @@ 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]")]
|
|
||||||
[ApiController]
|
|
||||||
public class ReadingsController : ControllerBase
|
|
||||||
{
|
|
||||||
private readonly Database _database;
|
|
||||||
|
|
||||||
public ReadingsController(Database database)
|
|
||||||
{
|
|
||||||
_database = database;
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpGet("recent")]
|
[HttpGet("recent")]
|
||||||
public async Task<ActionResult<WeatherReading>> GetRecent([FromQuery] string tz)
|
public async Task<ActionResult<WeatherReading>> GetRecent([FromQuery] string tz)
|
||||||
{
|
{
|
||||||
var recentReading = await _database.GetRecentReading();
|
var recentReading = await database.GetRecentReading();
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(tz))
|
if (string.IsNullOrWhiteSpace(tz))
|
||||||
return recentReading;
|
return recentReading;
|
||||||
@@ -46,13 +39,13 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Controllers
|
|||||||
[HttpGet("history")]
|
[HttpGet("history")]
|
||||||
public async Task<ActionResult<List<WeatherReading>>> GetHistory(DateTimeOffset start, DateTimeOffset end)
|
public async Task<ActionResult<List<WeatherReading>>> GetHistory(DateTimeOffset start, DateTimeOffset end)
|
||||||
{
|
{
|
||||||
return (await _database.GetReadingHistory(start, end)).ToList();
|
return (await database.GetReadingHistory(start, end)).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("aggregate")]
|
[HttpGet("aggregate")]
|
||||||
public async Task<ActionResult<WeatherAggregate>> GetHistoryAggregate(DateTimeOffset start, DateTimeOffset end)
|
public async Task<ActionResult<WeatherAggregate>> GetHistoryAggregate(DateTimeOffset start, DateTimeOffset end)
|
||||||
{
|
{
|
||||||
var readings = (await _database.GetReadingHistory(start, end)).ToList();
|
var readings = (await database.GetReadingHistory(start, end)).ToList();
|
||||||
|
|
||||||
return readings.Any() ? new WeatherAggregate(readings) : null;
|
return readings.Any() ? new WeatherAggregate(readings) : null;
|
||||||
}
|
}
|
||||||
@@ -60,25 +53,24 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Controllers
|
|||||||
[HttpGet("value-history")]
|
[HttpGet("value-history")]
|
||||||
public async Task<ActionResult<List<WeatherValue>>> GetValueHistory(WeatherValueType weatherValueType, DateTimeOffset start, DateTimeOffset end)
|
public async Task<ActionResult<List<WeatherValue>>> GetValueHistory(WeatherValueType weatherValueType, DateTimeOffset start, DateTimeOffset end)
|
||||||
{
|
{
|
||||||
return (await _database.GetReadingValueHistory(weatherValueType, start, end)).ToList();
|
return (await database.GetReadingValueHistory(weatherValueType, start, end)).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("value-history-grouped")]
|
[HttpGet("value-history-grouped")]
|
||||||
public async Task<ActionResult<List<WeatherValueGrouped>>> GetValueHistoryGrouped(WeatherValueType weatherValueType, DateTimeOffset start, DateTimeOffset end, int bucketMinutes = 2)
|
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();
|
return (await database.GetReadingValueHistoryGrouped(weatherValueType, start, end, bucketMinutes)).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("history-grouped")]
|
[HttpGet("history-grouped")]
|
||||||
public async Task<ActionResult<List<WeatherReadingGrouped>>> GetHistoryGrouped(DateTimeOffset start, DateTimeOffset end, int bucketMinutes = 2)
|
public async Task<ActionResult<List<WeatherReadingGrouped>>> GetHistoryGrouped(DateTimeOffset start, DateTimeOffset end, int bucketMinutes = 2)
|
||||||
{
|
{
|
||||||
return (await _database.GetReadingHistoryGrouped(start, end, bucketMinutes)).ToList();
|
return (await database.GetReadingHistoryGrouped(start, end, bucketMinutes)).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("wind-history-grouped")]
|
[HttpGet("wind-history-grouped")]
|
||||||
public async Task<ActionResult<List<WindHistoryGrouped>>> GetWindHistoryGrouped(DateTimeOffset start, DateTimeOffset end, int bucketMinutes = 60)
|
public async Task<ActionResult<List<WindHistoryGrouped>>> GetWindHistoryGrouped(DateTimeOffset start, DateTimeOffset end, int bucketMinutes = 60)
|
||||||
{
|
{
|
||||||
return (await _database.GetWindHistoryGrouped(start, end, bucketMinutes)).ToList();
|
return (await database.GetWindHistoryGrouped(start, end, bucketMinutes)).ToList();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,24 +7,17 @@ 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
|
|
||||||
{
|
|
||||||
private readonly IConfiguration _configuration;
|
|
||||||
|
|
||||||
public Database(IConfiguration configuration)
|
|
||||||
{
|
|
||||||
_configuration = configuration;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void EnsureDatabase()
|
public void EnsureDatabase()
|
||||||
{
|
{
|
||||||
var connectionStringBuilder = new SqlConnectionStringBuilder
|
var connectionStringBuilder = new SqlConnectionStringBuilder
|
||||||
{
|
{
|
||||||
DataSource = _configuration["Weather:Database:Host"],
|
DataSource = configuration["Weather:Database:Host"],
|
||||||
UserID = _configuration["Weather:Database:User"],
|
UserID = configuration["Weather:Database:User"],
|
||||||
Password = _configuration["Weather:Database:Password"],
|
Password = configuration["Weather:Database:Password"],
|
||||||
InitialCatalog = "master"
|
InitialCatalog = "master"
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -35,22 +28,22 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Data
|
|||||||
connection.Open();
|
connection.Open();
|
||||||
|
|
||||||
// Check to see if the database exists
|
// Check to see if the database exists
|
||||||
command.CommandText = $"SELECT CAST(1 as bit) from sys.databases WHERE name='{_configuration["Weather:Database:Name"]}'";
|
command.CommandText = $"SELECT CAST(1 as bit) from sys.databases WHERE name='{configuration["Weather:Database:Name"]}'";
|
||||||
var databaseExists = (bool?)command.ExecuteScalar();
|
var databaseExists = (bool?)command.ExecuteScalar();
|
||||||
|
|
||||||
// Create database if needed
|
// Create database if needed
|
||||||
if (!(databaseExists ?? false))
|
if (!(databaseExists ?? false))
|
||||||
{
|
{
|
||||||
command.CommandText = $"CREATE DATABASE {_configuration["Weather:Database:Name"]}";
|
command.CommandText = $"CREATE DATABASE {configuration["Weather:Database:Name"]}";
|
||||||
command.ExecuteNonQuery();
|
command.ExecuteNonQuery();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Switch to the database now that we're sure it exists
|
// Switch to the database now that we're sure it exists
|
||||||
connection.ChangeDatabase(_configuration["Weather:Database:Name"]);
|
connection.ChangeDatabase(configuration["Weather:Database:Name"]!);
|
||||||
|
|
||||||
var schema = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.Schema.sql");
|
var schema = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.Schema.sql");
|
||||||
|
|
||||||
// Make sure the database is up to date
|
// Make sure the database is up-to-date
|
||||||
command.CommandText = schema;
|
command.CommandText = schema;
|
||||||
command.ExecuteNonQuery();
|
command.ExecuteNonQuery();
|
||||||
}
|
}
|
||||||
@@ -59,10 +52,10 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Data
|
|||||||
{
|
{
|
||||||
var connectionStringBuilder = new SqlConnectionStringBuilder
|
var connectionStringBuilder = new SqlConnectionStringBuilder
|
||||||
{
|
{
|
||||||
DataSource = _configuration["Weather:Database:Host"],
|
DataSource = configuration["Weather:Database:Host"],
|
||||||
UserID = _configuration["Weather:Database:User"],
|
UserID = configuration["Weather:Database:User"],
|
||||||
Password = _configuration["Weather:Database:Password"],
|
Password = configuration["Weather:Database:Password"],
|
||||||
InitialCatalog = _configuration["Weather:Database:Name"]
|
InitialCatalog = configuration["Weather:Database:Name"]
|
||||||
};
|
};
|
||||||
|
|
||||||
var connection = new SqlConnection(connectionStringBuilder.ConnectionString);
|
var connection = new SqlConnection(connectionStringBuilder.ConnectionString);
|
||||||
@@ -148,5 +141,4 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Data
|
|||||||
|
|
||||||
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 });
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -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"]
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
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;
|
||||||
@@ -14,5 +14,4 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service
|
|||||||
{
|
{
|
||||||
return decimal.Truncate(value * DecimalEx.Pow(10, decimalPlaces)) / DecimalEx.Pow(10, decimalPlaces);
|
return decimal.Truncate(value * DecimalEx.Pow(10, decimalPlaces)) / DecimalEx.Pow(10, decimalPlaces);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -13,28 +13,19 @@ 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 : IHostedService
|
|
||||||
{
|
|
||||||
private readonly IConfiguration _configuration;
|
|
||||||
private readonly Database _database;
|
|
||||||
|
|
||||||
|
[UsedImplicitly]
|
||||||
|
public class MessageHandler(IConfiguration configuration, Database database) : IHostedService
|
||||||
|
{
|
||||||
private IConnection _queueConnection;
|
private IConnection _queueConnection;
|
||||||
private IModel _queueModel;
|
private IModel _queueModel;
|
||||||
|
|
||||||
private HubConnection _hubConnection;
|
private HubConnection _hubConnection;
|
||||||
|
|
||||||
public MessageHandler(IConfiguration configuration, Database database)
|
|
||||||
{
|
|
||||||
_configuration = configuration;
|
|
||||||
_database = database;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Task StartAsync(CancellationToken cancellationToken)
|
public Task StartAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var host = _configuration["Weather:Queue:Host"];
|
var host = configuration["Weather:Queue:Host"];
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(host))
|
if (string.IsNullOrEmpty(host))
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
@@ -44,22 +35,22 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service
|
|||||||
var factory = new ConnectionFactory
|
var factory = new ConnectionFactory
|
||||||
{
|
{
|
||||||
HostName = host,
|
HostName = host,
|
||||||
UserName = _configuration["Weather:Queue:User"],
|
UserName = configuration["Weather:Queue:User"],
|
||||||
Password = _configuration["Weather:Queue:Password"]
|
Password = configuration["Weather:Queue:Password"]
|
||||||
};
|
};
|
||||||
|
|
||||||
_queueConnection = factory.CreateConnection();
|
_queueConnection = factory.CreateConnection();
|
||||||
_queueModel = _queueConnection.CreateModel();
|
_queueModel = _queueConnection.CreateModel();
|
||||||
|
|
||||||
_queueModel.QueueDeclare(_configuration["Weather:Queue:Name"], true, false, false, null);
|
_queueModel.QueueDeclare(configuration["Weather:Queue:Name"], true, false, false, null);
|
||||||
|
|
||||||
var consumer = new EventingBasicConsumer(_queueModel);
|
var consumer = new EventingBasicConsumer(_queueModel);
|
||||||
consumer.Received += DeviceEventHandler;
|
consumer.Received += DeviceEventHandler;
|
||||||
|
|
||||||
_queueModel.BasicConsume(_configuration["Weather:Queue:Name"], true, consumer);
|
_queueModel.BasicConsume(configuration["Weather:Queue:Name"], true, consumer);
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(_configuration["Hub:Weather"]))
|
if (!string.IsNullOrEmpty(configuration["Hub:Weather"]))
|
||||||
_hubConnection = new HubConnectionBuilder().WithUrl(_configuration["Hub:Weather"]).Build();
|
_hubConnection = new HubConnectionBuilder().WithUrl(configuration["Hub:Weather"]).Build();
|
||||||
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
@@ -94,12 +85,12 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
_database.StoreWeatherData(weatherMessage);
|
database.StoreWeatherData(weatherMessage);
|
||||||
|
|
||||||
if (_hubConnection == null)
|
if (_hubConnection == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var weatherUpdate = new WeatherUpdate(weatherMessage, _database);
|
var weatherUpdate = new WeatherUpdate(weatherMessage, database);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -127,5 +118,4 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service
|
|||||||
{
|
{
|
||||||
Console.WriteLine(message);
|
Console.WriteLine(message);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,14 +1,14 @@
|
|||||||
|
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 class WeatherAggregate
|
|
||||||
{
|
|
||||||
public ReadingAggregate Humidity { get; set; }
|
public ReadingAggregate Humidity { get; set; }
|
||||||
|
|
||||||
public ReadingAggregate Temperature { get; set; }
|
public ReadingAggregate Temperature { get; set; }
|
||||||
@@ -45,5 +45,4 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Models
|
|||||||
|
|
||||||
RainTotal = readings.Sum(r => r.Rain);
|
RainTotal = readings.Sum(r => r.Rain);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
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 class WeatherReadingGrouped
|
|
||||||
{
|
|
||||||
public DateTimeOffset Bucket { get; set; }
|
public DateTimeOffset Bucket { get; set; }
|
||||||
|
|
||||||
public decimal AverageHumidity { get; set; }
|
public decimal AverageHumidity { get; set; }
|
||||||
@@ -17,5 +17,4 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Models
|
|||||||
public decimal AverageLightLevel { get; set; }
|
public decimal AverageLightLevel { get; set; }
|
||||||
|
|
||||||
public decimal RainTotal { get; set; }
|
public decimal RainTotal { get; set; }
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -5,11 +5,11 @@ 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]
|
|
||||||
public class WeatherUpdate : WeatherUpdateBase
|
|
||||||
{
|
|
||||||
private readonly Database _database;
|
private readonly Database _database;
|
||||||
|
|
||||||
public WeatherUpdate(WeatherMessage weatherMessage, Database database)
|
public WeatherUpdate(WeatherMessage weatherMessage, Database database)
|
||||||
@@ -97,5 +97,4 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Models
|
|||||||
.00000199m * temperature * temperature * humidity * humidity;
|
.00000199m * temperature * temperature * humidity * humidity;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -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
|
||||||
{
|
{
|
||||||
[PublicAPI]
|
|
||||||
public class WeatherValue
|
|
||||||
{
|
|
||||||
public DateTimeOffset Timestamp { get; set; }
|
public DateTimeOffset Timestamp { get; set; }
|
||||||
|
|
||||||
public decimal Value { get; set; }
|
public decimal Value { get; set; }
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -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
|
||||||
{
|
{
|
||||||
[PublicAPI]
|
|
||||||
public class WeatherValueGrouped
|
|
||||||
{
|
|
||||||
public DateTimeOffset Bucket { get; set; }
|
public DateTimeOffset Bucket { get; set; }
|
||||||
|
|
||||||
public decimal AverageValue { get; set; }
|
public decimal AverageValue { get; set; }
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
|
|
||||||
namespace ChrisKaczor.HomeMonitor.Weather.Service.Models
|
namespace ChrisKaczor.HomeMonitor.Weather.Service.Models;
|
||||||
|
|
||||||
|
[PublicAPI]
|
||||||
|
public enum WeatherValueType
|
||||||
{
|
{
|
||||||
[PublicAPI]
|
|
||||||
public enum WeatherValueType
|
|
||||||
{
|
|
||||||
WindSpeed,
|
WindSpeed,
|
||||||
Humidity,
|
Humidity,
|
||||||
HumidityTemperature,
|
HumidityTemperature,
|
||||||
@@ -14,5 +14,4 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Models
|
|||||||
LightLevel,
|
LightLevel,
|
||||||
Altitude,
|
Altitude,
|
||||||
SatelliteCount
|
SatelliteCount
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
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 class WindHistoryGrouped
|
|
||||||
{
|
|
||||||
public DateTimeOffset Bucket { get; set; }
|
public DateTimeOffset Bucket { get; set; }
|
||||||
|
|
||||||
public decimal MinimumSpeed { get; set; }
|
public decimal MinimumSpeed { get; set; }
|
||||||
@@ -15,5 +15,4 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Models
|
|||||||
public decimal MaximumSpeed { get; set; }
|
public decimal MaximumSpeed { get; set; }
|
||||||
|
|
||||||
public decimal AverageDirection { get; set; }
|
public decimal AverageDirection { get; set; }
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -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)
|
||||||
{
|
{
|
||||||
CreateWebHostBuilder(args).Build().Run();
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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();
|
var assembly = Assembly.GetExecutingAssembly();
|
||||||
|
|
||||||
using var stream = assembly.GetManifestResourceStream(resourceName);
|
using var stream = assembly.GetManifestResourceStream(resourceName);
|
||||||
|
|
||||||
if (stream == null)
|
if (stream == null)
|
||||||
throw new Exception($"Resource {resourceName} not found in {assembly.FullName}. Valid resources are: {string.Join(", ", assembly.GetManifestResourceNames())}.");
|
throw new Exception($"Resource {resourceName} not found in {assembly.FullName}. Valid resources are: {string.Join(", ", assembly.GetManifestResourceNames())}.");
|
||||||
|
|
||||||
using var reader = new StreamReader(stream);
|
using var reader = new StreamReader(stream);
|
||||||
|
|
||||||
return reader.ReadToEnd();
|
return reader.ReadToEnd();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -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>
|
||||||
|
|||||||
@@ -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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -2,7 +2,6 @@
|
|||||||
"Logging": {
|
"Logging": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
"Default": "Debug",
|
"Default": "Debug",
|
||||||
"System": "Information",
|
|
||||||
"Microsoft": "Information"
|
"Microsoft": "Information"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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/"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user