-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystemMonitorWorker.cs
More file actions
92 lines (74 loc) · 2.75 KB
/
SystemMonitorWorker.cs
File metadata and controls
92 lines (74 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using SystemPerformanceNotifierService.Services;
namespace SystemPerformanceNotifierService;
public class SystemMonitorWorker : BackgroundService
{
private readonly ILogger<SystemMonitorWorker> _logger;
private readonly IConfiguration _configuration;
private readonly ISystemInfoCollector _systemInfoCollector;
private readonly ILoggerFactory _loggerFactory;
private SerialCommunicator? _serialCommunicator;
public SystemMonitorWorker(
ILogger<SystemMonitorWorker> logger,
IConfiguration configuration,
ISystemInfoCollector systemInfoCollector,
ILoggerFactory loggerFactory)
{
_logger = logger;
_configuration = configuration;
_systemInfoCollector = systemInfoCollector;
_loggerFactory = loggerFactory;
}
public override async Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("System Monitor Service for ESP32 starting...");
try
{
await _systemInfoCollector.InitializeAsync();
_serialCommunicator = new SerialCommunicator(
_loggerFactory.CreateLogger<SerialCommunicator>(),
_configuration);
await _serialCommunicator.InitializeAsync();
_logger.LogInformation("System Monitor Service for ESP32 started successfully");
}
catch (InvalidOperationException ex) when (ex.Message.Contains("already in use"))
{
_logger.LogWarning("Serial port is in use by another application. Service will continue without serial output.");
_logger.LogInformation("System Monitor Service started in monitoring-only mode");
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to start System Monitor Service");
throw;
}
await base.StartAsync(cancellationToken);
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
var interval = _configuration.GetValue<int>("SystemPerformanceNotifier:TransmissionInterval", 1000);
_logger.LogInformation("Starting system monitoring loop with {Interval}ms interval", interval);
while (!stoppingToken.IsCancellationRequested)
{
try
{
var systemInfo = await _systemInfoCollector.CollectAsync();
if (_serialCommunicator != null)
{
await _serialCommunicator.SendSystemInfoAsync(systemInfo);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error in monitoring loop");
}
await Task.Delay(interval, stoppingToken);
}
}
public override async Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("System Monitor Service stopping...");
_serialCommunicator?.Dispose();
_systemInfoCollector?.Dispose();
await base.StopAsync(cancellationToken);
_logger.LogInformation("System Monitor Service stopped");
}
}