Skip to content

Commit 0e08a63

Browse files
Improve SmartHomeMonitorWatcherTask: CancellationToken propagation, sync, and logging (#60)
* Initial plan * Apply PR review feedback: CancellationToken, Unknown state, semaphore, logging improvements Co-authored-by: thomasneuberger <23504477+thomasneuberger@users.noreply.github.com> Agent-Logs-Url: https://github.com/thomasneuberger/TgHomeBot/sessions/02041d09-0799-491d-ac3a-ccd36acdea63 --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: thomasneuberger <23504477+thomasneuberger@users.noreply.github.com>
1 parent 5d32e9f commit 0e08a63

3 files changed

Lines changed: 31 additions & 12 deletions

File tree

TgHomeBot.Scheduling/Tasks/SmartHomeMonitorWatcherTask.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff 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
}

TgHomeBot.SmartHome.Contract/ISmartHomeConnector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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
}

TgHomeBot.SmartHome.HomeAssistant/HomeAssistantConnector.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff 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)

0 commit comments

Comments
 (0)