Skip to content

Commit

Permalink
Add a simple notification system to alert the user of any important d…
Browse files Browse the repository at this point in the history
…ata coming in, such as RaceControlMessages. Initial implementation sends a BEL to the console to make a audible alert to the user.
  • Loading branch information
JustAman62 committed May 4, 2024
1 parent 8399858 commit 5c9e790
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 4 deletions.
1 change: 0 additions & 1 deletion OpenF1.Console/ConsoleLoop.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Diagnostics;
using System.Security.Cryptography;
using Spectre.Console;
using Spectre.Console.Rendering;

Expand Down
4 changes: 4 additions & 0 deletions OpenF1.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

var services = serviceCollection.BuildServiceProvider();

var notifyService = services.GetRequiredService<INotifyService>();
// When notifications are received, send a ASCII BEL to the console to make a noise to alert the user.
notifyService.RegisterNotificationHandler(() => Console.Write("\u0007"));

var consoleLoop = services.GetRequiredService<ConsoleLoop>();

await consoleLoop.ExecuteAsync();
16 changes: 16 additions & 0 deletions OpenF1.Data/Interfaces/INotifyService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace OpenF1.Data;

public interface INotifyService
{
/// <summary>
/// Sends a notification to any registered handlers.
/// This should only be used to alert a user to some important action, and not to transmit any data.
/// </summary>
public void SendNotification();

/// <summary>
/// Registers the provided <paramref name="action"/> as a handler for any notification that is sent.
/// </summary>
/// <param name="action">The action to call when a notification has been sent.</param>
public void RegisterNotificationHandler(Action action);
}
13 changes: 13 additions & 0 deletions OpenF1.Data/NotifyService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

namespace OpenF1.Data;

public class NotifyService : INotifyService
{
private Action? _action;

/// <inheritdoc />
public void RegisterNotificationHandler(Action action) => _action = action;

/// <inheritdoc />
public void SendNotification() => _action?.Invoke();
}
9 changes: 7 additions & 2 deletions OpenF1.Data/Processors/RaceControlMessageProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
namespace OpenF1.Data;

public class RaceControlMessageProcessor : IProcessor<RaceControlMessageDataPoint>
public class RaceControlMessageProcessor(INotifyService notifyService) : IProcessor<RaceControlMessageDataPoint>
{
public RaceControlMessageDataPoint RaceControlMessages { get; private set; } = new();

public void Process(RaceControlMessageDataPoint data)
{
foreach (var message in data.Messages)
{
RaceControlMessages.Messages.TryAdd(message.Key, message.Value);
var added = RaceControlMessages.Messages.TryAdd(message.Key, message.Value);
if (added)
{
// New race control messages are important, so alert the user
notifyService.SendNotification();
}
}
}
}
4 changes: 3 additions & 1 deletion OpenF1.Data/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public static partial class ServiceCollectionExtensions
/// Adds the services required to support processing live timing data.
/// A <see cref="ILiveTimingClient"/> is registered to support ingesting live timing data from the live feed.
/// A <see cref="IJsonTimingClient"/> is registered to support ingesting previously recorded timing data from JSON files.
/// A <see cref="INotifyService"/> is registered to support sending notifications for when interesting data changes happen.
/// </summary>
/// <param name="collection">The <see cref="IServiceCollection"/> to add services to.</param>
/// <param name="configuration">The <see cref="IConfiguration"/> to bind <see cref="LiveTimingOptions"/></param>
Expand All @@ -21,7 +22,8 @@ public static IServiceCollection AddLiveTiming(this IServiceCollection collectio
.Configure<LiveTimingOptions>(configuration)
.AddAutoMapper(cfg => cfg.AddCollectionMappers(), typeof(TimingDataPointConfiguration).Assembly)
.AddLiveTimingClient()
.AddLiveTimingProcessors();
.AddLiveTimingProcessors()
.AddSingleton<INotifyService, NotifyService>();

return collection;
}
Expand Down

0 comments on commit 5c9e790

Please sign in to comment.