Compare commits
12 Commits
agent/hex/
...
a8b5fd42c3
| Author | SHA1 | Date | |
|---|---|---|---|
| a8b5fd42c3 | |||
| 2e8227c3f9 | |||
| cfd4a81b5f | |||
| 8a2f97d2cd | |||
| b43edad5f0 | |||
| f5ca20307e | |||
| 12888c4f3f | |||
| 1411b68a95 | |||
| 7daa7d637c | |||
| c1a115c938 | |||
| 920042acac | |||
|
|
1ee7562e81 |
@@ -413,6 +413,92 @@ public class PrintJobsController : ControllerBase
|
|||||||
return NoContent();
|
return NoContent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ── GET /api/printjobs/{id}/cost-summary ──────────────────────────
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the material cost summary for a specific print job.
|
||||||
|
/// Calculates total material cost from filament usage (grams derived)
|
||||||
|
/// and the spool's purchase price. Returns warnings instead of errors
|
||||||
|
/// when cost data is unavailable.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">The unique identifier of the print job.</param>
|
||||||
|
/// <returns>A cost summary with breakdown and any warnings about missing data.</returns>
|
||||||
|
/// <response code="200">Returns the cost summary. Warnings field lists any missing data.</response>
|
||||||
|
/// <response code="404">If the print job with the given ID is not found.</response>
|
||||||
|
[HttpGet("{id:guid}/cost-summary")]
|
||||||
|
[ProducesResponseType(typeof(CostSummaryResponse), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
public async Task<ActionResult<CostSummaryResponse>> GetCostSummary(Guid id)
|
||||||
|
{
|
||||||
|
_logger.LogDebug("Getting cost summary for print job {Id}", id);
|
||||||
|
|
||||||
|
var job = await _dbContext.PrintJobs
|
||||||
|
.Include(j => j.Spool)
|
||||||
|
.ThenInclude(s => s!.MaterialBase)
|
||||||
|
.FirstOrDefaultAsync(j => j.Id == id);
|
||||||
|
|
||||||
|
if (job is null)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Print job {Id} not found for cost summary", id);
|
||||||
|
return NotFound(new { error = $"Print job with ID '{id}' not found." });
|
||||||
|
}
|
||||||
|
|
||||||
|
var warnings = new List<string>();
|
||||||
|
var spool = job.Spool;
|
||||||
|
|
||||||
|
// Build response with what we have
|
||||||
|
var response = new CostSummaryResponse
|
||||||
|
{
|
||||||
|
PrintJobId = job.Id,
|
||||||
|
PrintName = job.PrintName,
|
||||||
|
SpoolId = job.SpoolId,
|
||||||
|
SpoolSerial = spool?.SpoolSerial ?? string.Empty,
|
||||||
|
SpoolBrand = spool?.Brand ?? string.Empty,
|
||||||
|
SpoolColorName = spool?.ColorName ?? string.Empty,
|
||||||
|
MmExtruded = job.MmExtruded,
|
||||||
|
GramsDerived = job.GramsDerived,
|
||||||
|
SpoolPurchasePrice = spool?.PurchasePrice,
|
||||||
|
SpoolWeightTotalGrams = spool?.WeightTotalGrams,
|
||||||
|
StoredCostPerPrint = job.CostPerPrint
|
||||||
|
};
|
||||||
|
|
||||||
|
// Validate spool data availability
|
||||||
|
if (spool is null)
|
||||||
|
{
|
||||||
|
warnings.Add("Spool data is not available for this print job. Cost cannot be calculated.");
|
||||||
|
response.Warnings = warnings;
|
||||||
|
return Ok(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if we can calculate cost
|
||||||
|
if (!spool.PurchasePrice.HasValue)
|
||||||
|
{
|
||||||
|
warnings.Add("Spool purchase price is not set. Cost per gram and total material cost cannot be calculated.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (spool.WeightTotalGrams <= 0)
|
||||||
|
{
|
||||||
|
warnings.Add("Spool total weight is zero or invalid. Cost per gram and total material cost cannot be calculated.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we have enough data, calculate the cost
|
||||||
|
if (spool.PurchasePrice.HasValue && spool.WeightTotalGrams > 0)
|
||||||
|
{
|
||||||
|
var pricePerGram = spool.PurchasePrice.Value / spool.WeightTotalGrams;
|
||||||
|
response.PricePerGram = Math.Round(pricePerGram, 4);
|
||||||
|
response.TotalMaterialCost = Math.Round(job.GramsDerived * pricePerGram, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Warn if grams derived is zero but mm extruded is non-zero
|
||||||
|
if (job.GramsDerived == 0 && job.MmExtruded > 0)
|
||||||
|
{
|
||||||
|
warnings.Add("GramsDerived is zero despite MmExtruded being non-zero. Cost may be inaccurate. Consider re-deriving grams from filament parameters.");
|
||||||
|
}
|
||||||
|
|
||||||
|
response.Warnings = warnings;
|
||||||
|
return Ok(response);
|
||||||
|
}
|
||||||
|
|
||||||
// ── Gram Derivation Formula ────────────────────────────────────
|
// ── Gram Derivation Formula ────────────────────────────────────
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
55
backend/API/DTOs/PrintJobs/CostSummaryResponse.cs
Normal file
55
backend/API/DTOs/PrintJobs/CostSummaryResponse.cs
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
namespace Extrudex.API.DTOs.PrintJobs;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Response DTO for the cost summary of a print job.
|
||||||
|
/// Provides a breakdown of material cost based on filament usage
|
||||||
|
/// and spool pricing data. If cost data is incomplete, warnings
|
||||||
|
/// are returned instead of throwing an error.
|
||||||
|
/// </summary>
|
||||||
|
public class CostSummaryResponse
|
||||||
|
{
|
||||||
|
/// <summary>Unique identifier of the print job.</summary>
|
||||||
|
public Guid PrintJobId { get; set; }
|
||||||
|
|
||||||
|
/// <summary>Human-readable name of the print job.</summary>
|
||||||
|
public string PrintName { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>Foreign key to the spool used for this print job.</summary>
|
||||||
|
public Guid SpoolId { get; set; }
|
||||||
|
|
||||||
|
/// <summary>Serial number of the spool.</summary>
|
||||||
|
public string SpoolSerial { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>Brand of the spool.</summary>
|
||||||
|
public string SpoolBrand { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>Color name of the spool.</summary>
|
||||||
|
public string SpoolColorName { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>Total millimeters of filament extruded during this print.</summary>
|
||||||
|
public decimal MmExtruded { get; set; }
|
||||||
|
|
||||||
|
/// <summary>Derived grams consumed for this print job.</summary>
|
||||||
|
public decimal GramsDerived { get; set; }
|
||||||
|
|
||||||
|
/// <summary>Purchase price of the full spool, if available.</summary>
|
||||||
|
public decimal? SpoolPurchasePrice { get; set; }
|
||||||
|
|
||||||
|
/// <summary>Total weight of the spool in grams when full.</summary>
|
||||||
|
public decimal? SpoolWeightTotalGrams { get; set; }
|
||||||
|
|
||||||
|
/// <summary>Calculated price per gram (purchase price / total weight), if available.</summary>
|
||||||
|
public decimal? PricePerGram { get; set; }
|
||||||
|
|
||||||
|
/// <summary>Calculated total material cost for this print job, if available.</summary>
|
||||||
|
public decimal? TotalMaterialCost { get; set; }
|
||||||
|
|
||||||
|
/// <summary>The CostPerPrint stored on the print job entity, if set.</summary>
|
||||||
|
public decimal? StoredCostPerPrint { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Warnings about missing data that prevent cost calculation.
|
||||||
|
/// Empty if all data is available and cost was calculated successfully.
|
||||||
|
/// </summary>
|
||||||
|
public List<string> Warnings { get; set; } = new();
|
||||||
|
}
|
||||||
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);
|
||||||
|
}
|
||||||
76
backend/Domain/Interfaces/IMoonrakerClient.cs
Normal file
76
backend/Domain/Interfaces/IMoonrakerClient.cs
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
namespace Extrudex.Domain.Interfaces;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Client for communicating with Moonraker REST API on Klipper-based printers
|
||||||
|
/// (e.g., Elegoo Centauri Carbon). Retrieves print job metadata including
|
||||||
|
/// filament usage data.
|
||||||
|
/// </summary>
|
||||||
|
public interface IMoonrakerClient
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieves the current printer status from Moonraker.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="hostnameOrIp">Printer hostname or IP address.</param>
|
||||||
|
/// <param name="port">Moonraker port (default: 7125).</param>
|
||||||
|
/// <param name="apiKey">Optional API key for authentication.</param>
|
||||||
|
/// <param name="cancellationToken">Cancellation token.</param>
|
||||||
|
/// <returns>The printer status string (e.g., "idle", "printing", "paused", "error").</returns>
|
||||||
|
Task<string> GetPrinterStatusAsync(
|
||||||
|
string hostnameOrIp,
|
||||||
|
int port,
|
||||||
|
string? apiKey = null,
|
||||||
|
CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieves filament usage data from the current or most recent print job.
|
||||||
|
/// Moonraker exposes this via the /api/objects endpoint querying
|
||||||
|
/// "history" and "print_stats" objects.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="hostnameOrIp">Printer hostname or IP address.</param>
|
||||||
|
/// <param name="port">Moonraker port (default: 7125).</param>
|
||||||
|
/// <param name="apiKey">Optional API key for authentication.</param>
|
||||||
|
/// <param name="cancellationToken">Cancellation token.</param>
|
||||||
|
/// <returns> Filament usage data from Moonraker, or null if unavailable.</returns>
|
||||||
|
Task<MoonrakerFilamentUsage?> GetFilamentUsageAsync(
|
||||||
|
string hostnameOrIp,
|
||||||
|
int port,
|
||||||
|
string? apiKey = null,
|
||||||
|
CancellationToken cancellationToken = default);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents filament usage data retrieved from a Moonraker-equipped printer.
|
||||||
|
/// Maps to Moonraker's print_stats and history objects.
|
||||||
|
/// </summary>
|
||||||
|
public class MoonrakerFilamentUsage
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Millimeters of filament extruded during the print job.
|
||||||
|
/// </summary>
|
||||||
|
public decimal MmExtruded { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The filename of the G-code file being or recently printed.
|
||||||
|
/// </summary>
|
||||||
|
public string? GcodeFileName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Current print state from Moonraker (e.g., "printing", "complete", "error").
|
||||||
|
/// </summary>
|
||||||
|
public string PrintState { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Total print time in seconds, if available from Moonraker.
|
||||||
|
/// </summary>
|
||||||
|
public double? PrintDurationSeconds { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Timestamp (UTC) when the print job was started, if available.
|
||||||
|
/// </summary>
|
||||||
|
public DateTime? StartedAt { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Timestamp (UTC) when the print job completed, if available.
|
||||||
|
/// </summary>
|
||||||
|
public DateTime? CompletedAt { get; set; }
|
||||||
|
}
|
||||||
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
303
backend/Infrastructure/Services/MoonrakerClient.cs
Normal file
303
backend/Infrastructure/Services/MoonrakerClient.cs
Normal file
@@ -0,0 +1,303 @@
|
|||||||
|
using System.Globalization;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
|
using System.Text.Json;
|
||||||
|
using Extrudex.Domain.Interfaces;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace Extrudex.Infrastructure.Services;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// HTTP client for communicating with the Moonraker REST API on
|
||||||
|
/// Klipper-based printers (e.g., Elegoo Centauri Carbon).
|
||||||
|
///
|
||||||
|
/// Moonraker endpoints used:
|
||||||
|
/// - GET /api/objects?print_stats — current print stats including filament used
|
||||||
|
/// - GET /api/objects?history — job history with filament usage per job
|
||||||
|
///
|
||||||
|
/// Authentication: optional X-Api-Key header when API key is configured.
|
||||||
|
/// </summary>
|
||||||
|
public class MoonrakerClient : IMoonrakerClient
|
||||||
|
{
|
||||||
|
private readonly HttpClient _httpClient;
|
||||||
|
private readonly ILogger<MoonrakerClient> _logger;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new MoonrakerClient with the specified HTTP client and logger.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="httpClient">The HTTP client used for API calls.</param>
|
||||||
|
/// <param name="logger">Logger for diagnostic output.</param>
|
||||||
|
public MoonrakerClient(HttpClient httpClient, ILogger<MoonrakerClient> logger)
|
||||||
|
{
|
||||||
|
_httpClient = httpClient;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async Task<string> GetPrinterStatusAsync(
|
||||||
|
string hostnameOrIp,
|
||||||
|
int port,
|
||||||
|
string? apiKey = null,
|
||||||
|
CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
var baseUrl = BuildBaseUrl(hostnameOrIp, port);
|
||||||
|
var requestUrl = $"{baseUrl}/api/objects?print_stats";
|
||||||
|
|
||||||
|
using var request = new HttpRequestMessage(HttpMethod.Get, requestUrl);
|
||||||
|
ApplyApiKey(request, apiKey);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var response = await _httpClient.SendAsync(request, cancellationToken);
|
||||||
|
response.EnsureSuccessStatusCode();
|
||||||
|
|
||||||
|
var json = await response.Content.ReadAsStringAsync(cancellationToken);
|
||||||
|
using var doc = JsonDocument.Parse(json);
|
||||||
|
|
||||||
|
// Moonraker returns: { "result": { "print_stats": { "state": "idle", ... } } }
|
||||||
|
var state = doc.RootElement
|
||||||
|
.GetProperty("result")
|
||||||
|
.GetProperty("print_stats")
|
||||||
|
.GetProperty("state")
|
||||||
|
.GetString();
|
||||||
|
|
||||||
|
return state ?? "unknown";
|
||||||
|
}
|
||||||
|
catch (HttpRequestException ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex,
|
||||||
|
"Moonraker printer status request failed for {Host}:{Port}",
|
||||||
|
hostnameOrIp, port);
|
||||||
|
return "offline";
|
||||||
|
}
|
||||||
|
catch (JsonException ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex,
|
||||||
|
"Malformed Moonraker response from {Host}:{Port}",
|
||||||
|
hostnameOrIp, port);
|
||||||
|
return "error";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async Task<MoonrakerFilamentUsage?> GetFilamentUsageAsync(
|
||||||
|
string hostnameOrIp,
|
||||||
|
int port,
|
||||||
|
string? apiKey = null,
|
||||||
|
CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
var baseUrl = BuildBaseUrl(hostnameOrIp, port);
|
||||||
|
|
||||||
|
// Fetch current print_stats (has live filament_used for active/recent job)
|
||||||
|
PrintStatsResult? printStats = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
printStats = await FetchPrintStatsAsync(baseUrl, apiKey, cancellationToken);
|
||||||
|
}
|
||||||
|
catch (HttpRequestException ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex,
|
||||||
|
"Moonraker print_stats request failed for {Host}:{Port}",
|
||||||
|
hostnameOrIp, port);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
catch (JsonException ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex,
|
||||||
|
"Malformed Moonraker print_stats response from {Host}:{Port}",
|
||||||
|
hostnameOrIp, port);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (printStats is null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
// Attempt to enrich with history data for timing info
|
||||||
|
HistoryResult? history = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
history = await FetchHistoryAsync(baseUrl, apiKey, cancellationToken);
|
||||||
|
}
|
||||||
|
catch (HttpRequestException ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex,
|
||||||
|
"Moonraker history request failed for {Host}:{Port}",
|
||||||
|
hostnameOrIp, port);
|
||||||
|
}
|
||||||
|
catch (JsonException ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex,
|
||||||
|
"Malformed Moonraker history response from {Host}:{Port}",
|
||||||
|
hostnameOrIp, port);
|
||||||
|
}
|
||||||
|
|
||||||
|
DateTime? startedAt = null;
|
||||||
|
DateTime? completedAt = null;
|
||||||
|
double? printDurationSeconds = null;
|
||||||
|
|
||||||
|
if (history is not null)
|
||||||
|
{
|
||||||
|
startedAt = history.StartTime;
|
||||||
|
completedAt = history.EndTime;
|
||||||
|
printDurationSeconds = history.PrintDuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new MoonrakerFilamentUsage
|
||||||
|
{
|
||||||
|
MmExtruded = printStats.FilamentUsedMm ?? 0m,
|
||||||
|
GcodeFileName = printStats.FileName,
|
||||||
|
PrintState = printStats.State ?? "unknown",
|
||||||
|
PrintDurationSeconds = printDurationSeconds,
|
||||||
|
StartedAt = startedAt,
|
||||||
|
CompletedAt = completedAt
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Fetches and parses print_stats from the Moonraker API.
|
||||||
|
/// </summary>
|
||||||
|
private async Task<PrintStatsResult?> FetchPrintStatsAsync(
|
||||||
|
string baseUrl,
|
||||||
|
string? apiKey,
|
||||||
|
CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var requestUrl = $"{baseUrl}/api/objects?print_stats";
|
||||||
|
|
||||||
|
using var request = new HttpRequestMessage(HttpMethod.Get, requestUrl);
|
||||||
|
ApplyApiKey(request, apiKey);
|
||||||
|
|
||||||
|
using var response = await _httpClient.SendAsync(request, cancellationToken);
|
||||||
|
response.EnsureSuccessStatusCode();
|
||||||
|
|
||||||
|
var json = await response.Content.ReadAsStringAsync(cancellationToken);
|
||||||
|
using var doc = JsonDocument.Parse(json);
|
||||||
|
|
||||||
|
if (!doc.RootElement.TryGetProperty("result", out var result) ||
|
||||||
|
!result.TryGetProperty("print_stats", out var stats))
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Moonraker response missing 'print_stats' object");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new PrintStatsResult
|
||||||
|
{
|
||||||
|
State = stats.TryGetProperty("state", out var stateEl) ? stateEl.GetString() : null,
|
||||||
|
FilamentUsedMm = stats.TryGetProperty("filament_used", out var filamentEl) &&
|
||||||
|
filamentEl.ValueKind == JsonValueKind.Number
|
||||||
|
? filamentEl.GetDecimal() : (decimal?)null,
|
||||||
|
FileName = stats.TryGetProperty("filename", out var fileEl) ? fileEl.GetString() : null
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Fetches and parses history (last job) from the Moonraker API.
|
||||||
|
/// </summary>
|
||||||
|
private async Task<HistoryResult?> FetchHistoryAsync(
|
||||||
|
string baseUrl,
|
||||||
|
string? apiKey,
|
||||||
|
CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var requestUrl = $"{baseUrl}/api/objects?history";
|
||||||
|
|
||||||
|
using var request = new HttpRequestMessage(HttpMethod.Get, requestUrl);
|
||||||
|
ApplyApiKey(request, apiKey);
|
||||||
|
|
||||||
|
using var response = await _httpClient.SendAsync(request, cancellationToken);
|
||||||
|
response.EnsureSuccessStatusCode();
|
||||||
|
|
||||||
|
var json = await response.Content.ReadAsStringAsync(cancellationToken);
|
||||||
|
using var doc = JsonDocument.Parse(json);
|
||||||
|
|
||||||
|
if (!doc.RootElement.TryGetProperty("result", out var result) ||
|
||||||
|
!result.TryGetProperty("history", out var history))
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Moonraker response missing 'history' object");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try last_job first, then job
|
||||||
|
JsonElement jobEl;
|
||||||
|
if (!history.TryGetProperty("last_job", out jobEl) &&
|
||||||
|
!history.TryGetProperty("job", out jobEl))
|
||||||
|
{
|
||||||
|
_logger.LogDebug("Moonraker history has no 'last_job' or 'job' entry");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new HistoryResult
|
||||||
|
{
|
||||||
|
StartTime = ParseDateTimeProperty(jobEl, "start_time"),
|
||||||
|
EndTime = ParseDateTimeProperty(jobEl, "end_time"),
|
||||||
|
PrintDuration = jobEl.TryGetProperty("print_duration", out var durEl) &&
|
||||||
|
durEl.ValueKind == JsonValueKind.Number
|
||||||
|
? durEl.GetDouble() : (double?)null
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Parses a Moonraker timestamp property (Unix epoch seconds or ISO 8601 string).
|
||||||
|
/// </summary>
|
||||||
|
private static DateTime? ParseDateTimeProperty(JsonElement element, string propertyName)
|
||||||
|
{
|
||||||
|
if (!element.TryGetProperty(propertyName, out var prop))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
// Moonraker returns Unix epoch seconds as a number
|
||||||
|
if (prop.ValueKind == JsonValueKind.Number)
|
||||||
|
{
|
||||||
|
var epochSeconds = prop.GetDouble();
|
||||||
|
return DateTime.UnixEpoch.AddSeconds(epochSeconds);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback: try parsing as ISO 8601 string
|
||||||
|
if (prop.ValueKind == JsonValueKind.String)
|
||||||
|
{
|
||||||
|
var str = prop.GetString();
|
||||||
|
if (str is not null &&
|
||||||
|
DateTime.TryParse(str, CultureInfo.InvariantCulture,
|
||||||
|
DateTimeStyles.AssumeUniversal, out var dt))
|
||||||
|
{
|
||||||
|
return dt.ToUniversalTime();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Builds the base URL for Moonraker API calls.
|
||||||
|
/// </summary>
|
||||||
|
private static string BuildBaseUrl(string hostnameOrIp, int port) =>
|
||||||
|
$"http://{hostnameOrIp}:{port}";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Applies the Moonraker API key to the request header if provided.
|
||||||
|
/// </summary>
|
||||||
|
private static void ApplyApiKey(HttpRequestMessage request, string? apiKey)
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrWhiteSpace(apiKey))
|
||||||
|
{
|
||||||
|
request.Headers.Add("X-Api-Key", apiKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Parsed result from Moonraker's print_stats object.
|
||||||
|
/// Extracted immediately from the JSON response to avoid JsonDocument disposal issues.
|
||||||
|
/// </summary>
|
||||||
|
private sealed class PrintStatsResult
|
||||||
|
{
|
||||||
|
public string? State { get; set; }
|
||||||
|
public decimal? FilamentUsedMm { get; set; }
|
||||||
|
public string? FileName { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Parsed result from Moonraker's history/last_job object.
|
||||||
|
/// </summary>
|
||||||
|
private sealed class HistoryResult
|
||||||
|
{
|
||||||
|
public DateTime? StartTime { get; set; }
|
||||||
|
public DateTime? EndTime { get; set; }
|
||||||
|
public double? PrintDuration { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
431
backend/Infrastructure/Services/MoonrakerUsagePoller.cs
Normal file
431
backend/Infrastructure/Services/MoonrakerUsagePoller.cs
Normal file
@@ -0,0 +1,431 @@
|
|||||||
|
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.GetFilamentUsageAsync"/>.</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.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Polls all active Moonraker printers for filament usage data
|
||||||
|
/// and persists any completed print usage records.
|
||||||
|
/// </summary>
|
||||||
|
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>();
|
||||||
|
|
||||||
|
// Get all active Moonraker printers
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Polls a single Moonraker printer for filament usage data.
|
||||||
|
/// If a completed print job is detected with usage data, it is persisted.
|
||||||
|
/// </summary>
|
||||||
|
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
|
||||||
|
{
|
||||||
|
// Update last-seen timestamp regardless of usage data
|
||||||
|
var usageData = await moonrakerClient.GetFilamentUsageAsync(
|
||||||
|
printer.HostnameOrIp,
|
||||||
|
printer.Port,
|
||||||
|
printer.ApiKey,
|
||||||
|
cancellationToken);
|
||||||
|
|
||||||
|
if (usageData is null)
|
||||||
|
{
|
||||||
|
_logger.LogDebug(
|
||||||
|
"No filament usage data from printer {PrinterName}.",
|
||||||
|
printer.Name);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update printer last-seen timestamp
|
||||||
|
printer.LastSeenAt = DateTime.UtcNow;
|
||||||
|
await dbContext.SaveChangesAsync(cancellationToken);
|
||||||
|
|
||||||
|
_logger.LogDebug(
|
||||||
|
"Printer {PrinterName}: state={State}, mm={Mm}, file={File}",
|
||||||
|
printer.Name, usageData.PrintState, usageData.MmExtruded,
|
||||||
|
usageData.GcodeFileName);
|
||||||
|
|
||||||
|
// Only record usage for completed prints
|
||||||
|
if (usageData.MmExtruded <= 0)
|
||||||
|
{
|
||||||
|
_logger.LogDebug(
|
||||||
|
"Printer {PrinterName} has no filament usage to record.",
|
||||||
|
printer.Name);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!IsCompleteState(usageData.PrintState))
|
||||||
|
{
|
||||||
|
_logger.LogDebug(
|
||||||
|
"Printer {PrinterName} print state '{State}' is not complete; skipping.",
|
||||||
|
printer.Name, usageData.PrintState);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deduplicate: avoid recording the same completed job twice
|
||||||
|
var deduplicationKey = $"{printer.Id}:{usageData.GcodeFileName}";
|
||||||
|
if (_recordedJobs.Contains(deduplicationKey))
|
||||||
|
{
|
||||||
|
_logger.LogDebug(
|
||||||
|
"Printer {PrinterName} job '{File}' already recorded; skipping.",
|
||||||
|
printer.Name, usageData.GcodeFileName);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find or create a PrintJob for this usage
|
||||||
|
var printJob = await FindOrCreatePrintJobAsync(
|
||||||
|
dbContext, printer, usageData, cancellationToken);
|
||||||
|
|
||||||
|
if (printJob is null)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(
|
||||||
|
"Could not find or create print job for printer {PrinterName}. " +
|
||||||
|
"No active spool found.",
|
||||||
|
printer.Name);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate grams from mm extruded using spool properties
|
||||||
|
var spool = await dbContext.Spools.FindAsync(
|
||||||
|
new object[] { printJob.SpoolId }, cancellationToken);
|
||||||
|
|
||||||
|
var gramsUsed = CalculateGramsUsed(usageData.MmExtruded, spool);
|
||||||
|
|
||||||
|
await usageService.RecordUsageAsync(
|
||||||
|
printJobId: printJob.Id,
|
||||||
|
spoolId: printJob.SpoolId,
|
||||||
|
printerId: printer.Id,
|
||||||
|
gramsUsed: gramsUsed,
|
||||||
|
mmExtruded: usageData.MmExtruded,
|
||||||
|
notes: $"Moonraker auto-recorded: {usageData.GcodeFileName}",
|
||||||
|
cancellationToken: cancellationToken);
|
||||||
|
|
||||||
|
// Mark job as recorded to prevent duplicates
|
||||||
|
_recordedJobs.Add(deduplicationKey);
|
||||||
|
|
||||||
|
_logger.LogInformation(
|
||||||
|
"Recorded Moonraker usage for printer {PrinterName}: " +
|
||||||
|
"{Mm}mm / {Grams}g, job '{File}'",
|
||||||
|
printer.Name, usageData.MmExtruded, gramsUsed,
|
||||||
|
usageData.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)
|
||||||
|
{
|
||||||
|
// Shutdown requested — rethrow to exit the poll loop
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Determines if a Moonraker print state indicates a completed job
|
||||||
|
/// that should have its usage recorded.
|
||||||
|
/// </summary>
|
||||||
|
private static bool IsCompleteState(string state) =>
|
||||||
|
state.Equals("complete", StringComparison.OrdinalIgnoreCase) ||
|
||||||
|
state.Equals("completed", StringComparison.OrdinalIgnoreCase);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Finds an existing PrintJob for the current g-code file on this printer,
|
||||||
|
/// or creates a new one. Returns null if no spool is available.
|
||||||
|
/// </summary>
|
||||||
|
private async Task<PrintJob?> FindOrCreatePrintJobAsync(
|
||||||
|
ExtrudexDbContext dbContext,
|
||||||
|
Printer printer,
|
||||||
|
MoonrakerFilamentUsage usageData,
|
||||||
|
CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
// Try to find an existing print job for this g-code file on this printer
|
||||||
|
if (!string.IsNullOrEmpty(usageData.GcodeFileName))
|
||||||
|
{
|
||||||
|
var existingJob = await dbContext.PrintJobs
|
||||||
|
.Where(j => j.PrinterId == printer.Id &&
|
||||||
|
j.GcodeFilePath == usageData.GcodeFileName &&
|
||||||
|
j.DataSource == DataSource.Moonraker &&
|
||||||
|
j.Status != JobStatus.Cancelled)
|
||||||
|
.OrderByDescending(j => j.CreatedAt)
|
||||||
|
.FirstOrDefaultAsync(cancellationToken);
|
||||||
|
|
||||||
|
if (existingJob is not null)
|
||||||
|
{
|
||||||
|
// Update the existing job with completion data
|
||||||
|
existingJob.MmExtruded = usageData.MmExtruded;
|
||||||
|
existingJob.GramsDerived = CalculateGramsUsed(
|
||||||
|
usageData.MmExtruded,
|
||||||
|
await dbContext.Spools.FindAsync(
|
||||||
|
new object[] { existingJob.SpoolId }, cancellationToken));
|
||||||
|
existingJob.Status = JobStatus.Completed;
|
||||||
|
existingJob.CompletedAt = usageData.CompletedAt ?? DateTime.UtcNow;
|
||||||
|
existingJob.StartedAt ??= usageData.StartedAt;
|
||||||
|
|
||||||
|
await dbContext.SaveChangesAsync(cancellationToken);
|
||||||
|
return existingJob;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// No existing job — find the first active spool for this printer
|
||||||
|
// via AMS slots, or fall back to any active spool
|
||||||
|
var spool = await FindActiveSpoolForPrinterAsync(dbContext, printer, cancellationToken);
|
||||||
|
|
||||||
|
if (spool is null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
var gramsDerived = CalculateGramsUsed(usageData.MmExtruded, spool);
|
||||||
|
|
||||||
|
var newJob = new PrintJob
|
||||||
|
{
|
||||||
|
PrinterId = printer.Id,
|
||||||
|
SpoolId = spool.Id,
|
||||||
|
PrintName = usageData.GcodeFileName ?? "Moonraker Print",
|
||||||
|
GcodeFilePath = usageData.GcodeFileName,
|
||||||
|
MmExtruded = usageData.MmExtruded,
|
||||||
|
GramsDerived = gramsDerived,
|
||||||
|
FilamentDiameterAtPrintMm = spool.FilamentDiameterMm,
|
||||||
|
MaterialDensityAtPrint = GetMaterialDensity(spool),
|
||||||
|
DataSource = DataSource.Moonraker,
|
||||||
|
Status = JobStatus.Completed,
|
||||||
|
StartedAt = usageData.StartedAt ?? DateTime.UtcNow,
|
||||||
|
CompletedAt = usageData.CompletedAt ?? DateTime.UtcNow,
|
||||||
|
Notes = "Auto-created by Moonraker usage poller"
|
||||||
|
};
|
||||||
|
|
||||||
|
dbContext.PrintJobs.Add(newJob);
|
||||||
|
await dbContext.SaveChangesAsync(cancellationToken);
|
||||||
|
|
||||||
|
return newJob;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Finds an active spool associated with the printer via AMS slots,
|
||||||
|
/// or falls back to any active spool in the system.
|
||||||
|
/// </summary>
|
||||||
|
private static async Task<Spool?> FindActiveSpoolForPrinterAsync(
|
||||||
|
ExtrudexDbContext dbContext,
|
||||||
|
Printer printer,
|
||||||
|
CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
// Try to find a spool loaded in the printer's AMS
|
||||||
|
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;
|
||||||
|
|
||||||
|
// Fallback: any active spool (for non-AMS printers)
|
||||||
|
return await dbContext.Spools
|
||||||
|
.Include(s => s.MaterialBase)
|
||||||
|
.Where(s => s.IsActive)
|
||||||
|
.OrderByDescending(s => s.WeightRemainingGrams)
|
||||||
|
.FirstOrDefaultAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Calculates grams used from mm extruded using the spool's filament
|
||||||
|
/// diameter and the material density.
|
||||||
|
/// Formula: grams = mm × π × (diameter/2)² × density
|
||||||
|
/// Where density is in g/cm³, diameter in mm, giving grams.
|
||||||
|
/// </summary>
|
||||||
|
private static decimal CalculateGramsUsed(decimal mmExtruded, Spool? spool)
|
||||||
|
{
|
||||||
|
if (spool is null)
|
||||||
|
return 0m;
|
||||||
|
|
||||||
|
var diameterMm = spool.FilamentDiameterMm;
|
||||||
|
var densityGcm3 = GetMaterialDensity(spool);
|
||||||
|
|
||||||
|
// Cross-section area (mm²) = π × (diameter/2)²
|
||||||
|
var radiusMm = diameterMm / 2m;
|
||||||
|
var crossSectionArea = Math.PI * (double)radiusMm * (double)radiusMm;
|
||||||
|
|
||||||
|
// Volume (mm³) = mm_extruded × cross_section_area
|
||||||
|
// Convert mm³ to cm³: 1 cm³ = 1000 mm³
|
||||||
|
// Weight (g) = volume_cm³ × density (g/cm³)
|
||||||
|
var volumeMm3 = (double)mmExtruded * crossSectionArea;
|
||||||
|
var volumeCm3 = volumeMm3 / 1000.0;
|
||||||
|
var grams = volumeCm3 * (double)densityGcm3;
|
||||||
|
|
||||||
|
return Math.Round((decimal)grams, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the material density for the spool's material base.
|
||||||
|
/// Falls back to 1.24 g/cm³ (typical PLA density) if not available.
|
||||||
|
/// </summary>
|
||||||
|
private static decimal GetMaterialDensity(Spool? spool)
|
||||||
|
{
|
||||||
|
// Standard material densities (g/cm³)
|
||||||
|
// These would ideally come from the MaterialBase entity,
|
||||||
|
// but we use sensible defaults for the initial integration.
|
||||||
|
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 // Default to PLA density
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
using Extrudex.API.Filters;
|
using Extrudex.API.Filters;
|
||||||
using Extrudex.API.Hubs;
|
using Extrudex.API.Hubs;
|
||||||
using Extrudex.Domain.Interfaces;
|
using Extrudex.Domain.Interfaces;
|
||||||
@@ -50,6 +51,23 @@ builder.Services.AddSwaggerGen(c =>
|
|||||||
// ── QR Code Generation ──────────────────────────────────────
|
// ── QR Code Generation ──────────────────────────────────────
|
||||||
builder.Services.AddSingleton<IQrCodeService, QrCodeService>();
|
builder.Services.AddSingleton<IQrCodeService, QrCodeService>();
|
||||||
|
|
||||||
|
// ── Filament Usage Service ──────────────────────────────────
|
||||||
|
builder.Services.AddScoped<IFilamentUsageService, FilamentUsageService>();
|
||||||
|
|
||||||
|
// ── Moonraker Client ───────────────────────────────────────
|
||||||
|
// Named HttpClient for Moonraker API calls with configurable timeout.
|
||||||
|
// Poller timeout is driven by MoonrakerPollerOptions.RequestTimeout.
|
||||||
|
builder.Services.AddHttpClient<IMoonrakerClient, MoonrakerClient>(client =>
|
||||||
|
{
|
||||||
|
client.DefaultRequestHeaders.Accept.Add(
|
||||||
|
new MediaTypeWithQualityHeaderValue("application/json"));
|
||||||
|
});
|
||||||
|
|
||||||
|
// ── 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());
|
||||||
|
|||||||
@@ -9,5 +9,10 @@
|
|||||||
"AllowedHosts": "*",
|
"AllowedHosts": "*",
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
"ExtrudexDb": "Host=localhost;Port=5432;Database=extrudex;Username=extrudex;Password=changeme"
|
"ExtrudexDb": "Host=localhost;Port=5432;Database=extrudex;Username=extrudex;Password=changeme"
|
||||||
|
},
|
||||||
|
"MoonrakerPoller": {
|
||||||
|
"Enabled": true,
|
||||||
|
"PollInterval": "00:00:30",
|
||||||
|
"RequestTimeout": "00:00:10"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
11
frontend/.dockerignore
Normal file
11
frontend/.dockerignore
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
node_modules
|
||||||
|
dist
|
||||||
|
.git
|
||||||
|
.gitignore
|
||||||
|
.angular
|
||||||
|
.vscode
|
||||||
|
*.md
|
||||||
|
.editorconfig
|
||||||
|
.prettierrc
|
||||||
|
src/test.ts
|
||||||
|
**/*.spec.ts
|
||||||
28
frontend/Dockerfile
Normal file
28
frontend/Dockerfile
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
# Stage 1: Build the Angular application
|
||||||
|
FROM node:22-alpine AS build
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Copy package files first for better layer caching
|
||||||
|
COPY package.json package-lock.json ./
|
||||||
|
RUN npm ci
|
||||||
|
|
||||||
|
# Copy source and build
|
||||||
|
COPY . .
|
||||||
|
RUN npx ng build --configuration production
|
||||||
|
|
||||||
|
# Stage 2: Serve static files with nginx
|
||||||
|
FROM nginx:alpine
|
||||||
|
|
||||||
|
# Remove default nginx config
|
||||||
|
RUN rm /etc/nginx/conf.d/default.conf
|
||||||
|
|
||||||
|
# Copy custom nginx config
|
||||||
|
COPY nginx.conf /etc/nginx/conf.d/default.conf
|
||||||
|
|
||||||
|
# Copy built Angular artifacts from build stage
|
||||||
|
COPY --from=build /app/dist/frontend/browser /usr/share/nginx/html
|
||||||
|
|
||||||
|
EXPOSE 80
|
||||||
|
|
||||||
|
CMD ["nginx", "-g", "daemon off;"]
|
||||||
42
frontend/nginx.conf
Normal file
42
frontend/nginx.conf
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
server {
|
||||||
|
listen 80;
|
||||||
|
server_name _;
|
||||||
|
root /usr/share/nginx/html;
|
||||||
|
index index.html;
|
||||||
|
|
||||||
|
# Gzip compression
|
||||||
|
gzip on;
|
||||||
|
gzip_types text/plain text/css application/json application/javascript text/xml application/xml text/javascript;
|
||||||
|
gzip_min_length 256;
|
||||||
|
|
||||||
|
# Angular SPA — fallback to index.html for client-side routing
|
||||||
|
location / {
|
||||||
|
try_files $uri $uri/ /index.html;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Cache static assets aggressively
|
||||||
|
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff2?|ttf|eot)$ {
|
||||||
|
expires 1y;
|
||||||
|
add_header Cache-Control "public, immutable";
|
||||||
|
}
|
||||||
|
|
||||||
|
# Proxy API requests to backend
|
||||||
|
# Uses resolver so nginx doesn't crash if backend isn't available at startup
|
||||||
|
resolver 127.0.0.11 valid=30s ipv6=off;
|
||||||
|
set $backend "extrudex-api:8080";
|
||||||
|
|
||||||
|
location /api/ {
|
||||||
|
proxy_pass http://$backend;
|
||||||
|
proxy_set_header Host $host;
|
||||||
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
|
proxy_set_header X-Forwarded-Proto $scheme;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Health check endpoint
|
||||||
|
location /health {
|
||||||
|
access_log off;
|
||||||
|
return 200 "ok";
|
||||||
|
add_header Content-Type text/plain;
|
||||||
|
}
|
||||||
|
}
|
||||||
16
frontend/package-lock.json
generated
16
frontend/package-lock.json
generated
@@ -8,6 +8,7 @@
|
|||||||
"name": "frontend",
|
"name": "frontend",
|
||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@angular/animations": "^21.2.10",
|
||||||
"@angular/cdk": "^21.2.8",
|
"@angular/cdk": "^21.2.8",
|
||||||
"@angular/common": "^21.2.0",
|
"@angular/common": "^21.2.0",
|
||||||
"@angular/compiler": "^21.2.0",
|
"@angular/compiler": "^21.2.0",
|
||||||
@@ -326,6 +327,21 @@
|
|||||||
"yarn": ">= 1.13.0"
|
"yarn": ">= 1.13.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/@angular/animations": {
|
||||||
|
"version": "21.2.10",
|
||||||
|
"resolved": "https://registry.npmjs.org/@angular/animations/-/animations-21.2.10.tgz",
|
||||||
|
"integrity": "sha512-sIzAcxwtRCJ/fu0tK4mo1ooiEaDxJ+Nl6s9nK1D1NP1em12VX03Jx8CMixp/kVtgh4mZnm1x6psBB0FUz3U3Ug==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"tslib": "^2.3.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": "^20.19.0 || ^22.12.0 || >=24.0.0"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"@angular/core": "21.2.10"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/@angular/build": {
|
"node_modules/@angular/build": {
|
||||||
"version": "21.2.8",
|
"version": "21.2.8",
|
||||||
"resolved": "https://registry.npmjs.org/@angular/build/-/build-21.2.8.tgz",
|
"resolved": "https://registry.npmjs.org/@angular/build/-/build-21.2.8.tgz",
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
"private": true,
|
"private": true,
|
||||||
"packageManager": "npm@11.11.0",
|
"packageManager": "npm@11.11.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@angular/animations": "^21.2.10",
|
||||||
"@angular/cdk": "^21.2.8",
|
"@angular/cdk": "^21.2.8",
|
||||||
"@angular/common": "^21.2.0",
|
"@angular/common": "^21.2.0",
|
||||||
"@angular/compiler": "^21.2.0",
|
"@angular/compiler": "^21.2.0",
|
||||||
|
|||||||
@@ -1,11 +1,15 @@
|
|||||||
import { ApplicationConfig, provideBrowserGlobalErrorListeners } from '@angular/core';
|
import { ApplicationConfig, provideBrowserGlobalErrorListeners } from '@angular/core';
|
||||||
import { provideRouter } from '@angular/router';
|
import { provideRouter } from '@angular/router';
|
||||||
|
import { provideHttpClient, withFetch } from '@angular/common/http';
|
||||||
|
import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';
|
||||||
|
|
||||||
import { routes } from './app.routes';
|
import { routes } from './app.routes';
|
||||||
|
|
||||||
export const appConfig: ApplicationConfig = {
|
export const appConfig: ApplicationConfig = {
|
||||||
providers: [
|
providers: [
|
||||||
provideBrowserGlobalErrorListeners(),
|
provideBrowserGlobalErrorListeners(),
|
||||||
provideRouter(routes)
|
provideRouter(routes),
|
||||||
|
provideHttpClient(withFetch()),
|
||||||
|
provideAnimationsAsync(),
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
<!-- Delete Filament Confirmation Dialog -->
|
||||||
|
<h2 mat-dialog-title>
|
||||||
|
<mat-icon aria-hidden="true">warning</mat-icon>
|
||||||
|
Delete Filament Spool?
|
||||||
|
</h2>
|
||||||
|
|
||||||
|
<mat-dialog-content>
|
||||||
|
<p class="dialog-description">
|
||||||
|
You are about to permanently remove this filament spool from inventory.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<!-- Spool details card -->
|
||||||
|
<div class="spool-details" role="list" aria-label="Spool details">
|
||||||
|
<div class="detail-row" role="listitem">
|
||||||
|
<span class="detail-label">Material</span>
|
||||||
|
<span class="detail-value">{{ filament.materialBaseName }}{{ filament.materialFinishName ? ' — ' + filament.materialFinishName : '' }}{{ filament.materialModifierName ? ' (' + filament.materialModifierName + ')' : '' }}</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="detail-row" role="listitem">
|
||||||
|
<span class="detail-label">Brand</span>
|
||||||
|
<span class="detail-value">{{ filament.brand }}</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="detail-row" role="listitem">
|
||||||
|
<span class="detail-label">Color</span>
|
||||||
|
<span class="detail-value color-value">
|
||||||
|
<span class="color-swatch-inline"
|
||||||
|
[style.background-color]="filament.colorHex"
|
||||||
|
[attr.aria-label]="filament.colorName">
|
||||||
|
</span>
|
||||||
|
{{ filament.colorName }}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="detail-row" role="listitem">
|
||||||
|
<span class="detail-label">Serial</span>
|
||||||
|
<span class="detail-value serial-value">{{ filament.spoolSerial }}</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="detail-row" role="listitem">
|
||||||
|
<span class="detail-label">Remaining</span>
|
||||||
|
<span class="detail-value">{{ formatWeight(filament.weightRemainingGrams) }} / {{ formatWeight(filament.weightTotalGrams) }}</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="detail-row" role="listitem">
|
||||||
|
<span class="detail-label">Status</span>
|
||||||
|
<span class="detail-value">
|
||||||
|
<span class="status-badge"
|
||||||
|
[class.active]="filament.isActive"
|
||||||
|
[class.inactive]="!filament.isActive">
|
||||||
|
{{ filament.isActive ? 'Active' : 'Inactive' }}
|
||||||
|
</span>
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<p class="dialog-warning">
|
||||||
|
<mat-icon aria-hidden="true">info</mat-icon>
|
||||||
|
This action cannot be undone.
|
||||||
|
</p>
|
||||||
|
</mat-dialog-content>
|
||||||
|
|
||||||
|
<mat-dialog-actions align="end">
|
||||||
|
<button mat-button
|
||||||
|
type="button"
|
||||||
|
(click)="onCancel()"
|
||||||
|
class="cancel-button">
|
||||||
|
Cancel
|
||||||
|
</button>
|
||||||
|
<button mat-flat-button
|
||||||
|
type="button"
|
||||||
|
color="warn"
|
||||||
|
(click)="onConfirm()"
|
||||||
|
class="confirm-button">
|
||||||
|
<mat-icon aria-hidden="true">delete</mat-icon>
|
||||||
|
Delete Spool
|
||||||
|
</button>
|
||||||
|
</mat-dialog-actions>
|
||||||
@@ -0,0 +1,150 @@
|
|||||||
|
/**
|
||||||
|
* Delete Filament Dialog Styles
|
||||||
|
* Touch-optimized confirmation dialog for spool removal
|
||||||
|
*/
|
||||||
|
|
||||||
|
$spacing-unit: 8px;
|
||||||
|
$color-critical: #ef4444;
|
||||||
|
$color-inactive: #94a3b8;
|
||||||
|
$color-active: #22c55e;
|
||||||
|
|
||||||
|
// Dialog title
|
||||||
|
h2[mat-dialog-title] {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: $spacing-unit;
|
||||||
|
color: $color-critical;
|
||||||
|
|
||||||
|
mat-icon {
|
||||||
|
font-size: 24px !important;
|
||||||
|
width: 24px !important;
|
||||||
|
height: 24px !important;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Description text
|
||||||
|
.dialog-description {
|
||||||
|
margin: 0 0 $spacing-unit * 2;
|
||||||
|
font-size: 14px;
|
||||||
|
line-height: 1.5;
|
||||||
|
color: var(--mat-sys-on-surface);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Spool details card
|
||||||
|
.spool-details {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: $spacing-unit;
|
||||||
|
padding: $spacing-unit * 1.5;
|
||||||
|
background-color: var(--mat-sys-surface-container);
|
||||||
|
border-radius: 8px;
|
||||||
|
margin-bottom: $spacing-unit * 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
.detail-row {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: center;
|
||||||
|
padding: $spacing-unit * 0.5 0;
|
||||||
|
font-size: 14px;
|
||||||
|
|
||||||
|
&:not(:last-child) {
|
||||||
|
border-bottom: 1px solid var(--mat-sys-outline-variant);
|
||||||
|
padding-bottom: $spacing-unit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.detail-label {
|
||||||
|
font-weight: 500;
|
||||||
|
color: var(--mat-sys-on-surface-variant);
|
||||||
|
flex-shrink: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.detail-value {
|
||||||
|
font-weight: 400;
|
||||||
|
color: var(--mat-sys-on-surface);
|
||||||
|
text-align: right;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Color swatch inline
|
||||||
|
.color-swatch-inline {
|
||||||
|
display: inline-block;
|
||||||
|
width: 18px;
|
||||||
|
height: 18px;
|
||||||
|
border-radius: 50%;
|
||||||
|
border: 1.5px solid rgba(0, 0, 0, 0.12);
|
||||||
|
vertical-align: middle;
|
||||||
|
margin-right: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.color-value {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: flex-end;
|
||||||
|
gap: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Serial value — monospace
|
||||||
|
.serial-value {
|
||||||
|
font-family: 'JetBrains Mono', 'Roboto Mono', monospace;
|
||||||
|
font-size: 13px;
|
||||||
|
letter-spacing: 0.02em;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Status badge — matches filament table styling
|
||||||
|
.status-badge {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
padding: 2px 8px;
|
||||||
|
border-radius: 10px;
|
||||||
|
font-size: 11px;
|
||||||
|
font-weight: 600;
|
||||||
|
text-transform: uppercase;
|
||||||
|
letter-spacing: 0.04em;
|
||||||
|
|
||||||
|
&.active {
|
||||||
|
background-color: rgba($color-active, 0.12);
|
||||||
|
color: $color-active;
|
||||||
|
}
|
||||||
|
|
||||||
|
&.inactive {
|
||||||
|
background-color: rgba($color-inactive, 0.12);
|
||||||
|
color: $color-inactive;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Warning text
|
||||||
|
.dialog-warning {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: $spacing-unit;
|
||||||
|
margin: 0;
|
||||||
|
font-size: 13px;
|
||||||
|
color: $color-critical;
|
||||||
|
|
||||||
|
mat-icon {
|
||||||
|
font-size: 18px !important;
|
||||||
|
width: 18px !important;
|
||||||
|
height: 18px !important;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dialog action buttons
|
||||||
|
mat-dialog-actions {
|
||||||
|
padding-top: $spacing-unit * 2;
|
||||||
|
|
||||||
|
.cancel-button {
|
||||||
|
min-width: 80px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.confirm-button {
|
||||||
|
min-width: 120px;
|
||||||
|
|
||||||
|
mat-icon {
|
||||||
|
font-size: 18px !important;
|
||||||
|
width: 18px !important;
|
||||||
|
height: 18px !important;
|
||||||
|
margin-right: 4px;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
|
||||||
|
import { CommonModule } from '@angular/common';
|
||||||
|
import {
|
||||||
|
MAT_DIALOG_DATA,
|
||||||
|
MatDialogRef,
|
||||||
|
MatDialogModule,
|
||||||
|
} from '@angular/material/dialog';
|
||||||
|
import { MatButtonModule } from '@angular/material/button';
|
||||||
|
import { MatIconModule } from '@angular/material/icon';
|
||||||
|
import { MatChipsModule } from '@angular/material/chips';
|
||||||
|
|
||||||
|
import { Filament } from '../../models/filament.model';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Data passed into the delete confirmation dialog.
|
||||||
|
*/
|
||||||
|
export interface DeleteFilamentDialogData {
|
||||||
|
filament: Filament;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete confirmation dialog for filament spool removal.
|
||||||
|
*
|
||||||
|
* Displays spool details (material, brand, color, serial, remaining weight)
|
||||||
|
* and requires the user to confirm before deletion proceeds.
|
||||||
|
* Cancel dismisses the dialog with no action.
|
||||||
|
*/
|
||||||
|
@Component({
|
||||||
|
selector: 'app-delete-filament-dialog',
|
||||||
|
standalone: true,
|
||||||
|
imports: [
|
||||||
|
CommonModule,
|
||||||
|
MatDialogModule,
|
||||||
|
MatButtonModule,
|
||||||
|
MatIconModule,
|
||||||
|
MatChipsModule,
|
||||||
|
],
|
||||||
|
templateUrl: './delete-filament-dialog.component.html',
|
||||||
|
styleUrl: './delete-filament-dialog.component.scss',
|
||||||
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||||
|
})
|
||||||
|
export class DeleteFilamentDialogComponent {
|
||||||
|
private readonly dialogRef = inject(
|
||||||
|
MatDialogRef<DeleteFilamentDialogComponent, boolean>
|
||||||
|
);
|
||||||
|
readonly data: DeleteFilamentDialogData = inject(MAT_DIALOG_DATA);
|
||||||
|
|
||||||
|
/** The filament spool being considered for deletion */
|
||||||
|
readonly filament = this.data.filament;
|
||||||
|
|
||||||
|
/** Format weight for display in dialog */
|
||||||
|
formatWeight(grams: number): string {
|
||||||
|
if (grams >= 1000) {
|
||||||
|
return `${(grams / 1000).toFixed(1)}kg`;
|
||||||
|
}
|
||||||
|
return `${Math.round(grams)}g`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Cancel — close dialog with false (no deletion) */
|
||||||
|
onCancel(): void {
|
||||||
|
this.dialogRef.close(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Confirm — close dialog with true (proceed with deletion) */
|
||||||
|
onConfirm(): void {
|
||||||
|
this.dialogRef.close(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
<!-- Filament Inventory Table — with low stock indicators -->
|
<!-- Filament Inventory Table — with low stock indicators and delete actions -->
|
||||||
<div class="filament-table-container" role="region" aria-label="Filament inventory">
|
<div class="filament-table-container" role="region" aria-label="Filament inventory">
|
||||||
|
|
||||||
<!-- Low Stock Alert Banner — shown when critical or low stock spools exist -->
|
<!-- Low Stock Alert Banner — shown when critical or low stock spools exist -->
|
||||||
@@ -106,10 +106,32 @@
|
|||||||
</td>
|
</td>
|
||||||
</ng-container>
|
</ng-container>
|
||||||
|
|
||||||
|
<!-- Actions Column — delete button -->
|
||||||
|
<ng-container matColumnDef="actions">
|
||||||
|
<th mat-header-cell *matHeaderCellDef>Actions</th>
|
||||||
|
<td mat-cell *matCellDef="let filament">
|
||||||
|
<button mat-icon-button
|
||||||
|
type="button"
|
||||||
|
color="warn"
|
||||||
|
[attr.aria-label]="'Delete ' + filament.materialBaseName + ' — ' + filament.colorName"
|
||||||
|
matTooltip="Delete spool"
|
||||||
|
matTooltipPosition="above"
|
||||||
|
[disabled]="deleting() === filament.id"
|
||||||
|
(click)="onDeleteClick(filament)">
|
||||||
|
@if (deleting() === filament.id) {
|
||||||
|
<mat-icon aria-hidden="true">hourglass_empty</mat-icon>
|
||||||
|
} @else {
|
||||||
|
<mat-icon aria-hidden="true">delete</mat-icon>
|
||||||
|
}
|
||||||
|
</button>
|
||||||
|
</td>
|
||||||
|
</ng-container>
|
||||||
|
|
||||||
<tr mat-header-row *matHeaderRowDef="columns()"></tr>
|
<tr mat-header-row *matHeaderRowDef="columns()"></tr>
|
||||||
<tr mat-row *matRowDef="let row; columns: columns();"
|
<tr mat-row *matRowDef="let row; columns: columns();"
|
||||||
[class.row-critical]="classifyStockLevel(row) === 'critical'"
|
[class.row-critical]="classifyStockLevel(row) === 'critical'"
|
||||||
[class.row-low]="classifyStockLevel(row) === 'low'">
|
[class.row-low]="classifyStockLevel(row) === 'low'"
|
||||||
|
[class.row-deleting]="deleting() === row.id">
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
|
|||||||
@@ -235,6 +235,20 @@ mat-chip {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Actions column
|
||||||
|
.actions-cell {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Row being deleted — subtle fade
|
||||||
|
:host ::ng-deep .row-deleting {
|
||||||
|
opacity: 0.5;
|
||||||
|
pointer-events: none;
|
||||||
|
transition: opacity 0.3s ease;
|
||||||
|
}
|
||||||
|
|
||||||
// Empty state
|
// Empty state
|
||||||
.empty-state {
|
.empty-state {
|
||||||
display: flex;
|
display: flex;
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import {
|
|||||||
Component,
|
Component,
|
||||||
Input,
|
Input,
|
||||||
computed,
|
computed,
|
||||||
|
inject,
|
||||||
signal,
|
signal,
|
||||||
} from '@angular/core';
|
} from '@angular/core';
|
||||||
import { CommonModule } from '@angular/common';
|
import { CommonModule } from '@angular/common';
|
||||||
@@ -12,12 +13,21 @@ import { MatIconModule } from '@angular/material/icon';
|
|||||||
import { MatProgressBarModule } from '@angular/material/progress-bar';
|
import { MatProgressBarModule } from '@angular/material/progress-bar';
|
||||||
import { MatTooltipModule } from '@angular/material/tooltip';
|
import { MatTooltipModule } from '@angular/material/tooltip';
|
||||||
import { MatSortModule, Sort } from '@angular/material/sort';
|
import { MatSortModule, Sort } from '@angular/material/sort';
|
||||||
|
import { MatButtonModule } from '@angular/material/button';
|
||||||
|
import { MatDialog, MatDialogModule } from '@angular/material/dialog';
|
||||||
|
import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
Filament,
|
Filament,
|
||||||
StockLevel,
|
StockLevel,
|
||||||
getRemainingPercent,
|
getRemainingPercent,
|
||||||
classifyStockLevel,
|
classifyStockLevel,
|
||||||
} from '../../models/filament.model';
|
} from '../../models/filament.model';
|
||||||
|
import { FilamentService } from '../../services/filament.service';
|
||||||
|
import {
|
||||||
|
DeleteFilamentDialogComponent,
|
||||||
|
DeleteFilamentDialogData,
|
||||||
|
} from '../delete-filament-dialog/delete-filament-dialog.component';
|
||||||
|
|
||||||
/** Display column definitions for the filament table */
|
/** Display column definitions for the filament table */
|
||||||
export type FilamentColumn =
|
export type FilamentColumn =
|
||||||
@@ -27,7 +37,8 @@ export type FilamentColumn =
|
|||||||
| 'serial'
|
| 'serial'
|
||||||
| 'remaining'
|
| 'remaining'
|
||||||
| 'stockLevel'
|
| 'stockLevel'
|
||||||
| 'status';
|
| 'status'
|
||||||
|
| 'actions';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-filament-table',
|
selector: 'app-filament-table',
|
||||||
@@ -40,16 +51,26 @@ export type FilamentColumn =
|
|||||||
MatProgressBarModule,
|
MatProgressBarModule,
|
||||||
MatTooltipModule,
|
MatTooltipModule,
|
||||||
MatSortModule,
|
MatSortModule,
|
||||||
|
MatButtonModule,
|
||||||
|
MatDialogModule,
|
||||||
|
MatSnackBarModule,
|
||||||
],
|
],
|
||||||
templateUrl: './filament-table.component.html',
|
templateUrl: './filament-table.component.html',
|
||||||
styleUrl: './filament-table.component.scss',
|
styleUrl: './filament-table.component.scss',
|
||||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||||
})
|
})
|
||||||
export class FilamentTableComponent {
|
export class FilamentTableComponent {
|
||||||
|
private readonly dialog = inject(MatDialog);
|
||||||
|
private readonly snackBar = inject(MatSnackBar);
|
||||||
|
private readonly filamentService = inject(FilamentService);
|
||||||
|
|
||||||
/** Filament data input — reactive signal for live updates */
|
/** Filament data input — reactive signal for live updates */
|
||||||
readonly filaments = signal<Filament[]>([]);
|
readonly filaments = signal<Filament[]>([]);
|
||||||
|
|
||||||
/** Columns to display — defaults to all columns */
|
/** Whether a delete operation is in progress */
|
||||||
|
readonly deleting = signal<string | null>(null);
|
||||||
|
|
||||||
|
/** Columns to display — defaults to all columns including actions */
|
||||||
@Input()
|
@Input()
|
||||||
set displayedColumns(cols: FilamentColumn[]) {
|
set displayedColumns(cols: FilamentColumn[]) {
|
||||||
this._displayedColumns.set(cols);
|
this._displayedColumns.set(cols);
|
||||||
@@ -65,6 +86,7 @@ export class FilamentTableComponent {
|
|||||||
'remaining',
|
'remaining',
|
||||||
'stockLevel',
|
'stockLevel',
|
||||||
'status',
|
'status',
|
||||||
|
'actions',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
/** Default columns for template binding */
|
/** Default columns for template binding */
|
||||||
@@ -252,6 +274,52 @@ export class FilamentTableComponent {
|
|||||||
this.sortedFilaments.set(sorted);
|
this.sortedFilaments.set(sorted);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Open the delete confirmation dialog for a filament spool.
|
||||||
|
* On confirm: calls DELETE endpoint and removes the row on success.
|
||||||
|
* On cancel: dialog dismissed, no action taken.
|
||||||
|
*/
|
||||||
|
onDeleteClick(filament: Filament): void {
|
||||||
|
const dialogData: DeleteFilamentDialogData = { filament };
|
||||||
|
const dialogRef = this.dialog.open(DeleteFilamentDialogComponent, {
|
||||||
|
data: dialogData,
|
||||||
|
width: '480px',
|
||||||
|
disableClose: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
dialogRef.afterClosed().subscribe((confirmed: boolean | undefined) => {
|
||||||
|
if (!confirmed) {
|
||||||
|
return; // User cancelled — no action
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mark as deleting for UI feedback
|
||||||
|
this.deleting.set(filament.id);
|
||||||
|
|
||||||
|
this.filamentService.deleteFilament(filament.id).subscribe({
|
||||||
|
next: () => {
|
||||||
|
// Remove the deleted filament from local data
|
||||||
|
const updated = this.filaments().filter((f) => f.id !== filament.id);
|
||||||
|
this.updateFilaments(updated);
|
||||||
|
this.deleting.set(null);
|
||||||
|
|
||||||
|
this.snackBar.open(
|
||||||
|
`Deleted ${filament.materialBaseName} — ${filament.colorName}`,
|
||||||
|
'Dismiss',
|
||||||
|
{ duration: 4000 }
|
||||||
|
);
|
||||||
|
},
|
||||||
|
error: () => {
|
||||||
|
this.deleting.set(null);
|
||||||
|
this.snackBar.open(
|
||||||
|
`Failed to delete ${filament.materialBaseName} — ${filament.colorName}. Please try again.`,
|
||||||
|
'Dismiss',
|
||||||
|
{ duration: 6000 }
|
||||||
|
);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/** Template helper: get remaining percent */
|
/** Template helper: get remaining percent */
|
||||||
getRemainingPercent = getRemainingPercent;
|
getRemainingPercent = getRemainingPercent;
|
||||||
|
|
||||||
|
|||||||
37
frontend/src/app/services/filament.service.ts
Normal file
37
frontend/src/app/services/filament.service.ts
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import { Injectable, inject } from '@angular/core';
|
||||||
|
import { HttpClient } from '@angular/common/http';
|
||||||
|
import { Observable } from 'rxjs';
|
||||||
|
|
||||||
|
import { Filament } from '../models/filament.model';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* API base URL — matches the Extrudex backend default.
|
||||||
|
* TODO: Move to environment config when multi-environment support is added.
|
||||||
|
*/
|
||||||
|
const API_BASE_URL = '/api';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Service for CRUD operations on filament spools.
|
||||||
|
* Communicates with the Extrudex backend SpoolsController.
|
||||||
|
*/
|
||||||
|
@Injectable({ providedIn: 'root' })
|
||||||
|
export class FilamentService {
|
||||||
|
private readonly http = inject(HttpClient);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch all filament spools from the backend.
|
||||||
|
* GET /api/spools
|
||||||
|
*/
|
||||||
|
getFilaments(): Observable<Filament[]> {
|
||||||
|
return this.http.get<Filament[]>(`${API_BASE_URL}/spools`);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Soft-delete a filament spool by ID.
|
||||||
|
* DELETE /api/spools/{id}
|
||||||
|
* Returns 204 No Content on success.
|
||||||
|
*/
|
||||||
|
deleteFilament(id: string): Observable<void> {
|
||||||
|
return this.http.delete<void>(`${API_BASE_URL}/spools/${id}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user