Add support for esp
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
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,18 +8,11 @@ public static class DepartureBoard
|
||||
{
|
||||
public record Command : IRequest<List<TimeTable>>;
|
||||
|
||||
public class Handler : IRequestHandler<Command, List<TimeTable>>
|
||||
public class Handler(IDepartureBoardService departureBoardService) : IRequestHandler<Command, List<TimeTable>>
|
||||
{
|
||||
private readonly IDepartureBoardService _departureBoardService;
|
||||
|
||||
public Handler(IDepartureBoardService departureBoardService)
|
||||
{
|
||||
_departureBoardService = departureBoardService;
|
||||
}
|
||||
|
||||
public async Task<List<TimeTable>> Handle(Command request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _departureBoardService.GetDepartureBoard() ?? new List<TimeTable>();
|
||||
return await departureBoardService.GetDepartureBoard() ?? new List<TimeTable>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,10 +5,6 @@ using MediatR;
|
||||
using Microsoft.Extensions.Options;
|
||||
using PuppeteerSharp;
|
||||
using RazorLight;
|
||||
using SixLabors.ImageSharp.Formats.Bmp;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
using SixLabors.ImageSharp.Processing;
|
||||
using Image = SixLabors.ImageSharp.Image;
|
||||
|
||||
namespace HomeApi.Handlers;
|
||||
|
||||
@@ -16,24 +12,18 @@ public static class ImageGeneration
|
||||
{
|
||||
public record Command : IRequest<Stream>;
|
||||
|
||||
public class Handler : IRequestHandler<Command, Stream>
|
||||
public class Handler(
|
||||
IWebHostEnvironment env,
|
||||
IMediator mediator,
|
||||
IOptions<ApiConfiguration> apiConfiguration)
|
||||
: IRequestHandler<Command, Stream>
|
||||
{
|
||||
private readonly ILogger<Handler> _logger;
|
||||
private readonly IWebHostEnvironment _env;
|
||||
private readonly IMediator _mediator;
|
||||
public Handler(
|
||||
IOptions<ApiConfiguration> apiConfiguration,
|
||||
ILogger<Handler> logger, IWebHostEnvironment env, IMediator mediator)
|
||||
{
|
||||
_logger = logger;
|
||||
_env = env;
|
||||
_mediator = mediator;
|
||||
}
|
||||
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 weather = await mediator.Send(new Weather.Command(), cancellationToken);
|
||||
var departureBoard = await mediator.Send(new DepartureBoard.Command(), cancellationToken);
|
||||
|
||||
var model = new Models.Image
|
||||
{
|
||||
@@ -50,13 +40,19 @@ public static class ImageGeneration
|
||||
.UseMemoryCachingProvider()
|
||||
.Build();
|
||||
|
||||
var path = Path.Combine(_env.WebRootPath, "index.cshtml");
|
||||
var path = Path.Combine(env.WebRootPath, "index.cshtml");
|
||||
|
||||
var template = await File.ReadAllTextAsync(path, cancellationToken);
|
||||
|
||||
var result = await engine.CompileRenderStringAsync("templateKey", template, model, viewBag: new ExpandoObject());
|
||||
dynamic viewBag = new ExpandoObject();
|
||||
viewBag.IsHighContrast = _apiConfiguration.EspConfiguration.IsHighContrastMode;
|
||||
|
||||
var result = await engine.CompileRenderStringAsync("templateKey", template, model, viewBag: viewBag);
|
||||
|
||||
return await CreateImage(result);
|
||||
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)
|
||||
@@ -68,63 +64,16 @@ public static class ImageGeneration
|
||||
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 = new[] { WaitUntilNavigation.Networkidle0 } });
|
||||
var stream = await page.ScreenshotStreamAsync(new ScreenshotOptions { Type = ScreenshotType.Png });
|
||||
|
||||
return await stream.ToBmpStream();
|
||||
|
||||
await page.SetContentAsync(htmlContent, new NavigationOptions { WaitUntil = [WaitUntilNavigation.Networkidle0] });
|
||||
return await page.ScreenshotStreamAsync(new ScreenshotOptions { Type = ScreenshotType.Jpeg, Quality = 60 });
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<Stream> ToBmpStream(this Stream stream)
|
||||
{
|
||||
var image = await Image.LoadAsync<Rgba32>(stream);
|
||||
// Resize or crop to 800x480 if necessary
|
||||
image.Mutate(x => x.Resize(800, 480));
|
||||
|
||||
// Reduce to 3-color e-paper palette
|
||||
image.ProcessPixelRows(accessor =>
|
||||
{
|
||||
for (var y = 0; y < accessor.Height; y++)
|
||||
{
|
||||
var row = accessor.GetRowSpan(y);
|
||||
for (var x = 0; x < row.Length; x++)
|
||||
{
|
||||
var pixel = row[x];
|
||||
|
||||
// Compute perceived brightness (gray)
|
||||
var brightness = 0.299f * pixel.R + 0.587f * pixel.G + 0.114f * pixel.B;
|
||||
|
||||
if (pixel.R > 150 && pixel.G < 80 && pixel.B < 80) // RED threshold
|
||||
{
|
||||
row[x] = new Rgba32(255, 0, 0); // Red
|
||||
}
|
||||
else if (brightness > 180)
|
||||
{
|
||||
row[x] = new Rgba32(255, 255, 255); // White
|
||||
}
|
||||
else
|
||||
{
|
||||
row[x] = new Rgba32(0, 0, 0); // Black
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
var bmpStream = new MemoryStream();
|
||||
var bmpEncoder = new BmpEncoder
|
||||
{
|
||||
BitsPerPixel = BmpBitsPerPixel.Pixel24
|
||||
};
|
||||
|
||||
await image.SaveAsync(bmpStream, bmpEncoder);
|
||||
bmpStream.Position = 0;
|
||||
|
||||
return bmpStream;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user