mirror of
https://github.com/9ParsonsB/Pulsar.git
synced 2025-07-01 16:33:43 -04:00
Add Cargo Service & Controller
This commit is contained in:
13
Pulsar/Features/Cargo/CargoController.cs
Normal file
13
Pulsar/Features/Cargo/CargoController.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using Observatory.Framework.Files;
|
||||
|
||||
namespace Pulsar.Features.Cargo;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class CargoController(ICargoService cargoService) : ControllerBase
|
||||
{
|
||||
public async Task<ActionResult<CargoFile>> Get()
|
||||
{
|
||||
return Ok(await cargoService.Get());
|
||||
}
|
||||
}
|
46
Pulsar/Features/Cargo/CargoService.cs
Normal file
46
Pulsar/Features/Cargo/CargoService.cs
Normal file
@ -0,0 +1,46 @@
|
||||
namespace Pulsar.Features.Cargo;
|
||||
|
||||
using Observatory.Framework.Files;
|
||||
|
||||
public interface ICargoService : IJournalHandler<CargoFile>;
|
||||
|
||||
public class CargoService(IOptions<PulsarConfiguration> options, ILogger<CargoService> logger, IEventHubContext hub) : ICargoService
|
||||
{
|
||||
public string FileName => "Cargo.json";
|
||||
|
||||
public async Task HandleFile(string filePath)
|
||||
{
|
||||
if (!FileHelper.ValidateFile(filePath))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var file = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
||||
var moduleInfo = await JsonSerializer.DeserializeAsync<CargoFile>(file);
|
||||
|
||||
if (moduleInfo == null)
|
||||
{
|
||||
logger.LogWarning("Failed to deserialize status file {FilePath}", filePath);
|
||||
return;
|
||||
}
|
||||
|
||||
await hub.Clients.All.CargoUpdated(moduleInfo);
|
||||
}
|
||||
|
||||
public async Task<CargoFile> Get()
|
||||
{
|
||||
var cargoFile = Path.Combine(options.Value.JournalDirectory, FileName);
|
||||
|
||||
if (!FileHelper.ValidateFile(cargoFile))
|
||||
{
|
||||
return new CargoFile();
|
||||
}
|
||||
|
||||
await using var file = File.Open(cargoFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
||||
var cargo = await JsonSerializer.DeserializeAsync<CargoFile>(file);
|
||||
if (cargo != null) return cargo;
|
||||
|
||||
logger.LogWarning("Failed to deserialize module info file {CargoFile}", cargoFile);
|
||||
return new CargoFile();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user