From 29821d358224acf90e6f2ea63261c3690437427a Mon Sep 17 00:00:00 2001 From: Tim Ittermann Date: Wed, 3 Apr 2024 13:19:53 +0200 Subject: [PATCH 1/8] Move EspUtilities to own namespace --- .../BinaryImageStreamCreater.cs | 2 +- {MqttExport => EspUtilities}/EspImageProvider.cs | 2 +- EspUtilities/EspUtilitiesInitExtension.cs | 13 +++++++++++++ {MqttExport => EspUtilities}/RunLengthCompressor.cs | 4 +--- MqttExport/MqttExporter.cs | 3 ++- MqttExport/MqttInitExtension.cs | 3 +-- Program.cs | 4 +++- 7 files changed, 22 insertions(+), 9 deletions(-) rename {MqttExport => EspUtilities}/BinaryImageStreamCreater.cs (98%) rename {MqttExport => EspUtilities}/EspImageProvider.cs (98%) create mode 100644 EspUtilities/EspUtilitiesInitExtension.cs rename {MqttExport => EspUtilities}/RunLengthCompressor.cs (98%) diff --git a/MqttExport/BinaryImageStreamCreater.cs b/EspUtilities/BinaryImageStreamCreater.cs similarity index 98% rename from MqttExport/BinaryImageStreamCreater.cs rename to EspUtilities/BinaryImageStreamCreater.cs index d9a3cae..7d9c1ec 100644 --- a/MqttExport/BinaryImageStreamCreater.cs +++ b/EspUtilities/BinaryImageStreamCreater.cs @@ -1,6 +1,6 @@ using SkiaSharp; -namespace DisplayUtil.MqttExport; +namespace DisplayUtil.EspUtilities; /// /// Responsible to render the Bitmap to the two color stream diff --git a/MqttExport/EspImageProvider.cs b/EspUtilities/EspImageProvider.cs similarity index 98% rename from MqttExport/EspImageProvider.cs rename to EspUtilities/EspImageProvider.cs index 81e427c..ea01479 100644 --- a/MqttExport/EspImageProvider.cs +++ b/EspUtilities/EspImageProvider.cs @@ -2,7 +2,7 @@ using DisplayUtil.Scenes; using SkiaSharp; -namespace DisplayUtil.MqttExport; +namespace DisplayUtil.EspUtilities; /// /// Responsible to provide the images in a form for the ESP. diff --git a/EspUtilities/EspUtilitiesInitExtension.cs b/EspUtilities/EspUtilitiesInitExtension.cs new file mode 100644 index 0000000..c9fab62 --- /dev/null +++ b/EspUtilities/EspUtilitiesInitExtension.cs @@ -0,0 +1,13 @@ +namespace DisplayUtil.EspUtilities; + +public static class EspUtilitiesInitExtension +{ + + public static IHostApplicationBuilder AddEspUtilities(this IHostApplicationBuilder builder) + { + builder.Services.AddScoped(); + return builder; + } + + +} \ No newline at end of file diff --git a/MqttExport/RunLengthCompressor.cs b/EspUtilities/RunLengthCompressor.cs similarity index 98% rename from MqttExport/RunLengthCompressor.cs rename to EspUtilities/RunLengthCompressor.cs index 1f882e7..fa9c91a 100644 --- a/MqttExport/RunLengthCompressor.cs +++ b/EspUtilities/RunLengthCompressor.cs @@ -1,8 +1,6 @@ using System.IO.Pipelines; -namespace DisplayUtil.MqttExport; - - +namespace DisplayUtil.EspUtilities; /// /// Compresses the stream diff --git a/MqttExport/MqttExporter.cs b/MqttExport/MqttExporter.cs index aa5e8a3..58dc21c 100644 --- a/MqttExport/MqttExporter.cs +++ b/MqttExport/MqttExporter.cs @@ -1,4 +1,5 @@ using System.Diagnostics; +using DisplayUtil.EspUtilities; using DisplayUtil.Scenes; using MQTTnet.Client; @@ -15,7 +16,7 @@ ExportingMqttClient exportingMqttClient { public async Task ExportScreenToMqtt(string providerId) { - var (data, size) = await espImageProvider.GetAsRunLengthAsync(providerId); + var (data, _) = await espImageProvider.GetAsRunLengthAsync(providerId); await exportingMqttClient.SendAsync(data); } } \ No newline at end of file diff --git a/MqttExport/MqttInitExtension.cs b/MqttExport/MqttInitExtension.cs index 669eb07..06c5e19 100644 --- a/MqttExport/MqttInitExtension.cs +++ b/MqttExport/MqttInitExtension.cs @@ -11,8 +11,7 @@ public static IHostApplicationBuilder AddMqttWriter(this IHostApplicationBuilder builder.Services.Configure( builder.Configuration.GetSection("Mqtt")); - builder.Services.AddScoped() - .AddScoped(); + builder.Services.AddScoped(); if (!CreateMqttClient(builder)) { diff --git a/Program.cs b/Program.cs index 53771fb..9a04cab 100644 --- a/Program.cs +++ b/Program.cs @@ -1,5 +1,6 @@ using System.Text; using DisplayUtil; +using DisplayUtil.EspUtilities; using DisplayUtil.MqttExport; using DisplayUtil.Scenes; using DisplayUtil.Serializing; @@ -14,7 +15,8 @@ builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); builder.AddHassSupport() - .AddMqttWriter(); + .AddMqttWriter() + .AddEspUtilities(); builder.Services.AddSingleton(FontProvider.Create()) .AddSingleton() From 5c4b224f00ab92a50bda35a0e6f6d1175ebb8cb3 Mon Sep 17 00:00:00 2001 From: Tim Ittermann Date: Wed, 3 Apr 2024 13:24:09 +0200 Subject: [PATCH 2/8] Move to XmlModel --- Program.cs | 2 +- Template/ScribanScreenProvider.cs | 2 +- {Serializing => XmlModel}/DefaultDefinition.cs | 2 +- {Serializing => XmlModel}/Models/Flexbox.cs | 2 +- {Serializing => XmlModel}/Models/HBox.cs | 2 +- {Serializing => XmlModel}/Models/ICollectionXmlModel.cs | 2 +- {Serializing => XmlModel}/Models/IXmlModel.cs | 2 +- {Serializing => XmlModel}/Models/Icon.cs | 2 +- {Serializing => XmlModel}/Models/Screen.cs | 2 +- {Serializing => XmlModel}/Models/Text.cs | 2 +- {Serializing => XmlModel}/Models/VBox.cs | 2 +- {Serializing => XmlModel}/SerializingResult.cs | 2 +- {Serializing => XmlModel}/XmlLayoutDeserializer.cs | 2 +- 13 files changed, 13 insertions(+), 13 deletions(-) rename {Serializing => XmlModel}/DefaultDefinition.cs (97%) rename {Serializing => XmlModel}/Models/Flexbox.cs (94%) rename {Serializing => XmlModel}/Models/HBox.cs (92%) rename {Serializing => XmlModel}/Models/ICollectionXmlModel.cs (97%) rename {Serializing => XmlModel}/Models/IXmlModel.cs (97%) rename {Serializing => XmlModel}/Models/Icon.cs (92%) rename {Serializing => XmlModel}/Models/Screen.cs (93%) rename {Serializing => XmlModel}/Models/Text.cs (95%) rename {Serializing => XmlModel}/Models/VBox.cs (92%) rename {Serializing => XmlModel}/SerializingResult.cs (85%) rename {Serializing => XmlModel}/XmlLayoutDeserializer.cs (97%) diff --git a/Program.cs b/Program.cs index 9a04cab..ec87dea 100644 --- a/Program.cs +++ b/Program.cs @@ -3,9 +3,9 @@ using DisplayUtil.EspUtilities; using DisplayUtil.MqttExport; using DisplayUtil.Scenes; -using DisplayUtil.Serializing; using DisplayUtil.Template; using DisplayUtil.Utils; +using DisplayUtil.XmlModel; var builder = WebApplication.CreateBuilder(args); builder.Configuration.AddJsonFile("appsettings.Local.json", true); diff --git a/Template/ScribanScreenProvider.cs b/Template/ScribanScreenProvider.cs index 50e796d..4cd9195 100644 --- a/Template/ScribanScreenProvider.cs +++ b/Template/ScribanScreenProvider.cs @@ -1,7 +1,7 @@ using DisplayUtil.Layouting; using DisplayUtil.Scenes; -using DisplayUtil.Serializing; using DisplayUtil.Utils; +using DisplayUtil.XmlModel; using Scriban.Parsing; using Scriban.Runtime; using SkiaSharp; diff --git a/Serializing/DefaultDefinition.cs b/XmlModel/DefaultDefinition.cs similarity index 97% rename from Serializing/DefaultDefinition.cs rename to XmlModel/DefaultDefinition.cs index f6e7ce2..20536b9 100644 --- a/Serializing/DefaultDefinition.cs +++ b/XmlModel/DefaultDefinition.cs @@ -1,6 +1,6 @@ using System.Xml.Serialization; -namespace DisplayUtil.Serializing; +namespace DisplayUtil.XmlModel; /// /// Setting defaults for the child elements diff --git a/Serializing/Models/Flexbox.cs b/XmlModel/Models/Flexbox.cs similarity index 94% rename from Serializing/Models/Flexbox.cs rename to XmlModel/Models/Flexbox.cs index 72add96..67b3c4a 100644 --- a/Serializing/Models/Flexbox.cs +++ b/XmlModel/Models/Flexbox.cs @@ -2,7 +2,7 @@ using DisplayUtil.Layouting; using DisplayUtil.Utils; -namespace DisplayUtil.Serializing.Models; +namespace DisplayUtil.XmlModel.Models; public class Flexbox : ICollectionXmlModel { diff --git a/Serializing/Models/HBox.cs b/XmlModel/Models/HBox.cs similarity index 92% rename from Serializing/Models/HBox.cs rename to XmlModel/Models/HBox.cs index a55ab4e..9b8bdc6 100644 --- a/Serializing/Models/HBox.cs +++ b/XmlModel/Models/HBox.cs @@ -4,7 +4,7 @@ using DisplayUtil.Utils; using SkiaSharp; -namespace DisplayUtil.Serializing.Models; +namespace DisplayUtil.XmlModel.Models; public class HBox : ICollectionXmlModel { diff --git a/Serializing/Models/ICollectionXmlModel.cs b/XmlModel/Models/ICollectionXmlModel.cs similarity index 97% rename from Serializing/Models/ICollectionXmlModel.cs rename to XmlModel/Models/ICollectionXmlModel.cs index 94be41f..df7ae66 100644 --- a/Serializing/Models/ICollectionXmlModel.cs +++ b/XmlModel/Models/ICollectionXmlModel.cs @@ -2,7 +2,7 @@ using DisplayUtil.Layouting; using DisplayUtil.Utils; -namespace DisplayUtil.Serializing.Models; +namespace DisplayUtil.XmlModel.Models; public record XmlSiteSize { diff --git a/Serializing/Models/IXmlModel.cs b/XmlModel/Models/IXmlModel.cs similarity index 97% rename from Serializing/Models/IXmlModel.cs rename to XmlModel/Models/IXmlModel.cs index e7ea9a6..486f5a0 100644 --- a/Serializing/Models/IXmlModel.cs +++ b/XmlModel/Models/IXmlModel.cs @@ -2,7 +2,7 @@ using DisplayUtil.Layouting; using DisplayUtil.Utils; -namespace DisplayUtil.Serializing.Models; +namespace DisplayUtil.XmlModel.Models; public abstract class IXmlModel { diff --git a/Serializing/Models/Icon.cs b/XmlModel/Models/Icon.cs similarity index 92% rename from Serializing/Models/Icon.cs rename to XmlModel/Models/Icon.cs index 9ac9cb2..a54c218 100644 --- a/Serializing/Models/Icon.cs +++ b/XmlModel/Models/Icon.cs @@ -4,7 +4,7 @@ using DisplayUtil.Layouting; using DisplayUtil.Utils; -namespace DisplayUtil.Serializing.Models; +namespace DisplayUtil.XmlModel.Models; public class Icon : IXmlModel { diff --git a/Serializing/Models/Screen.cs b/XmlModel/Models/Screen.cs similarity index 93% rename from Serializing/Models/Screen.cs rename to XmlModel/Models/Screen.cs index 7bb372f..ca1aff3 100644 --- a/Serializing/Models/Screen.cs +++ b/XmlModel/Models/Screen.cs @@ -2,7 +2,7 @@ using DisplayUtil.Layouting; using DisplayUtil.Utils; -namespace DisplayUtil.Serializing.Models; +namespace DisplayUtil.XmlModel.Models; public class Screen : IXmlModel { diff --git a/Serializing/Models/Text.cs b/XmlModel/Models/Text.cs similarity index 95% rename from Serializing/Models/Text.cs rename to XmlModel/Models/Text.cs index fde7b5f..08318b9 100644 --- a/Serializing/Models/Text.cs +++ b/XmlModel/Models/Text.cs @@ -4,7 +4,7 @@ using DisplayUtil.Utils; using SkiaSharp; -namespace DisplayUtil.Serializing.Models; +namespace DisplayUtil.XmlModel.Models; [XmlType(nameof(Text))] public class Text : IXmlModel diff --git a/Serializing/Models/VBox.cs b/XmlModel/Models/VBox.cs similarity index 92% rename from Serializing/Models/VBox.cs rename to XmlModel/Models/VBox.cs index 559799b..b4a9f46 100644 --- a/Serializing/Models/VBox.cs +++ b/XmlModel/Models/VBox.cs @@ -4,7 +4,7 @@ using DisplayUtil.Utils; using SkiaSharp; -namespace DisplayUtil.Serializing.Models; +namespace DisplayUtil.XmlModel.Models; [XmlType(nameof(VBox))] public class VBox : ICollectionXmlModel diff --git a/Serializing/SerializingResult.cs b/XmlModel/SerializingResult.cs similarity index 85% rename from Serializing/SerializingResult.cs rename to XmlModel/SerializingResult.cs index 6a5bbd2..e1b255c 100644 --- a/Serializing/SerializingResult.cs +++ b/XmlModel/SerializingResult.cs @@ -1,7 +1,7 @@ using DisplayUtil.Layouting; using SkiaSharp; -namespace DisplayUtil.Serializing; +namespace DisplayUtil.XmlModel; public record SerializingResult( Element Element, diff --git a/Serializing/XmlLayoutDeserializer.cs b/XmlModel/XmlLayoutDeserializer.cs similarity index 97% rename from Serializing/XmlLayoutDeserializer.cs rename to XmlModel/XmlLayoutDeserializer.cs index 3c2792e..ff1ca92 100644 --- a/Serializing/XmlLayoutDeserializer.cs +++ b/XmlModel/XmlLayoutDeserializer.cs @@ -4,7 +4,7 @@ using DisplayUtil.Utils; using SkiaSharp; -namespace DisplayUtil.Serializing; +namespace DisplayUtil.XmlModel; public class XmlLayoutDeserializer { From e82faf7072af6f88219513f634c0a259f0c2633f Mon Sep 17 00:00:00 2001 From: Tim Ittermann Date: Wed, 3 Apr 2024 13:47:50 +0200 Subject: [PATCH 3/8] Continue namespace moving --- Layouting/ElementCollection.cs | 1 - XmlModel/XmlLayoutDeserializer.cs | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/Layouting/ElementCollection.cs b/Layouting/ElementCollection.cs index 573f45a..b2790e4 100644 --- a/Layouting/ElementCollection.cs +++ b/Layouting/ElementCollection.cs @@ -1,5 +1,4 @@ using System.ComponentModel; -using DisplayUtil.Serializing.Models; using SkiaSharp; namespace DisplayUtil.Layouting; diff --git a/XmlModel/XmlLayoutDeserializer.cs b/XmlModel/XmlLayoutDeserializer.cs index ff1ca92..ebf7f72 100644 --- a/XmlModel/XmlLayoutDeserializer.cs +++ b/XmlModel/XmlLayoutDeserializer.cs @@ -1,7 +1,7 @@ using System.Xml.Serialization; using DisplayUtil.Layouting; -using DisplayUtil.Serializing.Models; using DisplayUtil.Utils; +using DisplayUtil.XmlModel.Models; using SkiaSharp; namespace DisplayUtil.XmlModel; From cb6bdc44ac8ffaa013f3b433ae92485d17a371ab Mon Sep 17 00:00:00 2001 From: Tim Ittermann Date: Wed, 3 Apr 2024 13:48:24 +0200 Subject: [PATCH 4/8] Exclude models as Template extension --- Template/ITemplateExtender.cs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Template/ITemplateExtender.cs diff --git a/Template/ITemplateExtender.cs b/Template/ITemplateExtender.cs new file mode 100644 index 0000000..c76b0e1 --- /dev/null +++ b/Template/ITemplateExtender.cs @@ -0,0 +1,28 @@ +using Scriban; +using Scriban.Runtime; + +namespace DisplayUtil.Template; + +/// +/// Enum for the Scope of the Template +/// +public enum EnrichScope +{ + /// + /// This template will used for ScreenRendering + /// + ScreenRendering +} + +/// +/// Extends the +/// +public interface ITemplateExtender +{ + /// + /// Enriches the Template + /// + /// The used Context + /// Scope of the enrichment + void Enrich(ScriptObject scriptObject, EnrichScope scope); +} \ No newline at end of file From 6940d1931d1296289080ae9f79760057fc692da9 Mon Sep 17 00:00:00 2001 From: Tim Ittermann Date: Wed, 3 Apr 2024 13:48:36 +0200 Subject: [PATCH 5/8] use new extender Pattern --- Template/TemplateContextProvider.cs | 50 ++++++----------------------- 1 file changed, 9 insertions(+), 41 deletions(-) diff --git a/Template/TemplateContextProvider.cs b/Template/TemplateContextProvider.cs index feb0f0d..f46d010 100644 --- a/Template/TemplateContextProvider.cs +++ b/Template/TemplateContextProvider.cs @@ -1,12 +1,6 @@ -using System.Dynamic; using System.Globalization; -using System.Reflection; -using System.Security.Cryptography; -using System.Text.Json; -using NetDaemon.HassModel; using Scriban; using Scriban.Runtime; -using Swashbuckle.AspNetCore.SwaggerGen; namespace DisplayUtil.Template; @@ -14,19 +8,19 @@ namespace DisplayUtil.Template; /// Provides the default template objects. /// Scoped /// -public class TemplateContextProvider(IHaContext haContext, TemplateLoader templateLoader) +public class TemplateContextProvider( + IEnumerable extenders, + TemplateLoader templateLoader) { - public TemplateContext GetTemplateContext() + public TemplateContext GetTemplateContext(EnrichScope scope) { var scriptObject = new ScriptObject(); scriptObject.Import("to_float", ToFloat); - // Hass Functions - var hassObject = new ScriptObject(); - hassObject.Import("get_state", GetState); - hassObject.Import("get_attribute", GetAttribute); - hassObject.Import("get_float_state", GetFloatState); - scriptObject.Add("hass", hassObject); + foreach (var extender in extenders) + { + extender.Enrich(scriptObject, scope); + } var context = new TemplateContext { @@ -39,35 +33,9 @@ public TemplateContext GetTemplateContext() return context; } - private string? GetState(string entityId) - { - var entity = haContext.GetState(entityId); - return entity?.State; - } - - private string? GetAttribute(string entityId, string attribute) - { - var entity = haContext.GetState(entityId); - var attributes = entity?.Attributes as Dictionary; - object? value = null; - - if (!attributes?.TryGetValue(attribute, out value) ?? true) - return null; - - return value?.ToString(); - } - - private float GetFloatState(string entityId) - { - var state = GetState(entityId); - if (state == null) return 0f; - return ToFloat(state); - } - - private float ToFloat(string? content) + public static float ToFloat(string? content) { if (content == null) return 0; return float.Parse(content, CultureInfo.InvariantCulture); } - } From e577e1a987b3ab0591a56c49244010b6be9fadf4 Mon Sep 17 00:00:00 2001 From: Tim Ittermann Date: Wed, 3 Apr 2024 13:48:50 +0200 Subject: [PATCH 6/8] Move HomeAssistant to custom NS --- {Utils => HomeAssistant}/HassExtensions.cs | 18 +++++--- {Utils => HomeAssistant}/HassHostedService.cs | 2 +- HomeAssistant/HassTemplateExtender.cs | 46 +++++++++++++++++++ 3 files changed, 58 insertions(+), 8 deletions(-) rename {Utils => HomeAssistant}/HassExtensions.cs (68%) rename {Utils => HomeAssistant}/HassHostedService.cs (97%) create mode 100644 HomeAssistant/HassTemplateExtender.cs diff --git a/Utils/HassExtensions.cs b/HomeAssistant/HassExtensions.cs similarity index 68% rename from Utils/HassExtensions.cs rename to HomeAssistant/HassExtensions.cs index cf04c31..80f4b6c 100644 --- a/Utils/HassExtensions.cs +++ b/HomeAssistant/HassExtensions.cs @@ -1,21 +1,25 @@ -using Microsoft.Extensions.Options; -using NetDaemon.Client; +using DisplayUtil.Utils; using NetDaemon.Client.Extensions; using NetDaemon.Client.Settings; using NetDaemon.HassModel; -namespace DisplayUtil.Utils; +namespace DisplayUtil.HomeAssistant; public static class HassExtension { public static IHostApplicationBuilder AddHassSupport(this IHostApplicationBuilder builder) { - - builder.Services.Configure( - builder.Configuration.GetSection("HomeAssistant") + var settings = builder.ConfigureAndGet( + "HomeAssistant" ); - builder.Services.AddHomeAssistantClient(); + if (settings is null + || settings.Host is null + ) return builder; + + builder.Services + .AddHomeAssistantClient() + .AddScoped(); // Hack: Initialize Hass Model var extensionType = typeof(DependencyInjectionSetup); diff --git a/Utils/HassHostedService.cs b/HomeAssistant/HassHostedService.cs similarity index 97% rename from Utils/HassHostedService.cs rename to HomeAssistant/HassHostedService.cs index 9a8b871..a11cfe6 100644 --- a/Utils/HassHostedService.cs +++ b/HomeAssistant/HassHostedService.cs @@ -4,7 +4,7 @@ using NetDaemon.Client.Settings; using NetDaemon.HassModel; -namespace DisplayUtil.Utils; +namespace DisplayUtil.HomeAssistant; internal class HassHostedService( IOptions options, diff --git a/HomeAssistant/HassTemplateExtender.cs b/HomeAssistant/HassTemplateExtender.cs new file mode 100644 index 0000000..0c1027b --- /dev/null +++ b/HomeAssistant/HassTemplateExtender.cs @@ -0,0 +1,46 @@ +using DisplayUtil.Template; +using NetDaemon.HassModel; +using Scriban.Runtime; + +namespace DisplayUtil.HomeAssistant; + +internal class HassTemplateExtender(IHaContext haContext) +: ITemplateExtender +{ + public void Enrich(ScriptObject context, EnrichScope scope) + { + // Hass Functions + var hassObject = new ScriptObject(); + hassObject.Import("get_state", GetState); + hassObject.Import("get_attribute", GetAttribute); + hassObject.Import("get_float_state", GetFloatState); + context.Add("hass", hassObject); + } + + private string? GetState(string entityId) + { + var entity = haContext.GetState(entityId); + return entity?.State; + } + + private string? GetAttribute(string entityId, string attribute) + { + var entity = haContext.GetState(entityId); + var attributes = entity?.Attributes as Dictionary; + object? value = null; + + if (!attributes?.TryGetValue(attribute, out value) ?? true) + return null; + + return value?.ToString(); + } + + private float GetFloatState(string entityId) + { + var state = GetState(entityId); + if (state == null) return 0f; + return TemplateContextProvider.ToFloat(state); + } + + +} \ No newline at end of file From db41ff3bb1b9ac09db3d4326aa4b6d325647b0ae Mon Sep 17 00:00:00 2001 From: Tim Ittermann Date: Wed, 3 Apr 2024 13:49:00 +0200 Subject: [PATCH 7/8] Add Util for Configuring --- Utils/ServiceCollectionExtension.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Utils/ServiceCollectionExtension.cs diff --git a/Utils/ServiceCollectionExtension.cs b/Utils/ServiceCollectionExtension.cs new file mode 100644 index 0000000..92fcb61 --- /dev/null +++ b/Utils/ServiceCollectionExtension.cs @@ -0,0 +1,16 @@ +namespace DisplayUtil.Utils; + +public static class ServiceCollectionExtension +{ + + public static TModel? ConfigureAndGet(this IHostApplicationBuilder builder, + string sectionName) + where TModel : class + { + var section = builder.Configuration.GetSection(sectionName); + builder.Services.Configure(section); + + return section.Get(); + } + +} \ No newline at end of file From 6e19986fe0e52d5b12460454e1889ecb59b0eafe Mon Sep 17 00:00:00 2001 From: Tim Ittermann Date: Wed, 3 Apr 2024 13:49:42 +0200 Subject: [PATCH 8/8] Remove test classes --- Program.cs | 7 ++--- TestFontSizeProvider.cs | 32 ------------------- TestLayoutProvider.cs | 68 --------------------------------------- TestProvider.cs | 70 ----------------------------------------- 4 files changed, 3 insertions(+), 174 deletions(-) delete mode 100644 TestFontSizeProvider.cs delete mode 100644 TestLayoutProvider.cs delete mode 100644 TestProvider.cs diff --git a/Program.cs b/Program.cs index ec87dea..f8fcd81 100644 --- a/Program.cs +++ b/Program.cs @@ -1,6 +1,7 @@ using System.Text; using DisplayUtil; using DisplayUtil.EspUtilities; +using DisplayUtil.HomeAssistant; using DisplayUtil.MqttExport; using DisplayUtil.Scenes; using DisplayUtil.Template; @@ -14,7 +15,8 @@ // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); -builder.AddHassSupport() +builder + .AddHassSupport() .AddMqttWriter() .AddEspUtilities(); @@ -28,9 +30,6 @@ builder.Services.AddTransient(); builder.Services.AddScreenProvider(o => o - .AddSingleton("test") - .AddSingleton("layout") - .AddSingleton("testFont") .AddScribanFiles() ); diff --git a/TestFontSizeProvider.cs b/TestFontSizeProvider.cs deleted file mode 100644 index 003324e..0000000 --- a/TestFontSizeProvider.cs +++ /dev/null @@ -1,32 +0,0 @@ -using DisplayUtil.Scenes; -using DisplayUtil.Utils; -using SkiaSharp; - -namespace DisplayUtil; - -internal class TestFontSizeProvider(FaIconDrawer iconDrawer) : IScreenProvider -{ - public Task GetImageAsync() - { - var bitmap = new SKBitmap(64, 32); - var canvas = new SKCanvas(bitmap); - canvas.Clear(SKColors.White); - - var productSans = SKTypeface.FromFile("./Resources/Roboto-Medium.ttf"); - - var paint = new SKPaint - { - IsAntialias = true, - TextSize = 32, - TextAlign = SKTextAlign.Left, - Color = SKColors.Black, - Style = SKPaintStyle.Fill, - Typeface = productSans - }; - canvas.DrawText("A", 0, 32, paint); - - iconDrawer.DrawIcon("couch", 32, new SKPoint(32, 0), canvas); - - return Task.FromResult(bitmap); - } -} \ No newline at end of file diff --git a/TestLayoutProvider.cs b/TestLayoutProvider.cs deleted file mode 100644 index f5da2bb..0000000 --- a/TestLayoutProvider.cs +++ /dev/null @@ -1,68 +0,0 @@ -using System.Globalization; -using DisplayUtil.Layouting; -using DisplayUtil.Scenes; -using DisplayUtil.Utils; -using SkiaSharp; - -namespace DisplayUtil; - -internal class TestLayoutProvider(FaIconDrawer iconDrawer) : IScreenProvider -{ - public Task GetImageAsync() - { - var productSans = SKTypeface.FromFile("./Resources/fonts/ProductSansRegular.ttf"); - - var testPaint = new SKPaint - { - IsAntialias = true, - TextSize = 32, - TextAlign = SKTextAlign.Left, - Color = SKColors.Black, - Style = SKPaintStyle.Fill, - Typeface = productSans - }; - - var now = DateTime.Now; - var locale = CultureInfo.GetCultureInfo("de-DE"); - - var mainFlex = new FlexboxElement(0, FlexDirection.Horizontal, JustifyContent.Between); - - var dateBox = new VBoxElement(2); - dateBox.Append( - new HBoxElement(10) - .Append(new IconElement("calendar", 32, iconDrawer)) - .Append(new TextElement($"{now.ToString("dddd", locale)}, ", testPaint)) - ) - .Append(new TextElement(now.ToString("d", locale), testPaint)); - mainFlex.Append(dateBox); - - var clockPaint = testPaint.Clone(); - clockPaint.Typeface = SKTypeface.FromFile("./Resources/fonts/ProductSansBold.ttf"); - clockPaint.TextSize = 66; - mainFlex.Append( - new TextElement(now.ToString("t", locale), clockPaint) - ); - - return Task.FromResult( - DrawManager.Draw( - Constants.EPaperDisplaySize, - mainFlex - ) - ); - - /*var headerBox = new BorderElement( - new Padding(Bottom: 2), - new PaddingElement(new Padding(Bottom: 2), mainFlex) - ); - - return Task.FromResult( - DrawManager.Draw( - Constants.EPaperDisplaySize, - new PaddingElement( - new Padding(20), - headerBox - ) - ) - );*/ - } -} \ No newline at end of file diff --git a/TestProvider.cs b/TestProvider.cs deleted file mode 100644 index b2bab32..0000000 --- a/TestProvider.cs +++ /dev/null @@ -1,70 +0,0 @@ -using DisplayUtil.Scenes; -using DisplayUtil.Utils; -using SkiaSharp; - -namespace DisplayUtil; - -internal class TestProvider(FaIconDrawer iconDrawer) : IScreenProvider -{ - private string[] _iconsList = GetIconsList(); - - private static string[] GetIconsList() - { - var iconsDir = new DirectoryInfo("./Resources/svgs/light"); - return iconsDir.GetFiles() - .OrderByDescending(e => e.Name) - .Select(e => Path.GetFileNameWithoutExtension(e.FullName)) - .ToArray(); - } - - public Task GetImageAsync() - { - var bitmap = new SKBitmap(480, 800); - var canvas = new SKCanvas(bitmap); - canvas.Clear(SKColors.Transparent); - - // Stress Test Icons - int maxHeight = 0; - int i = 0; - SKSize? lastSize = new SKSize(0, 0); - - for (int height = 0; height < bitmap.Height; height += maxHeight + 5) - { - maxHeight = 0; - for (int width = 0; width < bitmap.Width; width += (int)lastSize.Value.Width + 5) - { - var iconName = _iconsList[i++]; - lastSize = iconDrawer.DrawIcon(iconName, 16, width, height, canvas); - maxHeight = Math.Max(maxHeight, (int)lastSize.Value.Height); - } - } - - var productSans = SKTypeface.FromFile("./Resources/ProductSansRegular.ttf"); - - var paint = new SKPaint - { - IsAntialias = true, - TextSize = 50, - TextAlign = SKTextAlign.Center, - Color = 0xFF3498DB, - Style = SKPaintStyle.Fill, - Typeface = productSans - }; - canvas.DrawText("TestImage", 128, 128 + (paint.TextSize / 2), paint); - - - paint = new SKPaint - { - TextSize = 50, - TextAlign = SKTextAlign.Center, - Color = 0xFF0000FF, - Style = SKPaintStyle.Fill, - Typeface = productSans - }; - - canvas.DrawText("TimItt", 256, 300, paint); - - - return Task.FromResult(bitmap); - } -} \ No newline at end of file