Add MonitorWorker.cs
This commit is contained in:
@@ -56,6 +56,7 @@ namespace UCS_Status_Monitor.Controllers
|
||||
var newSystem = new Models.UCSSystem
|
||||
{
|
||||
ComputerName = telemetry.ComputerName,
|
||||
ConnectionState = true,
|
||||
LastMessageTime = DateTime.Now,
|
||||
StartTime = telemetry.StartTime != null ? DateTime.Parse(telemetry.StartTime) : DateTime.MinValue,
|
||||
LastMessageClient = telemetry.Date != null ? DateTime.Parse(telemetry.StartTime) : DateTime.MinValue,
|
||||
@@ -84,6 +85,7 @@ namespace UCS_Status_Monitor.Controllers
|
||||
else
|
||||
{
|
||||
system.LastMessageTime = DateTime.Now;
|
||||
system.ConnectionState = true;
|
||||
system.StartTime = telemetry.StartTime != null ? DateTime.Parse(telemetry.StartTime) : DateTime.MinValue;
|
||||
system.LastMessageClient = telemetry.Date != null ? DateTime.Parse(telemetry.StartTime) : DateTime.MinValue;
|
||||
system.Uptime = telemetry.Uptime ?? "??";
|
||||
|
||||
@@ -9,6 +9,7 @@ namespace UCS_Status_Monitor.Models
|
||||
public class UCSSystem
|
||||
{
|
||||
public int UCSSystemId { get; set; }
|
||||
public Boolean ConnectionState { get; set; }
|
||||
public string ComputerName { get; set; }
|
||||
public DateTime StartTime { get; set; }
|
||||
public DateTime LastMessageTime { get; set; }
|
||||
|
||||
65
UCS_Status_Monitor/MonitorWorker.cs
Normal file
65
UCS_Status_Monitor/MonitorWorker.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using UCS_Status_Monitor.Database;
|
||||
|
||||
namespace UCS_Status_Monitor
|
||||
{
|
||||
public class MonitorWorker : BackgroundService
|
||||
{
|
||||
private readonly ILogger<MonitorWorker> _logger;
|
||||
|
||||
public MonitorWorker(ILogger<MonitorWorker> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
//public override async Task StartAsync(CancellationToken stoppingToken)
|
||||
//{
|
||||
// Debug.WriteLine("StartAsync");
|
||||
// using var dbContext = new MonitorDbContext();
|
||||
// dbContext.Database.EnsureCreated();
|
||||
//}
|
||||
|
||||
//public override async Task StopAsync(CancellationToken stoppingToken)
|
||||
//{
|
||||
// Debug.WriteLine("StopAsync");
|
||||
//}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
using var dbContext = new MonitorDbContext();
|
||||
dbContext.Database.EnsureCreated();
|
||||
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
//_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
|
||||
Debug.WriteLine($"Worker running at: {DateTimeOffset.Now}");
|
||||
|
||||
foreach (var system in dbContext.UCSSystems)
|
||||
{
|
||||
if (system.ConnectionState == false)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
var seconds = (DateTime.Now - system.LastMessageTime).Seconds;
|
||||
if (seconds > 20)
|
||||
{
|
||||
system.ConnectionState = false;
|
||||
dbContext.Update(system);
|
||||
}
|
||||
}
|
||||
dbContext.SaveChanges();
|
||||
|
||||
await Task.Delay(1000, stoppingToken);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,5 @@
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace UCS_Status_Monitor
|
||||
{
|
||||
|
||||
@@ -1,15 +1,8 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.HttpsPolicy;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace UCS_Status_Monitor
|
||||
{
|
||||
@@ -25,9 +18,8 @@ namespace UCS_Status_Monitor
|
||||
// This method gets called by the runtime. Use this method to add services to the container.
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
//services.AddControllers();
|
||||
services.AddControllersWithViews();
|
||||
//services.AddDirectoryBrowser();
|
||||
services.AddControllersWithViews();
|
||||
services.AddHostedService<MonitorWorker>();
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
@@ -39,20 +31,10 @@ namespace UCS_Status_Monitor
|
||||
}
|
||||
|
||||
app.UseStaticFiles();
|
||||
|
||||
//app.UseDirectoryBrowser();
|
||||
|
||||
//app.UseHttpsRedirection();
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
//app.UseEndpoints(endpoints =>
|
||||
//{
|
||||
// endpoints.MapControllers();
|
||||
//});
|
||||
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapControllerRoute(
|
||||
@@ -60,15 +42,8 @@ namespace UCS_Status_Monitor
|
||||
pattern: "{controller=Home}/{action=Index}/{id?}");
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
using var dbContext = new Database.MonitorDbContext();
|
||||
dbContext.Database.EnsureCreated();
|
||||
|
||||
//using var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope();
|
||||
//var context = serviceScope.ServiceProvider.GetRequiredService<Database.MonitorDbContext>();
|
||||
//context.Database.EnsureCreated();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,63 +22,63 @@
|
||||
|
||||
|
||||
|
||||
@foreach (var item in Model)
|
||||
@foreach (var system in Model)
|
||||
{
|
||||
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<div class="alert alert-danger" role="alert">
|
||||
@item.ComputerName
|
||||
@system.ComputerName
|
||||
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
|
||||
Launch @item.ComputerName
|
||||
Launch @system.ComputerName
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col">
|
||||
<div class="alert alert-danger" role="alert">
|
||||
@item.ComputerName
|
||||
@system.ComputerName
|
||||
</div>
|
||||
</div>
|
||||
<div class="col">
|
||||
<div class="alert alert-danger" role="alert">
|
||||
@item.ComputerName
|
||||
@system.ComputerName
|
||||
</div>
|
||||
</div>
|
||||
<div class="col">
|
||||
<div class="alert alert-danger" role="alert">
|
||||
@item.ComputerName
|
||||
@system.ComputerName
|
||||
</div>
|
||||
</div>
|
||||
<div class="col">
|
||||
<div class="alert alert-danger" role="alert">
|
||||
@item.ComputerName
|
||||
@system.ComputerName
|
||||
</div>
|
||||
</div>
|
||||
@*<div class="w-100"></div>*@
|
||||
<div class="col">
|
||||
<div class="alert alert-danger" role="alert">
|
||||
@item.ComputerName
|
||||
@system.ComputerName
|
||||
</div>
|
||||
</div>
|
||||
<div class="col">
|
||||
<div class="alert alert-danger" role="alert">
|
||||
@item.ComputerName
|
||||
@system.ComputerName
|
||||
</div>
|
||||
</div>
|
||||
<div class="col">
|
||||
<div class="alert alert-danger" role="alert">
|
||||
@item.ComputerName
|
||||
@system.ComputerName
|
||||
</div>
|
||||
</div>
|
||||
<div class="col">
|
||||
<div class="alert alert-danger" role="alert">
|
||||
@item.ComputerName
|
||||
@system.ComputerName
|
||||
</div>
|
||||
</div>
|
||||
<div class="col">
|
||||
<div class="alert alert-danger" role="alert">
|
||||
@item.ComputerName
|
||||
@system.ComputerName
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -88,53 +88,53 @@
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<div class="alert alert-danger" role="alert">
|
||||
@item.ComputerName
|
||||
@system.ComputerName
|
||||
</div>
|
||||
</div>
|
||||
<div class="col">
|
||||
<div class="alert alert-danger" role="alert">
|
||||
@item.ComputerName
|
||||
@system.ComputerName
|
||||
</div>
|
||||
</div>
|
||||
<div class="col">
|
||||
<div class="alert alert-danger" role="alert">
|
||||
@item.ComputerName
|
||||
@system.ComputerName
|
||||
</div>
|
||||
</div>
|
||||
<div class="col">
|
||||
<div class="alert alert-danger" role="alert">
|
||||
@item.ComputerName
|
||||
@system.ComputerName
|
||||
</div>
|
||||
</div>
|
||||
<div class="col">
|
||||
<div class="alert alert-danger" role="alert">
|
||||
@item.ComputerName
|
||||
@system.ComputerName
|
||||
</div>
|
||||
</div>
|
||||
@*<div class="w-100"></div>*@
|
||||
<div class="col">
|
||||
<div class="alert alert-danger" role="alert">
|
||||
@item.ComputerName
|
||||
@system.ComputerName
|
||||
</div>
|
||||
</div>
|
||||
<div class="col">
|
||||
<div class="alert alert-danger" role="alert">
|
||||
@item.ComputerName
|
||||
@system.ComputerName
|
||||
</div>
|
||||
</div>
|
||||
<div class="col">
|
||||
<div class="alert alert-danger" role="alert">
|
||||
@item.ComputerName
|
||||
@system.ComputerName
|
||||
</div>
|
||||
</div>
|
||||
<div class="col">
|
||||
<div class="alert alert-danger" role="alert">
|
||||
@item.ComputerName
|
||||
@system.ComputerName
|
||||
</div>
|
||||
</div>
|
||||
<div class="col">
|
||||
<div class="alert alert-danger" role="alert">
|
||||
@item.ComputerName
|
||||
@system.ComputerName
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -143,30 +143,30 @@
|
||||
|
||||
|
||||
<div class="alert alert-danger" role="alert">
|
||||
@item.ComputerName
|
||||
@system.ComputerName
|
||||
</div>
|
||||
|
||||
<div class="alert alert-success" role="alert">
|
||||
@item.ComputerName
|
||||
@system.ComputerName
|
||||
</div>
|
||||
|
||||
|
||||
<div class="card" style="width: 18rem;">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title">@item.ComputerName</h5>
|
||||
<p class="card-text">@item.UCSVersion</p>
|
||||
<h5 class="card-title">@system.ComputerName</h5>
|
||||
<p class="card-text">@system.UCSVersion</p>
|
||||
<a href="#" class="btn btn-primary">Go somewhere</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<!-- Button trigger modal -->
|
||||
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
|
||||
Launch @item.ComputerName
|
||||
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="@(system.ComputerName + "Modal")">
|
||||
Launch @system.ComputerName
|
||||
</button>
|
||||
|
||||
<!-- Modal -->
|
||||
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
|
||||
<div class="modal fade" id="@(system.ComputerName + "Modal")" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
|
||||
<div class="modal-dialog" role="document">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
@@ -182,18 +182,29 @@
|
||||
<tr>
|
||||
<th scope="col">BoxID</th>
|
||||
<th scope="col">Name</th>
|
||||
<th scope="col">State</th>
|
||||
<th scope="col">Last Message</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var dev in item.Devices)
|
||||
@foreach (var dev in system.Devices.OrderBy(s => s.BoxID))
|
||||
{
|
||||
<tr>
|
||||
<td>@dev.BoxID</td>
|
||||
<td>@dev.Name</td>
|
||||
<td>@dev.State</td>
|
||||
</tr>
|
||||
}
|
||||
@if (dev.State == false)
|
||||
{
|
||||
<tr class="table-danger">
|
||||
<td>@dev.BoxID</td>
|
||||
<td>@dev.Name</td>
|
||||
<td>@dev.LastStateChange</td>
|
||||
</tr>
|
||||
}
|
||||
else
|
||||
{
|
||||
<tr class="table-success">
|
||||
<td>@dev.BoxID</td>
|
||||
<td>@dev.Name</td>
|
||||
<td>@dev.LastStateChange</td>
|
||||
</tr>
|
||||
}
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
@@ -203,8 +214,6 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
}
|
||||
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user