mirror of https://github.com/ErsatzTV/ErsatzTV.git
22 changed files with 305 additions and 66 deletions
@ -0,0 +1,8 @@ |
|||||||
|
using ErsatzTV.Core.Domain; |
||||||
|
|
||||||
|
namespace ErsatzTV.Core.Interfaces.Repositories; |
||||||
|
|
||||||
|
public interface IGraphicsElementRepository |
||||||
|
{ |
||||||
|
Task<Option<GraphicsElement>> GetGraphicsElementByPath(string path); |
||||||
|
} |
||||||
@ -0,0 +1,64 @@ |
|||||||
|
using ErsatzTV.Core.Domain; |
||||||
|
using ErsatzTV.Core.Interfaces.Repositories; |
||||||
|
using ErsatzTV.Core.Scheduling.YamlScheduling.Models; |
||||||
|
using Microsoft.Extensions.Logging; |
||||||
|
|
||||||
|
namespace ErsatzTV.Core.Scheduling.YamlScheduling.Handlers; |
||||||
|
|
||||||
|
public class YamlPlayoutGraphicsOffHandler(IGraphicsElementRepository graphicsElementRepository) : IYamlPlayoutHandler |
||||||
|
{ |
||||||
|
private readonly Dictionary<string, Option<GraphicsElement>> _graphicsElementCache = new(); |
||||||
|
|
||||||
|
public bool Reset => false; |
||||||
|
|
||||||
|
public async Task<bool> Handle( |
||||||
|
YamlPlayoutContext context, |
||||||
|
YamlPlayoutInstruction instruction, |
||||||
|
PlayoutBuildMode mode, |
||||||
|
Func<string, Task> executeSequence, |
||||||
|
ILogger<YamlPlayoutBuilder> logger, |
||||||
|
CancellationToken cancellationToken) |
||||||
|
{ |
||||||
|
if (instruction is not YamlPlayoutGraphicsOffInstruction graphicsOff) |
||||||
|
{ |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(graphicsOff.GraphicsOff)) |
||||||
|
{ |
||||||
|
context.ClearGraphicsElements(); |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
foreach (var ge in await GetGraphicsElementByPath(graphicsOff.GraphicsOff)) |
||||||
|
{ |
||||||
|
context.RemoveGraphicsElementId(ge.Id); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
private async Task<Option<GraphicsElement>> GetGraphicsElementByPath(string path) |
||||||
|
{ |
||||||
|
if (_graphicsElementCache.TryGetValue(path, out var cachedGraphicsElement)) |
||||||
|
{ |
||||||
|
foreach (GraphicsElement graphicsElement in cachedGraphicsElement) |
||||||
|
{ |
||||||
|
return graphicsElement; |
||||||
|
} |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
Option<GraphicsElement> maybeGraphicsElement = |
||||||
|
await graphicsElementRepository.GetGraphicsElementByPath(path); |
||||||
|
_graphicsElementCache.Add(path, maybeGraphicsElement); |
||||||
|
foreach (GraphicsElement graphicsElement in maybeGraphicsElement) |
||||||
|
{ |
||||||
|
return graphicsElement; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return Option<GraphicsElement>.None; |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,62 @@ |
|||||||
|
using ErsatzTV.Core.Domain; |
||||||
|
using ErsatzTV.Core.Interfaces.Repositories; |
||||||
|
using ErsatzTV.Core.Scheduling.YamlScheduling.Models; |
||||||
|
using Microsoft.Extensions.Logging; |
||||||
|
|
||||||
|
namespace ErsatzTV.Core.Scheduling.YamlScheduling.Handlers; |
||||||
|
|
||||||
|
public class YamlPlayoutGraphicsOnHandler(IGraphicsElementRepository graphicsElementRepository) : IYamlPlayoutHandler |
||||||
|
{ |
||||||
|
private readonly Dictionary<string, Option<GraphicsElement>> _graphicsElementCache = new(); |
||||||
|
|
||||||
|
public bool Reset => false; |
||||||
|
|
||||||
|
public async Task<bool> Handle( |
||||||
|
YamlPlayoutContext context, |
||||||
|
YamlPlayoutInstruction instruction, |
||||||
|
PlayoutBuildMode mode, |
||||||
|
Func<string, Task> executeSequence, |
||||||
|
ILogger<YamlPlayoutBuilder> logger, |
||||||
|
CancellationToken cancellationToken) |
||||||
|
{ |
||||||
|
if (instruction is not YamlPlayoutGraphicsOnInstruction graphicsOn) |
||||||
|
{ |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(graphicsOn.GraphicsOn)) |
||||||
|
{ |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
foreach (var ge in await GetGraphicsElementByPath(graphicsOn.GraphicsOn)) |
||||||
|
{ |
||||||
|
context.SetGraphicsElementId(ge.Id); |
||||||
|
} |
||||||
|
|
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
private async Task<Option<GraphicsElement>> GetGraphicsElementByPath(string path) |
||||||
|
{ |
||||||
|
if (_graphicsElementCache.TryGetValue(path, out var cachedGraphicsElement)) |
||||||
|
{ |
||||||
|
foreach (GraphicsElement graphicsElement in cachedGraphicsElement) |
||||||
|
{ |
||||||
|
return graphicsElement; |
||||||
|
} |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
Option<GraphicsElement> maybeGraphicsElement = |
||||||
|
await graphicsElementRepository.GetGraphicsElementByPath(path); |
||||||
|
_graphicsElementCache.Add(path, maybeGraphicsElement); |
||||||
|
foreach (GraphicsElement graphicsElement in maybeGraphicsElement) |
||||||
|
{ |
||||||
|
return graphicsElement; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return Option<GraphicsElement>.None; |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,9 @@ |
|||||||
|
using YamlDotNet.Serialization; |
||||||
|
|
||||||
|
namespace ErsatzTV.Core.Scheduling.YamlScheduling.Models; |
||||||
|
|
||||||
|
public class YamlPlayoutGraphicsOffInstruction : YamlPlayoutInstruction |
||||||
|
{ |
||||||
|
[YamlMember(Alias = "graphics_off", ApplyNamingConventions = false)] |
||||||
|
public string GraphicsOff { get; set; } |
||||||
|
} |
||||||
@ -0,0 +1,9 @@ |
|||||||
|
using YamlDotNet.Serialization; |
||||||
|
|
||||||
|
namespace ErsatzTV.Core.Scheduling.YamlScheduling.Models; |
||||||
|
|
||||||
|
public class YamlPlayoutGraphicsOnInstruction : YamlPlayoutInstruction |
||||||
|
{ |
||||||
|
[YamlMember(Alias = "graphics_on", ApplyNamingConventions = false)] |
||||||
|
public string GraphicsOn { get; set; } |
||||||
|
} |
||||||
@ -0,0 +1,24 @@ |
|||||||
|
using ErsatzTV.Core; |
||||||
|
using ErsatzTV.Core.Domain; |
||||||
|
using ErsatzTV.Core.Interfaces.Repositories; |
||||||
|
using ErsatzTV.Infrastructure.Extensions; |
||||||
|
using Microsoft.EntityFrameworkCore; |
||||||
|
|
||||||
|
namespace ErsatzTV.Infrastructure.Data.Repositories; |
||||||
|
|
||||||
|
public class GraphicsElementRepository(IDbContextFactory<TvContext> dbContextFactory) : IGraphicsElementRepository |
||||||
|
{ |
||||||
|
public async Task<Option<GraphicsElement>> GetGraphicsElementByPath(string path) |
||||||
|
{ |
||||||
|
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(); |
||||||
|
|
||||||
|
if (!path.StartsWith(FileSystemLayout.GraphicsElementsTemplatesFolder, StringComparison.Ordinal)) |
||||||
|
{ |
||||||
|
path = Path.Combine(FileSystemLayout.GraphicsElementsTemplatesFolder, path); |
||||||
|
} |
||||||
|
|
||||||
|
return await dbContext.GraphicsElements |
||||||
|
.AsNoTracking() |
||||||
|
.SelectOneAsync(ge => ge.Path, ge => ge.Path == path); |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue