Skip to content

Commit

Permalink
Merge pull request #5 from timia2109/feature/restructure
Browse files Browse the repository at this point in the history
Feature/restructure
  • Loading branch information
timia2109 authored Apr 3, 2024
2 parents 4f4a2b7 + 6e19986 commit 01a23e3
Show file tree
Hide file tree
Showing 29 changed files with 150 additions and 247 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using SkiaSharp;

namespace DisplayUtil.MqttExport;
namespace DisplayUtil.EspUtilities;

/// <summary>
/// Responsible to render the Bitmap to the two color stream
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using DisplayUtil.Scenes;
using SkiaSharp;

namespace DisplayUtil.MqttExport;
namespace DisplayUtil.EspUtilities;

/// <summary>
/// Responsible to provide the images in a form for the ESP.
Expand Down
13 changes: 13 additions & 0 deletions EspUtilities/EspUtilitiesInitExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace DisplayUtil.EspUtilities;

public static class EspUtilitiesInitExtension
{

public static IHostApplicationBuilder AddEspUtilities(this IHostApplicationBuilder builder)
{
builder.Services.AddScoped<EspImageProvider>();
return builder;
}


}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System.IO.Pipelines;

namespace DisplayUtil.MqttExport;


namespace DisplayUtil.EspUtilities;

/// <summary>
/// Compresses the stream
Expand Down
18 changes: 11 additions & 7 deletions Utils/HassExtensions.cs → HomeAssistant/HassExtensions.cs
Original file line number Diff line number Diff line change
@@ -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<HomeAssistantSettings>(
builder.Configuration.GetSection("HomeAssistant")
var settings = builder.ConfigureAndGet<HomeAssistantSettings>(
"HomeAssistant"
);

builder.Services.AddHomeAssistantClient();
if (settings is null
|| settings.Host is null
) return builder;

builder.Services
.AddHomeAssistantClient()
.AddScoped<HassTemplateExtender>();

// Hack: Initialize Hass Model
var extensionType = typeof(DependencyInjectionSetup);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using NetDaemon.Client.Settings;
using NetDaemon.HassModel;

namespace DisplayUtil.Utils;
namespace DisplayUtil.HomeAssistant;

internal class HassHostedService(
IOptions<HomeAssistantSettings> options,
Expand Down
46 changes: 46 additions & 0 deletions HomeAssistant/HassTemplateExtender.cs
Original file line number Diff line number Diff line change
@@ -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<string, object?>;
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);
}


}
1 change: 0 additions & 1 deletion Layouting/ElementCollection.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.ComponentModel;
using DisplayUtil.Serializing.Models;
using SkiaSharp;

namespace DisplayUtil.Layouting;
Expand Down
3 changes: 2 additions & 1 deletion MqttExport/MqttExporter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using DisplayUtil.EspUtilities;
using DisplayUtil.Scenes;
using MQTTnet.Client;

Expand All @@ -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);
}
}
3 changes: 1 addition & 2 deletions MqttExport/MqttInitExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ public static IHostApplicationBuilder AddMqttWriter(this IHostApplicationBuilder
builder.Services.Configure<MqttSettings>(
builder.Configuration.GetSection("Mqtt"));

builder.Services.AddScoped<EspImageProvider>()
.AddScoped<MqttExporter>();
builder.Services.AddScoped<MqttExporter>();

if (!CreateMqttClient(builder))
{
Expand Down
13 changes: 7 additions & 6 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System.Text;
using DisplayUtil;
using DisplayUtil.EspUtilities;
using DisplayUtil.HomeAssistant;
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);
Expand All @@ -13,8 +15,10 @@
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.AddHassSupport()
.AddMqttWriter();
builder
.AddHassSupport()
.AddMqttWriter()
.AddEspUtilities();

builder.Services.AddSingleton(FontProvider.Create())
.AddSingleton<XmlLayoutDeserializer>()
Expand All @@ -26,9 +30,6 @@
builder.Services.AddTransient<FaIconDrawer>();

builder.Services.AddScreenProvider(o => o
.AddSingleton<TestProvider>("test")
.AddSingleton<TestLayoutProvider>("layout")
.AddSingleton<TestFontSizeProvider>("testFont")
.AddScribanFiles()
);

Expand Down
28 changes: 28 additions & 0 deletions Template/ITemplateExtender.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Scriban;
using Scriban.Runtime;

namespace DisplayUtil.Template;

/// <summary>
/// Enum for the Scope of the Template
/// </summary>
public enum EnrichScope
{
/// <summary>
/// This template will used for ScreenRendering
/// </summary>
ScreenRendering
}

/// <summary>
/// Extends the <see cref="TemplateContext"/>
/// </summary>
public interface ITemplateExtender
{
/// <summary>
/// Enriches the Template
/// </summary>
/// <param name="scriptObject">The used Context</param>
/// <param name="scope">Scope of the enrichment</param>
void Enrich(ScriptObject scriptObject, EnrichScope scope);
}
2 changes: 1 addition & 1 deletion Template/ScribanScreenProvider.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
50 changes: 9 additions & 41 deletions Template/TemplateContextProvider.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
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;

/// <summary>
/// Provides the default template objects.
/// Scoped
/// </summary>
public class TemplateContextProvider(IHaContext haContext, TemplateLoader templateLoader)
public class TemplateContextProvider(
IEnumerable<ITemplateExtender> 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
{
Expand All @@ -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<string, object?>;
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);
}

}
32 changes: 0 additions & 32 deletions TestFontSizeProvider.cs

This file was deleted.

Loading

0 comments on commit 01a23e3

Please sign in to comment.