Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding async #509

Closed
wants to merge 33 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/DCSFlightpanels.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=ARN/@EntryIndexedValue">ARN</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=AV/@EntryIndexedValue">AV</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=BIOS/@EntryIndexedValue">BIOS</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=BIP/@EntryIndexedValue">BIP</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=BIPS/@EntryIndexedValue">BIPS</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=BLUE/@EntryIndexedValue">BLUE</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=BNA/@EntryIndexedValue">BNA</s:String>
Expand All @@ -25,6 +26,7 @@
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=DCSBIOSU/@EntryIndexedValue">DCSBIOSU</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=DCSFP/@EntryIndexedValue">DCSFP</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=DED/@EntryIndexedValue">DED</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=DRO/@EntryIndexedValue">DRO</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=EUFD/@EntryIndexedValue">EUFD</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=FA/@EntryIndexedValue">FA</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=FM/@EntryIndexedValue">FM</s:String>
Expand All @@ -49,17 +51,20 @@
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=LCD/@EntryIndexedValue">LCD</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=LCDPZ/@EntryIndexedValue">LCDPZ</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=LE/@EntryIndexedValue">LE</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=LED/@EntryIndexedValue">LED</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=LF/@EntryIndexedValue">LF</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=MB/@EntryIndexedValue">MB</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=MK/@EntryIndexedValue">MK</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=MT/@EntryIndexedValue">MT</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=NADIR/@EntryIndexedValue">NADIR</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=NOUSE/@EntryIndexedValue">NOUSE</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=NS/@EntryIndexedValue">NS</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=OK/@EntryIndexedValue">OK</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=OLE/@EntryIndexedValue">OLE</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=OS/@EntryIndexedValue">OS</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=OSCOMMANDS/@EntryIndexedValue">OSCOMMANDS</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=PURPLE/@EntryIndexedValue">PURPLE</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=PZ/@EntryIndexedValue">PZ</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=RED/@EntryIndexedValue">RED</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=REF/@EntryIndexedValue">REF</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=SA/@EntryIndexedValue">SA</s:String>
Expand Down Expand Up @@ -108,7 +113,9 @@
<s:String x:Key="/Default/CodeStyle/Naming/XamlNaming/Abbreviations/=XPDR/@EntryIndexedValue">XPDR</s:String>
<s:Boolean x:Key="/Default/UserDictionary/Words/=ABRIS/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=ACFT/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=ANEMO/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Arduino/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=ARRET/@EntryIndexedValue">True</s:Boolean>

<s:Boolean x:Key="/Default/UserDictionary/Words/=Backlit/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=BIPLED/@EntryIndexedValue">True</s:Boolean>
Expand Down Expand Up @@ -159,6 +166,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=TILS/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=USERDIRECTORY/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=USERPROFILE/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=VEILLE/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=VHFAM/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=VHFFM/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=VUHF/@EntryIndexedValue">True</s:Boolean>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ClassLibraryCommon;
using DCS_BIOS.Serialized;
using Newtonsoft.Json;
Expand All @@ -25,8 +26,9 @@ public abstract class DCSBIOSActionBindingBase : IDisposable
internal static Logger Logger = LogManager.GetCurrentClassLogger();
private bool _whenOnTurnedOn = true;
private string _description;
[NonSerialized] private Thread _sendDCSBIOSCommandsThread;
private volatile List<DCSBIOSInput> _dcsbiosInputs;
private Task _sendingCommandsTask;

[JsonIgnore] public bool HasSequence => _dcsbiosInputs.Count > 1;
internal abstract void ImportSettings(string settings);
public abstract string ExportSettings();
Expand Down Expand Up @@ -61,14 +63,7 @@ public void Dispose()

public bool IsRunning()
{
if (_sendDCSBIOSCommandsThread != null && (_sendDCSBIOSCommandsThread.ThreadState == ThreadState.Running ||
_sendDCSBIOSCommandsThread.ThreadState == ThreadState.WaitSleepJoin ||
_sendDCSBIOSCommandsThread.ThreadState == ThreadState.Unstarted))
{
return true;
}

return false;
return _sendingCommandsTask != null && _sendingCommandsTask.Status == TaskStatus.Running;
}


Expand All @@ -83,25 +78,16 @@ public bool IsRunning()
*
* 3) Command is repeatable, for single commands only. Just keep executing the command, use thread.
*/
public void SendDCSBIOSCommands(CancellationToken cancellationToken)
public async Task SendDCSBIOSCommandsAsync(CancellationToken cancellationToken)
{
var repeatable = false;


if (this is ActionTypeDCSBIOS)
{
//Ugly, only Streamdeck has IsRepeatable
repeatable = ((ActionTypeDCSBIOS)this).IsRepeatable();
}

if (DCSBIOSInputs.Count == 0)
{
return;
}

if (_isSequenced && _sequenceIndex <= DCSBIOSInputs.Count - 1)
{
DCSBIOSInputs[_sequenceIndex].SelectedDCSBIOSInterface.SendCommand();
await DCSBIOSInputs[_sequenceIndex].SelectedDCSBIOSInterface.SendCommand();
_sequenceIndex++;

if (_sequenceIndex >= DCSBIOSInputs.Count)
Expand All @@ -116,29 +102,26 @@ public void SendDCSBIOSCommands(CancellationToken cancellationToken)
{
Thread.Sleep(200);
}

_sendDCSBIOSCommandsThread = new Thread(() => SendDCSBIOSCommandsThread(DCSBIOSInputs, cancellationToken));
_sendDCSBIOSCommandsThread.Start();

_sendingCommandsTask = Task.Run(() => SendDCSBIOSCommandsAsync(DCSBIOSInputs, cancellationToken));
}

private void SendDCSBIOSCommandsThread(List<DCSBIOSInput> dcsbiosInputs, CancellationToken cancellationToken)
private async Task SendDCSBIOSCommandsAsync(List<DCSBIOSInput> dcsbiosInputs, CancellationToken cancellationToken)
{
try
{
foreach (var dcsbiosInput in dcsbiosInputs)
{
Thread.Sleep(dcsbiosInput.SelectedDCSBIOSInterface.Delay);

dcsbiosInput.SelectedDCSBIOSInterface.SendCommand();
await dcsbiosInput.SelectedDCSBIOSInterface.SendCommand();

if (_shutdownCommandsThread || cancellationToken.IsCancellationRequested)
{
return;
}
}
}
catch (ThreadAbortException)
{ }
catch (Exception ex)
{
Logger.Error(ex);
Expand Down
11 changes: 6 additions & 5 deletions src/NonVisuals/CockpitMaster/Panels/CDU737PanelBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace NonVisuals.CockpitMaster.Panels
using System.Threading.Tasks;

namespace NonVisuals.CockpitMaster.Panels
{
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -210,9 +212,8 @@ protected override void Dispose(bool disposing)
base.Dispose(disposing);
}

protected override void GamingPanelKnobChanged(bool isFirstReport, IEnumerable<object> hashSet)
protected override async Task GamingPanelKnobChangedAsync(bool isFirstReport, IEnumerable<object> hashSet)
{

}

public Dictionary<char, CDUCharset> ConvertTable {
Expand Down Expand Up @@ -435,7 +436,7 @@ public override void DcsBiosDataReceived(object sender, DCSBIOSDataEventArgs e)

}

private void OnReport(HidReport report)
private async Task OnReport(HidReport report)
{
if (TypeOfPanel == GamingPanelEnum.CDU737 && report.Data.Length == 64)
{
Expand All @@ -444,7 +445,7 @@ private void OnReport(HidReport report)
HashSet<object> hashSet = GetHashSetOfChangedKnobs(OldCDUPanelValues, NewCDUPanelValues);
if (hashSet.Count > 0)
{
GamingPanelKnobChanged(!FirstReportHasBeenRead, hashSet);
await GamingPanelKnobChangedAsync(!FirstReportHasBeenRead, hashSet);
AppEventHandler.SwitchesChanged(this, HIDSkeletonBase.HIDInstance, TypeOfPanel, hashSet);
}

Expand Down
5 changes: 3 additions & 2 deletions src/NonVisuals/CockpitMaster/Preprogrammed/CDU737PanelA10C.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using DCS_BIOS.Interfaces;
using NonVisuals.CockpitMaster.Switches;
using System.Collections.Generic;
using System.Threading.Tasks;
using NonVisuals.HID;
using DCS_BIOS.Serialized;
using DCS_BIOS.ControlLocator;
Expand Down Expand Up @@ -89,7 +90,7 @@ protected override void Dispose(bool disposing)
// Call base class implementation.
base.Dispose(disposing);
}
protected override void GamingPanelKnobChanged(bool isFirstReport, IEnumerable<object> hashSet)
protected override async Task GamingPanelKnobChangedAsync(bool isFirstReport, IEnumerable<object> hashSet)
{
if (isFirstReport)
{
Expand All @@ -100,7 +101,7 @@ protected override void GamingPanelKnobChanged(bool isFirstReport, IEnumerable<o
{
foreach (CDUMappedCommandKey key in hashSet)
{
_ = DCSBIOS.Send(key.MappedCommand());
await DCSBIOS.SendAsync(key.MappedCommand());
}

}
Expand Down
31 changes: 16 additions & 15 deletions src/NonVisuals/CockpitMaster/Preprogrammed/CDU737PanelAH64D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
using DCS_BIOS.Interfaces;
using NonVisuals.CockpitMaster.Switches;
using System.Collections.Generic;
using System.Threading.Tasks;
using NonVisuals.HID;
using DCS_BIOS.Serialized;
using DCS_BIOS.ControlLocator;

namespace NonVisuals.CockpitMaster.PreProgrammed
{

public class CDU737PanelAH64D : CDU737PanelBase , IDCSBIOSStringListener
public class CDU737PanelAH64D : CDU737PanelBase, IDCSBIOSStringListener
{
// List the DCSBios Mappings Here

Expand All @@ -38,7 +39,7 @@ public class CDU737PanelAH64D : CDU737PanelBase , IDCSBIOSStringListener
private DCSBIOSOutput _PLT_MASTER_WARNING_L;

public CDU737PanelAH64D(HIDSkeleton hidSkeleton) : base(hidSkeleton)
{}
{ }

public override void InitPanel()
{
Expand Down Expand Up @@ -84,7 +85,7 @@ public override void InitPanel()
SetLine(0, string.Format("{0,24}", "AH64D profile"));

StartListeningForHidPanelChanges();

}
catch (Exception ex)
{
Expand All @@ -111,9 +112,9 @@ protected override void Dispose(bool disposing)
base.Dispose(disposing);
}



protected override void GamingPanelKnobChanged(bool isFirstReport, IEnumerable<object> hashSet)

protected override async Task GamingPanelKnobChangedAsync(bool isFirstReport, IEnumerable<object> hashSet)
{
if (isFirstReport)
{
Expand All @@ -124,7 +125,7 @@ protected override void GamingPanelKnobChanged(bool isFirstReport, IEnumerable<o

foreach (CDUMappedCommandKey key in hashSet)
{
_ = DCSBIOS.Send(key.MappedCommand());
await DCSBIOS.SendAsync(key.MappedCommand());
}
}
catch (Exception)
Expand All @@ -135,7 +136,7 @@ protected override void GamingPanelKnobChanged(bool isFirstReport, IEnumerable<o

public override void DcsBiosDataReceived(object sender, DCSBIOSDataEventArgs e)
{

if (SettingsLoading)
{
return;
Expand All @@ -148,20 +149,20 @@ public override void DcsBiosDataReceived(object sender, DCSBIOSDataEventArgs e)

UpdateCounter(e.Address, e.Data);

( shouldUpdate, newValue) = ShouldHandleDCSBiosData(e, _PLT_EUFD_BRT);
(shouldUpdate, newValue) = ShouldHandleDCSBiosData(e, _PLT_EUFD_BRT);

if (shouldUpdate)
{
int eufdBright = (int)newValue;
// MAX_BRIGHT is 256 , so 655356 / 256 is 256 , we need to divide by 2^8
ScreenBrightness = eufdBright >> 8;
KeyboardBrightness= eufdBright >>8;
KeyboardBrightness = eufdBright >> 8;
refreshLedsAndBrightness();
}

// AH - 64D / PLT_MASTER_WARNING_L
(shouldUpdate, newValue) = ShouldHandleDCSBiosData(e, _PLT_MASTER_WARNING_L);
if(shouldUpdate)
if (shouldUpdate)
{
if (newValue == 1)
{
Expand Down Expand Up @@ -195,17 +196,17 @@ public void DCSBIOSStringReceived(object sender, DCSBIOSStringDataEventArgs e)
{
SetLine(0, string.Format("{0,24}", incomingData));
}

}

if (e.Address.Equals(_PLT_EUFD_LINE1.Address))
{
incomingData = e.StringData.Substring(38, 17);
if (HandleStringData(1, e, incomingData, ref linesChanged))
{
SetLine(1, string.Format("{0,24}", incomingData ));
SetLine(1, string.Format("{0,24}", incomingData));
}

}

if (e.Address.Equals(_PLT_EUFD_LINE2.Address))
Expand Down Expand Up @@ -254,9 +255,9 @@ public void DCSBIOSStringReceived(object sender, DCSBIOSStringDataEventArgs e)
incomingData = e.StringData.Substring(0, 18);
if (HandleStringData(7, e, incomingData, ref linesChanged))
{
SetLine(7, incomingData );
SetLine(7, incomingData);
}

}
if (e.Address.Equals(_PLT_EUFD_LINE9.Address))
{
Expand Down
5 changes: 3 additions & 2 deletions src/NonVisuals/CockpitMaster/Preprogrammed/CDU737PanelF14.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using DCS_BIOS.Interfaces;
using NonVisuals.CockpitMaster.Switches;
using System.Collections.Generic;
using System.Threading.Tasks;
using NonVisuals.HID;
using DCS_BIOS.Serialized;
using DCS_BIOS.ControlLocator;
Expand Down Expand Up @@ -149,7 +150,7 @@ protected override void Dispose(bool disposing)
// Call base class implementation.
base.Dispose(disposing);
}
protected override void GamingPanelKnobChanged(bool isFirstReport, IEnumerable<object> hashSet)
protected override async Task GamingPanelKnobChangedAsync(bool isFirstReport, IEnumerable<object> hashSet)
{
if (isFirstReport)
{
Expand All @@ -160,7 +161,7 @@ protected override void GamingPanelKnobChanged(bool isFirstReport, IEnumerable<o
{
foreach (CDUMappedCommandKey key in hashSet)
{
_ = DCSBIOS.Send(key.MappedCommand());
await DCSBIOS.SendAsync(key.MappedCommand());
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using DCS_BIOS.Interfaces;
using NonVisuals.CockpitMaster.Switches;
using System.Collections.Generic;
using System.Threading.Tasks;
using NonVisuals.HID;
using DCS_BIOS.Serialized;
using DCS_BIOS.ControlLocator;
Expand Down Expand Up @@ -122,7 +123,7 @@ protected override void Dispose(bool disposing)
}


protected override void GamingPanelKnobChanged(bool isFirstReport, IEnumerable<object> hashSet)
protected override async Task GamingPanelKnobChangedAsync(bool isFirstReport, IEnumerable<object> hashSet)
{
if (isFirstReport)
{
Expand All @@ -132,7 +133,7 @@ protected override void GamingPanelKnobChanged(bool isFirstReport, IEnumerable<o
{
foreach (CDUMappedCommandKey key in hashSet)
{
_ = DCSBIOS.Send(key.MappedCommand());
await DCSBIOS.SendAsync(key.MappedCommand());
}
}
catch (Exception)
Expand Down
Loading
Loading