Skip to content

Commit

Permalink
Merge pull request #16 from davewalker5/BSR-31-Fix-Linting-Issues
Browse files Browse the repository at this point in the history
Address SonarCloud linting issues
  • Loading branch information
davewalker5 committed Aug 27, 2023
2 parents 56b8789 + f66a2df commit 2ab1e32
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class ApplicationSettings
public string Host { get; set; } = "";
public int Port { get; set; }
public int SocketReadTimeout { get; set; }
public int ApplicationTimeout { get; set; }
public int TimeToRecent { get; set; }
public int TimeToStale { get; set; }
public int TimeToRemoval { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public enum CommandLineOptionType
Host,
Port,
SocketReadTimeout,
ApplicationTimeout,
TimeToRecent,
TimeToStale,
TimeToRemoval,
Expand Down
4 changes: 3 additions & 1 deletion src/BaseStationReader.Logic/AircraftTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ private bool UpdateAircraftProperties(Aircraft aircraft, Message msg)

foreach (var aircraftProperty in _aircraftProperties)
{
var messageProperty = _messageProperties.FirstOrDefault(x => x.Name == aircraftProperty.Name);
var messageProperty = Array.Find(_messageProperties, x => x.Name == aircraftProperty.Name);
if (messageProperty != null)
{
var original = aircraftProperty.GetValue(aircraft);
Expand Down Expand Up @@ -173,7 +173,9 @@ private void OnTimer(object? sender, EventArgs e)
{
// Determine how long it is since this aircraft updated
var aircraft = entry.Value;
#pragma warning disable S6561
var lastSeenSeconds = (int)(DateTime.Now - aircraft.LastSeen).TotalMilliseconds;
#pragma warning restore S6561

// If it's now stale, remove it. Otherwise, set the staleness level and send an update
if (lastSeenSeconds >= _removedMs)
Expand Down
32 changes: 28 additions & 4 deletions src/BaseStationReader.Terminal/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,29 @@
namespace BaseStationReader.Terminal
{
[ExcludeFromCodeCoverage]
public class Program
public static 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;
private static DateTime _lastUpdate = DateTime.Now;

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

// Configure the log file
#pragma warning disable CS8602
#pragma warning disable CS8602, S4792
Log.Logger = new LoggerConfiguration()
.WriteTo
.File(
_settings.LogFile,
rollingInterval: RollingInterval.Day,
rollOnFileSizeLimit: true)
.CreateLogger();
#pragma warning restore CS8602
#pragma warning restore CS8602, S4792

// Get the assembly information, construct the table title and log the start-up message
Assembly assembly = Assembly.GetExecutingAssembly();
Expand Down Expand Up @@ -83,6 +84,7 @@ await AnsiConsole.Live(_table)
parser.Add(CommandLineOptionType.Host, false, "--host", "-h", "Host to connect to for data stream", 1, 1);
parser.Add(CommandLineOptionType.Port, false, "--port", "-p", "Port to connect to for data stream", 1, 1);
parser.Add(CommandLineOptionType.SocketReadTimeout, false, "--read-timeout", "-t", "Timeout (ms) for socket read operations", 1, 1);
parser.Add(CommandLineOptionType.ApplicationTimeout, false, "--app-timeout", "-a", "Timeout (ms) after which the application will quit of no messages are recieved", 1, 1);
parser.Add(CommandLineOptionType.TimeToRecent, false, "--recent", "-r", "Time (ms) to 'recent' staleness", 1, 1);
parser.Add(CommandLineOptionType.TimeToStale, false, "--stale", "-s", "Time (ms) to 'stale' staleness", 1, 1);
parser.Add(CommandLineOptionType.TimeToRemoval, false, "--remove", "-x", "Time (ms) removal of stale records", 1, 1);
Expand All @@ -102,6 +104,9 @@ await AnsiConsole.Live(_table)
values = parser.GetValues(CommandLineOptionType.SocketReadTimeout);
if (values != null) settings!.SocketReadTimeout = int.Parse(values[0]);

values = parser.GetValues(CommandLineOptionType.ApplicationTimeout);
if (values != null) settings!.ApplicationTimeout = int.Parse(values[0]);

values = parser.GetValues(CommandLineOptionType.TimeToRecent);
if (values != null) settings!.TimeToRecent = int.Parse(values[0]);

Expand Down Expand Up @@ -161,12 +166,18 @@ private static async Task ShowTrackingTable(LiveDisplayContext ctx)
}

// Continously update the table
int elapsed = 0;
tracker.Start();
while (true)
while (elapsed <= _settings.ApplicationTimeout)
{
// Refresh and wait for a while
ctx.Refresh();
await Task.Delay(100);

// Check we've not exceeded the application timeout
#pragma warning disable S6561
elapsed = (int)(DateTime.Now - _lastUpdate).TotalMilliseconds;
#pragma warning restore S6561
}
}

Expand Down Expand Up @@ -216,13 +227,18 @@ private static void OnAircraftAdded(object? sender, AircraftNotificationEventArg
{
lock (_rowIndex)
{
// Update the timestamp used to implement the application timeout
_lastUpdate = DateTime.Now;

// Push the change to the SQL writer, if enabled
if (_settings!.EnableSqlWriter)
{
#pragma warning disable CS8602
_writer.Push(e.Aircraft);
#pragma warning restore CS8602
}

// Add the aircraft to the table
var rowIndex = _table.Rows.Count;
var rowData = GetAircraftRowData(e.Aircraft);
_table.AddRow(rowData);
Expand All @@ -238,13 +254,18 @@ private static void OnAircraftAdded(object? sender, AircraftNotificationEventArg
/// <param name="e"></param>
private static void OnAircraftUpdated(object? sender, AircraftNotificationEventArgs e)
{
// Update the timestamp used to implement the application timeout
_lastUpdate = DateTime.Now;

// Push the change to the SQL writer, if enabled
if (_settings!.EnableSqlWriter)
{
#pragma warning disable CS8602
_writer.Push(e.Aircraft);
#pragma warning restore CS8602
}

// Update the row
var rowIndex = _rowIndex[e.Aircraft.Address];
var rowData = GetAircraftRowData(e.Aircraft);
_table.RemoveRow(rowIndex);
Expand All @@ -260,6 +281,9 @@ private static void OnAircraftRemoved(object? sender, AircraftNotificationEventA
{
lock (_rowIndex)
{
// Update the timestamp used to implement the application timeout
_lastUpdate = DateTime.Now;

// Lock the aircraft record - if we see it again, a new record will be created
e.Aircraft.Locked = true;
if (_settings!.EnableSqlWriter)
Expand Down
3 changes: 2 additions & 1 deletion src/BaseStationReader.Terminal/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
"Host": "192.168.0.98",
"Port": 30003,
"SocketReadTimeout": 60000,
"ApplicationTimeout": 600000,
"TimeToRecent": 60000,
"TimeToStale": 120000,
"TimeToRemoval": 180000,
"LogFile": "C:\\MyApps\\AircraftTracker.log",
"EnableSqlWriter": true,
"EnableSqlWriter": false,
"WriterInterval": 30000,
"WriterBatchSize": 20000
},
Expand Down

0 comments on commit 2ab1e32

Please sign in to comment.