Add image generation (#2)
* Add image generation * Optimise for minimal memory * Added a new ui * Add support for esp
This commit was merged in pull request #2.
This commit is contained in:
31
HomeApi/Handlers/Configuration.cs
Normal file
31
HomeApi/Handlers/Configuration.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using HomeApi.Models;
|
||||
using HomeApi.Models.Configuration;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace HomeApi.Handlers;
|
||||
|
||||
public static class Configuration
|
||||
{
|
||||
public record Command : IRequest<MicroProcessorConfiguration>;
|
||||
|
||||
public class Handler(IOptions<ApiConfiguration> configuration)
|
||||
: IRequestHandler<Command, MicroProcessorConfiguration>
|
||||
{
|
||||
private readonly ApiConfiguration _apiConfiguration = configuration.Value;
|
||||
|
||||
public Task<MicroProcessorConfiguration> Handle(Command request, CancellationToken cancellationToken)
|
||||
{
|
||||
return Task.FromResult(new MicroProcessorConfiguration
|
||||
{
|
||||
InformationBoardImageUrl = _apiConfiguration.EspConfiguration.InformationBoardImageUrl,
|
||||
UpdateIntervalMinutes = _apiConfiguration.EspConfiguration.UpdateIntervalMinutes,
|
||||
BlackTextThreshold = _apiConfiguration.EspConfiguration.BlackTextThreshold,
|
||||
ContrastStrength = _apiConfiguration.EspConfiguration.ContrastStrength,
|
||||
DitheringStrength = _apiConfiguration.EspConfiguration.DitheringStrength,
|
||||
EnableDithering = _apiConfiguration.EspConfiguration.EnableDithering,
|
||||
EnhanceContrast = _apiConfiguration.EspConfiguration.EnhanceContrast
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
18
HomeApi/Handlers/DepartureBoard.cs
Normal file
18
HomeApi/Handlers/DepartureBoard.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using HomeApi.Integration;
|
||||
using HomeApi.Models;
|
||||
using MediatR;
|
||||
|
||||
namespace HomeApi.Handlers;
|
||||
|
||||
public static class DepartureBoard
|
||||
{
|
||||
public record Command : IRequest<List<TimeTable>>;
|
||||
|
||||
public class Handler(IDepartureBoardService departureBoardService) : IRequestHandler<Command, List<TimeTable>>
|
||||
{
|
||||
public async Task<List<TimeTable>> Handle(Command request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await departureBoardService.GetDepartureBoard() ?? new List<TimeTable>();
|
||||
}
|
||||
}
|
||||
}
|
||||
79
HomeApi/Handlers/ImageGeneration.cs
Normal file
79
HomeApi/Handlers/ImageGeneration.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using System.Dynamic;
|
||||
using System.Reflection;
|
||||
using HomeApi.Models.Configuration;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Options;
|
||||
using PuppeteerSharp;
|
||||
using RazorLight;
|
||||
|
||||
namespace HomeApi.Handlers;
|
||||
|
||||
public static class ImageGeneration
|
||||
{
|
||||
public record Command : IRequest<Stream>;
|
||||
|
||||
public class Handler(
|
||||
IWebHostEnvironment env,
|
||||
IMediator mediator,
|
||||
IOptions<ApiConfiguration> apiConfiguration)
|
||||
: IRequestHandler<Command, Stream>
|
||||
{
|
||||
private readonly ApiConfiguration _apiConfiguration = apiConfiguration.Value;
|
||||
|
||||
public async Task<Stream> Handle(Command request, CancellationToken cancellationToken)
|
||||
{
|
||||
var weather = await mediator.Send(new Weather.Command(), cancellationToken);
|
||||
var departureBoard = await mediator.Send(new DepartureBoard.Command(), cancellationToken);
|
||||
|
||||
var model = new Models.Image
|
||||
{
|
||||
Weather = weather,
|
||||
TimeTable = departureBoard
|
||||
};
|
||||
|
||||
if(weather is null)
|
||||
throw new Exception("Weather data not found");
|
||||
|
||||
var engine = new RazorLightEngineBuilder()
|
||||
.SetOperatingAssembly(Assembly.GetExecutingAssembly())
|
||||
.UseEmbeddedResourcesProject(typeof(ImageGeneration))
|
||||
.UseMemoryCachingProvider()
|
||||
.Build();
|
||||
|
||||
var path = Path.Combine(env.WebRootPath, "index.cshtml");
|
||||
|
||||
var template = await File.ReadAllTextAsync(path, cancellationToken);
|
||||
|
||||
dynamic viewBag = new ExpandoObject();
|
||||
viewBag.IsHighContrast = _apiConfiguration.EspConfiguration.IsHighContrastMode;
|
||||
|
||||
var result = await engine.CompileRenderStringAsync("templateKey", template, model, viewBag: viewBag);
|
||||
|
||||
if (!string.IsNullOrEmpty(result))
|
||||
return await CreateImage(result);
|
||||
|
||||
throw new Exception("Failed to generate HTML content for image.");
|
||||
}
|
||||
|
||||
private static async Task<Stream> CreateImage(string htmlContent)
|
||||
{
|
||||
var browserFetcher = new BrowserFetcher();
|
||||
await browserFetcher.DownloadAsync();
|
||||
var browser = await Puppeteer.LaunchAsync(new LaunchOptions
|
||||
{
|
||||
Headless = true,
|
||||
Args = ["--disable-gpu"]
|
||||
});
|
||||
|
||||
var page = await browser.NewPageAsync();
|
||||
await page.SetViewportAsync(new ViewPortOptions
|
||||
{
|
||||
Width = 800,
|
||||
Height = 480
|
||||
});
|
||||
|
||||
await page.SetContentAsync(htmlContent, new NavigationOptions { WaitUntil = [WaitUntilNavigation.Networkidle0] });
|
||||
return await page.ScreenshotStreamAsync(new ScreenshotOptions { Type = ScreenshotType.Jpeg, Quality = 60 });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ using Microsoft.Extensions.Options;
|
||||
|
||||
namespace HomeApi.Handlers;
|
||||
|
||||
public static class GetWeather
|
||||
public static class Weather
|
||||
{
|
||||
public record Command : IRequest<WeatherInformation>;
|
||||
|
||||
Reference in New Issue
Block a user