Skip to content

Commit

Permalink
Looping in range testing and option to show only changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jdahlblom committed Oct 15, 2023
1 parent 8ec5eaa commit 7d56124
Show file tree
Hide file tree
Showing 6 changed files with 288 additions and 82 deletions.
1 change: 1 addition & 0 deletions src/client/DCSInsight/JSON/DCSAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace DCSInsight.JSON
{
[Serializable]
public class DCSAPI
{
/// <summary>
Expand Down
21 changes: 0 additions & 21 deletions src/client/DCSInsight/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,27 +242,6 @@ private void ButtonConnect_OnClick(object sender, RoutedEventArgs e)
}
}

/*
private static List<DCSBIOSControl> ReadControlsFromDocJson(string inputPath)
{
// input is a map from category string to a map from key string to control definition
// we read it all then flatten the grand children (the control definitions)
var input = File.ReadAllText(inputPath);
try
{
return JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, DCSBIOSControl>>>(input)!
.Values
.SelectMany(category => category.Values)
.ToList();
}
catch (Exception e)
{
Logger.Error(e, "ReadControlsFromDocJson : Failed to read DCS-BIOS JSON.");
}
return null;
}*/

private void MainWindow_OnClosing(object sender, CancelEventArgs e)
{
try
Expand Down
49 changes: 49 additions & 0 deletions src/client/DCSInsight/Misc/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Globalization;
using System.IO;
using Newtonsoft.Json;

namespace DCSInsight.Misc
{
using TextBox = System.Windows.Controls.TextBox;

public static class Extensions
{
/// <summary>
/// Perform a deep Copy of the object, using Json as a serialization method. NOTE: Private members are not cloned using this method.
/// </summary>
/// <typeparam name="T">The type of object being copied.</typeparam>
/// <param name="source">The object instance to copy.</param>
/// <returns>The copied object.</returns>
public static T CloneJson<T>(this T source)
{
//Note to devs: Use Newtonsoft.Json to clone the object, not System.Text.Json; because there are a lot of Newtonsoft.Json [JsonIgnore] attributes that are ignored
//by the System.Text.Json serializer. This could lead to recursive serialization of unwanted properties that raises an exception.

if (!typeof(T).IsSerializable)
{
throw new ArgumentException($"DeepClone error. The type must be serializable");
}

//Following line fixes "Could not create an instance of type xxx Type is an interface or abstract class and cannot be instantiated."
//when deep cloning IKeyPressInfo from AddKeySequence(string description, SortedList<int, IKeyPressInfo> keySequence)
var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto };

var jsonString = JsonConvert.SerializeObject(source, settings);
return JsonConvert.DeserializeObject<T>(jsonString, settings);
}

/// <summary>
/// US Culture used. Decimal separator is '.'
/// </summary>
public static bool ValidateDouble(this TextBox textBox)
{
if (string.IsNullOrEmpty(textBox.Text))
{
return true;
}

return double.TryParse(textBox.Text, NumberStyles.Any, CultureInfo.InvariantCulture, out _);
}
}
}
95 changes: 95 additions & 0 deletions src/client/DCSInsight/Misc/ResultComparator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System;
using System.Diagnostics;
using System.Text;
using DCSInsight.JSON;

namespace DCSInsight.Misc
{
internal class ResultComparator
{
private readonly DCSAPI _dcsApi;
private bool _resultHasChanged;
private object _lockObject = new();

public ResultComparator(DCSAPI dcsApi)
{
_dcsApi = dcsApi;
_dcsApi.Result = int.MinValue.ToString(); // Set so that first value will be listed in the results.
}

public bool IsMatch(DCSAPI dcsApi)
{
try
{
lock (_lockObject)
{
if (dcsApi.Parameters.Count != _dcsApi.Parameters.Count) return false;

for (var i = 0; i < _dcsApi.ParamCount; i++)
{
if (_dcsApi.Parameters[i].Value != dcsApi.Parameters[i].Value)
{
return false;
}
}
}
}
catch (Exception ex)
{
Common.ShowErrorMessageBox(ex);
}

return true;
}

/// <summary>
/// Returns true if result is different since previous check
/// </summary>
/// <param name="dcsApi"></param>
/// <returns></returns>
public bool SetResult(DCSAPI dcsApi)
{
try
{
lock (_lockObject)
{
if (!IsMatch(dcsApi))
{
throw new Exception("SetResult() : This is not the matching DCSAPI.");
}

if (dcsApi.Result != _dcsApi.Result)
{
_dcsApi.Result = dcsApi.Result;
_resultHasChanged = true;
return true;
}
}
}
catch (Exception ex)
{
Common.ShowErrorMessageBox(ex);
}

_resultHasChanged = false;
return false;
}

public string GetResultString()
{
lock (_lockObject)
{
var currentTestString = new StringBuilder();

foreach (var dcsApiParameter in _dcsApi.Parameters)
{
currentTestString.Append($"{dcsApiParameter.ParameterName} [{dcsApiParameter.Value}], ");
}

currentTestString.Append($" result : {_dcsApi.Result}\n");

return currentTestString.ToString();
}
}
}
}
10 changes: 9 additions & 1 deletion src/client/DCSInsight/Windows/WindowRangeTest.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@
<ComboBox Width="auto" MinWidth="200" Name="ComboBoxAPI" FontFamily="Consolas" SelectionChanged="ComboBoxAPI_OnSelectionChanged" ></ComboBox>
<Button Content="Start" Width="50" Margin="10,0,0,0" BorderBrush="Gray" FontSize="14" Name="ButtonStart" Click="ButtonStart_OnClick"></Button>
<Button Content="Stop" Width="50" Margin="10,0,0,0" BorderBrush="Gray" FontSize="14" Name="ButtonStop" Click="ButtonStop_OnClick"></Button>
<CheckBox Name="CheckBoxTop" Content="On Top" BorderBrush="Gray" Margin="120,0,0,0" Checked="CheckBoxTop_OnChecked" Unchecked="CheckBoxTop_OnUnchecked"></CheckBox>
<Label VerticalAlignment="Center" Content="Loop" Margin="10,0,0,0" />
<Label VerticalAlignment="Center" Margin="0,0,0,0" >
<CheckBox Name="CheckBoxLoop" VerticalAlignment="Center" IsChecked="False" Checked="CheckBoxLoop_OnChecked" Unchecked="CheckBoxLoop_OnUnchecked"/>
</Label>
<Label VerticalAlignment="Center" Content="Show changes only" Margin="10,0,0,0" />
<Label VerticalAlignment="Center" Margin="0,0,0,0" >
<CheckBox Name="CheckBoxShowChangesOnly" VerticalAlignment="Center" IsChecked="False" Checked="CheckBoxShowChangesOnly_OnChecked" Unchecked="CheckBoxShowChangesOnly_OnUnchecked"/>
</Label>
<CheckBox Name="CheckBoxTop" Content="On Top" BorderBrush="Gray" Margin="20,0,0,0" Checked="CheckBoxTop_OnChecked" Unchecked="CheckBoxTop_OnUnchecked"></CheckBox>
<Button Width="20" Content="?" Margin="10,0,0,0" BorderBrush="Black" FontSize="14" Name="ButtonInformation" Click="ButtonInformation_OnClick"></Button>
</ToolBar>

Expand Down
Loading

0 comments on commit 7d56124

Please sign in to comment.