Skip to content

Commit

Permalink
feat: add Waveshare instructions (activity)
Browse files Browse the repository at this point in the history
  • Loading branch information
mu88 committed Jul 6, 2023
1 parent a297d8c commit 010c7a9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
37 changes: 33 additions & 4 deletions src/ScreenshotCreator.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.Options;
using ScreenshotCreator.Api;
using ScreenshotCreator.Logic;
Expand Down Expand Up @@ -28,25 +29,28 @@
async (HttpContext httpContext, ImageProcessor imageProcessor, Creator creator, IOptions<ScreenshotOptions> options) =>
{
await creator.CreateScreenshotAsync(options.Value.Width, options.Value.Height);
return await ReturnImageOrNotFoundAsync(httpContext, imageProcessor);
return await ReturnImageOrNotFoundAsync(httpContext, imageProcessor, options);
});
app.MapGet("createImageWithSizeNow",
async (uint width,
uint height,
HttpContext httpContext,
ImageProcessor imageProcessor,
IOptions<ScreenshotOptions> options,
Creator creator) =>
{
await creator.CreateScreenshotAsync(width, height);
return await ReturnImageOrNotFoundAsync(httpContext, imageProcessor);
return await ReturnImageOrNotFoundAsync(httpContext, imageProcessor, options);
});

app.Run();

async Task<IResult> ReturnImageOrNotFoundAsync(HttpContext httpContext,
ImageProcessor imageProcessor,
IOptions<ScreenshotOptions> options,
bool blackAndWhite = false,
bool asWaveshareBytes = false)
bool asWaveshareBytes = false,
bool addWaveshareInstructions = false)
{
var screenshotFile = Path.Combine(Environment.CurrentDirectory, ScreenshotOptions.ScreenshotFileName);
if (!File.Exists(screenshotFile)) return Results.NotFound();
Expand All @@ -57,11 +61,36 @@ async Task<IResult> ReturnImageOrNotFoundAsync(HttpContext httpContext,
? Results.Bytes(processingResult.Data, processingResult.MediaType)
: Results.File(processingResult.Data, processingResult.MediaType);

httpContext.Response.Headers.Add("last-modified-local-time", GetLastModifiedAsLocalTime(screenshotFile));
if (addWaveshareInstructions) AddWaveshareInstructions(httpContext.Response.Headers, options.Value, screenshotFile);

return result;
}

void AddWaveshareInstructions(IHeaderDictionary headers, ScreenshotOptions screenshotOptions, string screenshotFile)
{
headers.Add("waveshare-last-modified-local-time", GetLastModifiedAsLocalTime(screenshotFile));
headers.Add("waveshare-sleep-between-updates", CalculateSleepBetweenUpdates(screenshotOptions));
headers.Add("waveshare-update-screen", DisplayShouldBeActive(screenshotOptions.Activity) ? true.ToString() : false.ToString());
}

string CalculateSleepBetweenUpdates(ScreenshotOptions screenshotOptions) =>
DisplayShouldBeActive(screenshotOptions.Activity)
? screenshotOptions.RefreshIntervalInSeconds.ToString()
: screenshotOptions.Activity.RefreshIntervalWhenInactiveInSeconds.ToString();

bool DisplayShouldBeActive([NotNullWhen(false)] Activity? activity)
{
if (activity is null) return true;

var currentLocalTime = GetCurrentLocalTime();
return activity.ActiveFrom <= currentLocalTime && currentLocalTime <= activity.ActiveTo;
}

TimeOnly GetCurrentLocalTime() =>
TimeOnly.FromDateTime(TimeZoneInfo
.ConvertTimeFromUtc(DateTime.UtcNow,
TimeZoneInfo.FindSystemTimeZoneById(Environment.GetEnvironmentVariable("TZ") ?? TimeZoneInfo.Local.Id)));

string GetLastModifiedAsLocalTime(string file) =>
TimeZoneInfo
.ConvertTimeFromUtc(File.GetLastWriteTimeUtc(file), TimeZoneInfo.FindSystemTimeZoneById(Environment.GetEnvironmentVariable("TZ") ?? TimeZoneInfo.Local.Id))
Expand Down
6 changes: 5 additions & 1 deletion src/ScreenshotCreator.Logic/ScreenshotOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ public class ScreenshotOptions
public uint RefreshIntervalInSeconds { get; set; }

public bool BackgroundProcessingEnabled { get; set; }

public Activity? Activity { get; set; }
}

public enum UrlType
{
Any,
OpenHab
}
}

public record Activity(TimeOnly ActiveFrom, TimeOnly ActiveTo, uint RefreshIntervalWhenInactiveInSeconds);

0 comments on commit 010c7a9

Please sign in to comment.