-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
show agent status from websocket on frontend
- Loading branch information
Showing
16 changed files
with
472 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
namespace OpenAlprWebhookProcessor.Settings | ||
{ | ||
public class AgentStatus | ||
{ | ||
public bool IsConnected { get; set; } | ||
|
||
public string Hostname { get; set; } | ||
|
||
public string Version { get; set; } | ||
|
||
public int CpuCores { get; set; } | ||
|
||
public int CpuUsagePercent { get; set; } | ||
|
||
public long DaemonUptimeSeconds { get; set; } | ||
|
||
public long DiskFreeBytes { get; set; } | ||
|
||
public long SystemUptimeSeconds { get; set; } | ||
|
||
public long AgentEpochMs { get; set; } | ||
|
||
public bool AlprdActive { get; set; } | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
OpenAlprWebhookProcessor/Settings/GetAgent/GetAgentRequestHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
OpenAlprWebhookProcessor/Settings/GetAgentStatus/GetAgentStatusRequestHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using OpenAlprWebhookProcessor.Data; | ||
using OpenAlprWebhookProcessor.WebhookProcessor.OpenAlprWebsocket; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace OpenAlprWebhookProcessor.Settings | ||
{ | ||
public class GetAgentStatusRequestHandler | ||
{ | ||
private readonly ProcessorContext _processorContext; | ||
|
||
private readonly WebsocketClientOrganizer _websocketClientOrganizer; | ||
|
||
public GetAgentStatusRequestHandler( | ||
ProcessorContext processorContext, | ||
WebsocketClientOrganizer websocketClientOrganizer) | ||
{ | ||
_processorContext = processorContext; | ||
_websocketClientOrganizer = websocketClientOrganizer; | ||
} | ||
|
||
public async Task<AgentStatus> HandleAsync(CancellationToken cancellationToken) | ||
{ | ||
var agentUid = await _processorContext.Agents | ||
.AsNoTracking() | ||
.Select(x => x.Uid) | ||
.FirstOrDefaultAsync(cancellationToken); | ||
|
||
if (agentUid == null) | ||
{ | ||
return new AgentStatus() | ||
{ | ||
IsConnected = false, | ||
}; | ||
} | ||
|
||
var agentStatus = await _websocketClientOrganizer.GetAgentStatusAsync(agentUid, cancellationToken); | ||
|
||
if (agentStatus == null) | ||
{ | ||
return new AgentStatus() | ||
{ | ||
IsConnected = false, | ||
}; | ||
} | ||
|
||
return new AgentStatus() | ||
{ | ||
AgentEpochMs = agentStatus.AgentEpochMs, | ||
AlprdActive = agentStatus.AgentStatus.AlprdActive, | ||
Hostname = agentStatus.AgentStatus.AgentHostname, | ||
IsConnected = true, | ||
CpuCores = agentStatus.AgentStatus.CpuCores, | ||
CpuUsagePercent = agentStatus.AgentStatus.CpuUsagePercent, | ||
DaemonUptimeSeconds = agentStatus.AgentStatus.DaemonUptimeSeconds, | ||
DiskFreeBytes = agentStatus.AgentStatus.DiskDriveFreeBytes, | ||
Version = agentStatus.Version, | ||
SystemUptimeSeconds = agentStatus.AgentStatus.SystemUptimeSeconds, | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
OpenAlprWebhookProcessor/WebhookProcessor/OpenAlprWebsocket/WebsocketClientOrganizer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Net.WebSockets; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace OpenAlprWebhookProcessor.WebhookProcessor.OpenAlprWebsocket | ||
{ | ||
public class WebsocketClientOrganizer : BackgroundService | ||
{ | ||
private readonly ConcurrentDictionary<string, OpenAlprWebsocketClient> _connectedClients; | ||
|
||
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource(); | ||
|
||
private readonly ILogger<WebsocketClientOrganizer> _logger; | ||
|
||
public WebsocketClientOrganizer(ILogger<WebsocketClientOrganizer> logger) | ||
{ | ||
_logger = logger; | ||
_connectedClients = new ConcurrentDictionary<string, OpenAlprWebsocketClient>(); | ||
} | ||
|
||
protected override Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
public bool AddAgent( | ||
string agentId, | ||
OpenAlprWebsocketClient webSocketClient) | ||
{ | ||
return _connectedClients.TryAdd( | ||
agentId, | ||
webSocketClient); | ||
} | ||
|
||
public async Task RemoveAgentAsync( | ||
string agentId, | ||
CancellationToken cancellationToken) | ||
{ | ||
if (_connectedClients.TryRemove(agentId, out var webSocketClient)) | ||
{ | ||
await webSocketClient.CloseConnectionAsync(cancellationToken); | ||
} | ||
} | ||
|
||
public async Task<AgentStatusResponse> GetAgentStatusAsync( | ||
string agentId, | ||
CancellationToken cancellationToken) | ||
{ | ||
var agentExists = _connectedClients.TryGetValue(agentId, out var webSocketClient); | ||
|
||
if (!agentExists) | ||
{ | ||
throw new ArgumentException("AgentId is not connected."); | ||
} | ||
|
||
var transactionId = Guid.NewGuid(); | ||
|
||
await webSocketClient.SendGetAgentStatusRequestAsync(transactionId, cancellationToken); | ||
|
||
var stopwatch = new Stopwatch(); | ||
stopwatch.Start(); | ||
|
||
while (stopwatch.ElapsedMilliseconds < 100000) | ||
{ | ||
if (webSocketClient.TryGetAgentStatusResponse(transactionId, out var agentStatusResponse)) | ||
{ | ||
return agentStatusResponse; | ||
} | ||
|
||
await Task.Delay(1000, cancellationToken); | ||
} | ||
|
||
throw new TimeoutException("Agent did not respond to request."); | ||
} | ||
} | ||
} |
Oops, something went wrong.