Skip to content

Commit d6a88ce

Browse files
authored
Merge pull request #14 from emoacht/develop
Develop
2 parents f2019e9 + 3baaaa8 commit d6a88ce

37 files changed

+2109
-2258
lines changed
Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,47 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
52
using System.Threading;
63
using System.Threading.Tasks;
74
using System.Timers;
85

9-
namespace HelloSwitcher.Helper
6+
namespace HelloSwitcher.Helper;
7+
8+
/// <summary>
9+
/// Rx Sample like operator
10+
/// </summary>
11+
public class Sample
1012
{
11-
/// <summary>
12-
/// Rx Sample like operator
13-
/// </summary>
14-
public class Sample
15-
{
16-
private readonly System.Timers.Timer _timer;
17-
private readonly Func<string, CancellationToken, Task> _action;
18-
private readonly SemaphoreSlim _semaphore = new(1, 1);
13+
private readonly System.Timers.Timer _timer;
14+
private readonly Func<string, CancellationToken, Task> _action;
15+
private readonly SemaphoreSlim _semaphore = new(1, 1);
1916

20-
public Sample(TimeSpan dueTime, Func<string, CancellationToken, Task> action)
21-
{
22-
if (dueTime <= TimeSpan.Zero)
23-
throw new ArgumentOutOfRangeException(nameof(dueTime), dueTime, "The duration must be positive.");
17+
public Sample(TimeSpan dueTime, Func<string, CancellationToken, Task> action)
18+
{
19+
if (dueTime <= TimeSpan.Zero)
20+
throw new ArgumentOutOfRangeException(nameof(dueTime), dueTime, "The duration must be positive.");
2421

25-
_timer = new System.Timers.Timer(dueTime.TotalMilliseconds);
26-
_timer.Elapsed += OnElapsed;
22+
_timer = new System.Timers.Timer(dueTime.TotalMilliseconds);
23+
_timer.Elapsed += OnElapsed;
2724

28-
this._action = action ?? throw new ArgumentNullException(nameof(action));
29-
}
25+
this._action = action ?? throw new ArgumentNullException(nameof(action));
26+
}
3027

31-
private string _actionName;
32-
private CancellationToken _cancellationToken;
28+
private string _actionName;
29+
private CancellationToken _cancellationToken;
3330

34-
public void Push(string actionName, CancellationToken cancellationToken = default)
31+
public void Push(string actionName, CancellationToken cancellationToken = default)
32+
{
33+
if (_semaphore.Wait(0, cancellationToken))
3534
{
36-
if (_semaphore.Wait(0, cancellationToken))
37-
{
38-
_actionName = actionName;
39-
_cancellationToken = cancellationToken;
40-
_timer.Start();
41-
}
35+
_actionName = actionName;
36+
_cancellationToken = cancellationToken;
37+
_timer.Start();
4238
}
39+
}
4340

44-
private async void OnElapsed(object sender, ElapsedEventArgs e)
45-
{
46-
_timer.Stop();
47-
await _action.Invoke(_actionName, _cancellationToken);
48-
_semaphore.Release();
49-
}
41+
private async void OnElapsed(object sender, ElapsedEventArgs e)
42+
{
43+
_timer.Stop();
44+
await _action.Invoke(_actionName, _cancellationToken);
45+
_semaphore.Release();
5046
}
5147
}
Lines changed: 46 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,79 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
53
using System.Threading;
64
using System.Threading.Tasks;
75

8-
namespace HelloSwitcher.Models
6+
namespace HelloSwitcher.Models;
7+
8+
public class DeviceSwitcher
99
{
10-
public class DeviceSwitcher
11-
{
12-
private readonly Settings _settings;
13-
private readonly Logger _logger;
10+
private readonly Settings _settings;
11+
private readonly Logger _logger;
1412

15-
public DeviceSwitcher(Settings settings, Logger logger)
16-
{
17-
this._settings = settings ?? throw new ArgumentNullException(nameof(settings));
18-
this._logger = logger;
19-
}
13+
public DeviceSwitcher(Settings settings, Logger logger)
14+
{
15+
this._settings = settings ?? throw new ArgumentNullException(nameof(settings));
16+
this._logger = logger;
17+
}
2018

21-
public bool RemovableCameraExists => _removableCameraExists.GetValueOrDefault();
22-
private bool? _removableCameraExists = null;
19+
public bool RemovableCameraExists => _removableCameraExists.GetValueOrDefault();
20+
private bool? _removableCameraExists = null;
2321

24-
private readonly object _lock = new object();
22+
private readonly object _lock = new();
2523

26-
public Task CheckAsync(string actionName, CancellationToken cancellationToken = default) => CheckAsync(actionName, null, false, cancellationToken);
24+
public Task CheckAsync(string actionName, CancellationToken cancellationToken = default) => CheckAsync(actionName, null, false, cancellationToken);
2725

28-
public async Task CheckAsync(string actionName, string deviceName, bool exists, CancellationToken cancellationToken = default)
29-
{
30-
var result = new List<string> { actionName };
31-
void RecordResult() => _logger?.RecordOperation(string.Join(Environment.NewLine, result));
26+
public async Task CheckAsync(string actionName, string deviceName, bool exists, CancellationToken cancellationToken = default)
27+
{
28+
var result = new List<string> { actionName };
29+
void RecordResult() => _logger?.RecordOperation(string.Join(Environment.NewLine, result));
3230

33-
result.Add($"deviceName: [{deviceName}], exists: {exists}");
31+
result.Add($"deviceName: [{deviceName}], exists: {exists}");
3432

35-
lock (_lock)
33+
lock (_lock)
34+
{
35+
if ((deviceName is not null) && (_settings.RemovableCameraVidPid?.IsValid is true))
3636
{
37-
if ((deviceName is not null) && (_settings.RemovableCameraVidPid?.IsValid is true))
38-
{
39-
result.Add($"RemovableCameraVidPid: [{_settings.RemovableCameraVidPid}]");
37+
result.Add($"RemovableCameraVidPid: [{_settings.RemovableCameraVidPid}]");
4038

41-
if (!_settings.RemovableCameraVidPid.Equals(new VidPid(deviceName)))
42-
{
43-
RecordResult();
44-
return;
45-
}
46-
}
47-
else
48-
{
49-
result.Add($"RemovableCameraClassGuid: {_settings.RemovableCameraClassGuid}, RemovableCameraDeviceInstanceId: [{_settings.RemovableCameraDeviceInstanceId}]");
50-
51-
exists = DeviceUsbHelper.UsbCameraExists(_settings.RemovableCameraClassGuid, _settings.RemovableCameraDeviceInstanceId);
52-
}
53-
54-
result.Add($"removableCameraExists: [{_removableCameraExists}], exists: {exists}");
55-
56-
if (_removableCameraExists == exists)
39+
if (!_settings.RemovableCameraVidPid.Equals(new VidPid(deviceName)))
5740
{
5841
RecordResult();
5942
return;
6043
}
44+
}
45+
else
46+
{
47+
result.Add($"RemovableCameraClassGuid: {_settings.RemovableCameraClassGuid}, RemovableCameraDeviceInstanceId: [{_settings.RemovableCameraDeviceInstanceId}]");
6148

62-
_removableCameraExists = exists;
49+
exists = DeviceUsbHelper.UsbCameraExists(_settings.RemovableCameraClassGuid, _settings.RemovableCameraDeviceInstanceId);
6350
}
6451

65-
if (cancellationToken.IsCancellationRequested)
52+
result.Add($"removableCameraExists: [{_removableCameraExists}], exists: {exists}");
53+
54+
if (_removableCameraExists == exists)
6655
{
6756
RecordResult();
6857
return;
6958
}
7059

71-
if (!_removableCameraExists.Value)
72-
result.AddRange(await PnpUtility.EnableAsync(_settings.BuiltInCameraDeviceInstanceId, cancellationToken));
73-
else
74-
result.AddRange(await PnpUtility.DisableAsync(_settings.BuiltInCameraDeviceInstanceId, cancellationToken));
60+
_removableCameraExists = exists;
61+
}
7562

63+
if (cancellationToken.IsCancellationRequested)
64+
{
7665
RecordResult();
66+
return;
7767
}
7868

79-
public Task EnableAsync() => PnpUtility.EnableAsync(_settings.BuiltInCameraDeviceInstanceId);
80-
public Task DisableAsync() => PnpUtility.DisableAsync(_settings.BuiltInCameraDeviceInstanceId);
69+
if (!_removableCameraExists.Value)
70+
result.AddRange(await PnpUtility.EnableAsync(_settings.BuiltInCameraDeviceInstanceId, cancellationToken));
71+
else
72+
result.AddRange(await PnpUtility.DisableAsync(_settings.BuiltInCameraDeviceInstanceId, cancellationToken));
73+
74+
RecordResult();
8175
}
76+
77+
public Task EnableAsync() => PnpUtility.EnableAsync(_settings.BuiltInCameraDeviceInstanceId);
78+
public Task DisableAsync() => PnpUtility.DisableAsync(_settings.BuiltInCameraDeviceInstanceId);
8279
}

0 commit comments

Comments
 (0)