Skip to content

Commit

Permalink
Fixup attempts to push aircraft updates to the SQL writer when disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
davewalker5 committed Aug 27, 2023
1 parent 403cd1b commit 43f25db
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ReleaseVersion>1.13.0.0</ReleaseVersion>
<FileVersion>1.13.0.0</FileVersion>
<ProductVersion>1.13.0</ProductVersion>
<ReleaseVersion>1.14.0.0</ReleaseVersion>
<FileVersion>1.14.0.0</FileVersion>
<ProductVersion>1.14.0</ProductVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
43 changes: 27 additions & 16 deletions src/BaseStationReader.Terminal/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@ public class Program
private readonly static Table _table = new Table().Expand().BorderColor(Spectre.Console.Color.Grey);
private readonly static Dictionary<string, int> _rowIndex = new();
private static IQueuedWriter? _writer = null;
private static ApplicationSettings? _settings = null;

public static async Task Main(string[] args)
{
// Read the application config
ApplicationSettings? settings = BuildSettings(args);
_settings = BuildSettings(args);

// Configure the log file
#pragma warning disable CS8602
Log.Logger = new LoggerConfiguration()

Check warning on line 31 in src/BaseStationReader.Terminal/Program.cs

View workflow job for this annotation

GitHub Actions / build

Make sure that this logger's configuration is safe.

Check warning on line 31 in src/BaseStationReader.Terminal/Program.cs

View workflow job for this annotation

GitHub Actions / build

Make sure that this logger's configuration is safe.
.WriteTo
.File(
settings.LogFile,
_settings.LogFile,
rollingInterval: RollingInterval.Day,
rollOnFileSizeLimit: true)
.CreateLogger();
Expand All @@ -39,7 +40,7 @@ public static async Task Main(string[] args)
// Get the assembly information, construct the table title and log the start-up message
Assembly assembly = Assembly.GetExecutingAssembly();
FileVersionInfo info = FileVersionInfo.GetVersionInfo(assembly.Location);
var title = $"Aircraft Tracker v{info.FileVersion}: {settings.Host}:{settings.Port}";
var title = $"Aircraft Tracker v{info.FileVersion}: {_settings.Host}:{_settings.Port}";
_table.Title(title);
Log.Information(new string('=', 80));
Log.Information(title);
Expand All @@ -63,7 +64,7 @@ await AnsiConsole.Live(_table)
.Cropping(VerticalOverflowCropping.Bottom)
.StartAsync(async ctx =>
{
await ShowTrackingTable(ctx, settings);
await ShowTrackingTable(ctx);
});
}

Expand Down Expand Up @@ -92,7 +93,7 @@ await AnsiConsole.Live(_table)

// Apply the command line values over the defaults
var values = parser.GetValues(CommandLineOptionType.Host);
if (values != null) settings!.Host = values.First();
if (values != null) settings!.Host = values[0];

values = parser.GetValues(CommandLineOptionType.Port);
if (values != null) settings!.Port = int.Parse(values[0]);
Expand Down Expand Up @@ -125,33 +126,32 @@ await AnsiConsole.Live(_table)
/// Display and continuously update the tracking table
/// </summary>
/// <param name="ctx"></param>
/// <param name="settings"></param>
/// <returns></returns>
private static async Task ShowTrackingTable(LiveDisplayContext ctx, ApplicationSettings settings)
private static async Task ShowTrackingTable(LiveDisplayContext ctx)

Check warning on line 130 in src/BaseStationReader.Terminal/Program.cs

View workflow job for this annotation

GitHub Actions / build

Add a way to break out of this method's recursion.

Check warning on line 130 in src/BaseStationReader.Terminal/Program.cs

View workflow job for this annotation

GitHub Actions / build

Add a way to break out of this method's recursion.
{
// Set up the message reader and parser and the aircraft tracker
var reader = new MessageReader(settings.Host, settings.Port);
var reader = new MessageReader(_settings!.Host, _settings.Port);
var parsers = new Dictionary<MessageType, IMessageParser>
{
{ MessageType.MSG, new MsgMessageParser() }
};

// Set up the aircraft tracker
var trackerTimer = new TrackerTimer(settings.TimeToRecent / 10.0);
var tracker = new AircraftTracker(reader, parsers, trackerTimer, settings.TimeToRecent, settings.TimeToStale, settings.TimeToRemoval);
var trackerTimer = new TrackerTimer(_settings.TimeToRecent / 10.0);
var tracker = new AircraftTracker(reader, parsers, trackerTimer, _settings.TimeToRecent, _settings.TimeToStale, _settings.TimeToRemoval);

// Wire up the aircraft tracking events
tracker.AircraftAdded += OnAircraftAdded;
tracker.AircraftUpdated += OnAircraftUpdated;
tracker.AircraftRemoved += OnAircraftRemoved;

// Set up the queued database writer
if (settings.EnableSqlWriter)
if (_settings.EnableSqlWriter)
{
BaseStationReaderDbContext context = new BaseStationReaderDbContextFactory().CreateDbContext(Array.Empty<string>());
var manager = new AircraftManager(context);
var writerTimer = new TrackerTimer(settings.WriterInterval);
_writer = new QueuedWriter(manager, writerTimer, settings.WriterBatchSize);
var writerTimer = new TrackerTimer(_settings.WriterInterval);
_writer = new QueuedWriter(manager, writerTimer, _settings.WriterBatchSize);
_writer.BatchWritten += OnBatchWritten;
_writer.Start();
}
Expand Down Expand Up @@ -212,9 +212,13 @@ private static void OnAircraftAdded(object? sender, AircraftNotificationEventArg
{
lock (_rowIndex)
{
if (_settings!.EnableSqlWriter)
{
#pragma warning disable CS8602
_writer.Push(e.Aircraft);
_writer.Push(e.Aircraft);
#pragma warning restore CS8602
}

var rowIndex = _table.Rows.Count;
var rowData = GetAircraftRowData(e.Aircraft);
_table.AddRow(rowData);
Expand All @@ -230,9 +234,13 @@ private static void OnAircraftAdded(object? sender, AircraftNotificationEventArg
/// <param name="e"></param>
private static void OnAircraftUpdated(object? sender, AircraftNotificationEventArgs e)
{
if (_settings!.EnableSqlWriter)
{
#pragma warning disable CS8602
_writer.Push(e.Aircraft);
_writer.Push(e.Aircraft);
#pragma warning restore CS8602
}

var rowIndex = _rowIndex[e.Aircraft.Address];
var rowData = GetAircraftRowData(e.Aircraft);
_table.RemoveRow(rowIndex);
Expand All @@ -250,9 +258,12 @@ private static void OnAircraftRemoved(object? sender, AircraftNotificationEventA
{
// Lock the aircraft record - if we see it again, a new record will be created
e.Aircraft.Locked = true;
if (_settings!.EnableSqlWriter)
{
#pragma warning disable CS8602
_writer.Push(e.Aircraft);
_writer.Push(e.Aircraft);
#pragma warning restore CS8602
}

// Locate the entry in the table and remove it
var row = _rowIndex[e.Aircraft.Address];
Expand Down

0 comments on commit 43f25db

Please sign in to comment.