Skip to content

Commit

Permalink
Fix HorizonAction #393
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenquyhy committed Mar 22, 2023
1 parent d1e0ec8 commit 66fc25d
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions FlightStreamDeck.Logics/Actions/HorizonAction.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace FlightStreamDeck.Logics.Actions;
using System;

namespace FlightStreamDeck.Logics.Actions;

[StreamDeckAction("tech.flighttracker.streamdeck.artificial.horizon")]
public class HorizonAction : BaseAction
Expand All @@ -7,8 +9,8 @@ public class HorizonAction : BaseAction
private readonly IFlightConnector flightConnector;
private readonly IImageLogic imageLogic;
private readonly SimVarManager simVarManager;
private string bankValue = "PLANE BANK DEGREES";
private string pitchValue = "PLANE PITCH DEGREES";
private readonly SimVarRegistration bank;
private readonly SimVarRegistration pitch;

private double currentBankValue = 0;
private double currentPitchValue = 0;
Expand All @@ -19,6 +21,9 @@ public HorizonAction(ILogger<HorizonAction> logger, IFlightConnector flightConne
this.flightConnector = flightConnector;
this.imageLogic = imageLogic;
this.simVarManager = simVarManager;

this.bank = simVarManager.GetRegistration("PLANE BANK DEGREES") ?? throw new InvalidOperationException("Cannot get registration for AMBIENT WIND DIRECTION");
this.pitch = simVarManager.GetRegistration("PLANE PITCH DEGREES") ?? throw new InvalidOperationException("Cannot get registration for PLANE PITCH DEGREES");
}

protected override async Task OnWillAppear(ActionEventArgs<AppearancePayload> args)
Expand All @@ -32,9 +37,9 @@ protected override async Task OnWillAppear(ActionEventArgs<AppearancePayload> ar

private async void FlightConnector_GenericValuesUpdated(object? sender, ToggleValueUpdatedEventArgs e)
{
bool TryGetNewValue(string variable, double currentValue, out double value)
bool TryGetNewValue(SimVarRegistration variable, double currentValue, out double value)
{
if (e.GenericValueStatus.TryGetValue(new SimVarRegistration(variable, null), out var newValue) && currentValue != newValue)
if (e.GenericValueStatus.TryGetValue(variable, out var newValue) && currentValue != newValue)
{
value = newValue;
return true;
Expand All @@ -45,12 +50,12 @@ bool TryGetNewValue(string variable, double currentValue, out double value)

bool isUpdated = false;

if (TryGetNewValue(bankValue, currentBankValue, out var newBank))
if (TryGetNewValue(bank, currentBankValue, out var newBank))
{
currentBankValue = newBank;
isUpdated = true;
}
if (TryGetNewValue(pitchValue, currentPitchValue, out var newPitch))
if (TryGetNewValue(pitch, currentPitchValue, out var newPitch))
{
currentPitchValue = newPitch;
isUpdated = true;
Expand All @@ -71,12 +76,12 @@ protected override Task OnWillDisappear(ActionEventArgs<AppearancePayload> args)

private void RegisterValues()
{
simVarManager.RegisterSimValues(simVarManager.GetRegistration(bankValue), simVarManager.GetRegistration(pitchValue));
simVarManager.RegisterSimValues(bank, pitch);
}

private void DeRegisterValues()
{
simVarManager.DeRegisterSimValues(simVarManager.GetRegistration(bankValue), simVarManager.GetRegistration(pitchValue));
simVarManager.DeRegisterSimValues(bank, pitch);
}

protected override Task OnKeyDown(ActionEventArgs<KeyPayload> args)
Expand Down

0 comments on commit 66fc25d

Please sign in to comment.