diff --git a/SmartHouse.Services/.dockerignore b/SmartHouse.Services/.dockerignore new file mode 100644 index 0000000..fe1152b --- /dev/null +++ b/SmartHouse.Services/.dockerignore @@ -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/** \ No newline at end of file diff --git a/SmartHouse.Services/Program.cs b/SmartHouse.Services/Program.cs new file mode 100644 index 0000000..03518b4 --- /dev/null +++ b/SmartHouse.Services/Program.cs @@ -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(options => + options.UseSqlServer(context.Configuration.GetConnectionString("SmartHouse"))); + + // services.AddHostedService(); + }) + .Build(); + +await host.RunAsync(); \ No newline at end of file diff --git a/SmartHouse.Services/SmartHouse.Services.Database/Data/SmartHouseDbContext.cs b/SmartHouse.Services/SmartHouse.Services.Database/Data/SmartHouseDbContext.cs new file mode 100644 index 0000000..453b633 --- /dev/null +++ b/SmartHouse.Services/SmartHouse.Services.Database/Data/SmartHouseDbContext.cs @@ -0,0 +1,29 @@ +// This file has been auto generated by EF Core Power Tools. +#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 options) + : base(options) + { + } + + public virtual DbSet DailyReadings { get; set; } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + OnModelCreatingPartial(modelBuilder); + } + + partial void OnModelCreatingPartial(ModelBuilder modelBuilder); +} \ No newline at end of file diff --git a/SmartHouse.Services/SmartHouse.Services.Database/Device.cs b/SmartHouse.Services/SmartHouse.Services.Database/Device.cs new file mode 100644 index 0000000..78d35ad --- /dev/null +++ b/SmartHouse.Services/SmartHouse.Services.Database/Device.cs @@ -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; } + } +} \ No newline at end of file diff --git a/SmartHouse.Services/SmartHouse.Services.Database/Models/DailyReading.cs b/SmartHouse.Services/SmartHouse.Services.Database/Models/DailyReading.cs new file mode 100644 index 0000000..78d12f7 --- /dev/null +++ b/SmartHouse.Services/SmartHouse.Services.Database/Models/DailyReading.cs @@ -0,0 +1,26 @@ +// This file has been auto generated by EF Core Power Tools. +#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; } +} \ No newline at end of file diff --git a/SmartHouse.Services/SmartHouse.Services.Database/SmartHouse.Services.Database.csproj b/SmartHouse.Services/SmartHouse.Services.Database/SmartHouse.Services.Database.csproj index ab2e61d..5862a56 100644 --- a/SmartHouse.Services/SmartHouse.Services.Database/SmartHouse.Services.Database.csproj +++ b/SmartHouse.Services/SmartHouse.Services.Database/SmartHouse.Services.Database.csproj @@ -10,4 +10,8 @@ + + + + diff --git a/SmartHouse.Services/SmartHouse.Services.Database/efpt.config.json b/SmartHouse.Services/SmartHouse.Services.Database/efpt.config.json new file mode 100644 index 0000000..7f170c5 --- /dev/null +++ b/SmartHouse.Services/SmartHouse.Services.Database/efpt.config.json @@ -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 +} \ No newline at end of file diff --git a/SmartHouse.Services/SmartHouse.Services.DbUP/Dockerfile b/SmartHouse.Services/SmartHouse.Services.DbUP/Dockerfile new file mode 100644 index 0000000..bc77d78 --- /dev/null +++ b/SmartHouse.Services/SmartHouse.Services.DbUP/Dockerfile @@ -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"] \ No newline at end of file diff --git a/SmartHouse.Services/SmartHouse.Services.DbUP/Program.cs b/SmartHouse.Services/SmartHouse.Services.DbUP/Program.cs new file mode 100644 index 0000000..efe3625 --- /dev/null +++ b/SmartHouse.Services/SmartHouse.Services.DbUP/Program.cs @@ -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; \ No newline at end of file diff --git a/SmartHouse.Services/SmartHouse.Services.DbUP/Properties/launchSettings.json b/SmartHouse.Services/SmartHouse.Services.DbUP/Properties/launchSettings.json new file mode 100644 index 0000000..397fb8b --- /dev/null +++ b/SmartHouse.Services/SmartHouse.Services.DbUP/Properties/launchSettings.json @@ -0,0 +1,10 @@ +{ + "profiles": { + "SmartHouse.Services.DbUP": { + "commandName": "Project" + }, + "Container (Dockerfile)": { + "commandName": "Docker" + } + } +} \ No newline at end of file diff --git a/SmartHouse.Services/SmartHouse.Services.DbUP/Scripts/001-CreateSchema.sql b/SmartHouse.Services/SmartHouse.Services.DbUP/Scripts/001-CreateSchema.sql new file mode 100644 index 0000000..841c4f3 --- /dev/null +++ b/SmartHouse.Services/SmartHouse.Services.DbUP/Scripts/001-CreateSchema.sql @@ -0,0 +1 @@ +create schema SunCast \ No newline at end of file diff --git a/SmartHouse.Services/SmartHouse.Services.DbUP/Scripts/002-DailySuncast.sql b/SmartHouse.Services/SmartHouse.Services.DbUP/Scripts/002-DailySuncast.sql new file mode 100644 index 0000000..10865a9 --- /dev/null +++ b/SmartHouse.Services/SmartHouse.Services.DbUP/Scripts/002-DailySuncast.sql @@ -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 \ No newline at end of file diff --git a/SmartHouse.Services/SmartHouse.Services.DbUP/Scripts/003-DailySuncastPrimaryKey.sql b/SmartHouse.Services/SmartHouse.Services.DbUP/Scripts/003-DailySuncastPrimaryKey.sql new file mode 100644 index 0000000..45ba35d --- /dev/null +++ b/SmartHouse.Services/SmartHouse.Services.DbUP/Scripts/003-DailySuncastPrimaryKey.sql @@ -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] diff --git a/SmartHouse.Services/SmartHouse.Services.DbUP/SmartHouse.Services.DbUP.csproj b/SmartHouse.Services/SmartHouse.Services.DbUP/SmartHouse.Services.DbUP.csproj new file mode 100644 index 0000000..d5ceee7 --- /dev/null +++ b/SmartHouse.Services/SmartHouse.Services.DbUP/SmartHouse.Services.DbUP.csproj @@ -0,0 +1,16 @@ + + + + Exe + net10.0 + enable + enable + Linux + + + + + + + + diff --git a/SmartHouse.Services/SmartHouse.Services.EVCharging/Program.cs b/SmartHouse.Services/SmartHouse.Services.EVCharging/Program.cs index e177e87..d2af846 100644 --- a/SmartHouse.Services/SmartHouse.Services.EVCharging/Program.cs +++ b/SmartHouse.Services/SmartHouse.Services.EVCharging/Program.cs @@ -1,9 +1,13 @@ +using Microsoft.EntityFrameworkCore; +using SmartHouse.Services.Database; using SmartHouse.Services.EVCharging; var builder = Host.CreateApplicationBuilder(args); builder.AddServiceDefaults(); builder.Services.AddHostedService(); +//builder.Services.AddDbContext(options => +// options.UseSqlServer(builder.Configuration.GetConnectionString("SmartHouseDatabase"))); var host = builder.Build(); host.Run(); \ No newline at end of file diff --git a/SmartHouse.Services/SmartHouse.Services.EVCharging/SmartHouse.Services.EVCharging.csproj b/SmartHouse.Services/SmartHouse.Services.EVCharging/SmartHouse.Services.EVCharging.csproj index 5539061..3132d46 100644 --- a/SmartHouse.Services/SmartHouse.Services.EVCharging/SmartHouse.Services.EVCharging.csproj +++ b/SmartHouse.Services/SmartHouse.Services.EVCharging/SmartHouse.Services.EVCharging.csproj @@ -8,10 +8,14 @@ - + + + + + diff --git a/SmartHouse.Services/SmartHouse.Services.Helper/PerfLog.cs b/SmartHouse.Services/SmartHouse.Services.Helper/PerfLog.cs index 1ea6b71..516a5dc 100644 --- a/SmartHouse.Services/SmartHouse.Services.Helper/PerfLog.cs +++ b/SmartHouse.Services/SmartHouse.Services.Helper/PerfLog.cs @@ -1,10 +1,6 @@ using Microsoft.Extensions.Logging; +using 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 { diff --git a/SmartHouse.Services/SmartHouse.Services.Siemens/Program.cs b/SmartHouse.Services/SmartHouse.Services.Siemens/Program.cs index 8d19ec1..a49f261 100644 --- a/SmartHouse.Services/SmartHouse.Services.Siemens/Program.cs +++ b/SmartHouse.Services/SmartHouse.Services.Siemens/Program.cs @@ -3,6 +3,7 @@ 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) diff --git a/SmartHouse.Services/SmartHouse.Services.SunCast/Program.cs b/SmartHouse.Services/SmartHouse.Services.SunCast/Program.cs index 316cac8..cceb3d5 100644 --- a/SmartHouse.Services/SmartHouse.Services.SunCast/Program.cs +++ b/SmartHouse.Services/SmartHouse.Services.SunCast/Program.cs @@ -1,9 +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(); +builder.Services.AddDbContext(options => + options.UseSqlServer(builder.Configuration.GetConnectionString("SmartHouseDatabase"))); var host = builder.Build(); host.Run(); \ No newline at end of file diff --git a/SmartHouse.Services/SmartHouse.Services.SunCast/SmartHouse.Services.SunCast.csproj b/SmartHouse.Services/SmartHouse.Services.SunCast/SmartHouse.Services.SunCast.csproj index c9a5e3c..44a3a21 100644 --- a/SmartHouse.Services/SmartHouse.Services.SunCast/SmartHouse.Services.SunCast.csproj +++ b/SmartHouse.Services/SmartHouse.Services.SunCast/SmartHouse.Services.SunCast.csproj @@ -8,10 +8,17 @@ - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + diff --git a/SmartHouse.Services/SmartHouse.Services.SunCast/appsettings.Development.json b/SmartHouse.Services/SmartHouse.Services.SunCast/appsettings.Development.json index b2dcdb6..8764a88 100644 --- a/SmartHouse.Services/SmartHouse.Services.SunCast/appsettings.Development.json +++ b/SmartHouse.Services/SmartHouse.Services.SunCast/appsettings.Development.json @@ -4,5 +4,8 @@ "Default": "Information", "Microsoft.Hosting.Lifetime": "Information" } + }, + "ConnectionStrings": { + "SmartHouse": "Data Source=localhost;Initial Catalog=SmartHouse;Integrated Security=True" } } diff --git a/SmartHouse.Services/SmartHouse.Services.sln b/SmartHouse.Services/SmartHouse.Services.sln index 004dcf4..9cde03a 100644 --- a/SmartHouse.Services/SmartHouse.Services.sln +++ b/SmartHouse.Services/SmartHouse.Services.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 18 -VisualStudioVersion = 18.1.11312.151 d18.0 +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 @@ -19,6 +19,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SmartHouse.Services.Service 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 @@ -57,6 +59,10 @@ Global {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 diff --git a/SmartHouse.Services/appsettings.json b/SmartHouse.Services/appsettings.json new file mode 100644 index 0000000..e56bb36 --- /dev/null +++ b/SmartHouse.Services/appsettings.json @@ -0,0 +1,5 @@ +{ + "ConnectionStrings": { + "SmartHouse": "Data Source=localhost;Initial Catalog=SmartHouse;Integrated Security=True" + } +} \ No newline at end of file