From 1e42c923c8ca072072bcec1fc07fe8849d679014 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20H=C3=B6glinger?= Date: Sat, 19 Dec 2015 02:56:50 +0100 Subject: [PATCH] Replaced ScpTimer with Rx pattern --- ScpCleanWipe/Properties/CommonInfo.cs | 4 +- ScpControl.Shared/Properties/CommonInfo.cs | 4 +- ScpControl/Bluetooth/BthDevice.Designer.cs | 11 -- ScpControl/Bluetooth/BthDevice.cs | 48 ++++--- ScpControl/Properties/CommonInfo.cs | 4 +- ScpControl/ScpControl.csproj | 8 +- ScpControl/Usb/UsbDevice.Designer.cs | 10 -- ScpControl/Usb/UsbDevice.cs | 132 ++++++++++-------- ScpControl/Usb/UsbDevice.resx | 3 - ScpControlPanel/Properties/CommonInfo.cs | 4 +- .../Properties/CommonInfo.cs | 4 +- ScpDriverInstaller/Properties/CommonInfo.cs | 4 +- ScpGamepadAnalyzer/Properties/CommonInfo.cs | 4 +- ScpMonitor/Properties/CommonInfo.cs | 4 +- ScpPair/Properties/CommonInfo.cs | 4 +- ScpProfiler/Properties/CommonInfo.cs | 4 +- ScpServer/Properties/CommonInfo.cs | 4 +- ScpService/Properties/CommonInfo.cs | 4 +- ScpSettings/Properties/CommonInfo.cs | 4 +- ScpTrayApp/Properties/CommonInfo.cs | 4 +- ScpXInputBridge/Properties/CommonInfo.cs | 4 +- 21 files changed, 138 insertions(+), 134 deletions(-) diff --git a/ScpCleanWipe/Properties/CommonInfo.cs b/ScpCleanWipe/Properties/CommonInfo.cs index 4ac0f45b..1bd7432c 100644 --- a/ScpCleanWipe/Properties/CommonInfo.cs +++ b/ScpCleanWipe/Properties/CommonInfo.cs @@ -12,5 +12,5 @@ [assembly: ComVisible(false)] -[assembly: AssemblyVersion("1.6.207.15352")] -[assembly: AssemblyFileVersion("1.6.207.15352")] \ No newline at end of file +[assembly: AssemblyVersion("1.6.207.15353")] +[assembly: AssemblyFileVersion("1.6.207.15353")] \ No newline at end of file diff --git a/ScpControl.Shared/Properties/CommonInfo.cs b/ScpControl.Shared/Properties/CommonInfo.cs index 4ac0f45b..1bd7432c 100644 --- a/ScpControl.Shared/Properties/CommonInfo.cs +++ b/ScpControl.Shared/Properties/CommonInfo.cs @@ -12,5 +12,5 @@ [assembly: ComVisible(false)] -[assembly: AssemblyVersion("1.6.207.15352")] -[assembly: AssemblyFileVersion("1.6.207.15352")] \ No newline at end of file +[assembly: AssemblyVersion("1.6.207.15353")] +[assembly: AssemblyFileVersion("1.6.207.15353")] \ No newline at end of file diff --git a/ScpControl/Bluetooth/BthDevice.Designer.cs b/ScpControl/Bluetooth/BthDevice.Designer.cs index 5b44ef8c..0885674a 100644 --- a/ScpControl/Bluetooth/BthDevice.Designer.cs +++ b/ScpControl/Bluetooth/BthDevice.Designer.cs @@ -29,19 +29,8 @@ protected override void Dispose(bool disposing) private void InitializeComponent() { this.components = new System.ComponentModel.Container(); - this.tmUpdate = new ScpControl.ScpTimer(this.components); - // - // tmUpdate - // - this.tmUpdate.Enabled = false; - this.tmUpdate.Interval = ((uint)(10u)); - this.tmUpdate.Tag = null; - this.tmUpdate.Tick += new System.EventHandler(this.On_Timer); - } #endregion - - private ScpTimer tmUpdate; } } diff --git a/ScpControl/Bluetooth/BthDevice.cs b/ScpControl/Bluetooth/BthDevice.cs index c335c4c3..884e7596 100644 --- a/ScpControl/Bluetooth/BthDevice.cs +++ b/ScpControl/Bluetooth/BthDevice.cs @@ -1,6 +1,8 @@ using System; using System.ComponentModel; using System.Net.NetworkInformation; +using System.Reactive.Concurrency; +using System.Reactive.Linq; using ScpControl.ScpCore; using ScpControl.Shared.Core; using ScpControl.Sound; @@ -13,6 +15,15 @@ namespace ScpControl.Bluetooth /// public partial class BthDevice : BthConnection, IDsDevice { + #region Private fields + + private readonly IObservable _outputReportSchedule = Observable.Interval(TimeSpan.FromMilliseconds(10), + Scheduler.Default); + + private IDisposable _outputReportTask; + + #endregion + #region Protected fields protected bool m_Blocked, m_IsIdle = true, m_IsDisconnect; @@ -51,9 +62,22 @@ public DsConnection Connection #region Public methods + public ScpHidReport NewHidReport() + { + return new ScpHidReport + { + PadId = PadId, + PadState = State, + ConnectionType = Connection, + Model = Model, + PadMacAddress = DeviceAddress, + BatteryStatus = (byte) Battery + }; + } + public virtual bool Start() { - tmUpdate.Enabled = true; + _outputReportTask = _outputReportSchedule.Subscribe(tick => OnTimer()); // play connection sound if (GlobalConfiguration.Instance.IsBluetoothConnectSoundEnabled) @@ -78,24 +102,12 @@ public virtual bool Disconnect() return BluetoothDevice.HCI_Disconnect(HciHandle) > 0; } - public ScpHidReport NewHidReport() - { - return new ScpHidReport - { - PadId = PadId, - PadState = State, - ConnectionType = Connection, - Model = Model, - PadMacAddress = DeviceAddress, - BatteryStatus = (byte) Battery - }; - } - public virtual bool Stop() { if (State == DsState.Connected) { - tmUpdate.Enabled = false; + if (_outputReportTask != null) + _outputReportTask.Dispose(); State = DsState.Reserved; m_Packet = 0; @@ -151,7 +163,7 @@ public override string ToString() case DsState.Connected: return string.Format("Pad {0} : {1} {2} - {3} {4:X8} {5}", PadId, Model, - DeviceAddress, + DeviceAddress.AsFriendlyName(), Connection, m_Packet, Battery @@ -188,7 +200,7 @@ protected virtual void Process(DateTime now) { } - protected virtual void On_Timer(object sender, EventArgs e) + protected virtual void OnTimer() { if (State != DsState.Connected) return; @@ -256,4 +268,4 @@ public BthDevice(IBthDevice device, PhysicalAddress master, byte lsb, byte msb) #endregion } -} +} \ No newline at end of file diff --git a/ScpControl/Properties/CommonInfo.cs b/ScpControl/Properties/CommonInfo.cs index 4ac0f45b..1bd7432c 100644 --- a/ScpControl/Properties/CommonInfo.cs +++ b/ScpControl/Properties/CommonInfo.cs @@ -12,5 +12,5 @@ [assembly: ComVisible(false)] -[assembly: AssemblyVersion("1.6.207.15352")] -[assembly: AssemblyFileVersion("1.6.207.15352")] \ No newline at end of file +[assembly: AssemblyVersion("1.6.207.15353")] +[assembly: AssemblyFileVersion("1.6.207.15353")] \ No newline at end of file diff --git a/ScpControl/ScpControl.csproj b/ScpControl/ScpControl.csproj index 1acd9a19..6c9c8b76 100644 --- a/ScpControl/ScpControl.csproj +++ b/ScpControl/ScpControl.csproj @@ -23,7 +23,7 @@ AssemblyVersionAttribute - 1.6.207.15352 + 1.6.207.15353 true @@ -240,12 +240,6 @@ ScpProxy.cs - - Component - - - ScpTimer.cs - Component diff --git a/ScpControl/Usb/UsbDevice.Designer.cs b/ScpControl/Usb/UsbDevice.Designer.cs index 6ed1052d..633be45b 100644 --- a/ScpControl/Usb/UsbDevice.Designer.cs +++ b/ScpControl/Usb/UsbDevice.Designer.cs @@ -28,21 +28,11 @@ protected override void Dispose(bool disposing) /// private void InitializeComponent() { - this.components = new System.ComponentModel.Container(); - this.tmUpdate = new ScpControl.ScpTimer(this.components); - // - // tmUpdate - // - this.tmUpdate.Enabled = false; - this.tmUpdate.Interval = ((uint)(10u)); - this.tmUpdate.Tag = null; - this.tmUpdate.Tick += new System.EventHandler(this.On_Timer); } #endregion - private ScpTimer tmUpdate; } } diff --git a/ScpControl/Usb/UsbDevice.cs b/ScpControl/Usb/UsbDevice.cs index b5e6f75f..167bb524 100644 --- a/ScpControl/Usb/UsbDevice.cs +++ b/ScpControl/Usb/UsbDevice.cs @@ -1,6 +1,8 @@ using System; using System.ComponentModel; using System.Net.NetworkInformation; +using System.Reactive.Concurrency; +using System.Reactive.Linq; using System.Threading; using System.Threading.Tasks; using HidSharp.ReportDescriptors.Parser; @@ -16,32 +18,18 @@ namespace ScpControl.Usb /// public partial class UsbDevice : ScpDevice, IDsDevice { - private CancellationTokenSource _hidCancellationTokenSource = new CancellationTokenSource(); + #region Private fields - public override string ToString() - { - switch (State) - { - case DsState.Disconnected: + private readonly IObservable _outputReportSchedule = Observable.Interval(TimeSpan.FromMilliseconds(10), + Scheduler.Default); - return string.Format("Pad {0} : Disconnected", PadId); - - case DsState.Reserved: - - return string.Format("Pad {0} : {1} {2} - Reserved", PadId, Model, DeviceAddress.AsFriendlyName()); + private CancellationTokenSource _hidCancellationTokenSource = new CancellationTokenSource(); - case DsState.Connected: + private IDisposable _outputReportTask; - return string.Format("Pad {0} : {1} {2} - {3} {4:X8} {5}", PadId, Model, - DeviceAddress.AsFriendlyName(), - Connection, - PacketCounter, - Battery - ); - } + #endregion - throw new Exception(); - } + #region Private methods /// /// Worker thread polling for incoming Usb interrupts. @@ -49,7 +37,7 @@ public override string ToString() /// Task cancellation token. private void HidWorker(object o) { - var token = (CancellationToken)o; + var token = (CancellationToken) o; var transfered = 0; var buffer = new byte[64]; @@ -73,10 +61,7 @@ private void HidWorker(object o) Log.Debug("-- Usb Device : HID_Worker_Thread Exiting"); } - private void On_Timer(object sender, EventArgs e) - { - Process(DateTime.Now); - } + #endregion #region Protected fields @@ -159,7 +144,24 @@ public virtual DsConnection Connection #endregion - #region Actions + #region Public methods + + /// + /// Crafts a new with current devices meta data. + /// + /// The new HID . + public ScpHidReport NewHidReport() + { + return new ScpHidReport + { + PadId = PadId, + PadState = State, + ConnectionType = Connection, + Model = Model, + PadMacAddress = DeviceAddress, + BatteryStatus = (byte) Battery + }; + } public override bool Start() { @@ -170,7 +172,7 @@ public override bool Start() Task.Factory.StartNew(HidWorker, _hidCancellationTokenSource.Token); - tmUpdate.Enabled = true; + _outputReportTask = _outputReportSchedule.Subscribe(tick => Process(DateTime.Now)); Rumble(0, 0); Log.DebugFormat("-- Started Device Instance [{0}] Local [{1}] Remote [{2}]", m_Instance, @@ -216,26 +218,13 @@ public virtual bool Disconnect() return true; } - protected virtual void Process(DateTime now) - { - } - - protected virtual void ParseHidReport(byte[] report) - { - } - - protected virtual bool Shutdown() - { - Stop(); - - return RestartDevice(m_Instance); - } - public override bool Stop() { if (IsActive) { - tmUpdate.Enabled = false; + if (_outputReportTask != null) + _outputReportTask.Dispose(); + State = DsState.Reserved; _hidCancellationTokenSource.Cancel(); @@ -253,7 +242,9 @@ public override bool Close() { base.Close(); - tmUpdate.Enabled = false; + if (_outputReportTask != null) + _outputReportTask.Dispose(); + State = DsState.Disconnected; OnHidReportReceived(NewHidReport()); @@ -262,19 +253,50 @@ public override bool Close() return !IsActive; } + public override string ToString() + { + switch (State) + { + case DsState.Disconnected: + + return string.Format("Pad {0} : Disconnected", PadId); + + case DsState.Reserved: + + return string.Format("Pad {0} : {1} {2} - Reserved", PadId, Model, DeviceAddress.AsFriendlyName()); + + case DsState.Connected: + + return string.Format("Pad {0} : {1} {2} - {3} {4:X8} {5}", PadId, Model, + DeviceAddress.AsFriendlyName(), + Connection, + PacketCounter, + Battery + ); + } + + throw new Exception(); + } + #endregion - public ScpHidReport NewHidReport() + #region Protected methods + + protected virtual void Process(DateTime now) { - return new ScpHidReport() - { - PadId = PadId, - PadState = State, - ConnectionType = Connection, - Model = Model, - PadMacAddress = DeviceAddress, - BatteryStatus = (byte)Battery - }; } + + protected virtual void ParseHidReport(byte[] report) + { + } + + protected virtual bool Shutdown() + { + Stop(); + + return RestartDevice(m_Instance); + } + + #endregion } } diff --git a/ScpControl/Usb/UsbDevice.resx b/ScpControl/Usb/UsbDevice.resx index 0e7f0dd2..e5858cc2 100644 --- a/ScpControl/Usb/UsbDevice.resx +++ b/ScpControl/Usb/UsbDevice.resx @@ -117,9 +117,6 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - 17, 17 - False diff --git a/ScpControlPanel/Properties/CommonInfo.cs b/ScpControlPanel/Properties/CommonInfo.cs index 4ac0f45b..1bd7432c 100644 --- a/ScpControlPanel/Properties/CommonInfo.cs +++ b/ScpControlPanel/Properties/CommonInfo.cs @@ -12,5 +12,5 @@ [assembly: ComVisible(false)] -[assembly: AssemblyVersion("1.6.207.15352")] -[assembly: AssemblyFileVersion("1.6.207.15352")] \ No newline at end of file +[assembly: AssemblyVersion("1.6.207.15353")] +[assembly: AssemblyFileVersion("1.6.207.15353")] \ No newline at end of file diff --git a/ScpDebugInfoCollector/Properties/CommonInfo.cs b/ScpDebugInfoCollector/Properties/CommonInfo.cs index 4ac0f45b..1bd7432c 100644 --- a/ScpDebugInfoCollector/Properties/CommonInfo.cs +++ b/ScpDebugInfoCollector/Properties/CommonInfo.cs @@ -12,5 +12,5 @@ [assembly: ComVisible(false)] -[assembly: AssemblyVersion("1.6.207.15352")] -[assembly: AssemblyFileVersion("1.6.207.15352")] \ No newline at end of file +[assembly: AssemblyVersion("1.6.207.15353")] +[assembly: AssemblyFileVersion("1.6.207.15353")] \ No newline at end of file diff --git a/ScpDriverInstaller/Properties/CommonInfo.cs b/ScpDriverInstaller/Properties/CommonInfo.cs index 4ac0f45b..1bd7432c 100644 --- a/ScpDriverInstaller/Properties/CommonInfo.cs +++ b/ScpDriverInstaller/Properties/CommonInfo.cs @@ -12,5 +12,5 @@ [assembly: ComVisible(false)] -[assembly: AssemblyVersion("1.6.207.15352")] -[assembly: AssemblyFileVersion("1.6.207.15352")] \ No newline at end of file +[assembly: AssemblyVersion("1.6.207.15353")] +[assembly: AssemblyFileVersion("1.6.207.15353")] \ No newline at end of file diff --git a/ScpGamepadAnalyzer/Properties/CommonInfo.cs b/ScpGamepadAnalyzer/Properties/CommonInfo.cs index 4ac0f45b..1bd7432c 100644 --- a/ScpGamepadAnalyzer/Properties/CommonInfo.cs +++ b/ScpGamepadAnalyzer/Properties/CommonInfo.cs @@ -12,5 +12,5 @@ [assembly: ComVisible(false)] -[assembly: AssemblyVersion("1.6.207.15352")] -[assembly: AssemblyFileVersion("1.6.207.15352")] \ No newline at end of file +[assembly: AssemblyVersion("1.6.207.15353")] +[assembly: AssemblyFileVersion("1.6.207.15353")] \ No newline at end of file diff --git a/ScpMonitor/Properties/CommonInfo.cs b/ScpMonitor/Properties/CommonInfo.cs index 4ac0f45b..1bd7432c 100644 --- a/ScpMonitor/Properties/CommonInfo.cs +++ b/ScpMonitor/Properties/CommonInfo.cs @@ -12,5 +12,5 @@ [assembly: ComVisible(false)] -[assembly: AssemblyVersion("1.6.207.15352")] -[assembly: AssemblyFileVersion("1.6.207.15352")] \ No newline at end of file +[assembly: AssemblyVersion("1.6.207.15353")] +[assembly: AssemblyFileVersion("1.6.207.15353")] \ No newline at end of file diff --git a/ScpPair/Properties/CommonInfo.cs b/ScpPair/Properties/CommonInfo.cs index 4ac0f45b..1bd7432c 100644 --- a/ScpPair/Properties/CommonInfo.cs +++ b/ScpPair/Properties/CommonInfo.cs @@ -12,5 +12,5 @@ [assembly: ComVisible(false)] -[assembly: AssemblyVersion("1.6.207.15352")] -[assembly: AssemblyFileVersion("1.6.207.15352")] \ No newline at end of file +[assembly: AssemblyVersion("1.6.207.15353")] +[assembly: AssemblyFileVersion("1.6.207.15353")] \ No newline at end of file diff --git a/ScpProfiler/Properties/CommonInfo.cs b/ScpProfiler/Properties/CommonInfo.cs index 4ac0f45b..1bd7432c 100644 --- a/ScpProfiler/Properties/CommonInfo.cs +++ b/ScpProfiler/Properties/CommonInfo.cs @@ -12,5 +12,5 @@ [assembly: ComVisible(false)] -[assembly: AssemblyVersion("1.6.207.15352")] -[assembly: AssemblyFileVersion("1.6.207.15352")] \ No newline at end of file +[assembly: AssemblyVersion("1.6.207.15353")] +[assembly: AssemblyFileVersion("1.6.207.15353")] \ No newline at end of file diff --git a/ScpServer/Properties/CommonInfo.cs b/ScpServer/Properties/CommonInfo.cs index 4ac0f45b..1bd7432c 100644 --- a/ScpServer/Properties/CommonInfo.cs +++ b/ScpServer/Properties/CommonInfo.cs @@ -12,5 +12,5 @@ [assembly: ComVisible(false)] -[assembly: AssemblyVersion("1.6.207.15352")] -[assembly: AssemblyFileVersion("1.6.207.15352")] \ No newline at end of file +[assembly: AssemblyVersion("1.6.207.15353")] +[assembly: AssemblyFileVersion("1.6.207.15353")] \ No newline at end of file diff --git a/ScpService/Properties/CommonInfo.cs b/ScpService/Properties/CommonInfo.cs index 4ac0f45b..1bd7432c 100644 --- a/ScpService/Properties/CommonInfo.cs +++ b/ScpService/Properties/CommonInfo.cs @@ -12,5 +12,5 @@ [assembly: ComVisible(false)] -[assembly: AssemblyVersion("1.6.207.15352")] -[assembly: AssemblyFileVersion("1.6.207.15352")] \ No newline at end of file +[assembly: AssemblyVersion("1.6.207.15353")] +[assembly: AssemblyFileVersion("1.6.207.15353")] \ No newline at end of file diff --git a/ScpSettings/Properties/CommonInfo.cs b/ScpSettings/Properties/CommonInfo.cs index 4ac0f45b..1bd7432c 100644 --- a/ScpSettings/Properties/CommonInfo.cs +++ b/ScpSettings/Properties/CommonInfo.cs @@ -12,5 +12,5 @@ [assembly: ComVisible(false)] -[assembly: AssemblyVersion("1.6.207.15352")] -[assembly: AssemblyFileVersion("1.6.207.15352")] \ No newline at end of file +[assembly: AssemblyVersion("1.6.207.15353")] +[assembly: AssemblyFileVersion("1.6.207.15353")] \ No newline at end of file diff --git a/ScpTrayApp/Properties/CommonInfo.cs b/ScpTrayApp/Properties/CommonInfo.cs index 4ac0f45b..1bd7432c 100644 --- a/ScpTrayApp/Properties/CommonInfo.cs +++ b/ScpTrayApp/Properties/CommonInfo.cs @@ -12,5 +12,5 @@ [assembly: ComVisible(false)] -[assembly: AssemblyVersion("1.6.207.15352")] -[assembly: AssemblyFileVersion("1.6.207.15352")] \ No newline at end of file +[assembly: AssemblyVersion("1.6.207.15353")] +[assembly: AssemblyFileVersion("1.6.207.15353")] \ No newline at end of file diff --git a/ScpXInputBridge/Properties/CommonInfo.cs b/ScpXInputBridge/Properties/CommonInfo.cs index 4ac0f45b..1bd7432c 100644 --- a/ScpXInputBridge/Properties/CommonInfo.cs +++ b/ScpXInputBridge/Properties/CommonInfo.cs @@ -12,5 +12,5 @@ [assembly: ComVisible(false)] -[assembly: AssemblyVersion("1.6.207.15352")] -[assembly: AssemblyFileVersion("1.6.207.15352")] \ No newline at end of file +[assembly: AssemblyVersion("1.6.207.15353")] +[assembly: AssemblyFileVersion("1.6.207.15353")] \ No newline at end of file