-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.cs
More file actions
84 lines (73 loc) · 2.53 KB
/
App.cs
File metadata and controls
84 lines (73 loc) · 2.53 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
using ChannelsDVR_Log_Monitor.Services.ChannelsDevices;
using ChannelsDVR_Log_Monitor.Services.ChannelsLogs;
using ChannelsDVR_Log_Monitor.Services.Notifications;
using Serilog;
namespace ChannelsDVR_Log_Monitor;
public class App
{
private readonly IChannelsLogService _channelsLogService;
private readonly IChannelsDevicesService _channelsDevicesService;
private readonly NotificationHandler _notificationHandler;
public App(
IChannelsLogService channelsLogService,
IChannelsDevicesService channelsDevicesService,
NotificationHandler notificationHandler
)
{
_notificationHandler = notificationHandler;
_channelsLogService = channelsLogService;
// Subscribe the notification handler to the log service's event
_channelsLogService.OnNewLogs += async (_, notifications) =>
{
try
{
await _notificationHandler.HandleNewMessagesAsync(notifications);
}
catch (Exception ex)
{
Log.Error($"Error handling new logs: {ex}");
}
};
_channelsDevicesService = channelsDevicesService;
// Subscribe the notification handler to the log service's event
_channelsDevicesService.OnNewDeviceChanges += async (_, notifications) =>
{
try
{
await _notificationHandler.HandleNewMessagesAsync(notifications);
}
catch (Exception ex)
{
Log.Error($"Error handling new logs: {ex}");
}
};
}
public async Task RunAsync(CancellationToken cancellationToken)
{
try
{
Log.Information("Initializing log service...");
await _channelsLogService.InitializeAsync();
Log.Information("Initializing devices service...");
await _channelsDevicesService.InitializeAsync();
Log.Information("Application started.");
// Keep the application running until a cancellation is requested
while (!cancellationToken.IsCancellationRequested)
{
await Task.Delay(Timeout.Infinite, cancellationToken);
}
}
catch (TaskCanceledException)
{
Log.Information("Application stopping...");
}
catch (Exception ex)
{
Log.Error($"Unexpected Error: {ex}");
}
finally
{
Log.Information("Shutting down application gracefully.");
}
}
}