CUB-33: integrate Moonraker filament usage polling
Some checks failed
Dev Build / build-test (pull_request) Failing after 2m21s
Some checks failed
Dev Build / build-test (pull_request) Failing after 2m21s
This commit is contained in:
50
backend/Domain/Interfaces/IFilamentUsageService.cs
Normal file
50
backend/Domain/Interfaces/IFilamentUsageService.cs
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
using Extrudex.Domain.Entities;
|
||||||
|
|
||||||
|
namespace Extrudex.Domain.Interfaces;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Service for persisting and querying filament usage records.
|
||||||
|
/// Tracks consumption per print job and per spool for COGS and inventory tracking.
|
||||||
|
/// </summary>
|
||||||
|
public interface IFilamentUsageService
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Records a new filament usage entry for a print job.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="printJobId">The print job that consumed the filament.</param>
|
||||||
|
/// <param name="spoolId">The spool that provided the filament.</param>
|
||||||
|
/// <param name="printerId">The printer that executed the print.</param>
|
||||||
|
/// <param name="gramsUsed">Grams of filament consumed.</param>
|
||||||
|
/// <param name="mmExtruded">Millimeters of filament extruded.</param>
|
||||||
|
/// <param name="notes">Optional notes about this usage record.</param>
|
||||||
|
/// <param name="cancellationToken">Cancellation token.</param>
|
||||||
|
/// <returns>The created FilamentUsage entity.</returns>
|
||||||
|
Task<FilamentUsage> RecordUsageAsync(
|
||||||
|
Guid printJobId,
|
||||||
|
Guid spoolId,
|
||||||
|
Guid printerId,
|
||||||
|
decimal gramsUsed,
|
||||||
|
decimal mmExtruded,
|
||||||
|
string? notes = null,
|
||||||
|
CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieves all filament usage records for a specific print job.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="printJobId">The print job ID.</param>
|
||||||
|
/// <param name="cancellationToken">Cancellation token.</param>
|
||||||
|
/// <returns>Collection of filament usage records for the print job.</returns>
|
||||||
|
Task<IReadOnlyList<FilamentUsage>> GetByPrintJobAsync(
|
||||||
|
Guid printJobId,
|
||||||
|
CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieves all filament usage records for a specific spool.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="spoolId">The spool ID.</param>
|
||||||
|
/// <param name="cancellationToken">Cancellation token.</param>
|
||||||
|
/// <returns>Collection of filament usage records for the spool.</returns>
|
||||||
|
Task<IReadOnlyList<FilamentUsage>> GetBySpoolAsync(
|
||||||
|
Guid spoolId,
|
||||||
|
CancellationToken cancellationToken = default);
|
||||||
|
}
|
||||||
79
backend/Infrastructure/Services/FilamentUsageService.cs
Normal file
79
backend/Infrastructure/Services/FilamentUsageService.cs
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
using Extrudex.Domain.Entities;
|
||||||
|
using Extrudex.Domain.Interfaces;
|
||||||
|
using Extrudex.Infrastructure.Data;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace Extrudex.Infrastructure.Services;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// EF Core–backed implementation of the filament usage service.
|
||||||
|
/// Persists usage records to the database and provides query methods
|
||||||
|
/// for retrieving usage by print job or spool.
|
||||||
|
/// </summary>
|
||||||
|
public class FilamentUsageService : IFilamentUsageService
|
||||||
|
{
|
||||||
|
private readonly ExtrudexDbContext _dbContext;
|
||||||
|
private readonly ILogger<FilamentUsageService> _logger;
|
||||||
|
|
||||||
|
public FilamentUsageService(
|
||||||
|
ExtrudexDbContext dbContext,
|
||||||
|
ILogger<FilamentUsageService> logger)
|
||||||
|
{
|
||||||
|
_dbContext = dbContext;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async Task<FilamentUsage> RecordUsageAsync(
|
||||||
|
Guid printJobId,
|
||||||
|
Guid spoolId,
|
||||||
|
Guid printerId,
|
||||||
|
decimal gramsUsed,
|
||||||
|
decimal mmExtruded,
|
||||||
|
string? notes = null,
|
||||||
|
CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
var usage = new FilamentUsage
|
||||||
|
{
|
||||||
|
PrintJobId = printJobId,
|
||||||
|
SpoolId = spoolId,
|
||||||
|
PrinterId = printerId,
|
||||||
|
GramsUsed = gramsUsed,
|
||||||
|
MmExtruded = mmExtruded,
|
||||||
|
RecordedAt = DateTime.UtcNow,
|
||||||
|
Notes = notes
|
||||||
|
};
|
||||||
|
|
||||||
|
_dbContext.FilamentUsages.Add(usage);
|
||||||
|
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||||
|
|
||||||
|
_logger.LogInformation(
|
||||||
|
"Recorded filament usage: {Grams}g / {Mm}mm for print job {JobId} on spool {SpoolId}",
|
||||||
|
gramsUsed, mmExtruded, printJobId, spoolId);
|
||||||
|
|
||||||
|
return usage;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async Task<IReadOnlyList<FilamentUsage>> GetByPrintJobAsync(
|
||||||
|
Guid printJobId,
|
||||||
|
CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
return await _dbContext.FilamentUsages
|
||||||
|
.Where(u => u.PrintJobId == printJobId)
|
||||||
|
.OrderByDescending(u => u.RecordedAt)
|
||||||
|
.ToListAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async Task<IReadOnlyList<FilamentUsage>> GetBySpoolAsync(
|
||||||
|
Guid spoolId,
|
||||||
|
CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
return await _dbContext.FilamentUsages
|
||||||
|
.Where(u => u.SpoolId == spoolId)
|
||||||
|
.OrderByDescending(u => u.RecordedAt)
|
||||||
|
.ToListAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
390
backend/Infrastructure/Services/MoonrakerUsagePoller.cs
Normal file
390
backend/Infrastructure/Services/MoonrakerUsagePoller.cs
Normal file
@@ -0,0 +1,390 @@
|
|||||||
|
using Extrudex.Domain.DTOs.Moonraker;
|
||||||
|
using Extrudex.Domain.Entities;
|
||||||
|
using Extrudex.Domain.Enums;
|
||||||
|
using Extrudex.Domain.Interfaces;
|
||||||
|
using Extrudex.Infrastructure.Data;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
|
namespace Extrudex.Infrastructure.Services;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Configuration options for the Moonraker usage polling service.
|
||||||
|
/// </summary>
|
||||||
|
public class MoonrakerPollerOptions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// How often to poll each Moonraker printer for filament usage data.
|
||||||
|
/// Default: 30 seconds.
|
||||||
|
/// </summary>
|
||||||
|
public TimeSpan PollInterval { get; set; } = TimeSpan.FromSeconds(30);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Timeout for individual Moonraker HTTP requests.
|
||||||
|
/// Default: 10 seconds.
|
||||||
|
/// </summary>
|
||||||
|
public TimeSpan RequestTimeout { get; set; } = TimeSpan.FromSeconds(10);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Whether the polling service is enabled. Default: true.
|
||||||
|
/// Set to false to disable polling (e.g., in development or testing).
|
||||||
|
/// </summary>
|
||||||
|
public bool Enabled { get; set; } = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Background service that periodically polls Moonraker-connected printers
|
||||||
|
/// for filament usage data. When a print job is detected as complete,
|
||||||
|
/// the usage data is persisted to the FilamentUsage table via
|
||||||
|
/// <see cref="IFilamentUsageService"/>.
|
||||||
|
///
|
||||||
|
/// <para>Polling logic:</para>
|
||||||
|
/// <list type="number">
|
||||||
|
/// <item>Query the database for all active printers with ConnectionType == Moonraker.</item>
|
||||||
|
/// <item>For each printer, call <see cref="IMoonrakerClient.GetPrintStatsAsync"/> for live data
|
||||||
|
/// and <see cref="IMoonrakerClient.GetPrintHistoryAsync"/> for completed job history.</item>
|
||||||
|
/// <item>If usage data is available and the print state is "complete",
|
||||||
|
/// create or update a FilamentUsage record.</item>
|
||||||
|
/// <item>If the printer is unreachable or returns malformed data, log a warning
|
||||||
|
/// and continue to the next printer (no crash).</item>
|
||||||
|
/// </list>
|
||||||
|
///
|
||||||
|
/// <para>Error handling:</para>
|
||||||
|
/// <list type="bullet">
|
||||||
|
/// <item>API unreachable: logged as warning, poller continues for other printers.</item>
|
||||||
|
/// <item>Malformed response: logged as warning, poller continues.</item>
|
||||||
|
/// <item>Database errors: logged as error, poller continues.</item>
|
||||||
|
/// </list>
|
||||||
|
/// </summary>
|
||||||
|
public class MoonrakerUsagePoller : BackgroundService
|
||||||
|
{
|
||||||
|
private readonly IServiceScopeFactory _scopeFactory;
|
||||||
|
private readonly ILogger<MoonrakerUsagePoller> _logger;
|
||||||
|
private readonly MoonrakerPollerOptions _options;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tracks which Moonraker print jobs have already been recorded,
|
||||||
|
/// keyed by "printerId:gcodeFileName" to avoid duplicate recording.
|
||||||
|
/// </summary>
|
||||||
|
private readonly HashSet<string> _recordedJobs = new();
|
||||||
|
|
||||||
|
public MoonrakerUsagePoller(
|
||||||
|
IServiceScopeFactory scopeFactory,
|
||||||
|
ILogger<MoonrakerUsagePoller> logger,
|
||||||
|
IOptions<MoonrakerPollerOptions> options)
|
||||||
|
{
|
||||||
|
_scopeFactory = scopeFactory;
|
||||||
|
_logger = logger;
|
||||||
|
_options = options.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
|
{
|
||||||
|
if (!_options.Enabled)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Moonraker usage poller is disabled via configuration.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.LogInformation(
|
||||||
|
"Moonraker usage poller starting. Poll interval: {Interval}",
|
||||||
|
_options.PollInterval);
|
||||||
|
|
||||||
|
while (!stoppingToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await PollAllPrintersAsync(stoppingToken);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex,
|
||||||
|
"Unexpected error in Moonraker usage poller cycle. Continuing.");
|
||||||
|
}
|
||||||
|
|
||||||
|
await Task.Delay(_options.PollInterval, stoppingToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.LogInformation("Moonraker usage poller stopping.");
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task PollAllPrintersAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
using var scope = _scopeFactory.CreateScope();
|
||||||
|
var dbContext = scope.ServiceProvider.GetRequiredService<ExtrudexDbContext>();
|
||||||
|
var moonrakerClient = scope.ServiceProvider.GetRequiredService<IMoonrakerClient>();
|
||||||
|
var usageService = scope.ServiceProvider.GetRequiredService<IFilamentUsageService>();
|
||||||
|
|
||||||
|
var printers = await dbContext.Printers
|
||||||
|
.Where(p => p.IsActive && p.ConnectionType == ConnectionType.Moonraker)
|
||||||
|
.ToListAsync(cancellationToken);
|
||||||
|
|
||||||
|
if (printers.Count == 0)
|
||||||
|
{
|
||||||
|
_logger.LogDebug("No active Moonraker printers found.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.LogDebug("Polling {Count} Moonraker printer(s).", printers.Count);
|
||||||
|
|
||||||
|
foreach (var printer in printers)
|
||||||
|
{
|
||||||
|
await PollPrinterAsync(
|
||||||
|
printer, moonrakerClient, usageService, dbContext, cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task PollPrinterAsync(
|
||||||
|
Printer printer,
|
||||||
|
IMoonrakerClient moonrakerClient,
|
||||||
|
IFilamentUsageService usageService,
|
||||||
|
ExtrudexDbContext dbContext,
|
||||||
|
CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
_logger.LogDebug(
|
||||||
|
"Polling Moonraker printer {PrinterName} ({Host}:{Port})",
|
||||||
|
printer.Name, printer.HostnameOrIp, printer.Port);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var printStats = await moonrakerClient.GetPrintStatsAsync(
|
||||||
|
printer.HostnameOrIp,
|
||||||
|
printer.Port,
|
||||||
|
printer.ApiKey,
|
||||||
|
cancellationToken);
|
||||||
|
|
||||||
|
if (printStats is null)
|
||||||
|
{
|
||||||
|
_logger.LogDebug(
|
||||||
|
"No print stats available from printer {PrinterName}.", printer.Name);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
printer.LastSeenAt = DateTime.UtcNow;
|
||||||
|
await dbContext.SaveChangesAsync(cancellationToken);
|
||||||
|
|
||||||
|
_logger.LogDebug(
|
||||||
|
"Printer {PrinterName}: state={State}, filament={Mm}mm, file={File}",
|
||||||
|
printer.Name, printStats.State, printStats.FilamentUsedMm, printStats.Filename);
|
||||||
|
|
||||||
|
decimal mmExtruded = printStats.FilamentUsedMm;
|
||||||
|
if (mmExtruded <= 0)
|
||||||
|
{
|
||||||
|
_logger.LogDebug(
|
||||||
|
"Printer {PrinterName} has no filament usage to record.", printer.Name);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!IsCompleteState(printStats.State))
|
||||||
|
{
|
||||||
|
_logger.LogDebug(
|
||||||
|
"Printer {PrinterName} print state '{State}' is not complete; skipping.",
|
||||||
|
printer.Name, printStats.State);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
string gcodeFileName = printStats.Filename ?? $"unknown-{Guid.NewGuid():N}";
|
||||||
|
var deduplicationKey = $"{printer.Id}:{gcodeFileName}";
|
||||||
|
if (_recordedJobs.Contains(deduplicationKey))
|
||||||
|
{
|
||||||
|
_logger.LogDebug(
|
||||||
|
"Printer {PrinterName} job '{File}' already recorded; skipping.",
|
||||||
|
printer.Name, gcodeFileName);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
DateTime? startedAt = null;
|
||||||
|
DateTime? completedAt = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var history = await moonrakerClient.GetPrintHistoryAsync(
|
||||||
|
printer.HostnameOrIp, printer.Port, printer.ApiKey,
|
||||||
|
limit: 1, cancellationToken);
|
||||||
|
|
||||||
|
if (history.Items.Count > 0)
|
||||||
|
{
|
||||||
|
var latestJob = history.Items[0];
|
||||||
|
startedAt = latestJob.StartTime;
|
||||||
|
completedAt = latestJob.EndTime;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogDebug(ex,
|
||||||
|
"Could not fetch history for printer {PrinterName}; proceeding with stats only.",
|
||||||
|
printer.Name);
|
||||||
|
}
|
||||||
|
|
||||||
|
var printJob = await FindOrCreatePrintJobAsync(
|
||||||
|
dbContext, printer, mmExtruded, gcodeFileName,
|
||||||
|
startedAt, completedAt, cancellationToken);
|
||||||
|
|
||||||
|
if (printJob is null)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(
|
||||||
|
"Could not find or create print job for printer {PrinterName}. No active spool found.",
|
||||||
|
printer.Name);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var spool = await dbContext.Spools.FindAsync(
|
||||||
|
new object[] { printJob.SpoolId }, cancellationToken);
|
||||||
|
|
||||||
|
var gramsUsed = CalculateGramsUsed(mmExtruded, spool);
|
||||||
|
|
||||||
|
await usageService.RecordUsageAsync(
|
||||||
|
printJobId: printJob.Id,
|
||||||
|
spoolId: printJob.SpoolId,
|
||||||
|
printerId: printer.Id,
|
||||||
|
gramsUsed: gramsUsed,
|
||||||
|
mmExtruded: mmExtruded,
|
||||||
|
notes: $"Moonraker auto-recorded: {gcodeFileName}",
|
||||||
|
cancellationToken: cancellationToken);
|
||||||
|
|
||||||
|
_recordedJobs.Add(deduplicationKey);
|
||||||
|
|
||||||
|
_logger.LogInformation(
|
||||||
|
"Recorded Moonraker usage for printer {PrinterName}: {Mm}mm / {Grams}g, job '{File}'",
|
||||||
|
printer.Name, mmExtruded, gramsUsed, gcodeFileName);
|
||||||
|
}
|
||||||
|
catch (HttpRequestException ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex,
|
||||||
|
"Moonraker API unreachable for printer {PrinterName} ({Host}:{Port}). Will retry next cycle.",
|
||||||
|
printer.Name, printer.HostnameOrIp, printer.Port);
|
||||||
|
}
|
||||||
|
catch (TaskCanceledException) when (cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
catch (TaskCanceledException ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex,
|
||||||
|
"Moonraker request timed out for printer {PrinterName} ({Host}:{Port}).",
|
||||||
|
printer.Name, printer.HostnameOrIp, printer.Port);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex,
|
||||||
|
"Unexpected error polling Moonraker printer {PrinterName}. Continuing to next printer.",
|
||||||
|
printer.Name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool IsCompleteState(string state) =>
|
||||||
|
state.Equals("complete", StringComparison.OrdinalIgnoreCase) ||
|
||||||
|
state.Equals("completed", StringComparison.OrdinalIgnoreCase);
|
||||||
|
|
||||||
|
private async Task<PrintJob?> FindOrCreatePrintJobAsync(
|
||||||
|
ExtrudexDbContext dbContext,
|
||||||
|
Printer printer,
|
||||||
|
decimal mmExtruded,
|
||||||
|
string gcodeFileName,
|
||||||
|
DateTime? startedAt,
|
||||||
|
DateTime? completedAt,
|
||||||
|
CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrEmpty(gcodeFileName))
|
||||||
|
{
|
||||||
|
var existingJob = await dbContext.PrintJobs
|
||||||
|
.Where(j => j.PrinterId == printer.Id &&
|
||||||
|
j.GcodeFilePath == gcodeFileName &&
|
||||||
|
j.DataSource == DataSource.Moonraker &&
|
||||||
|
j.Status != JobStatus.Cancelled)
|
||||||
|
.OrderByDescending(j => j.CreatedAt)
|
||||||
|
.FirstOrDefaultAsync(cancellationToken);
|
||||||
|
|
||||||
|
if (existingJob is not null)
|
||||||
|
{
|
||||||
|
existingJob.MmExtruded = mmExtruded;
|
||||||
|
existingJob.GramsDerived = CalculateGramsUsed(
|
||||||
|
mmExtruded,
|
||||||
|
await dbContext.Spools.FindAsync(
|
||||||
|
new object[] { existingJob.SpoolId }, cancellationToken));
|
||||||
|
existingJob.Status = JobStatus.Completed;
|
||||||
|
existingJob.CompletedAt = completedAt ?? DateTime.UtcNow;
|
||||||
|
existingJob.StartedAt ??= startedAt;
|
||||||
|
await dbContext.SaveChangesAsync(cancellationToken);
|
||||||
|
return existingJob;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var spool = await FindActiveSpoolForPrinterAsync(dbContext, printer, cancellationToken);
|
||||||
|
if (spool is null) return null;
|
||||||
|
|
||||||
|
var gramsDerived = CalculateGramsUsed(mmExtruded, spool);
|
||||||
|
|
||||||
|
var newJob = new PrintJob
|
||||||
|
{
|
||||||
|
PrinterId = printer.Id,
|
||||||
|
SpoolId = spool.Id,
|
||||||
|
PrintName = gcodeFileName ?? "Moonraker Print",
|
||||||
|
GcodeFilePath = gcodeFileName,
|
||||||
|
MmExtruded = mmExtruded,
|
||||||
|
GramsDerived = gramsDerived,
|
||||||
|
FilamentDiameterAtPrintMm = spool.FilamentDiameterMm,
|
||||||
|
MaterialDensityAtPrint = GetMaterialDensity(spool),
|
||||||
|
DataSource = DataSource.Moonraker,
|
||||||
|
Status = JobStatus.Completed,
|
||||||
|
StartedAt = startedAt ?? DateTime.UtcNow,
|
||||||
|
CompletedAt = completedAt ?? DateTime.UtcNow,
|
||||||
|
Notes = "Auto-created by Moonraker usage poller"
|
||||||
|
};
|
||||||
|
|
||||||
|
dbContext.PrintJobs.Add(newJob);
|
||||||
|
await dbContext.SaveChangesAsync(cancellationToken);
|
||||||
|
|
||||||
|
return newJob;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task<Spool?> FindActiveSpoolForPrinterAsync(
|
||||||
|
ExtrudexDbContext dbContext,
|
||||||
|
Printer printer,
|
||||||
|
CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var amsSpool = await dbContext.AmsSlots
|
||||||
|
.Include(s => s.Spool)
|
||||||
|
.ThenInclude(s => s!.MaterialBase)
|
||||||
|
.Include(s => s.AmsUnit)
|
||||||
|
.Where(s => s.AmsUnit.PrinterId == printer.Id && s.Spool != null && s.Spool.IsActive)
|
||||||
|
.Select(s => s.Spool)
|
||||||
|
.FirstOrDefaultAsync(cancellationToken);
|
||||||
|
|
||||||
|
if (amsSpool is not null) return amsSpool;
|
||||||
|
|
||||||
|
return await dbContext.Spools
|
||||||
|
.Include(s => s.MaterialBase)
|
||||||
|
.Where(s => s.IsActive)
|
||||||
|
.OrderByDescending(s => s.WeightRemainingGrams)
|
||||||
|
.FirstOrDefaultAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static decimal CalculateGramsUsed(decimal mmExtruded, Spool? spool)
|
||||||
|
{
|
||||||
|
if (spool is null) return 0m;
|
||||||
|
var diameterMm = spool.FilamentDiameterMm;
|
||||||
|
var densityGcm3 = GetMaterialDensity(spool);
|
||||||
|
var radiusMm = diameterMm / 2m;
|
||||||
|
var crossSectionArea = Math.PI * (double)radiusMm * (double)radiusMm;
|
||||||
|
var volumeMm3 = (double)mmExtruded * crossSectionArea;
|
||||||
|
var volumeCm3 = volumeMm3 / 1000.0;
|
||||||
|
var grams = volumeCm3 * (double)densityGcm3;
|
||||||
|
return Math.Round((decimal)grams, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static decimal GetMaterialDensity(Spool? spool)
|
||||||
|
{
|
||||||
|
return spool?.MaterialBase?.Name?.ToUpperInvariant() switch
|
||||||
|
{
|
||||||
|
"PLA" => 1.24m,
|
||||||
|
"PETG" => 1.27m,
|
||||||
|
"ABS" => 1.04m,
|
||||||
|
"ASA" => 1.07m,
|
||||||
|
"TPU" => 1.21m,
|
||||||
|
"NYLON" or "PA" => 1.13m,
|
||||||
|
"PC" => 1.20m,
|
||||||
|
_ => 1.24m
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -61,6 +61,14 @@ builder.Services.AddSingleton<ILowStockDetector, LowStockDetector>();
|
|||||||
// ── Usage Logging ───────────────────────────────────────────
|
// ── Usage Logging ───────────────────────────────────────────
|
||||||
builder.Services.AddScoped<IUsageLogService, UsageLogService>();
|
builder.Services.AddScoped<IUsageLogService, UsageLogService>();
|
||||||
|
|
||||||
|
// ── Filament Usage Service ──────────────────────────────────
|
||||||
|
builder.Services.AddScoped<IFilamentUsageService, FilamentUsageService>();
|
||||||
|
|
||||||
|
// ── Moonraker Usage Poller (Background Service) ─────────────
|
||||||
|
builder.Services.Configure<MoonrakerPollerOptions>(
|
||||||
|
builder.Configuration.GetSection("MoonrakerPoller"));
|
||||||
|
builder.Services.AddHostedService<MoonrakerUsagePoller>();
|
||||||
|
|
||||||
// ── FluentValidation ──────────────────────────────────────
|
// ── FluentValidation ──────────────────────────────────────
|
||||||
// Registers all validators from the API assembly into DI.
|
// Registers all validators from the API assembly into DI.
|
||||||
builder.Services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly());
|
builder.Services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly());
|
||||||
|
|||||||
@@ -23,5 +23,11 @@
|
|||||||
},
|
},
|
||||||
"FilamentAlerts": {
|
"FilamentAlerts": {
|
||||||
"LowStockThresholdPercent": 20
|
"LowStockThresholdPercent": 20
|
||||||
|
},
|
||||||
|
"MoonrakerPoller": {
|
||||||
|
"Enabled": true,
|
||||||
|
"PollInterval": "00:00:30",
|
||||||
|
"RequestTimeout": "00:00:10"
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user