-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from emoacht/develop
Develop
- Loading branch information
Showing
37 changed files
with
2,109 additions
and
2,258 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,47 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using System.Timers; | ||
|
||
namespace HelloSwitcher.Helper | ||
namespace HelloSwitcher.Helper; | ||
|
||
/// <summary> | ||
/// Rx Sample like operator | ||
/// </summary> | ||
public class Sample | ||
{ | ||
/// <summary> | ||
/// Rx Sample like operator | ||
/// </summary> | ||
public class Sample | ||
{ | ||
private readonly System.Timers.Timer _timer; | ||
private readonly Func<string, CancellationToken, Task> _action; | ||
private readonly SemaphoreSlim _semaphore = new(1, 1); | ||
private readonly System.Timers.Timer _timer; | ||
private readonly Func<string, CancellationToken, Task> _action; | ||
private readonly SemaphoreSlim _semaphore = new(1, 1); | ||
|
||
public Sample(TimeSpan dueTime, Func<string, CancellationToken, Task> action) | ||
{ | ||
if (dueTime <= TimeSpan.Zero) | ||
throw new ArgumentOutOfRangeException(nameof(dueTime), dueTime, "The duration must be positive."); | ||
public Sample(TimeSpan dueTime, Func<string, CancellationToken, Task> action) | ||
{ | ||
if (dueTime <= TimeSpan.Zero) | ||
throw new ArgumentOutOfRangeException(nameof(dueTime), dueTime, "The duration must be positive."); | ||
|
||
_timer = new System.Timers.Timer(dueTime.TotalMilliseconds); | ||
_timer.Elapsed += OnElapsed; | ||
_timer = new System.Timers.Timer(dueTime.TotalMilliseconds); | ||
_timer.Elapsed += OnElapsed; | ||
|
||
this._action = action ?? throw new ArgumentNullException(nameof(action)); | ||
} | ||
this._action = action ?? throw new ArgumentNullException(nameof(action)); | ||
} | ||
|
||
private string _actionName; | ||
private CancellationToken _cancellationToken; | ||
private string _actionName; | ||
private CancellationToken _cancellationToken; | ||
|
||
public void Push(string actionName, CancellationToken cancellationToken = default) | ||
public void Push(string actionName, CancellationToken cancellationToken = default) | ||
{ | ||
if (_semaphore.Wait(0, cancellationToken)) | ||
{ | ||
if (_semaphore.Wait(0, cancellationToken)) | ||
{ | ||
_actionName = actionName; | ||
_cancellationToken = cancellationToken; | ||
_timer.Start(); | ||
} | ||
_actionName = actionName; | ||
_cancellationToken = cancellationToken; | ||
_timer.Start(); | ||
} | ||
} | ||
|
||
private async void OnElapsed(object sender, ElapsedEventArgs e) | ||
{ | ||
_timer.Stop(); | ||
await _action.Invoke(_actionName, _cancellationToken); | ||
_semaphore.Release(); | ||
} | ||
private async void OnElapsed(object sender, ElapsedEventArgs e) | ||
{ | ||
_timer.Stop(); | ||
await _action.Invoke(_actionName, _cancellationToken); | ||
_semaphore.Release(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,79 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace HelloSwitcher.Models | ||
namespace HelloSwitcher.Models; | ||
|
||
public class DeviceSwitcher | ||
{ | ||
public class DeviceSwitcher | ||
{ | ||
private readonly Settings _settings; | ||
private readonly Logger _logger; | ||
private readonly Settings _settings; | ||
private readonly Logger _logger; | ||
|
||
public DeviceSwitcher(Settings settings, Logger logger) | ||
{ | ||
this._settings = settings ?? throw new ArgumentNullException(nameof(settings)); | ||
this._logger = logger; | ||
} | ||
public DeviceSwitcher(Settings settings, Logger logger) | ||
{ | ||
this._settings = settings ?? throw new ArgumentNullException(nameof(settings)); | ||
this._logger = logger; | ||
} | ||
|
||
public bool RemovableCameraExists => _removableCameraExists.GetValueOrDefault(); | ||
private bool? _removableCameraExists = null; | ||
public bool RemovableCameraExists => _removableCameraExists.GetValueOrDefault(); | ||
private bool? _removableCameraExists = null; | ||
|
||
private readonly object _lock = new object(); | ||
private readonly object _lock = new(); | ||
|
||
public Task CheckAsync(string actionName, CancellationToken cancellationToken = default) => CheckAsync(actionName, null, false, cancellationToken); | ||
public Task CheckAsync(string actionName, CancellationToken cancellationToken = default) => CheckAsync(actionName, null, false, cancellationToken); | ||
|
||
public async Task CheckAsync(string actionName, string deviceName, bool exists, CancellationToken cancellationToken = default) | ||
{ | ||
var result = new List<string> { actionName }; | ||
void RecordResult() => _logger?.RecordOperation(string.Join(Environment.NewLine, result)); | ||
public async Task CheckAsync(string actionName, string deviceName, bool exists, CancellationToken cancellationToken = default) | ||
{ | ||
var result = new List<string> { actionName }; | ||
void RecordResult() => _logger?.RecordOperation(string.Join(Environment.NewLine, result)); | ||
|
||
result.Add($"deviceName: [{deviceName}], exists: {exists}"); | ||
result.Add($"deviceName: [{deviceName}], exists: {exists}"); | ||
|
||
lock (_lock) | ||
lock (_lock) | ||
{ | ||
if ((deviceName is not null) && (_settings.RemovableCameraVidPid?.IsValid is true)) | ||
{ | ||
if ((deviceName is not null) && (_settings.RemovableCameraVidPid?.IsValid is true)) | ||
{ | ||
result.Add($"RemovableCameraVidPid: [{_settings.RemovableCameraVidPid}]"); | ||
result.Add($"RemovableCameraVidPid: [{_settings.RemovableCameraVidPid}]"); | ||
|
||
if (!_settings.RemovableCameraVidPid.Equals(new VidPid(deviceName))) | ||
{ | ||
RecordResult(); | ||
return; | ||
} | ||
} | ||
else | ||
{ | ||
result.Add($"RemovableCameraClassGuid: {_settings.RemovableCameraClassGuid}, RemovableCameraDeviceInstanceId: [{_settings.RemovableCameraDeviceInstanceId}]"); | ||
|
||
exists = DeviceUsbHelper.UsbCameraExists(_settings.RemovableCameraClassGuid, _settings.RemovableCameraDeviceInstanceId); | ||
} | ||
|
||
result.Add($"removableCameraExists: [{_removableCameraExists}], exists: {exists}"); | ||
|
||
if (_removableCameraExists == exists) | ||
if (!_settings.RemovableCameraVidPid.Equals(new VidPid(deviceName))) | ||
{ | ||
RecordResult(); | ||
return; | ||
} | ||
} | ||
else | ||
{ | ||
result.Add($"RemovableCameraClassGuid: {_settings.RemovableCameraClassGuid}, RemovableCameraDeviceInstanceId: [{_settings.RemovableCameraDeviceInstanceId}]"); | ||
|
||
_removableCameraExists = exists; | ||
exists = DeviceUsbHelper.UsbCameraExists(_settings.RemovableCameraClassGuid, _settings.RemovableCameraDeviceInstanceId); | ||
} | ||
|
||
if (cancellationToken.IsCancellationRequested) | ||
result.Add($"removableCameraExists: [{_removableCameraExists}], exists: {exists}"); | ||
|
||
if (_removableCameraExists == exists) | ||
{ | ||
RecordResult(); | ||
return; | ||
} | ||
|
||
if (!_removableCameraExists.Value) | ||
result.AddRange(await PnpUtility.EnableAsync(_settings.BuiltInCameraDeviceInstanceId, cancellationToken)); | ||
else | ||
result.AddRange(await PnpUtility.DisableAsync(_settings.BuiltInCameraDeviceInstanceId, cancellationToken)); | ||
_removableCameraExists = exists; | ||
} | ||
|
||
if (cancellationToken.IsCancellationRequested) | ||
{ | ||
RecordResult(); | ||
return; | ||
} | ||
|
||
public Task EnableAsync() => PnpUtility.EnableAsync(_settings.BuiltInCameraDeviceInstanceId); | ||
public Task DisableAsync() => PnpUtility.DisableAsync(_settings.BuiltInCameraDeviceInstanceId); | ||
if (!_removableCameraExists.Value) | ||
result.AddRange(await PnpUtility.EnableAsync(_settings.BuiltInCameraDeviceInstanceId, cancellationToken)); | ||
else | ||
result.AddRange(await PnpUtility.DisableAsync(_settings.BuiltInCameraDeviceInstanceId, cancellationToken)); | ||
|
||
RecordResult(); | ||
} | ||
|
||
public Task EnableAsync() => PnpUtility.EnableAsync(_settings.BuiltInCameraDeviceInstanceId); | ||
public Task DisableAsync() => PnpUtility.DisableAsync(_settings.BuiltInCameraDeviceInstanceId); | ||
} |
Oops, something went wrong.