Compare commits
31 Commits
agent/rex/
...
a108c6bcc0
| Author | SHA1 | Date | |
|---|---|---|---|
| a108c6bcc0 | |||
| e9e856a012 | |||
| 46d28676f0 | |||
| ed0efd598b | |||
| 19415003a2 | |||
| 7904fcda02 | |||
| 3d3b7059cf | |||
| fc6134b162 | |||
| 51bfb6d115 | |||
| aa182af979 | |||
| ac033859a8 | |||
| e209c3891e | |||
| d3c1b929c5 | |||
| c3a0f210a1 | |||
| 2017843dc1 | |||
| c150f54c64 | |||
| a8b5fd42c3 | |||
| 73363206ec | |||
| 174dd294e9 | |||
| 0378aee43e | |||
| 72a39ec766 | |||
| c05b9dd87d | |||
| 5a577e1871 | |||
| 2e8227c3f9 | |||
| d207c49ffd | |||
| 5b9dde13fe | |||
| fd9fcd47ab | |||
| d43985cad9 | |||
| 6aa31f4be3 | |||
| 4ba98966eb | |||
| 61178ebb7b |
220
README.md
220
README.md
@@ -0,0 +1,220 @@
|
|||||||
|
# Extrudex
|
||||||
|
|
||||||
|
> Filament inventory and print tracking system for CubeCraft Creations.
|
||||||
|
|
||||||
|
Extrudex replaces Spoolman with a fully custom solution built for Joshua's 7-printer fleet. It tracks spool stock, per-print material consumption, and cost-of-goods — with a touch-optimized kiosk interface on a Raspberry Pi 5.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Tech Stack
|
||||||
|
|
||||||
|
| Layer | Technology |
|
||||||
|
|---|---|
|
||||||
|
| Backend | ASP.NET Core Web API (.NET 8) |
|
||||||
|
| Database | PostgreSQL (snake_case via EF Core) |
|
||||||
|
| ORM | Entity Framework Core |
|
||||||
|
| Real-time | SignalR (`PrinterHub`) |
|
||||||
|
| Printer integration | Moonraker REST/WebSocket (Elegoo) · MQTTnet + TLS (Bambu Lab) |
|
||||||
|
| Frontend | Angular 17+, Angular Material |
|
||||||
|
| Deployment | Docker · Docker Compose |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Project Structure
|
||||||
|
|
||||||
|
```
|
||||||
|
Extrudex/
|
||||||
|
├── backend/
|
||||||
|
│ ├── Domain/
|
||||||
|
│ │ ├── Base/ # BaseEntity, AuditableEntity
|
||||||
|
│ │ ├── Entities/ # Spool, Printer, PrintJob, FilamentUsage,
|
||||||
|
│ │ │ # AmsUnit, AmsSlot, MaterialBase,
|
||||||
|
│ │ │ # MaterialFinish, MaterialModifier
|
||||||
|
│ │ ├── Enums/ # ConnectionType, DataSource, JobStatus,
|
||||||
|
│ │ │ # PrinterStatus, PrinterType, QrResourceType
|
||||||
|
│ │ └── Interfaces/ # ICostPerPrintService, IFilamentUsageSyncService,
|
||||||
|
│ │ # IMoonrakerClient, IQrCodeService
|
||||||
|
│ ├── Infrastructure/
|
||||||
|
│ │ ├── Configuration/ # FilamentUsageSyncOptions
|
||||||
|
│ │ ├── Data/
|
||||||
|
│ │ │ ├── Configurations/ # EF Core fluent configs (snake_case)
|
||||||
|
│ │ │ ├── Migrations/ # EF migrations
|
||||||
|
│ │ │ ├── Seed/ # SeedData.cs
|
||||||
|
│ │ │ └── ExtrudexDbContext.cs
|
||||||
|
│ │ └── Services/ # CostPerPrintService, FilamentUsageSyncService,
|
||||||
|
│ │ # MoonrakerClient, QrCodeService
|
||||||
|
│ └── API/
|
||||||
|
│ ├── Controllers/ # Filaments, Spools, Printers, PrintJobs,
|
||||||
|
│ │ # MaterialBases, MaterialFinishes,
|
||||||
|
│ │ # MaterialModifiers, MaterialLookups,
|
||||||
|
│ │ # CostAnalysis, QR
|
||||||
|
│ ├── DTOs/ # Request/response shapes per domain
|
||||||
|
│ ├── Filters/ # FluentValidationFilter
|
||||||
|
│ ├── Hubs/ # PrinterHub, IPrinterClient
|
||||||
|
│ ├── Jobs/ # FilamentUsageSyncJob (background)
|
||||||
|
│ ├── Validators/ # FluentValidation validators
|
||||||
|
│ ├── Program.cs
|
||||||
|
│ └── appsettings.json
|
||||||
|
├── frontend/
|
||||||
|
│ └── src/app/
|
||||||
|
│ ├── components/ # DashboardSummary, FilamentFilter, FilamentTable
|
||||||
|
│ ├── models/ # Filament, Agent model types
|
||||||
|
│ └── app.routes.ts
|
||||||
|
├── design/ # UX specs and mockups (kiosk + mobile)
|
||||||
|
├── docker-compose.dev.yml
|
||||||
|
├── deploy.sh
|
||||||
|
└── README.md
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Domain Model
|
||||||
|
|
||||||
|
### Materials (normalized taxonomy)
|
||||||
|
|
||||||
|
| Entity | Description |
|
||||||
|
|---|---|
|
||||||
|
| `MaterialBase` | The base material type — PLA, PETG, ABS, ASA, TPU, etc. |
|
||||||
|
| `MaterialFinish` | Required. Surface finish — Basic (default), Matte, Silk, Sparkle, etc. |
|
||||||
|
| `MaterialModifier` | Optional. Composite fill — Carbon Fiber, Glass Fiber, Wood, etc. |
|
||||||
|
|
||||||
|
**Rules:**
|
||||||
|
- `MaterialFinish` is required — every spool must have one. Default is `"Basic"`.
|
||||||
|
- `MaterialModifier` is optional — plain PLA has no modifier.
|
||||||
|
|
||||||
|
### Consumption calculation
|
||||||
|
|
||||||
|
```
|
||||||
|
grams_used = mm_extruded × filament_cross_section_area × material_density
|
||||||
|
```
|
||||||
|
|
||||||
|
Grams are always derived, never assumed from printer telemetry directly.
|
||||||
|
|
||||||
|
### Printers
|
||||||
|
|
||||||
|
| Type | Integration |
|
||||||
|
|---|---|
|
||||||
|
| Bambu Lab (×5) | MQTTnet with TLS |
|
||||||
|
| Elegoo Centauri Carbon | Moonraker REST + WebSocket |
|
||||||
|
| Elegoo Saturn (resin ×2) | Manual / future |
|
||||||
|
|
||||||
|
AMS units and slots are modelled as `AmsUnit` → `AmsSlot[]` → `Spool`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Key Design Decisions
|
||||||
|
|
||||||
|
1. **Spoolman rejected** — Full custom system for data model control and workflow flexibility.
|
||||||
|
2. **`"Basic"` not `"Standard"`** — Default `MaterialFinish` value is `Basic`.
|
||||||
|
3. **`MaterialFinish` is required** — No null/optional finish state allowed.
|
||||||
|
4. **`MaterialModifier` is optional** — Not every spool has a modifier.
|
||||||
|
5. **Derived consumption** — Grams calculated from mm × density, never assumed.
|
||||||
|
6. **Push over poll** — SignalR and MQTT preferred over periodic polling.
|
||||||
|
7. **Snake_case PostgreSQL** — All database identifiers follow this convention via EF Core.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Getting Started
|
||||||
|
|
||||||
|
### Prerequisites
|
||||||
|
|
||||||
|
- .NET 8 SDK
|
||||||
|
- Node.js 20+
|
||||||
|
- Docker + Docker Compose
|
||||||
|
- PostgreSQL (or use the dev compose stack)
|
||||||
|
|
||||||
|
### Backend
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd backend
|
||||||
|
|
||||||
|
# Restore and build
|
||||||
|
dotnet restore
|
||||||
|
dotnet build
|
||||||
|
|
||||||
|
# Apply migrations
|
||||||
|
dotnet ef database update
|
||||||
|
|
||||||
|
# Run API (dev)
|
||||||
|
dotnet run --project API
|
||||||
|
```
|
||||||
|
|
||||||
|
API runs at `http://localhost:5000` · Swagger at `http://localhost:5000/swagger`
|
||||||
|
|
||||||
|
### Frontend
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd frontend
|
||||||
|
npm install
|
||||||
|
ng serve
|
||||||
|
```
|
||||||
|
|
||||||
|
Frontend runs at `http://localhost:4200`
|
||||||
|
|
||||||
|
### Docker (dev stack)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker-compose -f docker-compose.dev.yml up
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
`backend/appsettings.json` — override in `appsettings.Development.json` or environment variables:
|
||||||
|
|
||||||
|
| Key | Default | Description |
|
||||||
|
|---|---|---|
|
||||||
|
| `ConnectionStrings:ExtrudexDb` | `Host=localhost;...` | PostgreSQL connection string |
|
||||||
|
| `FilamentUsageSync:PollingInterval` | `00:05:00` | Sync job interval |
|
||||||
|
| `FilamentUsageSync:RequestTimeout` | `00:00:30` | Moonraker request timeout |
|
||||||
|
| `FilamentUsageSync:Enabled` | `true` | Enable/disable background sync |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Real-Time Events
|
||||||
|
|
||||||
|
SignalR hub endpoint: `/hubs/printer`
|
||||||
|
|
||||||
|
Clients receive `PrinterHub` events for live printer status, job progress, and spool consumption updates.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## API Overview
|
||||||
|
|
||||||
|
| Route prefix | Resource |
|
||||||
|
|---|---|
|
||||||
|
| `/api/filaments` | Filament catalog |
|
||||||
|
| `/api/spools` | Spool inventory |
|
||||||
|
| `/api/printers` | Printer registry |
|
||||||
|
| `/api/print-jobs` | Print job tracking |
|
||||||
|
| `/api/material-bases` | Material base types |
|
||||||
|
| `/api/material-finishes` | Material finishes |
|
||||||
|
| `/api/material-modifiers` | Material modifiers |
|
||||||
|
| `/api/material-lookups` | Combined material lookup |
|
||||||
|
| `/api/cost-analysis` | Cost-per-print and COGS |
|
||||||
|
| `/api/qr` | QR code generation |
|
||||||
|
|
||||||
|
Full schema available at `/swagger` when running in dev.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## CI
|
||||||
|
|
||||||
|
Gitea Actions pipeline (`.gitea/workflows/dev.yml`) runs on every push to `dev`:
|
||||||
|
|
||||||
|
- `dotnet build`
|
||||||
|
- Frontend `ng build`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Branch & PR Rules
|
||||||
|
|
||||||
|
- All feature branches target `dev` — **never `main`**
|
||||||
|
- Branch naming: `agent/<agent>/CUB-N-short-description`
|
||||||
|
- PR titles: `CUB-N: short description`
|
||||||
|
- PRs require Otto review before Joshua merges
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
*Built by CubeCraft Creations · Orchestrated by Otto*
|
||||||
|
|||||||
@@ -40,15 +40,18 @@ public class FilamentsController : ControllerBase
|
|||||||
/// <response code="200">Returns the paginated list of filament spools.</response>
|
/// <response code="200">Returns the paginated list of filament spools.</response>
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[ProducesResponseType(typeof(PagedResponse<FilamentResponse>), StatusCodes.Status200OK)]
|
[ProducesResponseType(typeof(PagedResponse<FilamentResponse>), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
public async Task<ActionResult<PagedResponse<FilamentResponse>>> GetFilaments(
|
public async Task<ActionResult<PagedResponse<FilamentResponse>>> GetFilaments(
|
||||||
[FromQuery] FilamentQueryParameters query)
|
[FromQuery] FilamentQueryParameters query)
|
||||||
{
|
{
|
||||||
_logger.LogDebug(
|
_logger.LogDebug(
|
||||||
"Getting filaments: pageNumber={PageNumber}, pageSize={PageSize}, " +
|
"Getting filaments: pageNumber={PageNumber}, pageSize={PageSize}, " +
|
||||||
"materialBaseId={MaterialBaseId}, materialFinishId={MaterialFinishId}, " +
|
"materialBaseId={MaterialBaseId}, materialFinishId={MaterialFinishId}, " +
|
||||||
"materialModifierId={MaterialModifierId}, brand={Brand}, isActive={IsActive}",
|
"materialModifierId={MaterialModifierId}, brand={Brand}, isActive={IsActive}, " +
|
||||||
|
"includeArchived={IncludeArchived}, storageLocation={StorageLocation}",
|
||||||
query.PageNumber, query.PageSize, query.MaterialBaseId,
|
query.PageNumber, query.PageSize, query.MaterialBaseId,
|
||||||
query.MaterialFinishId, query.MaterialModifierId, query.Brand, query.IsActive);
|
query.MaterialFinishId, query.MaterialModifierId, query.Brand, query.IsActive,
|
||||||
|
query.IncludeArchived, query.StorageLocation);
|
||||||
|
|
||||||
// Clamp pagination values
|
// Clamp pagination values
|
||||||
var pageNumber = Math.Max(1, query.PageNumber);
|
var pageNumber = Math.Max(1, query.PageNumber);
|
||||||
@@ -77,6 +80,15 @@ public class FilamentsController : ControllerBase
|
|||||||
if (query.IsActive.HasValue)
|
if (query.IsActive.HasValue)
|
||||||
spoolQuery = spoolQuery.Where(s => s.IsActive == query.IsActive.Value);
|
spoolQuery = spoolQuery.Where(s => s.IsActive == query.IsActive.Value);
|
||||||
|
|
||||||
|
// Exclude archived spools by default; include when explicitly requested
|
||||||
|
if (query.IncludeArchived != true)
|
||||||
|
spoolQuery = spoolQuery.Where(s => !s.IsArchived);
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(query.StorageLocation))
|
||||||
|
spoolQuery = spoolQuery.Where(s =>
|
||||||
|
s.StorageLocation != null &&
|
||||||
|
s.StorageLocation.ToLower().Contains(query.StorageLocation.ToLower()));
|
||||||
|
|
||||||
var totalCount = await spoolQuery.CountAsync();
|
var totalCount = await spoolQuery.CountAsync();
|
||||||
|
|
||||||
var items = await spoolQuery
|
var items = await spoolQuery
|
||||||
@@ -185,7 +197,9 @@ public class FilamentsController : ControllerBase
|
|||||||
SpoolSerial = request.SpoolSerial,
|
SpoolSerial = request.SpoolSerial,
|
||||||
PurchasePrice = request.PurchasePrice,
|
PurchasePrice = request.PurchasePrice,
|
||||||
PurchaseDate = request.PurchaseDate,
|
PurchaseDate = request.PurchaseDate,
|
||||||
IsActive = request.IsActive
|
IsActive = request.IsActive,
|
||||||
|
IsArchived = request.IsArchived,
|
||||||
|
StorageLocation = request.StorageLocation
|
||||||
};
|
};
|
||||||
|
|
||||||
_dbContext.Spools.Add(entity);
|
_dbContext.Spools.Add(entity);
|
||||||
@@ -267,6 +281,8 @@ public class FilamentsController : ControllerBase
|
|||||||
entity.PurchasePrice = request.PurchasePrice;
|
entity.PurchasePrice = request.PurchasePrice;
|
||||||
entity.PurchaseDate = request.PurchaseDate;
|
entity.PurchaseDate = request.PurchaseDate;
|
||||||
entity.IsActive = request.IsActive;
|
entity.IsActive = request.IsActive;
|
||||||
|
entity.IsArchived = request.IsArchived;
|
||||||
|
entity.StorageLocation = request.StorageLocation;
|
||||||
|
|
||||||
await _dbContext.SaveChangesAsync();
|
await _dbContext.SaveChangesAsync();
|
||||||
|
|
||||||
@@ -307,6 +323,8 @@ public class FilamentsController : ControllerBase
|
|||||||
PurchasePrice = s.PurchasePrice,
|
PurchasePrice = s.PurchasePrice,
|
||||||
PurchaseDate = s.PurchaseDate,
|
PurchaseDate = s.PurchaseDate,
|
||||||
IsActive = s.IsActive,
|
IsActive = s.IsActive,
|
||||||
|
IsArchived = s.IsArchived,
|
||||||
|
StorageLocation = s.StorageLocation,
|
||||||
CreatedAt = s.CreatedAt,
|
CreatedAt = s.CreatedAt,
|
||||||
UpdatedAt = s.UpdatedAt,
|
UpdatedAt = s.UpdatedAt,
|
||||||
QrCodeUrl = $"/api/qr/spool/{s.Id}"
|
QrCodeUrl = $"/api/qr/spool/{s.Id}"
|
||||||
|
|||||||
@@ -59,6 +59,12 @@ public class FilamentResponse
|
|||||||
/// <summary>Whether the spool is currently active and available.</summary>
|
/// <summary>Whether the spool is currently active and available.</summary>
|
||||||
public bool IsActive { get; set; }
|
public bool IsActive { get; set; }
|
||||||
|
|
||||||
|
/// <summary>Whether the spool has been archived (removed from active inventory).</summary>
|
||||||
|
public bool IsArchived { get; set; }
|
||||||
|
|
||||||
|
/// <summary>Physical storage location (e.g., "Shelf A", "Drawer 3"). Null if unset.</summary>
|
||||||
|
public string? StorageLocation { get; set; }
|
||||||
|
|
||||||
/// <summary>Timestamp when this record was created (UTC).</summary>
|
/// <summary>Timestamp when this record was created (UTC).</summary>
|
||||||
public DateTime CreatedAt { get; set; }
|
public DateTime CreatedAt { get; set; }
|
||||||
|
|
||||||
@@ -133,6 +139,15 @@ public class CreateFilamentRequest
|
|||||||
|
|
||||||
/// <summary>Whether the spool is active. Defaults to true.</summary>
|
/// <summary>Whether the spool is active. Defaults to true.</summary>
|
||||||
public bool IsActive { get; set; } = true;
|
public bool IsActive { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>Whether the spool is archived. Defaults to false.
|
||||||
|
/// </summary>
|
||||||
|
public bool IsArchived { get; set; } = false;
|
||||||
|
|
||||||
|
/// <summary>Physical storage location (e.g., "Shelf A", "Drawer 3"). Optional.
|
||||||
|
/// </summary>
|
||||||
|
[StringLength(200, ErrorMessage = "StorageLocation must not exceed 200 characters.")]
|
||||||
|
public string? StorageLocation { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -196,4 +211,11 @@ public class UpdateFilamentRequest
|
|||||||
|
|
||||||
/// <summary>Whether the spool is active.</summary>
|
/// <summary>Whether the spool is active.</summary>
|
||||||
public bool IsActive { get; set; } = true;
|
public bool IsActive { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>Whether the spool is archived. Defaults to false.</summary>
|
||||||
|
public bool IsArchived { get; set; } = false;
|
||||||
|
|
||||||
|
/// <summary>Physical storage location (e.g., "Shelf A", "Drawer 3"). Optional.</summary>
|
||||||
|
[StringLength(200, ErrorMessage = "StorageLocation must not exceed 200 characters.")]
|
||||||
|
public string? StorageLocation { get; set; }
|
||||||
}
|
}
|
||||||
@@ -30,4 +30,11 @@ public class FilamentQueryParameters
|
|||||||
|
|
||||||
/// <summary>Optional filter by active status. True = active only, False = inactive only.</summary>
|
/// <summary>Optional filter by active status. True = active only, False = inactive only.</summary>
|
||||||
public bool? IsActive { get; set; }
|
public bool? IsActive { get; set; }
|
||||||
|
|
||||||
|
/// <summary>Whether to include archived spools in results. Defaults to false (excludes archived).
|
||||||
|
/// </summary>
|
||||||
|
public bool? IncludeArchived { get; set; }
|
||||||
|
|
||||||
|
/// <summary>Optional filter by storage location (case-insensitive partial match).</summary>
|
||||||
|
public string? StorageLocation { get; set; }
|
||||||
}
|
}
|
||||||
@@ -10,6 +10,9 @@ namespace Extrudex.API.Validators;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class CreateFilamentRequestValidator : AbstractValidator<CreateFilamentRequest>
|
public class CreateFilamentRequestValidator : AbstractValidator<CreateFilamentRequest>
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes validation rules for <see cref="CreateFilamentRequest"/>.
|
||||||
|
/// </summary>
|
||||||
public CreateFilamentRequestValidator()
|
public CreateFilamentRequestValidator()
|
||||||
{
|
{
|
||||||
RuleFor(x => x.MaterialBaseId)
|
RuleFor(x => x.MaterialBaseId)
|
||||||
@@ -52,6 +55,12 @@ public class CreateFilamentRequestValidator : AbstractValidator<CreateFilamentRe
|
|||||||
RuleFor(x => x.PurchasePrice!.Value)
|
RuleFor(x => x.PurchasePrice!.Value)
|
||||||
.GreaterThanOrEqualTo(0).WithMessage("Purchase price must be non-negative.");
|
.GreaterThanOrEqualTo(0).WithMessage("Purchase price must be non-negative.");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
When(x => x.StorageLocation != null, () =>
|
||||||
|
{
|
||||||
|
RuleFor(x => x.StorageLocation!)
|
||||||
|
.MaximumLength(200).WithMessage("StorageLocation must not exceed 200 characters.");
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,6 +71,9 @@ public class CreateFilamentRequestValidator : AbstractValidator<CreateFilamentRe
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class UpdateFilamentRequestValidator : AbstractValidator<UpdateFilamentRequest>
|
public class UpdateFilamentRequestValidator : AbstractValidator<UpdateFilamentRequest>
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes validation rules for <see cref="UpdateFilamentRequest"/>.
|
||||||
|
/// </summary>
|
||||||
public UpdateFilamentRequestValidator()
|
public UpdateFilamentRequestValidator()
|
||||||
{
|
{
|
||||||
RuleFor(x => x.MaterialBaseId)
|
RuleFor(x => x.MaterialBaseId)
|
||||||
@@ -104,5 +116,11 @@ public class UpdateFilamentRequestValidator : AbstractValidator<UpdateFilamentRe
|
|||||||
RuleFor(x => x.PurchasePrice!.Value)
|
RuleFor(x => x.PurchasePrice!.Value)
|
||||||
.GreaterThanOrEqualTo(0).WithMessage("Purchase price must be non-negative.");
|
.GreaterThanOrEqualTo(0).WithMessage("Purchase price must be non-negative.");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
When(x => x.StorageLocation != null, () =>
|
||||||
|
{
|
||||||
|
RuleFor(x => x.StorageLocation!)
|
||||||
|
.MaximumLength(200).WithMessage("StorageLocation must not exceed 200 characters.");
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
19
backend/Domain/DTOs/Moonraker/MoonrakerDisplayStatus.cs
Normal file
19
backend/Domain/DTOs/Moonraker/MoonrakerDisplayStatus.cs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
namespace Extrudex.Domain.DTOs.Moonraker;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Response DTO for Moonraker /printer/objects/query?display_status endpoint.
|
||||||
|
/// Contains progress percentage and message for the current print job.
|
||||||
|
/// Used by the SignalR hub to push real-time progress to connected clients.
|
||||||
|
/// </summary>
|
||||||
|
public class MoonrakerDisplayStatus
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Print progress as a decimal between 0 and 1 (0% to 100%).
|
||||||
|
/// </summary>
|
||||||
|
public decimal Progress { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Status message displayed on the printer LCD (e.g., "Printing...", "Heating...").
|
||||||
|
/// </summary>
|
||||||
|
public string Message { get; set; } = string.Empty;
|
||||||
|
}
|
||||||
20
backend/Domain/DTOs/Moonraker/MoonrakerHistoryResponse.cs
Normal file
20
backend/Domain/DTOs/Moonraker/MoonrakerHistoryResponse.cs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
namespace Extrudex.Domain.DTOs.Moonraker;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Response DTO for the Moonraker /server/history/items endpoint.
|
||||||
|
/// Wraps the paginated list of print job history items.
|
||||||
|
/// </summary>
|
||||||
|
public class MoonrakerHistoryResponse
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The list of print job history items returned by Moonraker.
|
||||||
|
/// Most recent jobs appear first (descending by start time).
|
||||||
|
/// </summary>
|
||||||
|
public List<MoonrakerPrintJob> Items { get; set; } = [];
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Total number of print jobs available on the server
|
||||||
|
/// (for pagination; the Items list may be a subset).
|
||||||
|
/// </summary>
|
||||||
|
public int TotalCount { get; set; }
|
||||||
|
}
|
||||||
56
backend/Domain/DTOs/Moonraker/MoonrakerPrintJob.cs
Normal file
56
backend/Domain/DTOs/Moonraker/MoonrakerPrintJob.cs
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
namespace Extrudex.Domain.DTOs.Moonraker;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Response DTO for a single Moonraker print job history item.
|
||||||
|
/// Maps to the objects returned by /server/history/items.
|
||||||
|
/// Contains filament usage, duration, and status for a completed or active print.
|
||||||
|
/// </summary>
|
||||||
|
public class MoonrakerPrintJob
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Unique Moonraker job identifier (e.g., "000001").
|
||||||
|
/// </summary>
|
||||||
|
public string JobId { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Filename of the G-code file that was printed.
|
||||||
|
/// </summary>
|
||||||
|
public string Filename { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Current status of this print job: "completed", "cancelled", "error", "in_progress".
|
||||||
|
/// </summary>
|
||||||
|
public string Status { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Total filament used in millimeters for this print job.
|
||||||
|
/// This is the primary measurement; grams are derived from this value.
|
||||||
|
/// </summary>
|
||||||
|
public decimal FilamentUsedMm { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Total print duration in seconds.
|
||||||
|
/// </summary>
|
||||||
|
public decimal PrintDurationSeconds { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Total print duration including setup and warmup, in seconds.
|
||||||
|
/// </summary>
|
||||||
|
public decimal TotalDurationSeconds { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Timestamp when the print job started (UTC).
|
||||||
|
/// </summary>
|
||||||
|
public DateTime? StartTime { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Timestamp when the print job ended (UTC). Null if still in progress.
|
||||||
|
/// </summary>
|
||||||
|
public DateTime? EndTime { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Metadata dictionary from Moonraker. May contain filament_type,
|
||||||
|
/// filament_name, nozzle_diameter, and other slicer-provided fields.
|
||||||
|
/// </summary>
|
||||||
|
public Dictionary<string, object> Metadata { get; set; } = new();
|
||||||
|
}
|
||||||
36
backend/Domain/DTOs/Moonraker/MoonrakerPrintStats.cs
Normal file
36
backend/Domain/DTOs/Moonraker/MoonrakerPrintStats.cs
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
namespace Extrudex.Domain.DTOs.Moonraker;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Response DTO for Moonraker /printer/objects/query?print_stats endpoint.
|
||||||
|
/// Contains real-time print statistics including current job state,
|
||||||
|
/// filament consumed, and file being printed.
|
||||||
|
/// </summary>
|
||||||
|
public class MoonrakerPrintStats
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Current print state: "standby", "printing", "paused", "complete", "error", "cancelled".
|
||||||
|
/// </summary>
|
||||||
|
public string State { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Total filament used in millimeters for the current print session.
|
||||||
|
/// </summary>
|
||||||
|
public decimal FilamentUsedMm { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Total print duration in seconds for the current print session.
|
||||||
|
/// </summary>
|
||||||
|
public decimal PrintDurationSeconds { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Filename of the G-code file currently being printed.
|
||||||
|
/// Null if no print is active.
|
||||||
|
/// </summary>
|
||||||
|
public string? Filename { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Detailed message from Klipper about the current print state.
|
||||||
|
/// May contain error details when state is "error".
|
||||||
|
/// </summary>
|
||||||
|
public string? Message { get; set; }
|
||||||
|
}
|
||||||
26
backend/Domain/DTOs/Moonraker/MoonrakerPrinterInfo.cs
Normal file
26
backend/Domain/DTOs/Moonraker/MoonrakerPrinterInfo.cs
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
namespace Extrudex.Domain.DTOs.Moonraker;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Response DTO for the Moonraker /printer/info endpoint.
|
||||||
|
/// Contains the current operational state of the Klipper printer.
|
||||||
|
/// Used to determine whether the printer is idle, printing, paused, or in error.
|
||||||
|
/// </summary>
|
||||||
|
public class MoonrakerPrinterInfo
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Current Klipper state: "ready", "startup", "shutdown", "error", "cancelled".
|
||||||
|
/// A "ready" state means the printer is connected and idle.
|
||||||
|
/// </summary>
|
||||||
|
public string State { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Detailed state message from Klipper. May contain error details
|
||||||
|
/// when the state is "error" or "shutdown".
|
||||||
|
/// </summary>
|
||||||
|
public string StateMessage { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Whether the Klipper firmware is currently connected and responsive.
|
||||||
|
/// </summary>
|
||||||
|
public bool KlippyReady { get; set; }
|
||||||
|
}
|
||||||
25
backend/Domain/DTOs/Moonraker/MoonrakerRequest.cs
Normal file
25
backend/Domain/DTOs/Moonraker/MoonrakerRequest.cs
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
namespace Extrudex.Domain.DTOs.Moonraker;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Request DTO for querying the Moonraker API.
|
||||||
|
/// Encapsulates the connection parameters needed to reach a specific
|
||||||
|
/// Moonraker instance on a Klipper-based printer.
|
||||||
|
/// </summary>
|
||||||
|
public class MoonrakerRequest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Hostname or IP address of the Moonraker printer.
|
||||||
|
/// </summary>
|
||||||
|
public string HostnameOrIp { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Port number for the Moonraker API. Default: 7125.
|
||||||
|
/// </summary>
|
||||||
|
public int Port { get; set; } = 7125;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Optional API key for authenticating with Moonraker.
|
||||||
|
/// Required when the server has API key authentication enabled.
|
||||||
|
/// </summary>
|
||||||
|
public string? ApiKey { get; set; }
|
||||||
|
}
|
||||||
44
backend/Domain/DTOs/Moonraker/MoonrakerServerInfo.cs
Normal file
44
backend/Domain/DTOs/Moonraker/MoonrakerServerInfo.cs
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
namespace Extrudex.Domain.DTOs.Moonraker;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Response DTO for the Moonraker /server/info endpoint.
|
||||||
|
/// Contains server identification and operational state.
|
||||||
|
/// Used to verify connectivity and determine Moonraker version.
|
||||||
|
/// </summary>
|
||||||
|
public class MoonrakerServerInfo
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The hostname of the Moonraker server (e.g., "mainsail").
|
||||||
|
/// </summary>
|
||||||
|
public string Hostname { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Moonraker software version string (e.g., "0.8.0-89ee464").
|
||||||
|
/// </summary>
|
||||||
|
public string SoftwareVersion { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// CPU model string reported by the host system.
|
||||||
|
/// </summary>
|
||||||
|
public string CpuInfo { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Whether Klipper is currently connected to the MCU.
|
||||||
|
/// </summary>
|
||||||
|
public bool KlippyConnected { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The current Klipper state (e.g., "ready", "startup", "error").
|
||||||
|
/// </summary>
|
||||||
|
public string KlippyState { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Whether the Moonraker API requires an authentication token.
|
||||||
|
/// </summary>
|
||||||
|
public bool ApiKeyRequired { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// List of registered Moonraker plugin names.
|
||||||
|
/// </summary>
|
||||||
|
public List<string> Plugins { get; set; } = [];
|
||||||
|
}
|
||||||
@@ -93,6 +93,20 @@ public class Spool : AuditableEntity
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public bool IsActive { get; set; } = true;
|
public bool IsActive { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Whether the spool has been archived (removed from active inventory).
|
||||||
|
/// Archived spools are retained for historical records but hidden from
|
||||||
|
/// default inventory views. Distinguishes long-term archival from
|
||||||
|
/// temporary inactivity (e.g., spool swapped out of AMS).
|
||||||
|
/// </summary>
|
||||||
|
public bool IsArchived { get; set; } = false;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Physical storage location of the spool (e.g., "Shelf A", "Drawer 3", "AMS Tray 2").
|
||||||
|
/// Optional — not every spool has a designated storage location.
|
||||||
|
/// </summary>
|
||||||
|
public string? StorageLocation { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Navigation collection of AMS slots where this spool is loaded.
|
/// Navigation collection of AMS slots where this spool is loaded.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
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);
|
||||||
|
}
|
||||||
131
backend/Domain/Interfaces/IMoonrakerClient.cs
Normal file
131
backend/Domain/Interfaces/IMoonrakerClient.cs
Normal file
@@ -0,0 +1,131 @@
|
|||||||
|
using Extrudex.Domain.DTOs.Moonraker;
|
||||||
|
|
||||||
|
namespace Extrudex.Domain.Interfaces;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Client interface for communicating with Moonraker REST API endpoints
|
||||||
|
/// on Klipper-based printers (e.g., Elegoo Centauri Carbon).
|
||||||
|
/// Provides strongly-typed methods for server discovery, printer status,
|
||||||
|
/// print job history, and real-time telemetry.
|
||||||
|
/// </summary>
|
||||||
|
public interface IMoonrakerClient
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Checks whether the Moonraker server is reachable and responding.
|
||||||
|
/// Calls the /server/info endpoint and returns the server information
|
||||||
|
/// if successful, or null if the server is unreachable.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="hostnameOrIp">The printer's hostname or IP address.</param>
|
||||||
|
/// <param name="port">The Moonraker API port (default: 7125).</param>
|
||||||
|
/// <param name="apiKey">Optional API key for authentication.</param>
|
||||||
|
/// <param name="cancellationToken">Cancellation token for the HTTP request.</param>
|
||||||
|
/// <returns>Server info if reachable; <c>null</c> if unreachable.</returns>
|
||||||
|
Task<MoonrakerServerInfo?> GetServerInfoAsync(
|
||||||
|
string hostnameOrIp,
|
||||||
|
int port,
|
||||||
|
string? apiKey,
|
||||||
|
CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Checks whether the Moonraker server is reachable and responding.
|
||||||
|
/// This is a convenience method equivalent to calling GetServerInfoAsync
|
||||||
|
/// and checking for a non-null result.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="hostnameOrIp">The printer's hostname or IP address.</param>
|
||||||
|
/// <param name="port">The Moonraker API port (default: 7125).</param>
|
||||||
|
/// <param name="apiKey">Optional API key for authentication.</param>
|
||||||
|
/// <param name="cancellationToken">Cancellation token for the HTTP request.</param>
|
||||||
|
/// <returns><c>true</c> if the server responded successfully; otherwise <c>false</c>.</returns>
|
||||||
|
Task<bool> IsReachableAsync(
|
||||||
|
string hostnameOrIp,
|
||||||
|
int port,
|
||||||
|
string? apiKey,
|
||||||
|
CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Fetches the current printer info from the /printer/info endpoint.
|
||||||
|
/// Returns the Klipper state and readiness status.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="hostnameOrIp">The printer's hostname or IP address.</param>
|
||||||
|
/// <param name="port">The Moonraker API port (default: 7125).</param>
|
||||||
|
/// <param name="apiKey">Optional API key for authentication.</param>
|
||||||
|
/// <param name="cancellationToken">Cancellation token for the HTTP request.</param>
|
||||||
|
/// <returns>Printer info if successful; <c>null</c> if the request failed.</returns>
|
||||||
|
Task<MoonrakerPrinterInfo?> GetPrinterInfoAsync(
|
||||||
|
string hostnameOrIp,
|
||||||
|
int port,
|
||||||
|
string? apiKey,
|
||||||
|
CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Fetches print job history from the /server/history/items endpoint.
|
||||||
|
/// Returns the most recent print jobs with filament usage data,
|
||||||
|
/// print duration, and completion status.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="hostnameOrIp">The printer's hostname or IP address.</param>
|
||||||
|
/// <param name="port">The Moonraker API port (default: 7125).</param>
|
||||||
|
/// <param name="apiKey">Optional API key for authentication.</param>
|
||||||
|
/// <param name="limit">Maximum number of history items to return. Default: 50.</param>
|
||||||
|
/// <param name="cancellationToken">Cancellation token for the HTTP request.</param>
|
||||||
|
/// <returns>History response with print jobs; empty list if request failed.</returns>
|
||||||
|
Task<MoonrakerHistoryResponse> GetPrintHistoryAsync(
|
||||||
|
string hostnameOrIp,
|
||||||
|
int port,
|
||||||
|
string? apiKey,
|
||||||
|
int limit = 50,
|
||||||
|
CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Fetches the current print statistics from the /printer/objects/query endpoint.
|
||||||
|
/// Returns real-time data including filament used, print duration,
|
||||||
|
/// and current print state for the active or most recent print.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="hostnameOrIp">The printer's hostname or IP address.</param>
|
||||||
|
/// <param name="port">The Moonraker API port (default: 7125).</param>
|
||||||
|
/// <param name="apiKey">Optional API key for authentication.</param>
|
||||||
|
/// <param name="cancellationToken">Cancellation token for the HTTP request.</param>
|
||||||
|
/// <returns>Print stats if successful; <c>null</c> if the request failed.</returns>
|
||||||
|
Task<MoonrakerPrintStats?> GetPrintStatsAsync(
|
||||||
|
string hostnameOrIp,
|
||||||
|
int port,
|
||||||
|
string? apiKey,
|
||||||
|
CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Fetches the current display status from the /printer/objects/query endpoint.
|
||||||
|
/// Returns progress percentage and status message for the active print.
|
||||||
|
/// Used by SignalR to push real-time progress updates to connected clients.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="hostnameOrIp">The printer's hostname or IP address.</param>
|
||||||
|
/// <param name="port">The Moonraker API port (default: 7125).</param>
|
||||||
|
/// <param name="apiKey">Optional API key for authentication.</param>
|
||||||
|
/// <param name="cancellationToken">Cancellation token for the HTTP request.</param>
|
||||||
|
/// <returns>Display status if successful; <c>null</c> if the request failed.</returns>
|
||||||
|
Task<MoonrakerDisplayStatus?> GetDisplayStatusAsync(
|
||||||
|
string hostnameOrIp,
|
||||||
|
int port,
|
||||||
|
string? apiKey,
|
||||||
|
CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Fetches the current filament usage data from the Moonraker server.
|
||||||
|
/// Returns a dictionary of usage metrics reported by the printer.
|
||||||
|
///
|
||||||
|
/// <para>
|
||||||
|
/// <b>Prefer GetPrintHistoryAsync or GetPrintStatsAsync for new code.</b>
|
||||||
|
/// This method is retained for backward compatibility with the
|
||||||
|
/// FilamentUsageSyncService and returns a dictionary of metric names
|
||||||
|
/// to their decimal values for callers that don't need typed DTOs.
|
||||||
|
/// </para>
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="hostnameOrIp">The printer's hostname or IP address.</param>
|
||||||
|
/// <param name="port">The Moonraker API port (default: 7125).</param>
|
||||||
|
/// <param name="apiKey">Optional API key for authentication.</param>
|
||||||
|
/// <param name="cancellationToken">Cancellation token for the HTTP request.</param>
|
||||||
|
/// <returns>A dictionary of usage metric names to their decimal values.</returns>
|
||||||
|
Task<Dictionary<string, decimal>> GetFilamentUsageAsync(
|
||||||
|
string hostnameOrIp,
|
||||||
|
int port,
|
||||||
|
string? apiKey,
|
||||||
|
CancellationToken cancellationToken = default);
|
||||||
|
}
|
||||||
@@ -68,6 +68,15 @@ public class SpoolConfiguration : BaseEntityConfiguration<Spool>
|
|||||||
.HasDefaultValue(true)
|
.HasDefaultValue(true)
|
||||||
.IsRequired();
|
.IsRequired();
|
||||||
|
|
||||||
|
builder.Property(e => e.IsArchived)
|
||||||
|
.HasColumnName("is_archived")
|
||||||
|
.HasDefaultValue(false)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
builder.Property(e => e.StorageLocation)
|
||||||
|
.HasColumnName("storage_location")
|
||||||
|
.HasMaxLength(200);
|
||||||
|
|
||||||
// Unique index on spool_serial — critical for barcode/QR scanning
|
// Unique index on spool_serial — critical for barcode/QR scanning
|
||||||
builder.HasIndex(e => e.SpoolSerial)
|
builder.HasIndex(e => e.SpoolSerial)
|
||||||
.IsUnique()
|
.IsUnique()
|
||||||
@@ -89,6 +98,14 @@ public class SpoolConfiguration : BaseEntityConfiguration<Spool>
|
|||||||
builder.HasIndex(e => e.IsActive)
|
builder.HasIndex(e => e.IsActive)
|
||||||
.HasDatabaseName("ix_spools_is_active");
|
.HasDatabaseName("ix_spools_is_active");
|
||||||
|
|
||||||
|
// Index on is_archived for inventory filtering (exclude archived from default views)
|
||||||
|
builder.HasIndex(e => e.IsArchived)
|
||||||
|
.HasDatabaseName("ix_spools_is_archived");
|
||||||
|
|
||||||
|
// Composite index on is_active + is_archived for common inventory queries
|
||||||
|
builder.HasIndex(e => new { e.IsActive, e.IsArchived })
|
||||||
|
.HasDatabaseName("ix_spools_active_archived");
|
||||||
|
|
||||||
// Relationships
|
// Relationships
|
||||||
builder.HasOne(e => e.MaterialBase)
|
builder.HasOne(e => e.MaterialBase)
|
||||||
.WithMany(e => e.Spools)
|
.WithMany(e => e.Spools)
|
||||||
|
|||||||
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
447
backend/Infrastructure/Services/MoonrakerClient.cs
Normal file
447
backend/Infrastructure/Services/MoonrakerClient.cs
Normal file
@@ -0,0 +1,447 @@
|
|||||||
|
using System.Net.Http.Json;
|
||||||
|
using System.Text.Json;
|
||||||
|
using Extrudex.Domain.DTOs.Moonraker;
|
||||||
|
using Extrudex.Domain.Interfaces;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace Extrudex.Infrastructure.Configuration;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// HTTP client for communicating with Moonraker REST API endpoints
|
||||||
|
/// on Klipper-based printers (e.g., Elegoo Centauri Carbon).
|
||||||
|
/// Provides strongly-typed methods for server discovery, printer status,
|
||||||
|
/// print job history, and real-time telemetry.
|
||||||
|
/// </summary>
|
||||||
|
public class MoonrakerClient : IMoonrakerClient
|
||||||
|
{
|
||||||
|
private readonly HttpClient _httpClient;
|
||||||
|
private readonly ILogger<MoonrakerClient> _logger;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new MoonrakerClient with the configured HTTP client and logger.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="httpClient">The HTTP client for making requests to Moonraker endpoints.</param>
|
||||||
|
/// <param name="logger">Logger for diagnostic output.</param>
|
||||||
|
public MoonrakerClient(HttpClient httpClient, ILogger<MoonrakerClient> logger)
|
||||||
|
{
|
||||||
|
_httpClient = httpClient;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async Task<MoonrakerServerInfo?> GetServerInfoAsync(
|
||||||
|
string hostnameOrIp,
|
||||||
|
int port,
|
||||||
|
string? apiKey,
|
||||||
|
CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
var baseUrl = BuildBaseUrl(hostnameOrIp, port);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var request = CreateRequest(HttpMethod.Get, $"{baseUrl}/server/info", apiKey);
|
||||||
|
using var response = await _httpClient.SendAsync(request, cancellationToken);
|
||||||
|
response.EnsureSuccessStatusCode();
|
||||||
|
|
||||||
|
var json = await response.Content.ReadFromJsonAsync<JsonElement>(cancellationToken: cancellationToken);
|
||||||
|
|
||||||
|
var serverInfo = new MoonrakerServerInfo();
|
||||||
|
|
||||||
|
if (json.TryGetProperty("result", out var result))
|
||||||
|
{
|
||||||
|
if (result.TryGetProperty("hostname", out var hostname))
|
||||||
|
serverInfo.Hostname = hostname.GetString() ?? string.Empty;
|
||||||
|
if (result.TryGetProperty("software_version", out var version))
|
||||||
|
serverInfo.SoftwareVersion = version.GetString() ?? string.Empty;
|
||||||
|
if (result.TryGetProperty("cpu_info", out var cpuInfo))
|
||||||
|
serverInfo.CpuInfo = cpuInfo.GetString() ?? string.Empty;
|
||||||
|
if (result.TryGetProperty("klippy_connected", out var klippyConnected))
|
||||||
|
serverInfo.KlippyConnected = klippyConnected.GetBoolean();
|
||||||
|
if (result.TryGetProperty("klippy_state", out var klippyState))
|
||||||
|
serverInfo.KlippyState = klippyState.GetString() ?? string.Empty;
|
||||||
|
if (result.TryGetProperty("api_key_required", out var apiKeyRequired))
|
||||||
|
serverInfo.ApiKeyRequired = apiKeyRequired.GetBoolean();
|
||||||
|
if (result.TryGetProperty("plugins", out var plugins))
|
||||||
|
serverInfo.Plugins = plugins.EnumerateArray()
|
||||||
|
.Select(p => p.GetString() ?? string.Empty)
|
||||||
|
.Where(s => !string.IsNullOrEmpty(s))
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.LogDebug(
|
||||||
|
"Retrieved server info from Moonraker at {Host}:{Port} — version {Version}, klippy {State}",
|
||||||
|
hostnameOrIp, port, serverInfo.SoftwareVersion, serverInfo.KlippyState);
|
||||||
|
|
||||||
|
return serverInfo;
|
||||||
|
}
|
||||||
|
catch (HttpRequestException ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex,
|
||||||
|
"Failed to retrieve server info from Moonraker at {Host}:{Port}",
|
||||||
|
hostnameOrIp, port);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
catch (JsonException ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex,
|
||||||
|
"Failed to parse Moonraker server info response from {Host}:{Port}",
|
||||||
|
hostnameOrIp, port);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async Task<bool> IsReachableAsync(
|
||||||
|
string hostnameOrIp,
|
||||||
|
int port,
|
||||||
|
string? apiKey,
|
||||||
|
CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
var serverInfo = await GetServerInfoAsync(hostnameOrIp, port, apiKey, cancellationToken);
|
||||||
|
return serverInfo is not null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async Task<MoonrakerPrinterInfo?> GetPrinterInfoAsync(
|
||||||
|
string hostnameOrIp,
|
||||||
|
int port,
|
||||||
|
string? apiKey,
|
||||||
|
CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
var baseUrl = BuildBaseUrl(hostnameOrIp, port);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var request = CreateRequest(HttpMethod.Get, $"{baseUrl}/printer/info", apiKey);
|
||||||
|
using var response = await _httpClient.SendAsync(request, cancellationToken);
|
||||||
|
response.EnsureSuccessStatusCode();
|
||||||
|
|
||||||
|
var json = await response.Content.ReadFromJsonAsync<JsonElement>(cancellationToken: cancellationToken);
|
||||||
|
|
||||||
|
var printerInfo = new MoonrakerPrinterInfo();
|
||||||
|
|
||||||
|
if (json.TryGetProperty("result", out var result))
|
||||||
|
{
|
||||||
|
if (result.TryGetProperty("state", out var state))
|
||||||
|
printerInfo.State = state.GetString() ?? string.Empty;
|
||||||
|
if (result.TryGetProperty("state_message", out var stateMessage))
|
||||||
|
printerInfo.StateMessage = stateMessage.GetString() ?? string.Empty;
|
||||||
|
if (result.TryGetProperty("klippy_ready", out var klippyReady))
|
||||||
|
printerInfo.KlippyReady = klippyReady.GetBoolean();
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.LogDebug(
|
||||||
|
"Retrieved printer info from Moonraker at {Host}:{Port} — state: {State}",
|
||||||
|
hostnameOrIp, port, printerInfo.State);
|
||||||
|
|
||||||
|
return printerInfo;
|
||||||
|
}
|
||||||
|
catch (HttpRequestException ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex,
|
||||||
|
"Failed to retrieve printer info from Moonraker at {Host}:{Port}",
|
||||||
|
hostnameOrIp, port);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
catch (JsonException ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex,
|
||||||
|
"Failed to parse Moonraker printer info response from {Host}:{Port}",
|
||||||
|
hostnameOrIp, port);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async Task<MoonrakerHistoryResponse> GetPrintHistoryAsync(
|
||||||
|
string hostnameOrIp,
|
||||||
|
int port,
|
||||||
|
string? apiKey,
|
||||||
|
int limit = 50,
|
||||||
|
CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
var baseUrl = BuildBaseUrl(hostnameOrIp, port);
|
||||||
|
var historyResponse = new MoonrakerHistoryResponse();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var request = CreateRequest(
|
||||||
|
HttpMethod.Get,
|
||||||
|
$"{baseUrl}/server/history/items?limit={limit}",
|
||||||
|
apiKey);
|
||||||
|
|
||||||
|
using var response = await _httpClient.SendAsync(request, cancellationToken);
|
||||||
|
response.EnsureSuccessStatusCode();
|
||||||
|
|
||||||
|
var json = await response.Content.ReadFromJsonAsync<JsonElement>(cancellationToken: cancellationToken);
|
||||||
|
|
||||||
|
if (json.TryGetProperty("result", out var result))
|
||||||
|
{
|
||||||
|
if (result.TryGetProperty("count", out var count))
|
||||||
|
historyResponse.TotalCount = count.GetInt32();
|
||||||
|
|
||||||
|
if (result.TryGetProperty("items", out var items))
|
||||||
|
{
|
||||||
|
foreach (var item in items.EnumerateArray())
|
||||||
|
{
|
||||||
|
var job = MapPrintJob(item);
|
||||||
|
historyResponse.Items.Add(job);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.LogDebug(
|
||||||
|
"Retrieved {JobCount} print history items from Moonraker at {Host}:{Port}",
|
||||||
|
historyResponse.Items.Count, hostnameOrIp, port);
|
||||||
|
}
|
||||||
|
catch (HttpRequestException ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex,
|
||||||
|
"Failed to retrieve print history from Moonraker at {Host}:{Port}",
|
||||||
|
hostnameOrIp, port);
|
||||||
|
}
|
||||||
|
catch (JsonException ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex,
|
||||||
|
"Failed to parse Moonraker history response from {Host}:{Port}",
|
||||||
|
hostnameOrIp, port);
|
||||||
|
}
|
||||||
|
|
||||||
|
return historyResponse;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async Task<MoonrakerPrintStats?> GetPrintStatsAsync(
|
||||||
|
string hostnameOrIp,
|
||||||
|
int port,
|
||||||
|
string? apiKey,
|
||||||
|
CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
var baseUrl = BuildBaseUrl(hostnameOrIp, port);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var request = CreateRequest(
|
||||||
|
HttpMethod.Get,
|
||||||
|
$"{baseUrl}/printer/objects/query?print_stats",
|
||||||
|
apiKey);
|
||||||
|
|
||||||
|
using var response = await _httpClient.SendAsync(request, cancellationToken);
|
||||||
|
response.EnsureSuccessStatusCode();
|
||||||
|
|
||||||
|
var json = await response.Content.ReadFromJsonAsync<JsonElement>(cancellationToken: cancellationToken);
|
||||||
|
|
||||||
|
if (json.TryGetProperty("result", out var result)
|
||||||
|
&& result.TryGetProperty("status", out var status)
|
||||||
|
&& status.TryGetProperty("print_stats", out var printStats))
|
||||||
|
{
|
||||||
|
var stats = MapPrintStats(printStats);
|
||||||
|
|
||||||
|
_logger.LogDebug(
|
||||||
|
"Retrieved print stats from Moonraker at {Host}:{Port} — state: {State}, filament: {FilamentMm}mm",
|
||||||
|
hostnameOrIp, port, stats.State, stats.FilamentUsedMm);
|
||||||
|
|
||||||
|
return stats;
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.LogWarning(
|
||||||
|
"Moonraker print_stats not found in response from {Host}:{Port}",
|
||||||
|
hostnameOrIp, port);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
catch (HttpRequestException ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex,
|
||||||
|
"Failed to retrieve print stats from Moonraker at {Host}:{Port}",
|
||||||
|
hostnameOrIp, port);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
catch (JsonException ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex,
|
||||||
|
"Failed to parse Moonraker print stats response from {Host}:{Port}",
|
||||||
|
hostnameOrIp, port);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async Task<MoonrakerDisplayStatus?> GetDisplayStatusAsync(
|
||||||
|
string hostnameOrIp,
|
||||||
|
int port,
|
||||||
|
string? apiKey,
|
||||||
|
CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
var baseUrl = BuildBaseUrl(hostnameOrIp, port);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var request = CreateRequest(
|
||||||
|
HttpMethod.Get,
|
||||||
|
$"{baseUrl}/printer/objects/query?display_status",
|
||||||
|
apiKey);
|
||||||
|
|
||||||
|
using var response = await _httpClient.SendAsync(request, cancellationToken);
|
||||||
|
response.EnsureSuccessStatusCode();
|
||||||
|
|
||||||
|
var json = await response.Content.ReadFromJsonAsync<JsonElement>(cancellationToken: cancellationToken);
|
||||||
|
|
||||||
|
if (json.TryGetProperty("result", out var result)
|
||||||
|
&& result.TryGetProperty("status", out var status)
|
||||||
|
&& status.TryGetProperty("display_status", out var displayStatus))
|
||||||
|
{
|
||||||
|
var ds = new MoonrakerDisplayStatus();
|
||||||
|
|
||||||
|
if (displayStatus.TryGetProperty("progress", out var progress))
|
||||||
|
ds.Progress = progress.GetDecimal();
|
||||||
|
if (displayStatus.TryGetProperty("message", out var message))
|
||||||
|
ds.Message = message.GetString() ?? string.Empty;
|
||||||
|
|
||||||
|
_logger.LogDebug(
|
||||||
|
"Retrieved display status from Moonraker at {Host}:{Port} — progress: {Progress:P0}",
|
||||||
|
hostnameOrIp, port, ds.Progress);
|
||||||
|
|
||||||
|
return ds;
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.LogWarning(
|
||||||
|
"Moonraker display_status not found in response from {Host}:{Port}",
|
||||||
|
hostnameOrIp, port);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
catch (HttpRequestException ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex,
|
||||||
|
"Failed to retrieve display status from Moonraker at {Host}:{Port}",
|
||||||
|
hostnameOrIp, port);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
catch (JsonException ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex,
|
||||||
|
"Failed to parse Moonraker display status response from {Host}:{Port}",
|
||||||
|
hostnameOrIp, port);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async Task<Dictionary<string, decimal>> GetFilamentUsageAsync(
|
||||||
|
string hostnameOrIp,
|
||||||
|
int port,
|
||||||
|
string? apiKey,
|
||||||
|
CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
// Delegate to the typed GetPrintHistoryAsync and extract metrics
|
||||||
|
var history = await GetPrintHistoryAsync(hostnameOrIp, port, apiKey, limit: 1, cancellationToken);
|
||||||
|
var result = new Dictionary<string, decimal>();
|
||||||
|
|
||||||
|
if (history.Items.Count > 0)
|
||||||
|
{
|
||||||
|
var latestJob = history.Items[0];
|
||||||
|
result["mm_extruded"] = latestJob.FilamentUsedMm;
|
||||||
|
result["print_duration_seconds"] = latestJob.PrintDurationSeconds;
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.LogDebug(
|
||||||
|
"Retrieved filament usage from Moonraker at {Host}:{Port}: {MetricCount} metrics",
|
||||||
|
hostnameOrIp, port, result.Count);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Builds the base URL for Moonraker API calls from hostname and port.
|
||||||
|
/// </summary>
|
||||||
|
private static string BuildBaseUrl(string hostnameOrIp, int port)
|
||||||
|
{
|
||||||
|
return $"http://{hostnameOrIp}:{port}";
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates an HttpRequestMessage with the optional API key header.
|
||||||
|
/// </summary>
|
||||||
|
private static HttpRequestMessage CreateRequest(HttpMethod method, string url, string? apiKey)
|
||||||
|
{
|
||||||
|
var request = new HttpRequestMessage(method, url);
|
||||||
|
if (!string.IsNullOrEmpty(apiKey))
|
||||||
|
{
|
||||||
|
request.Headers.Add("X-Api-Key", apiKey);
|
||||||
|
}
|
||||||
|
return request;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Maps a JSON element representing a Moonraker print job history item
|
||||||
|
/// to a <see cref="MoonrakerPrintJob"/> DTO.
|
||||||
|
/// </summary>
|
||||||
|
private static MoonrakerPrintJob MapPrintJob(JsonElement item)
|
||||||
|
{
|
||||||
|
var job = new MoonrakerPrintJob();
|
||||||
|
|
||||||
|
if (item.TryGetProperty("job_id", out var jobId))
|
||||||
|
job.JobId = jobId.GetString() ?? string.Empty;
|
||||||
|
if (item.TryGetProperty("filename", out var filename))
|
||||||
|
job.Filename = filename.GetString() ?? string.Empty;
|
||||||
|
if (item.TryGetProperty("status", out var status))
|
||||||
|
job.Status = status.GetString() ?? string.Empty;
|
||||||
|
if (item.TryGetProperty("filament_used", out var filamentUsed))
|
||||||
|
job.FilamentUsedMm = filamentUsed.GetDecimal();
|
||||||
|
if (item.TryGetProperty("print_duration", out var printDuration))
|
||||||
|
job.PrintDurationSeconds = printDuration.GetDecimal();
|
||||||
|
if (item.TryGetProperty("total_duration", out var totalDuration))
|
||||||
|
job.TotalDurationSeconds = totalDuration.GetDecimal();
|
||||||
|
|
||||||
|
if (item.TryGetProperty("start_time", out var startTime) && startTime.ValueKind != JsonValueKind.Null)
|
||||||
|
{
|
||||||
|
if (startTime.TryGetInt64(out var startTimeSeconds))
|
||||||
|
job.StartTime = DateTimeOffset.FromUnixTimeSeconds(startTimeSeconds).UtcDateTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (item.TryGetProperty("end_time", out var endTime) && endTime.ValueKind != JsonValueKind.Null)
|
||||||
|
{
|
||||||
|
if (endTime.TryGetInt64(out var endTimeSeconds))
|
||||||
|
job.EndTime = DateTimeOffset.FromUnixTimeSeconds(endTimeSeconds).UtcDateTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (item.TryGetProperty("metadata", out var metadata) && metadata.ValueKind == JsonValueKind.Object)
|
||||||
|
{
|
||||||
|
foreach (var prop in metadata.EnumerateObject())
|
||||||
|
{
|
||||||
|
object value = prop.Value.ValueKind switch
|
||||||
|
{
|
||||||
|
JsonValueKind.String => prop.Value.GetString() ?? string.Empty,
|
||||||
|
JsonValueKind.Number => prop.Value.GetDecimal(),
|
||||||
|
JsonValueKind.True => true,
|
||||||
|
JsonValueKind.False => false,
|
||||||
|
_ => prop.Value.ToString() ?? string.Empty
|
||||||
|
};
|
||||||
|
job.Metadata[prop.Name] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return job;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Maps a JSON element representing Moonraker print_stats
|
||||||
|
/// to a <see cref="MoonrakerPrintStats"/> DTO.
|
||||||
|
/// </summary>
|
||||||
|
private static MoonrakerPrintStats MapPrintStats(JsonElement printStats)
|
||||||
|
{
|
||||||
|
var stats = new MoonrakerPrintStats();
|
||||||
|
|
||||||
|
if (printStats.TryGetProperty("state", out var state))
|
||||||
|
stats.State = state.GetString() ?? string.Empty;
|
||||||
|
if (printStats.TryGetProperty("filament_used", out var filamentUsed))
|
||||||
|
stats.FilamentUsedMm = filamentUsed.GetDecimal();
|
||||||
|
if (printStats.TryGetProperty("print_duration", out var printDuration))
|
||||||
|
stats.PrintDurationSeconds = printDuration.GetDecimal();
|
||||||
|
if (printStats.TryGetProperty("filename", out var filename) && filename.ValueKind != JsonValueKind.Null)
|
||||||
|
stats.Filename = filename.GetString();
|
||||||
|
if (printStats.TryGetProperty("message", out var message) && message.ValueKind != JsonValueKind.Null)
|
||||||
|
stats.Message = message.GetString();
|
||||||
|
|
||||||
|
return stats;
|
||||||
|
}
|
||||||
|
}
|
||||||
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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user