Implemented power state and task streaming over signalr

This commit is contained in:
2024-12-30 01:16:23 +01:00
parent 394d8b05ed
commit 0bd9074494
14 changed files with 436 additions and 4 deletions

View File

@@ -0,0 +1,22 @@
using System.Text.Json;
using MoonCore.Attributes;
using MoonCore.Extended.Helpers;
using MoonlightServers.Daemon.Configuration;
namespace MoonlightServers.Daemon.Helpers;
[Singleton]
public class AccessTokenHelper
{
private readonly AppConfiguration Configuration;
public AccessTokenHelper(AppConfiguration configuration)
{
Configuration = configuration;
}
public bool Process(string accessToken, out Dictionary<string, JsonElement> data)
{
return JwtHelper.TryVerifyAndDecodePayload(Configuration.Security.Token, accessToken, out data);
}
}

View File

@@ -0,0 +1,7 @@
namespace MoonlightServers.Daemon.Helpers;
public class ServerConsoleConnection
{
public DateTime AuthenticatedUntil { get; set; }
public int ServerId { get; set; }
}

View File

@@ -0,0 +1,44 @@
using Microsoft.AspNetCore.SignalR;
using MoonlightServers.Daemon.Models;
using MoonlightServers.DaemonShared.Enums;
namespace MoonlightServers.Daemon.Helpers;
public class ServerConsoleMonitor
{
private readonly Server Server;
private readonly IHubClients Clients;
public ServerConsoleMonitor(Server server, IHubClients clients)
{
Server = server;
Clients = clients;
}
public void Initialize()
{
Server.StateMachine.OnTransitioned += OnPowerStateChanged;
Server.OnTaskAdded += OnTaskNotify;
}
public void Destroy()
{
Server.StateMachine.OnTransitioned -= OnPowerStateChanged;
}
private async Task OnTaskNotify(string task)
{
await Clients.Group($"server-{Server.Configuration.Id}").SendAsync(
"TaskNotify",
task
);
}
private async Task OnPowerStateChanged(ServerState serverState)
{
await Clients.Group($"server-{Server.Configuration.Id}").SendAsync(
"PowerStateChanged",
serverState.ToString()
);
}
}