Skip to content

Commit

Permalink
Make Device nullable.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Jul 26, 2023
1 parent 2ac886a commit c8d1e9c
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions Server/Hubs/AgentHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,16 @@ public AgentHub(IDataService dataService,
// TODO: Replace with new invoke capability in .NET 7 in ScriptingController.
public static IMemoryCache ApiScriptResults { get; } = new MemoryCache(new MemoryCacheOptions());

private Device Device
private Device? Device
{
get
{
if (Context.Items["Device"] is Device device)
{
return device;
}
else
{
throw new InvalidOperationException("Device not set.");
}
_logger.LogWarning("Device has not been set in the context items.");
return null;
}
set
{
Expand All @@ -67,6 +65,11 @@ private Device Device

public Task Chat(string message, bool disconnected, string browserConnectionId)
{
if (Device is null)
{
return Task.CompletedTask;
}

if (_circuitManager.TryGetConnection(browserConnectionId, out var connection))
{
return connection.InvokeCircuitEvent(CircuitEventName.ChatReceived, Device.ID, $"{Device.DeviceName}", message, disconnected);
Expand All @@ -80,6 +83,11 @@ public Task Chat(string message, bool disconnected, string browserConnectionId)

public async Task CheckForPendingScriptRuns()
{
if (Device is null)
{
return;
}

var authToken = _expiringTokenService.GetToken(Time.Now.AddMinutes(AppConstants.ScriptRunExpirationMinutes));
var scriptRuns = await _dataService.GetPendingScriptRuns(Device.ID);
foreach (var run in scriptRuns)
Expand Down Expand Up @@ -221,7 +229,7 @@ public string GetServerUrl()

public string GetServerVerificationToken()
{
return $"{Device.ServerVerificationToken}";
return $"{Device?.ServerVerificationToken}";
}

public override Task OnDisconnectedAsync(Exception? exception)
Expand Down Expand Up @@ -288,6 +296,10 @@ public Task SendLogs(string logChunk, string requesterConnectionId)
}
public void SetServerVerificationToken(string verificationToken)
{
if (Device is null)
{
return;
}
Device.ServerVerificationToken = verificationToken;
_dataService.SetServerVerificationToken(Device.ID, verificationToken);
}
Expand Down

0 comments on commit c8d1e9c

Please sign in to comment.