4 Commits

Author SHA1 Message Date
37dc8bed35 Startet på database emigrations med dbup.
Pauset til fordel for at få opvaskemaskine gjort smart.
2026-01-12 11:58:38 +01:00
3b197f0b85 Merge pull request 'feature/projekt_struktur' (#1) from feature/projekt_struktur into develop
Reviewed-on: #1
2025-12-21 10:58:13 +00:00
09b0793a47 grundkæggende projekt struktur er på plads 2025-12-21 11:57:04 +01:00
8a76681add Projekt struktur er på plads. 2025-12-21 11:56:17 +01:00
56 changed files with 1115 additions and 0 deletions

15
.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,15 @@
# Default ignored files
/shelf/
/workspace.xml
# Rider ignored files
/contentModel.xml
/projectSettingsUpdater.xml
/.idea.SmartHouse.Services.iml
/modules.xml
# Ignored default folder with query files
/queries/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/

4
.idea/encodings.xml generated Normal file
View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with BOM under Windows, with no BOM otherwise" />
</project>

8
.idea/indexLayout.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="UserContentModel">
<attachedFolders />
<explicitIncludes />
<explicitExcludes />
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@@ -0,0 +1,30 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
!**/.gitignore
!.git/HEAD
!.git/config
!.git/packed-refs
!.git/refs/heads/**

View File

@@ -0,0 +1,15 @@
# Default ignored files
/shelf/
/workspace.xml
# Rider ignored files
/.idea.SmartHouse.Services.iml
/contentModel.xml
/modules.xml
/projectSettingsUpdater.xml
# Ignored default folder with query files
/queries/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with BOM under Windows, with no BOM otherwise" />
</project>

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="UserContentModel">
<attachedFolders />
<explicitIncludes />
<explicitExcludes />
</component>
</project>

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

View File

@@ -0,0 +1,16 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using SmartHouse.Services.Database.Models;
var host = Host.CreateDefaultBuilder(args)
.ConfigureServices((context, services) =>
{
services.AddDbContext<SmartHouseContext>(options =>
options.UseSqlServer(context.Configuration.GetConnectionString("SmartHouse")));
// services.AddHostedService<YourBackgroundService>();
})
.Build();
await host.RunAsync();

View File

@@ -0,0 +1,13 @@
var builder = DistributedApplication.CreateBuilder(args);
builder.AddProject<Projects.SmartHouse_Services_Database>("smarthouse-services-database");
builder.AddProject<Projects.SmartHouse_Services_EVCharging>("smarthouse-services-evcharging");
builder.AddProject<Projects.SmartHouse_Services_HomeAssistent>("smarthouse-services-homeassistent");
builder.AddProject<Projects.SmartHouse_Services_Siemens>("smarthouse-services-siemens");
builder.AddProject<Projects.SmartHouse_Services_SunCast>("smarthouse-services-suncast");
builder.Build().Run();

View File

@@ -0,0 +1,31 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:17223;http://localhost:15039",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21227",
"ASPIRE_DASHBOARD_MCP_ENDPOINT_URL": "https://localhost:23091",
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22221"
}
},
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:15039",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19207",
"ASPIRE_DASHBOARD_MCP_ENDPOINT_URL": "http://localhost:18000",
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20178"
}
}
}
}

View File

@@ -0,0 +1,19 @@
<Project Sdk="Aspire.AppHost.Sdk/13.1.0">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UserSecretsId>90b46a44-f221-4903-b340-6fee7c8848f2</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\SmartHouse.Services.Database\SmartHouse.Services.Database.csproj" />
<ProjectReference Include="..\SmartHouse.Services.EVCharging\SmartHouse.Services.EVCharging.csproj" />
<ProjectReference Include="..\SmartHouse.Services.HomeAssistent\SmartHouse.Services.HomeAssistent.csproj" />
<ProjectReference Include="..\SmartHouse.Services.Siemens\SmartHouse.Services.Siemens.csproj" />
<ProjectReference Include="..\SmartHouse.Services.SunCast\SmartHouse.Services.SunCast.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Aspire.Hosting.Dcp": "Warning"
}
}
}

View File

@@ -0,0 +1,5 @@
namespace SmartHouse.Services.Database;
public class Class1
{
}

View File

@@ -0,0 +1,29 @@
// <auto-generated> This file has been auto generated by EF Core Power Tools. </auto-generated>
#nullable disable
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using SmartHouse.Services.Database.Models;
namespace SmartHouse.Services.Database.Data;
public partial class SmartHouseDbContext : DbContext
{
public SmartHouseDbContext()
{
}
public SmartHouseDbContext(DbContextOptions<SmartHouseDbContext> options)
: base(options)
{
}
public virtual DbSet<DailyReading> DailyReadings { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}

View File

@@ -0,0 +1,9 @@
namespace SmartHouse.Services.Database.Entities
{
public class Device
{
public int Id { get; set; }
public string Name { get; set; } = null!;
public string? Location { get; set; }
}
}

View File

@@ -0,0 +1,26 @@
// <auto-generated> This file has been auto generated by EF Core Power Tools. </auto-generated>
#nullable disable
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
namespace SmartHouse.Services.Database.Models;
[Table("DailyReading", Schema = "SunCast")]
public partial class DailyReading
{
[Key]
public int Id { get; set; }
public DateTime Date { get; set; }
public double SuncastValue { get; set; }
public double GrowattValue { get; set; }
public double DiffValue { get; set; }
public double DiffProcent { get; set; }
}

View File

@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\SmartHouse.Services.ServiceDefaults\SmartHouse.Services.ServiceDefaults.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.22" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,53 @@
{
"CodeGenerationMode": 4,
"ContextClassName": "SmartHouseDbContext",
"ContextNamespace": "",
"FilterSchemas": false,
"IncludeConnectionString": true,
"IrregularWords": null,
"MinimumProductVersion": "2.6.1301",
"ModelNamespace": null,
"OutputContextPath": "Data",
"OutputPath": "Models",
"PluralRules": null,
"PreserveCasingWithRegex": true,
"ProjectRootNamespace": "SmartHouse.Services.Database",
"Schemas": null,
"SelectedHandlebarsLanguage": 2,
"SelectedToBeGenerated": 0,
"SingularRules": null,
"T4TemplatePath": null,
"Tables": [
{
"Name": "[SunCast].[DailyReading]",
"ObjectType": 0
}
],
"UiHint": null,
"UncountableWords": null,
"UseAsyncStoredProcedureCalls": true,
"UseBoolPropertiesWithoutDefaultSql": false,
"UseDatabaseNames": true,
"UseDatabaseNamesForRoutines": true,
"UseDateOnlyTimeOnly": true,
"UseDbContextSplitting": false,
"UseDecimalDataAnnotationForSprocResult": true,
"UseFluentApiOnly": false,
"UseHandleBars": false,
"UseHierarchyId": false,
"UseInflector": true,
"UseInternalAccessModifiersForSprocsAndFunctions": false,
"UseLegacyPluralizer": false,
"UseManyToManyEntity": true,
"UseNoDefaultConstructor": false,
"UseNoNavigations": false,
"UseNoObjectFilter": false,
"UseNodaTime": false,
"UseNullableReferences": false,
"UsePrefixNavigationNaming": false,
"UseSchemaFolders": false,
"UseSchemaNamespaces": false,
"UseSpatial": false,
"UseT4": false,
"UseT4Split": false
}

View File

@@ -0,0 +1,28 @@
# See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
# This stage is used when running from VS in fast mode (Default for Debug configuration)
FROM mcr.microsoft.com/dotnet/runtime:10.0 AS base
USER $APP_UID
WORKDIR /app
# This stage is used to build the service project
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["SmartHouse.Services.DbUP/SmartHouse.Services.DbUP.csproj", "SmartHouse.Services.DbUP/"]
RUN dotnet restore "./SmartHouse.Services.DbUP/SmartHouse.Services.DbUP.csproj"
COPY . .
WORKDIR "/src/SmartHouse.Services.DbUP"
RUN dotnet build "./SmartHouse.Services.DbUP.csproj" -c $BUILD_CONFIGURATION -o /app/build
# This stage is used to publish the service project to be copied to the final stage
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./SmartHouse.Services.DbUP.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
# This stage is used in production or when running from VS in regular mode (Default when not using the Debug configuration)
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "SmartHouse.Services.DbUP.dll"]

View File

@@ -0,0 +1,29 @@
using DbUp;
using System.Reflection;
var connectionString = "Server=vps.maximuss.dk;Database=SmartHouse;Trusted_Connection=True;MultipleActiveResultSets=true";
EnsureDatabase.For.SqlDatabase(connectionString);
var upgrader =
DeployChanges.To
.SqlDatabase(connectionString)
.WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly())
.LogToConsole()
.Build();
var result = upgrader.PerformUpgrade();
if (!result.Successful)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(result.Error);
Console.ResetColor();
#if DEBUG
Console.ReadLine();
#endif
return -1;
}
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Success!");
Console.ResetColor();
return 0;

View File

@@ -0,0 +1,10 @@
{
"profiles": {
"SmartHouse.Services.DbUP": {
"commandName": "Project"
},
"Container (Dockerfile)": {
"commandName": "Docker"
}
}
}

View File

@@ -0,0 +1 @@
create schema SunCast

View File

@@ -0,0 +1,12 @@
CREATE TABLE SunCast.DailyReading
(
Id int NOT NULL IDENTITY (1, 1),
Date datetime2(7) NOT NULL,
SuncastValue float(53) NOT NULL,
GrowattValue float(53) NOT NULL,
DiffValue float(53) NOT NULL,
DiffProcent float(53) NOT NULL
) ON [PRIMARY]
GO
ALTER TABLE SunCast.Dailyreading SET (LOCK_ESCALATION = TABLE)
GO

View File

@@ -0,0 +1,5 @@
ALTER TABLE SunCast.DailyReading ADD CONSTRAINT
PK_DailyReading PRIMARY KEY CLUSTERED
(
Id
) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

View File

@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="dbup" Version="5.0.41" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.23.0" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,13 @@
using Microsoft.EntityFrameworkCore;
using SmartHouse.Services.Database;
using SmartHouse.Services.EVCharging;
var builder = Host.CreateApplicationBuilder(args);
builder.AddServiceDefaults();
builder.Services.AddHostedService<Worker>();
//builder.Services.AddDbContext<SmartHouseDbContext>(options =>
// options.UseSqlServer(builder.Configuration.GetConnectionString("SmartHouseDatabase")));
var host = builder.Build();
host.Run();

View File

@@ -0,0 +1,12 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"SmartHouse.Services.EVCharging": {
"commandName": "Project",
"dotnetRunMessages": true,
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk.Worker">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>dotnet-SmartHouse.Services.EVCharging-8f24ed80-dad3-439c-b4e6-c654b00d1ae2</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="dbup-sqlserver" Version="6.0.16" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="10.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SmartHouse.Services.Database\SmartHouse.Services.Database.csproj" />
<ProjectReference Include="..\SmartHouse.Services.ServiceDefaults\SmartHouse.Services.ServiceDefaults.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,17 @@
namespace SmartHouse.Services.EVCharging;
public class Worker(ILogger<Worker> logger) : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
if (logger.IsEnabled(LogLevel.Information))
{
logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
}
await Task.Delay(1000, stoppingToken);
}
}
}

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}

View File

@@ -0,0 +1,27 @@
using Microsoft.Extensions.Logging;
namespace SmartHouse.Services.Helper
{
public interface IPerfLog
{
void Info(string message, params object[] args);
void Warn(string message, params object[] args);
}
public sealed class PerfLog : IPerfLog
{
private readonly ILogger _log;
public PerfLog(ILoggerFactory factory)
{
// Matcher din rule: Performance.* -> perf.log
_log = factory.CreateLogger("Performance.Worker");
}
public void Info(string message, params object[] args)
=> _log.LogInformation(message, args);
public void Warn(string message, params object[] args)
=> _log.LogWarning(message, args);
}
}

View File

@@ -0,0 +1,20 @@
using Microsoft.Extensions.Logging;
using SmartHouse.Services.Helper;
public sealed class PerfLog : IPerfLog
{
private readonly ILogger _log;
public PerfLog(ILoggerFactory factory)
{
// Matcher rule: Performance.* → perf.log
_log = factory.CreateLogger("Performance.Worker");
}
public void Info(string message, params object[] args)
=> _log.LogInformation(message, args);
public void Warn(string message, params object[] args)
=> _log.LogWarning(message, args);
}

View File

@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NLog" Version="6.0.7" />
<PackageReference Include="NLog.Extensions.Hosting" Version="6.1.0" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,5 @@
namespace SmartHouse.Services.HomeAssistent;
public class Class1
{
}

View File

@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\SmartHouse.Services.ServiceDefaults\SmartHouse.Services.ServiceDefaults.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,127 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.ServiceDiscovery;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
namespace Microsoft.Extensions.Hosting;
// Adds common Aspire services: service discovery, resilience, health checks, and OpenTelemetry.
// This project should be referenced by each service project in your solution.
// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults
public static class Extensions
{
private const string HealthEndpointPath = "/health";
private const string AlivenessEndpointPath = "/alive";
public static TBuilder AddServiceDefaults<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
{
builder.ConfigureOpenTelemetry();
builder.AddDefaultHealthChecks();
builder.Services.AddServiceDiscovery();
builder.Services.ConfigureHttpClientDefaults(http =>
{
// Turn on resilience by default
http.AddStandardResilienceHandler();
// Turn on service discovery by default
http.AddServiceDiscovery();
});
// Uncomment the following to restrict the allowed schemes for service discovery.
// builder.Services.Configure<ServiceDiscoveryOptions>(options =>
// {
// options.AllowedSchemes = ["https"];
// });
return builder;
}
public static TBuilder ConfigureOpenTelemetry<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
{
builder.Logging.AddOpenTelemetry(logging =>
{
logging.IncludeFormattedMessage = true;
logging.IncludeScopes = true;
});
builder.Services.AddOpenTelemetry()
.WithMetrics(metrics =>
{
metrics.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddRuntimeInstrumentation();
})
.WithTracing(tracing =>
{
tracing.AddSource(builder.Environment.ApplicationName)
.AddAspNetCoreInstrumentation(tracing =>
// Exclude health check requests from tracing
tracing.Filter = context =>
!context.Request.Path.StartsWithSegments(HealthEndpointPath)
&& !context.Request.Path.StartsWithSegments(AlivenessEndpointPath)
)
// Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package)
//.AddGrpcClientInstrumentation()
.AddHttpClientInstrumentation();
});
builder.AddOpenTelemetryExporters();
return builder;
}
private static TBuilder AddOpenTelemetryExporters<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
{
var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);
if (useOtlpExporter)
{
builder.Services.AddOpenTelemetry().UseOtlpExporter();
}
// Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package)
//if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]))
//{
// builder.Services.AddOpenTelemetry()
// .UseAzureMonitor();
//}
return builder;
}
public static TBuilder AddDefaultHealthChecks<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
{
builder.Services.AddHealthChecks()
// Add a default liveness check to ensure app is responsive
.AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]);
return builder;
}
public static WebApplication MapDefaultEndpoints(this WebApplication app)
{
// Adding health checks endpoints to applications in non-development environments has security implications.
// See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments.
if (app.Environment.IsDevelopment())
{
// All health checks must pass for app to be considered ready to accept traffic after starting
app.MapHealthChecks(HealthEndpointPath);
// Only health checks tagged with the "live" tag must pass for app to be considered alive
app.MapHealthChecks(AlivenessEndpointPath, new HealthCheckOptions
{
Predicate = r => r.Tags.Contains("live")
});
}
return app;
}
}

View File

@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireSharedProject>true</IsAspireSharedProject>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App"/>
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="10.1.0"/>
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" Version="10.1.0"/>
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.14.0"/>
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.14.0"/>
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.14.0"/>
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.14.0"/>
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.14.0"/>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,90 @@
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
throwConfigExceptions="true"
internalLogLevel="Warn"
internalLogFile="logs/nlog-internal.log">
<!-- ========================= -->
<!-- VARIABLES -->
<!-- ========================= -->
<variable name="appName" value="MyApp" />
<variable name="environment" value="${aspnet-environment:whenEmpty=Production}" />
<variable name="logRoot" value="${basedir}/logs" />
<variable name="year" value="${date:format=yyyy}" />
<variable name="month" value="${date:format=MM}" />
<variable name="day" value="${date:format=dd}" />
<!-- ========================= -->
<!-- LAYOUTS -->
<!-- ========================= -->
<layout name="jsonLayout" xsi:type="JsonLayout" includeAllProperties="true">
<attribute name="timestamp" layout="${date:format=o}" />
<attribute name="level" layout="${level}" />
<attribute name="logger" layout="${logger}" />
<attribute name="message" layout="${message}" />
<attribute name="exception" layout="${exception:format=ToString}" />
<attribute name="correlationId" layout="${mdlc:item=CorrelationId}" />
<attribute name="traceId" layout="${activityid}" />
<attribute name="environment" layout="${environment}" />
<attribute name="application" layout="${appName}" />
</layout>
<!-- ========================= -->
<!-- TARGETS -->
<!-- ========================= -->
<targets async="true">
<!-- Console (dev / Docker) -->
<target xsi:type="Console"
name="console"
layout="${jsonLayout}" />
<!-- App log -->
<target xsi:type="File"
name="appFile"
fileName="${logRoot}/${year}/${month}/${day}/app.log"
layout="${jsonLayout}"
concurrentWrites="true"
keepFileOpen="false" />
<!-- Error log -->
<target xsi:type="File"
name="errorFile"
fileName="${logRoot}/${year}/${month}/${day}/error.log"
layout="${jsonLayout}"
concurrentWrites="true"
keepFileOpen="false" />
<!-- Performance log -->
<target xsi:type="File"
name="performanceFile"
fileName="${logRoot}/${year}/${month}/${day}/perf.log"
layout="${jsonLayout}"
concurrentWrites="true"
keepFileOpen="false" />
</targets>
<!-- ========================= -->
<!-- RULES (MINIMAL) -->
<!-- ========================= -->
<rules>
<!-- Normal app flow -->
<logger name="*" minlevel="Info" writeTo="console,appFile" />
<!-- Errors -->
<logger name="*" minlevel="Error" writeTo="errorFile" />
<!-- Performance -->
<logger name="Performance.*" minlevel="Info" writeTo="performanceFile" />
<!-- Reduce framework noise -->
<logger name="Microsoft.*" maxlevel="Info" final="true" />
<logger name="System.*" maxlevel="Warn" final="true" />
</rules>
</nlog>

View File

@@ -0,0 +1,57 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NLog;
using NLog.Extensions.Logging;
using SmartHouse.Services.Helper;
using SmartHouse.Services.Siemens;
// NLog internal bootstrap logger (til hvis noget går galt før hosten er oppe)
var bootstrapLogger = LogManager.Setup()
.LoadConfigurationFromFile("NLog.config", optional: false)
.GetCurrentClassLogger();
try
{
var builder = Host.CreateApplicationBuilder(args);
// Aspire service defaults (behold den)
builder.AddServiceDefaults();
// 1) Fjern standard logging providers
builder.Logging.ClearProviders();
// 2) (Valgfrit men anbefalet) Brug scopes (CorrelationId m.m.)
builder.Logging.Configure(o =>
{
o.ActivityTrackingOptions =
ActivityTrackingOptions.TraceId |
ActivityTrackingOptions.SpanId |
ActivityTrackingOptions.ParentId;
});
// 3) Tilføj NLog som provider (læser din NLog.config)
builder.Logging.AddNLog(new NLogProviderOptions
{
CaptureMessageTemplates = true,
CaptureMessageProperties = true,
IncludeScopes = true
});
// Perf logger wrapper (DI-friendly, uden NLog i din domænekode)
builder.Services.AddSingleton<IPerfLog, PerfLog>();
builder.Services.AddHostedService<Worker>();
var host = builder.Build();
host.Run();
}
catch (Exception ex)
{
bootstrapLogger.Error(ex, "Host terminated unexpectedly");
throw;
}
finally
{
LogManager.Shutdown();
}

View File

@@ -0,0 +1,12 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"SmartHouse.Services.Siemens": {
"commandName": "Project",
"dotnetRunMessages": true,
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk.Worker">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>dotnet-SmartHouse.Services.Siemens-09de8319-5d84-47b2-8d81-a7eec428f8da</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.1" />
<PackageReference Include="NLog" Version="6.0.7" />
<PackageReference Include="NLog.Web.AspNetCore" Version="6.1.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SmartHouse.Services.Helper\SmartHouse.Services.Helper.csproj" />
<ProjectReference Include="..\SmartHouse.Services.ServiceDefaults\SmartHouse.Services.ServiceDefaults.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,17 @@
namespace SmartHouse.Services.Siemens;
public class Worker(ILogger<Worker> logger) : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
if (logger.IsEnabled(LogLevel.Information))
{
logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
}
await Task.Delay(1000, stoppingToken);
}
}
}

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}

View File

@@ -0,0 +1,14 @@
using Microsoft.EntityFrameworkCore;
using SmartHouse.Services.Database;
using SmartHouse.Services.Database.Data;
using SmartHouse.Services.SunCast;
var builder = Host.CreateApplicationBuilder(args);
builder.AddServiceDefaults();
builder.Services.AddHostedService<Worker>();
builder.Services.AddDbContext<SmartHouseDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("SmartHouseDatabase")));
var host = builder.Build();
host.Run();

View File

@@ -0,0 +1,12 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"SmartHouse.Services.SunCast": {
"commandName": "Project",
"dotnetRunMessages": true,
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk.Worker">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>dotnet-SmartHouse.Services.SunCast-5f688ae7-b693-4c9b-aa20-147fcd5c7b79</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="10.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SmartHouse.Services.Database\SmartHouse.Services.Database.csproj" />
<ProjectReference Include="..\SmartHouse.Services.ServiceDefaults\SmartHouse.Services.ServiceDefaults.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,17 @@
namespace SmartHouse.Services.SunCast;
public class Worker(ILogger<Worker> logger) : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
if (logger.IsEnabled(LogLevel.Information))
{
logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
}
await Task.Delay(1000, stoppingToken);
}
}
}

View File

@@ -0,0 +1,11 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"ConnectionStrings": {
"SmartHouse": "Data Source=localhost;Initial Catalog=SmartHouse;Integrated Security=True"
}
}

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}

View File

@@ -0,0 +1,73 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 18
VisualStudioVersion = 18.1.11312.151
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SmartHouse.Services.HomeAssistent", "SmartHouse.Services.HomeAssistent\SmartHouse.Services.HomeAssistent.csproj", "{5EE8D007-801D-4E7A-8F40-D89C7A61B5FD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SmartHouse.Services.Database", "SmartHouse.Services.Database\SmartHouse.Services.Database.csproj", "{8430DCBF-8341-4F00-9017-035699B07FED}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SmartHouse.Services.Siemens", "SmartHouse.Services.Siemens\SmartHouse.Services.Siemens.csproj", "{158F4BB1-78CC-42FE-B712-5489F8626750}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SmartHouse.Services.SunCast", "SmartHouse.Services.SunCast\SmartHouse.Services.SunCast.csproj", "{02ADF617-BBE0-4681-ABAE-164A1BF43804}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SmartHouse.Services.EVCharging", "SmartHouse.Services.EVCharging\SmartHouse.Services.EVCharging.csproj", "{9C8B5C4D-0B34-4ACC-A5ED-8DE9705E3801}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SmartHouse.Services.AppHost", "SmartHouse.Services.AppHost\SmartHouse.Services.AppHost.csproj", "{966ADDAD-5DF9-4FC3-8ACC-D751EA633DF1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SmartHouse.Services.ServiceDefaults", "SmartHouse.Services.ServiceDefaults\SmartHouse.Services.ServiceDefaults.csproj", "{980D4FF9-B1F4-4F5D-846F-38EE5E15CC0E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SmartHouse.Services.Helper", "SmartHouse.Services.Helper\SmartHouse.Services.Helper.csproj", "{119F70AB-85D8-4715-A41D-AD8509CEC551}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SmartHouse.Services.DbUP", "SmartHouse.Services.DbUP\SmartHouse.Services.DbUP.csproj", "{888E76E7-9AB3-4F05-99E5-ACDE1078A4C0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5EE8D007-801D-4E7A-8F40-D89C7A61B5FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5EE8D007-801D-4E7A-8F40-D89C7A61B5FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5EE8D007-801D-4E7A-8F40-D89C7A61B5FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5EE8D007-801D-4E7A-8F40-D89C7A61B5FD}.Release|Any CPU.Build.0 = Release|Any CPU
{8430DCBF-8341-4F00-9017-035699B07FED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8430DCBF-8341-4F00-9017-035699B07FED}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8430DCBF-8341-4F00-9017-035699B07FED}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8430DCBF-8341-4F00-9017-035699B07FED}.Release|Any CPU.Build.0 = Release|Any CPU
{158F4BB1-78CC-42FE-B712-5489F8626750}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{158F4BB1-78CC-42FE-B712-5489F8626750}.Debug|Any CPU.Build.0 = Debug|Any CPU
{158F4BB1-78CC-42FE-B712-5489F8626750}.Release|Any CPU.ActiveCfg = Release|Any CPU
{158F4BB1-78CC-42FE-B712-5489F8626750}.Release|Any CPU.Build.0 = Release|Any CPU
{02ADF617-BBE0-4681-ABAE-164A1BF43804}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{02ADF617-BBE0-4681-ABAE-164A1BF43804}.Debug|Any CPU.Build.0 = Debug|Any CPU
{02ADF617-BBE0-4681-ABAE-164A1BF43804}.Release|Any CPU.ActiveCfg = Release|Any CPU
{02ADF617-BBE0-4681-ABAE-164A1BF43804}.Release|Any CPU.Build.0 = Release|Any CPU
{9C8B5C4D-0B34-4ACC-A5ED-8DE9705E3801}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9C8B5C4D-0B34-4ACC-A5ED-8DE9705E3801}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9C8B5C4D-0B34-4ACC-A5ED-8DE9705E3801}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9C8B5C4D-0B34-4ACC-A5ED-8DE9705E3801}.Release|Any CPU.Build.0 = Release|Any CPU
{966ADDAD-5DF9-4FC3-8ACC-D751EA633DF1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{966ADDAD-5DF9-4FC3-8ACC-D751EA633DF1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{966ADDAD-5DF9-4FC3-8ACC-D751EA633DF1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{966ADDAD-5DF9-4FC3-8ACC-D751EA633DF1}.Release|Any CPU.Build.0 = Release|Any CPU
{980D4FF9-B1F4-4F5D-846F-38EE5E15CC0E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{980D4FF9-B1F4-4F5D-846F-38EE5E15CC0E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{980D4FF9-B1F4-4F5D-846F-38EE5E15CC0E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{980D4FF9-B1F4-4F5D-846F-38EE5E15CC0E}.Release|Any CPU.Build.0 = Release|Any CPU
{119F70AB-85D8-4715-A41D-AD8509CEC551}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{119F70AB-85D8-4715-A41D-AD8509CEC551}.Debug|Any CPU.Build.0 = Debug|Any CPU
{119F70AB-85D8-4715-A41D-AD8509CEC551}.Release|Any CPU.ActiveCfg = Release|Any CPU
{119F70AB-85D8-4715-A41D-AD8509CEC551}.Release|Any CPU.Build.0 = Release|Any CPU
{888E76E7-9AB3-4F05-99E5-ACDE1078A4C0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{888E76E7-9AB3-4F05-99E5-ACDE1078A4C0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{888E76E7-9AB3-4F05-99E5-ACDE1078A4C0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{888E76E7-9AB3-4F05-99E5-ACDE1078A4C0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6F06BFFD-CD82-4F8C-AB9B-D63797D89B84}
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,5 @@
{
"ConnectionStrings": {
"SmartHouse": "Data Source=localhost;Initial Catalog=SmartHouse;Integrated Security=True"
}
}