Add timer to send alerts

This commit is contained in:
2024-05-28 16:44:57 -04:00
parent 6bf61966b8
commit 4917493f21
5 changed files with 109 additions and 22 deletions

View File

@@ -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);
}
}

View File

@@ -20,6 +20,7 @@ public static class Program
builder.Services.AddTransient<Database>();
builder.Services.AddHostedService<MessageHandler>();
builder.Services.AddHostedService<DeviceCheckService>();
// -- --

View File

@@ -40,6 +40,7 @@
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="8.0.1" />
<PackageReference Include="MQTTnet.AspNetCore" Version="4.3.3.952" />
<PackageReference Include="RestSharp" Version="111.1.0" />
</ItemGroup>
<ItemGroup>

View File

@@ -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": ""
}
}

View File

@@ -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/"