11using System ;
22using System . Collections . Generic ;
3- using System . Linq ;
4- using System . Text ;
53using System . Threading ;
64using 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