-
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.
- Loading branch information
Showing
7 changed files
with
123 additions
and
14 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 |
---|---|---|
@@ -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 |
---|---|---|
@@ -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
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