diff --git a/Watcher/Controllers/HomeController.cs b/Watcher/Controllers/HomeController.cs index 2032eb2..5579444 100644 --- a/Watcher/Controllers/HomeController.cs +++ b/Watcher/Controllers/HomeController.cs @@ -4,8 +4,8 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; // Local Namespaces using Watcher.Data; -using Watcher.Models; using Watcher.ViewModels; +using Watcher.Services; namespace Watcher.Controllers { @@ -52,9 +52,9 @@ namespace Watcher.Controllers .ToListAsync(), Containers = await _context.Containers .OrderBy(s => s.Name) - .ToListAsync() + .ToListAsync(), }; - + //ViewBag.NetworkConnection = ReturnNetworkStatus(); return View(viewModel); } @@ -88,5 +88,10 @@ namespace Watcher.Controllers return PartialView("_DashboardStats", model); } + public String ReturnNetworkStatus() + { + return "OK"; + } + } } diff --git a/Watcher/Program.cs b/Watcher/Program.cs index c20d108..fd948b8 100644 --- a/Watcher/Program.cs +++ b/Watcher/Program.cs @@ -5,6 +5,7 @@ using Serilog; using Watcher.Data; using Watcher.Models; +using Watcher.Services; //using Watcher.Services; //using Watcher.Workers; @@ -15,7 +16,7 @@ var builder = WebApplication.CreateBuilder(args); // Serilog konfigurieren – nur Logs, die nicht von Microsoft stammen Log.Logger = new LoggerConfiguration() .MinimumLevel.Information() - .MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Warning) // <-- + .MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Warning) .Enrich.FromLogContext() .WriteTo.File( "logs/watcher-.log", @@ -33,6 +34,9 @@ builder.Services.AddControllersWithViews(); // HttpContentAccessor builder.Services.AddHttpContextAccessor(); +// Background Services +builder.Services.AddHostedService(); + // ---------- Konfiguration ---------- DotNetEnv.Env.Load(); diff --git a/Watcher/Services/NetworkCheck.cs b/Watcher/Services/NetworkCheck.cs new file mode 100644 index 0000000..a4baf03 --- /dev/null +++ b/Watcher/Services/NetworkCheck.cs @@ -0,0 +1,59 @@ +using System.Composition; +using System.Net.NetworkInformation; + +namespace Watcher.Services; +public class NetworkCheck : BackgroundService +{ + private readonly ILogger _logger; + + public static String networkCheckResult + { + get { return networkCheckResult; } + set { networkCheckResult = value; } + + } + + public NetworkCheck(ILogger logger) + { + _logger = logger; + } + + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + var timer = new PeriodicTimer(TimeSpan.FromSeconds(30)); + + while (await timer.WaitForNextTickAsync(stoppingToken)) + { + // Hintergrundprozess abwarten + await checkConnectionAsync(); + + // 5 Sekdunden Offset + await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken); + } + } + + public Task checkConnectionAsync() + { + _logger.LogInformation("Networkcheck started."); + + string host = "8.8.8.8"; + Ping p = new Ping(); + + try + { + PingReply reply = p.Send(host, 3000); + if (reply.Status == IPStatus.Success) + { + _logger.LogInformation("Ping successfull. Watcher is online."); + } + } + catch + { + _logger.LogError("Ping failed. Watcher appears to have no network connection."); + } + + _logger.LogInformation("Networkcheck finished."); + + return Task.CompletedTask; + } +} \ No newline at end of file diff --git a/Watcher/Views/Home/_DashboardStats.cshtml b/Watcher/Views/Home/_DashboardStats.cshtml index 3e23ccb..ee6e81c 100644 --- a/Watcher/Views/Home/_DashboardStats.cshtml +++ b/Watcher/Views/Home/_DashboardStats.cshtml @@ -59,7 +59,7 @@
Netzwerk - OK + NetworkConnection
diff --git a/Watcher/persistence/watcher.db-shm b/Watcher/persistence/watcher.db-shm deleted file mode 100644 index fe9ac28..0000000 Binary files a/Watcher/persistence/watcher.db-shm and /dev/null differ diff --git a/Watcher/persistence/watcher.db-wal b/Watcher/persistence/watcher.db-wal deleted file mode 100644 index e69de29..0000000