diff --git a/Environment/Service/DeviceCheckService.cs b/Environment/Service/DeviceCheckService.cs new file mode 100644 index 0000000..6088391 --- /dev/null +++ b/Environment/Service/DeviceCheckService.cs @@ -0,0 +1,77 @@ +using ChrisKaczor.HomeMonitor.Environment.Service.Data; +using RestSharp; + +namespace ChrisKaczor.HomeMonitor.Environment.Service; + +public class DeviceCheckService(Database _database, IConfiguration _configuration) : IHostedService +{ + private Timer? _timer; + private TimeSpan _warningInterval; + + private readonly string _botToken = _configuration["Telegram:BotToken"]!; + private readonly string _chatId = _configuration["Telegram:ChatId"]!; + private readonly RestClient _restClient = new(); + + public Task StartAsync(CancellationToken cancellationToken) + { + WriteLog("DeviceCheckService started"); + + _warningInterval = TimeSpan.Parse(_configuration["Environment:DeviceWarningInterval"]!); + + var checkInterval = TimeSpan.Parse(_configuration["Environment:DeviceCheckInterval"]!); + + _timer = new Timer((state) => DoWork().Wait(), null, TimeSpan.Zero, checkInterval); + + return Task.CompletedTask; + } + + private async Task DoWork() + { + WriteLog("Checking devices started"); + + var devices = await _database.GetDevicesAsync(); + + foreach (var device in devices) + { + WriteLog($"Checking device: {device.Name}"); + + var message = string.Empty; + + WriteLog($"Device {device.Name} last updated: {(device.LastUpdated == null ? "never" : device.LastUpdated.ToString())}"); + + if (device.LastUpdated == null) + { + message = $"Device has never reported: {device.Name}"; + } + else if (DateTime.UtcNow - device.LastUpdated > _warningInterval) + { + message = $"Device has not reported recently: {device.Name}"; + } + + if (message.Length > 0) + { + var encodedMessage = Uri.EscapeDataString(message); + + var restRequest = new RestRequest($"https://api.telegram.org/bot{_botToken}/sendMessage?chat_id={_chatId}&text={encodedMessage}"); + + await _restClient.GetAsync(restRequest); + } + } + + WriteLog("Checking devices finished"); + } + + public Task StopAsync(CancellationToken cancellationToken) + { + WriteLog("DeviceCheckService stopped"); + + _timer?.Change(Timeout.Infinite, 0); + + return Task.CompletedTask; + } + + private static void WriteLog(string message) + { + Console.WriteLine(message); + } +} \ No newline at end of file diff --git a/Environment/Service/Program.cs b/Environment/Service/Program.cs index a85f74a..e7ac160 100644 --- a/Environment/Service/Program.cs +++ b/Environment/Service/Program.cs @@ -20,6 +20,7 @@ public static class Program builder.Services.AddTransient(); builder.Services.AddHostedService(); + builder.Services.AddHostedService(); // -- -- diff --git a/Environment/Service/Service.csproj b/Environment/Service/Service.csproj index 75e4955..8c63f45 100644 --- a/Environment/Service/Service.csproj +++ b/Environment/Service/Service.csproj @@ -40,6 +40,7 @@ + diff --git a/Environment/Service/appsettings.Development.json b/Environment/Service/appsettings.Development.json index 1dc206b..1af1a15 100644 --- a/Environment/Service/appsettings.Development.json +++ b/Environment/Service/appsettings.Development.json @@ -1,25 +1,31 @@ { - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "Mqtt": { + "Server": "172.23.10.3" + }, + "Environment": { + "Database": { + "Host": "localhost", + "User": "postgres", + "Password": "postgres", + "Port": 5432, + "Name": "Environment", + "TrustServerCertificate": true }, - "Mqtt": { - "Server": "172.23.10.3" + "Hub": { + "Url": "http://localhost:8080/environment" }, - "Environment": { - "Database": { - "Host": "localhost", - "User": "postgres", - "Password": "postgres", - "Port": 5432, - "Name": "Environment", - "TrustServerCertificate": true - }, - "Hub": { - "Url": "http://localhost:8080/environment" - } - }, - "AuthorizationToken": "test-token" + "DeviceCheckInterval": "00:00:30", + "DeviceWarningInterval": "00:00:10" + }, + "AuthorizationToken": "test-token", + "Telegram": { + "BotToken": "", + "ChatId": "" + } } diff --git a/Environment/Service/appsettings.json b/Environment/Service/appsettings.json index be22a58..778dae6 100644 --- a/Environment/Service/appsettings.json +++ b/Environment/Service/appsettings.json @@ -21,7 +21,9 @@ }, "Hub": { "Url": "http://hub-server/environment" - } + }, + "DeviceCheckInterval": "01:00:00", + "DeviceWarningInterval": "01:00:00" }, "Telemetry": { "Endpoint": "http://signoz-otel-collector.platform:4317/"