File tree Expand file tree Collapse file tree
TgHomeBot.Scheduling/Tasks
TgHomeBot.SmartHome.Contract
TgHomeBot.SmartHome.HomeAssistant Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -14,7 +14,17 @@ public class SmartHomeMonitorWatcherTask(ILogger<SmartHomeMonitorWatcherTask> lo
1414
1515 public async Task ExecuteAsync ( CancellationToken cancellationToken )
1616 {
17- var isRunning = await smartHomeConnector . EnsureMonitorIsRunning ( ) ;
18- logger . LogInformation ( "Smart Home Monitor is running: {IsRunning}" , isRunning ) ;
17+ cancellationToken . ThrowIfCancellationRequested ( ) ;
18+
19+ var isRunning = await smartHomeConnector . EnsureMonitorIsRunning ( cancellationToken ) . ConfigureAwait ( false ) ;
20+
21+ if ( ! isRunning )
22+ {
23+ logger . LogWarning ( "Smart Home Monitor is not running and could not be started." ) ;
24+ }
25+ else
26+ {
27+ logger . LogDebug ( "Smart Home Monitor is running." ) ;
28+ }
1929 }
2030}
Original file line number Diff line number Diff line change @@ -8,5 +8,5 @@ public interface ISmartHomeConnector
88 Task < IReadOnlyList < SmartDevice > > GetDevices ( IReadOnlyList < MonitoredDevice > requestedDevices ) ;
99 Task < ISmartHomeMonitor > CreateMonitorAsync ( IReadOnlyList < MonitoredDevice > devices ,
1010 CancellationToken cancellationToken ) ;
11- Task < bool > EnsureMonitorIsRunning ( ) ;
11+ Task < bool > EnsureMonitorIsRunning ( CancellationToken cancellationToken ) ;
1212}
Original file line number Diff line number Diff line change @@ -67,19 +67,28 @@ public async Task<ISmartHomeMonitor> CreateMonitorAsync(IReadOnlyList<MonitoredD
6767 }
6868 }
6969
70- public async Task < bool > EnsureMonitorIsRunning ( )
70+ public async Task < bool > EnsureMonitorIsRunning ( CancellationToken cancellationToken )
7171 {
7272 try
7373 {
74- switch ( _smartHomeMonitor ? . State )
74+ await _semaphore . WaitAsync ( cancellationToken ) ;
75+ try
76+ {
77+ switch ( _smartHomeMonitor ? . State )
78+ {
79+ case MonitorState . Idle :
80+ case MonitorState . Unknown :
81+ await _smartHomeMonitor ! . StartMonitoringAsync ( cancellationToken ) ;
82+ return true ;
83+ case MonitorState . Listening :
84+ return true ;
85+ default :
86+ return false ;
87+ }
88+ }
89+ finally
7590 {
76- case MonitorState . Idle :
77- await _smartHomeMonitor . StartMonitoringAsync ( CancellationToken . None ) ;
78- return true ;
79- case MonitorState . Listening :
80- return true ;
81- default :
82- return false ;
91+ _semaphore . Release ( ) ;
8392 }
8493 }
8594 catch ( Exception e )
You can’t perform that action at this time.
0 commit comments