Skip to content

Commit

Permalink
Merge pull request #14 from emoacht/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
emoacht authored Aug 4, 2024
2 parents f2019e9 + 3baaaa8 commit d6a88ce
Show file tree
Hide file tree
Showing 37 changed files with 2,109 additions and 2,258 deletions.
66 changes: 31 additions & 35 deletions Source/HelloSwitcher.Core/Helper/Sample.cs
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();
}
}
95 changes: 46 additions & 49 deletions Source/HelloSwitcher.Core/Models/DeviceSwitcher.cs
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);
}
Loading

0 comments on commit d6a88ce

Please sign in to comment.