-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from timia2109/feature/mqtt-url-export
Feature/mqtt url export
- Loading branch information
Showing
11 changed files
with
259 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,44 @@ | ||
using System.Text; | ||
|
||
namespace DisplayUtil.EspUtilities; | ||
|
||
public static class EspUtilitiesInitExtension | ||
{ | ||
public const string CompressedImageRoute = "/esp/{providerId}", | ||
PlainImageRoute = "/esp/bits/{providerId}"; | ||
|
||
public static IHostApplicationBuilder AddEspUtilities(this IHostApplicationBuilder builder) | ||
{ | ||
builder.Services.AddScoped<EspImageProvider>(); | ||
return builder; | ||
} | ||
|
||
public static WebApplication UseEspUtilities(this WebApplication app) | ||
{ | ||
app.MapGet(CompressedImageRoute, async (string providerId, HttpContext ctx, EspImageProvider espProvider) => | ||
{ | ||
var (data, size) = await espProvider.GetAsRunLengthAsync(providerId); | ||
var base64 = Convert.ToBase64String(data); | ||
ctx.Response.Headers.Append("X-Width", size.Width.ToString()); | ||
ctx.Response.Headers.Append("X-Height", size.Height.ToString()); | ||
return Results.Text(base64, "text/plain", Encoding.ASCII); | ||
}) | ||
.WithName("Get ESP Image") | ||
.WithOpenApi(); | ||
|
||
app.MapGet(PlainImageRoute, async (string providerId, HttpContext ctx, EspImageProvider espProvider) => | ||
{ | ||
var (data, size) = await espProvider.GetAsPlainBytesAsync(providerId); | ||
var base64 = Convert.ToBase64String(data); | ||
ctx.Response.Headers.Append("X-Width", size.Width.ToString()); | ||
ctx.Response.Headers.Append("X-Height", size.Height.ToString()); | ||
return Results.Text(base64, "text/plain", Encoding.ASCII); | ||
}) | ||
.WithName("Get ESP Bit Image") | ||
.WithOpenApi(); | ||
|
||
return app; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace DisplayUtil.MqttExport; | ||
|
||
public class MqttExportJob( | ||
IServiceScopeFactory scopeFactory, | ||
IOptions<MqttSettings> optionsSnapshot | ||
) : IHostedService | ||
{ | ||
private CancellationTokenSource? _cancellationTokenSource; | ||
|
||
private async Task RunAsync(CancellationToken cancellation) | ||
{ | ||
while (!cancellation.IsCancellationRequested) | ||
{ | ||
await using (var scope = scopeFactory.CreateAsyncScope()) | ||
{ | ||
var renderer = scope.ServiceProvider | ||
.GetRequiredService<MqttUrlRenderer>(); | ||
await renderer.RenderUrlAndPublish(); | ||
} | ||
|
||
await Task.Delay(optionsSnapshot.Value.RefreshInterval!.Value, | ||
cancellation); | ||
} | ||
} | ||
|
||
public Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
_cancellationTokenSource = new CancellationTokenSource(); | ||
_ = RunAsync(_cancellationTokenSource.Token); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
_cancellationTokenSource?.Cancel(); | ||
return Task.CompletedTask; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,47 @@ | ||
namespace DisplayUtil.MqttExport; | ||
|
||
/// <summary> | ||
/// Settings for the MQTT Handling | ||
/// </summary> | ||
public record MqttSettings | ||
{ | ||
/// <summary> | ||
/// URI of MQTT Server | ||
/// </summary> | ||
public string? Uri { get; init; } | ||
|
||
/// <summary> | ||
/// User | ||
/// </summary> | ||
public string? User { get; init; } | ||
|
||
/// <summary> | ||
/// Password | ||
/// </summary> | ||
public string? Password { get; init; } | ||
|
||
/// <summary> | ||
/// MQTT Topic | ||
/// </summary> | ||
public string? Topic { get; init; } | ||
|
||
/// <summary> | ||
/// Template used, to detect the MQTT Template | ||
/// </summary> | ||
public string? ScreenDetectTemplate { get; init; } | ||
|
||
/// <summary> | ||
/// Interval for rerendering the Template | ||
/// </summary> | ||
public TimeSpan? RefreshInterval { get; init; } | ||
|
||
/// <summary> | ||
/// Should the message only be updated, when the value changes? | ||
/// </summary> | ||
public bool IncrementalUpdate { get; init; } | ||
|
||
/// <summary> | ||
/// Hostname of the current server | ||
/// </summary> | ||
public string? ServerHostName { get; init; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using DisplayUtil.Template; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace DisplayUtil.MqttExport; | ||
|
||
public class MqttUrlRenderer( | ||
TemplateRenderer renderer, | ||
MqttExporter exporter, | ||
IOptionsSnapshot<MqttSettings> options | ||
) | ||
{ | ||
|
||
/// <summary> | ||
/// Exports the Uri to MQTT | ||
/// </summary> | ||
/// <returns>Task</returns> | ||
public async Task RenderUrlAndPublish() | ||
{ | ||
var template = options.Value.ScreenDetectTemplate!; | ||
|
||
var result = await renderer.RenderAsync(template, | ||
EnrichScope.TemplateProvider); | ||
|
||
await exporter.ExportUriToMqtt(result); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.