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

Rework JournalHandler Again

Reduce Code Dupe
Finished ModulesInfo
Started ShipLocker
This commit is contained in:
Ben Parsons 2024-04-21 23:40:44 +10:00
parent b8967814d5
commit b44d822eb9
6 changed files with 138 additions and 59 deletions

View File

@ -1,3 +1,6 @@
using Pulsar.Features.ModulesInfo;
using Pulsar.Features.ShipLocker;
namespace Pulsar.Features;
public interface IFileHandler
@ -7,7 +10,11 @@ public interface IFileHandler
public interface IFileHandlerService : IFileHandler;
public class FileHandlerService(ILogger<FileHandlerService> logger, IStatusService statusService) : IFileHandlerService
public class FileHandlerService(
ILogger<FileHandlerService> logger,
IStatusService statusService,
IShipLockerService shipLockerService,
IModulesInfoService modulesService) : IFileHandlerService
{
public static readonly string MarketFileName = "Market.json";
public static readonly string StatusFileName = "Status.json";
@ -41,7 +48,9 @@ public class FileHandlerService(ILogger<FileHandlerService> logger, IStatusServi
private readonly Dictionary<string, IJournalHandler> Handlers = new()
{
{ StatusFileName, statusService }
{ StatusFileName, statusService },
{ ModulesInfoFileName, modulesService },
{ ShipLockerFileName, shipLockerService }
};
public async Task HandleFile(string path)

View File

@ -8,7 +8,7 @@ namespace Pulsar.Features;
public interface IJournalHandler : IFileHandler
{
string FileName { get; }
Task HandleFile(string filePath);
public bool ValidateFile(string filePath);
}
@ -21,3 +21,38 @@ public interface IJournalHandler<T> : IJournalHandler
{
Task<T> Get();
}
public abstract class JournalHandlerBase<T>(ILogger logger) : IJournalHandler<T>
where T: IJournal
{
public abstract string FileName { get; }
public abstract Task HandleFile(string filePath);
public bool ValidateFile(string filePath)
{
if (!File.Exists(filePath))
{
logger.LogWarning("Journal file {JournalFile} does not exist", filePath);
return false;
}
var fileInfo = new FileInfo(filePath);
if (!string.Equals(fileInfo.Name, FileName, StringComparison.InvariantCultureIgnoreCase))
{
logger.LogWarning("Journal file {JournalFile} is not a {NameOfCurrentHandler} file", filePath, nameof(T));
return false;
}
if (fileInfo.Length == 0)
{
logger.LogWarning("Journal file {JournalFile} is empty", filePath);
return false;
}
return true;
}
public abstract Task<T> Get();
}

View File

@ -4,21 +4,47 @@ using Observatory.Framework.Files;
public interface IModulesInfoService : IJournalHandler<ModuleInfoFile>;
public class ModulesInfoService : IModulesInfoService
public class ModulesInfoService(
ILogger<ModulesInfoService> logger,
IOptions<PulsarConfiguration> options,
IEventHubContext hub)
: JournalHandlerBase<ModuleInfoFile>(logger), IModulesInfoService
{
public string FileName => FileHandlerService.ModulesInfoFileName;
public Task HandleFile(string filePath)
public override string FileName => FileHandlerService.ModulesInfoFileName;
public override async Task HandleFile(string filePath)
{
throw new NotImplementedException();
if (!ValidateFile(filePath))
{
return;
}
var file = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
var moduleInfo = await JsonSerializer.DeserializeAsync<ModuleInfoFile>(file);
if (moduleInfo == null)
{
logger.LogWarning("Failed to deserialize status file {FilePath}", filePath);
return;
}
await hub.Clients.All.ModuleInfoUpdated(moduleInfo);
}
public bool ValidateFile(string filePath)
public override async Task<ModuleInfoFile> Get()
{
throw new NotImplementedException();
}
var moduleInfoFile = Path.Combine(options.Value.JournalDirectory, FileName);
public Task<ModuleInfoFile> Get()
{
throw new NotImplementedException();
if (!ValidateFile(moduleInfoFile))
{
return new ModuleInfoFile();
}
await using var file = File.Open(moduleInfoFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
var moduleInfo = await JsonSerializer.DeserializeAsync<ModuleInfoFile>(file);
if (moduleInfo != null) return moduleInfo;
logger.LogWarning("Failed to deserialize module info file {ModuleInfoFile}", moduleInfoFile);
return new ModuleInfoFile();
}
}

View File

@ -0,0 +1,12 @@
namespace Pulsar.Features.ShipLocker;
[ApiController]
[Route("api/[controller]")]
public class ShipLockerController(IShipLockerService shipLockerService) : ControllerBase
{
[HttpGet]
public async Task<IActionResult> Get()
{
return Ok(await shipLockerService.Get());
}
}

View File

@ -0,0 +1,21 @@
namespace Pulsar.Features.ShipLocker;
using Observatory.Framework.Files.Journal.Odyssey;
public interface IShipLockerService : IJournalHandler<ShipLockerMaterials>;
public class ShipLockerService(ILogger<ShipLockerService> logger)
: JournalHandlerBase<ShipLockerMaterials>(logger), IShipLockerService
{
public override string FileName => FileHandlerService.ShipLockerFileName;
public override Task<ShipLockerMaterials> Get()
{
throw new NotImplementedException();
}
public override Task HandleFile(string filePath)
{
throw new NotImplementedException();
}
}

View File

@ -3,11 +3,13 @@ namespace Pulsar.Features.Status;
using Observatory.Framework.Files;
public interface IStatusService : IJournalHandler<Status>;
public class StatusService(ILogger<StatusService> logger, IOptions<PulsarConfiguration> options, IEventHubContext hub) : IStatusService
{
public string FileName => FileHandlerService.StatusFileName;
public async Task HandleFile(string filePath)
public class StatusService(ILogger<StatusService> logger, IOptions<PulsarConfiguration> options, IEventHubContext hub)
: JournalHandlerBase<Status>(logger), IStatusService
{
public override string FileName => FileHandlerService.StatusFileName;
public override async Task HandleFile(string filePath)
{
if (!ValidateFile(filePath))
{
@ -26,32 +28,7 @@ public class StatusService(ILogger<StatusService> logger, IOptions<PulsarConfigu
await hub.Clients.All.StatusUpdated(status);
}
public bool ValidateFile(string filePath)
{
if (!File.Exists(filePath))
{
logger.LogWarning("Status file {StatusFile} does not exist", filePath);
return false;
}
var fileInfo = new FileInfo(filePath);
if (!string.Equals(fileInfo.Name, FileName, StringComparison.InvariantCultureIgnoreCase))
{
logger.LogWarning("File {StatusFile} is not a status file", filePath);
return false;
}
if (fileInfo.Length == 0)
{
logger.LogWarning("Status file {StatusFile} is empty", filePath);
return false;
}
return true;
}
public async Task<Status> Get()
public override async Task<Status> Get()
{
var statusFile = Path.Combine(options.Value.JournalDirectory, FileName);
@ -67,5 +44,4 @@ public class StatusService(ILogger<StatusService> logger, IOptions<PulsarConfigu
logger.LogWarning("Failed to deserialize status file {StatusFile}", statusFile);
return new Status();
}
}