2
0
mirror of https://github.com/9ParsonsB/Pulsar.git synced 2025-04-05 17:39:39 -04:00

Added Basic File Watched

Started structure of Status Service
Not Yet Tested
This commit is contained in:
Ben Parsons 2024-04-18 23:45:29 +10:00
parent ac30d3cd2a
commit 6f4330ef12
9 changed files with 137 additions and 32 deletions

View File

@ -2,7 +2,6 @@ namespace Pulsar.Features;
using Observatory.Framework.Files; using Observatory.Framework.Files;
using Observatory.Framework.Files.Journal; using Observatory.Framework.Files.Journal;
using Microsoft.AspNetCore.SignalR;
public class EventsHub : Hub<IEventsHub> public class EventsHub : Hub<IEventsHub>
{ {

View File

@ -0,0 +1,68 @@
namespace Pulsar.Features;
public interface IFileHandlerService
{
void HandleFile(string path);
}
public class FileHandlerService(ILogger<FileHandlerService> logger, IStatusService statusService) : IFileHandlerService
{
public static readonly string MarketFileName = "Market.json";
public static readonly string StatusFileName = "Status.json";
public static readonly string OutfittingFileName = "Outfitting.json";
public static readonly string ShipyardFileName = "Shipyard.json";
public static readonly string ModulesFileName = "Modules.json";
public static readonly string JournalFileName = "Journal.";
public static readonly string RouteFileName = "Route.json";
public static readonly string CargoFileName = "Cargo.json";
public static readonly string BackpackFileName = "Backpack.json";
public static readonly string ModulesInfoFileName = "ModulesInfo.json";
public static readonly string ShipLockerFileName = "ShipLocker.json";
public static readonly string NavRouteFileName = "NavRoute.json";
public static readonly string[] AllFileNames =
[
MarketFileName,
StatusFileName,
OutfittingFileName,
ShipyardFileName,
ModulesFileName,
JournalFileName,
RouteFileName,
CargoFileName,
BackpackFileName,
ModulesInfoFileName,
ShipLockerFileName,
NavRouteFileName
];
private readonly Dictionary<string, IJournalHandler> Handlers = new()
{
{ StatusFileName, statusService },
};
public void HandleFile(string path)
{
var fileInfo = new FileInfo(path);
var fileName = fileInfo.Name;
// only scan the file if we recognize it
var match = AllFileNames.FirstOrDefault(
f => fileName.StartsWith(f, StringComparison.InvariantCultureIgnoreCase));
if (string.IsNullOrWhiteSpace(match))
{
logger.LogWarning("File {FileName} is not recognized", fileName);
return;
}
if (Handlers.TryGetValue(match, out var handler))
{
logger.LogInformation("Handling file {FileName}", fileName);
handler.HandleFile(fileInfo.Name);
return;
}
logger.LogInformation("File {FileName} was not handled", fileName);
}
}

View File

@ -0,0 +1,40 @@
namespace Pulsar.Features;
public class FileWatcherService(IOptions<PulsarConfiguration> options, IFileHandlerService fileHandlerService) : IHostedService
{
private FileSystemWatcher watcher = null!;
public Task StartAsync(CancellationToken cancellationToken)
{
watcher = new FileSystemWatcher(options.Value.JournalDirectory)
{
EnableRaisingEvents = true,
IncludeSubdirectories = true
};
watcher.BeginInit();
watcher.Created += HandleFileChanged;
watcher.Changed += HandleFileChanged;
watcher.Renamed += HandleFileChanged; // ?
watcher.IncludeSubdirectories = false;
watcher.EnableRaisingEvents = true;
watcher.NotifyFilter = NotifyFilters.LastWrite & NotifyFilters.Size & NotifyFilters.FileName;
watcher.EndInit();
return Task.CompletedTask;
}
private void HandleFileChanged(object sender, FileSystemEventArgs e)
{
fileHandlerService.HandleFile(e.FullPath);
}
public Task StopAsync(CancellationToken cancellationToken)
{
watcher.Dispose();
return Task.CompletedTask;
}
}

View File

@ -0,0 +1,8 @@
namespace Pulsar.Features.Journal;
[ApiController]
[Route("api/journal")]
public class JournalController : ControllerBase
{
}

View File

@ -1,5 +1,3 @@
using Microsoft.AspNetCore.SignalR;
namespace Pulsar.Features.Status; namespace Pulsar.Features.Status;
[ApiController] [ApiController]

View File

@ -0,0 +1,18 @@
namespace Pulsar.Features.Status;
public class StatusService : IStatusService
{
public void HandleFile(string fileInfo)
{
throw new NotImplementedException();
}
}
public interface IStatusService : IJournalHandler
{
}
public interface IJournalHandler
{
void HandleFile(string fileInfo);
}

View File

@ -1,9 +1,11 @@
global using Pulsar; global using Pulsar;
global using Pulsar.Utils; global using Pulsar.Utils;
global using Pulsar.Features.Status;
global using System.Text; global using System.Text;
global using System.Text.Json; global using System.Text.Json;
global using System.Text.Json.Nodes; global using System.Text.Json.Nodes;
global using System.Text.Json.Serialization; global using System.Text.Json.Serialization;
global using Microsoft.AspNetCore.Mvc; global using Microsoft.AspNetCore.Mvc;
global using Microsoft.AspNetCore.SignalR;
global using Microsoft.Extensions.Options; global using Microsoft.Extensions.Options;

View File

@ -1,29 +0,0 @@
namespace Pulsar;
public static class LoggingUtils
{
internal static void LogError(Exception ex, string context)
{
var docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var errorMessage = new StringBuilder();
var timestamp = DateTime.Now.ToString("G");
errorMessage
.AppendLine($"[{timestamp}] Error encountered in Elite Observatory {context}")
.AppendLine(FormatExceptionMessage(ex))
.AppendLine();
File.AppendAllText(docPath + Path.DirectorySeparatorChar + "ObservatoryCrashLog.txt",
errorMessage.ToString());
}
static string FormatExceptionMessage(Exception ex, bool inner = false)
{
var errorMessage = new StringBuilder();
errorMessage
.AppendLine($"{(inner ? "Inner e" : "E")}xception message: {ex.Message}")
.AppendLine("Stack trace:")
.AppendLine(ex.StackTrace);
if (ex.InnerException != null)
errorMessage.AppendLine(FormatExceptionMessage(ex.InnerException, true));
return errorMessage.ToString();
}
}

View File

@ -19,6 +19,7 @@ builder.Services.Configure<JsonOptions>(options =>
options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull); options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull);
builder.Services.AddReverseProxy().LoadFromConfig(builder.Configuration.GetSection("ReverseProxy")); builder.Services.AddReverseProxy().LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));
builder.Services.AddSpaYarp(); builder.Services.AddSpaYarp();
builder.Services.AddHostedService<FileWatcherService>();
var app = builder.Build(); var app = builder.Build();