Skip to content

Commit

Permalink
Fix Adf doesn't send correct value for 3 digit input and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenquyhy committed Nov 13, 2022
1 parent 788457b commit 1df03a6
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 29 deletions.
15 changes: 12 additions & 3 deletions FlightStreamDeck.Logics/Actions/NavCom/AdfHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,21 @@ public class AdfHandler : HzHandler
public AdfHandler(
IFlightConnector flightConnector, IEventRegistrar eventRegistrar, IEventDispatcher eventDispatcher,
TOGGLE_VALUE active, TOGGLE_VALUE? standby, TOGGLE_VALUE? batteryVariable, TOGGLE_VALUE? avionicsVariable,
KnownEvents? toggle, KnownEvents? set,
string minPattern, string mask) :
base(flightConnector, eventRegistrar, eventDispatcher, active, standby, batteryVariable, avionicsVariable, toggle, set, minPattern, mask)
KnownEvents? toggle, KnownEvents? set) :
base(
flightConnector, eventRegistrar, eventDispatcher, active, standby, batteryVariable, avionicsVariable, toggle, set,
"0100",
""
)
{
}

protected override string AddDefaultPattern(string value)
{
// no-op
return value;
}

/// <summary>
/// Convert from default MHz to kHz
/// </summary>
Expand Down
16 changes: 11 additions & 5 deletions FlightStreamDeck.Logics/Actions/NavCom/NavComHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ public void DeRegisterSimValues()

public async Task TriggerAsync(string value, bool swap)
{
if (value.Length < MinPattern.Length)
{
// Add the default mask
value += MinPattern.Substring(value.Length);
}
value = AddDefaultPattern(value);

uint data = FormatValueForSimConnect(value);
eventDispatcher.Trigger(set.ToString(), data);
Expand Down Expand Up @@ -118,6 +114,16 @@ public void SwapFrequencies()
}
}

protected virtual string AddDefaultPattern(string value)
{
if (value.Length < MinPattern.Length)
{
value += MinPattern.Substring(value.Length);
}

return value;
}

protected abstract uint FormatValueForSimConnect(string value);

protected virtual string FormatValueForDisplay(double value, TOGGLE_VALUE simvar)
Expand Down
9 changes: 2 additions & 7 deletions FlightStreamDeck.Logics/Actions/NavComAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public class NavComAction : BaseAction<NavComSettings>, EmbedLinkLogic.IAction
private const string maxComVal = "136990";
private const string minXpdrVal = "0000";
private const string maxXpdrVal = "7777";
private const string minAdfVal = "0100";

private readonly RegistrationParameters registration;
private readonly ILogger<NavComAction> logger;
Expand Down Expand Up @@ -323,9 +322,7 @@ private void SwitchTo(string? type, TOGGLE_VALUE? batteryVariable, TOGGLE_VALUE?
batteryVariable,
avionicsVariable,
KnownEvents.ADF1_RADIO_SWAP,
KnownEvents.ADF_STBY_SET,
minAdfVal,
""
KnownEvents.ADF_STBY_SET
);
break;
case "ADF2":
Expand All @@ -338,9 +335,7 @@ private void SwitchTo(string? type, TOGGLE_VALUE? batteryVariable, TOGGLE_VALUE?
batteryVariable,
avionicsVariable,
KnownEvents.ADF2_RADIO_SWAP,
KnownEvents.ADF2_STBY_SET,
minAdfVal,
""
KnownEvents.ADF2_STBY_SET
);
break;
default:
Expand Down
6 changes: 4 additions & 2 deletions FlightStreamDeck.Logics/IEventRegistrar.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
namespace FlightStreamDeck.Logics
using static FlightStreamDeck.Logics.SimEventManager;

namespace FlightStreamDeck.Logics
{
public interface IEventRegistrar
{
void RegisterEvent(string? eventName);
(EventEnum eventEnum, bool isValid)? RegisterEvent(string? eventName);
void ReInitializeEvents();
}
}
5 changes: 3 additions & 2 deletions FlightStreamDeck.Logics/SimEventManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ public SimEventManager(ILogger<SimEventManager> logger, IFlightConnector flightC
flightConnector.InvalidEventRegistered += FlightConnector_InvalidEventRegistered;
}

public void RegisterEvent(string? eventName)
public (EventEnum eventEnum, bool isValid)? RegisterEvent(string? eventName)
{
if (IsValid(eventName))
{
eventName = NormalizeEventName(eventName);
lock (registeredEvents)
{
registeredEvents.AddOrUpdate(
return registeredEvents.AddOrUpdate(
eventName,
addValueFactory: eventName =>
{
Expand Down Expand Up @@ -68,6 +68,7 @@ public void RegisterEvent(string? eventName)
);
}
}
return null;
}

public void ReInitializeEvents()
Expand Down
67 changes: 67 additions & 0 deletions FlightStreamDeck.LogicsTests/Actions/NavCom/AdfHandlerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using FlightStreamDeck.Logics.Actions.NavCom;

namespace FlightStreamDeck.Logics.Tests.Actions.NavCom;

[TestClass]
public class AdfHandlerTests
{
[TestMethod]
public async Task TestSetAdf1200()
{
Mock<IFlightConnector> mockFlightConnector = new Mock<IFlightConnector>();

var eventManager = new SimEventManager(
new LoggerFactory().CreateLogger<SimEventManager>(),
mockFlightConnector.Object
);

var registration = eventManager.RegisterEvent(KnownEvents.ADF_STBY_SET.ToString());
Assert.IsNotNull(registration);

var adfHandler = new AdfHandler(
mockFlightConnector.Object,
eventManager,
eventManager,
Core.TOGGLE_VALUE.ADF_ACTIVE_FREQUENCY__1,
Core.TOGGLE_VALUE.ADF_STANDBY_FREQUENCY__1,
null,
null,
KnownEvents.ADF1_RADIO_SWAP,
KnownEvents.ADF_STBY_SET
);

await adfHandler.TriggerAsync("1200", false);

mockFlightConnector.Verify(connector => connector.Trigger(registration.Value.eventEnum, 301989888));
}

[TestMethod]
public async Task TestSetAdf120()
{
Mock<IFlightConnector> mockFlightConnector = new Mock<IFlightConnector>();

var eventManager = new SimEventManager(
new LoggerFactory().CreateLogger<SimEventManager>(),
mockFlightConnector.Object
);

var registration = eventManager.RegisterEvent(KnownEvents.ADF_STBY_SET.ToString());
Assert.IsNotNull(registration);

var adfHandler = new AdfHandler(
mockFlightConnector.Object,
eventManager,
eventManager,
Core.TOGGLE_VALUE.ADF_ACTIVE_FREQUENCY__1,
Core.TOGGLE_VALUE.ADF_STANDBY_FREQUENCY__1,
null,
null,
KnownEvents.ADF1_RADIO_SWAP,
KnownEvents.ADF_STBY_SET
);

await adfHandler.TriggerAsync("120", false);

mockFlightConnector.Verify(connector => connector.Trigger(registration.Value.eventEnum, 18874368));
}
}
3 changes: 1 addition & 2 deletions FlightStreamDeck.LogicsTests/ComparisonEvaluatorTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;

namespace FlightStreamDeck.Logics.Tests
Expand Down
1 change: 0 additions & 1 deletion FlightStreamDeck.LogicsTests/EnumConverterTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using FlightStreamDeck.Core;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace FlightStreamDeck.Logics.Tests
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="Moq" Version="4.18.2" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="coverlet.collector" Version="3.1.2">
Expand Down
4 changes: 4 additions & 0 deletions FlightStreamDeck.LogicsTests/GlobalImports.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
global using Microsoft.Extensions.Logging;
global using Microsoft.VisualStudio.TestTools.UnitTesting;
global using Moq;
global using System.Threading.Tasks;
13 changes: 6 additions & 7 deletions FlightStreamDeck.LogicsTests/ImageLogicTests.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System;
using System.IO;

namespace FlightStreamDeck.Logics.Tests
{
[TestClass()]
[TestClass]
public class ImageLogicTests
{
[TestMethod()]
[TestMethod]
public void GetHorizonImageTest()
{
ImageLogic images = new ImageLogic();
Expand All @@ -28,7 +27,7 @@ public void GetHorizonImageTest()
}
}

[TestMethod()]
[TestMethod]
public void GetHorizonImageBoundaryTest()
{
ImageLogic images = new ImageLogic();
Expand All @@ -43,7 +42,7 @@ public void GetHorizonImageBoundaryTest()
images.GetHorizonImage(10, -20, -3000);
}

[TestMethod()]
[TestMethod]
public void GetGaugeImageTest()
{
ImageLogic images = new ImageLogic();
Expand All @@ -64,7 +63,7 @@ public void GetGaugeImageTest()
}
}

[TestMethod()]
[TestMethod]
public void GetCustomImageTest_NoCoordinateRangeError()
{
ImageLogic images = new ImageLogic();
Expand Down

0 comments on commit 1df03a6

Please sign in to comment.