Compare commits
1 Commits
agent/dex/
...
fc564c6c5a
| Author | SHA1 | Date | |
|---|---|---|---|
| fc564c6c5a |
@@ -6,32 +6,31 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strconv"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/CubeCraft-Creations/Extrudex/backend/internal/clients"
|
||||
"github.com/CubeCraft-Creations/Extrudex/backend/internal/config"
|
||||
"github.com/CubeCraft-Creations/Extrudex/backend/internal/db"
|
||||
"github.com/CubeCraft-Creations/Extrudex/backend/internal/models"
|
||||
"github.com/CubeCraft-Creations/Extrudex/backend/internal/router"
|
||||
"github.com/CubeCraft-Creations/Extrudex/backend/internal/sse"
|
||||
"github.com/CubeCraft-Creations/Extrudex/backend/internal/workers"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Setup structured logging
|
||||
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
|
||||
Level: slog.LevelInfo,
|
||||
})))
|
||||
|
||||
// Load configuration
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
slog.Error("failed to load config", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
slog.Info("config loaded", "port", cfg.Port, "cors_origin", cfg.CorsOrigin)
|
||||
|
||||
// Connect to database
|
||||
dbPool, err := db.NewPool(cfg.DatabaseURL)
|
||||
if err != nil {
|
||||
slog.Error("failed to connect to database", "error", err)
|
||||
@@ -39,38 +38,30 @@ func main() {
|
||||
}
|
||||
defer db.ClosePool(dbPool)
|
||||
|
||||
slog.Info("database connected")
|
||||
|
||||
// Create SSE broadcaster and start it
|
||||
sseBC := sse.NewBroadcaster(128)
|
||||
sseBC.Start()
|
||||
defer sseBC.Stop()
|
||||
|
||||
slog.Info("sse broadcaster started")
|
||||
|
||||
// Create router
|
||||
r := router.New(cfg, dbPool, sseBC)
|
||||
|
||||
// ── Workers ─────────────────────────────────────────────────────────
|
||||
|
||||
var wg sync.WaitGroup
|
||||
workersCtx, cancelWorkers := context.WithCancel(context.Background())
|
||||
defer cancelWorkers()
|
||||
|
||||
pollInterval, _ := time.ParseDuration(cfg.MoonrakerPollInterval)
|
||||
if pollInterval <= 0 {
|
||||
pollInterval = 10 * time.Second
|
||||
}
|
||||
|
||||
activePrinters := listActivePrinters(workersCtx, dbPool)
|
||||
for _, p := range activePrinters {
|
||||
startWorkerForPrinter(workersCtx, &wg, cfg, dbPool, p, pollInterval)
|
||||
}
|
||||
|
||||
// ── HTTP server ─────────────────────────────────────────────────────
|
||||
|
||||
// Create HTTP server
|
||||
// WriteTimeout is 0 for SSE support — the Chi middleware.Timeout(60s)
|
||||
// handles request-level timeouts on non-SSE routes.
|
||||
server := &http.Server{
|
||||
Addr: ":" + cfg.Port,
|
||||
Handler: r,
|
||||
ReadTimeout: 15 * time.Second,
|
||||
WriteTimeout: 0,
|
||||
WriteTimeout: 0, // disabled for SSE long-lived connections
|
||||
IdleTimeout: 60 * time.Second,
|
||||
}
|
||||
|
||||
// Start server in goroutine
|
||||
go func() {
|
||||
slog.Info("server starting", "addr", server.Addr)
|
||||
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
@@ -79,119 +70,21 @@ func main() {
|
||||
}
|
||||
}()
|
||||
|
||||
// Wait for shutdown signal
|
||||
quit := make(chan os.Signal, 1)
|
||||
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-quit
|
||||
|
||||
slog.Info("server shutting down")
|
||||
cancelWorkers()
|
||||
|
||||
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer shutdownCancel()
|
||||
// Graceful shutdown
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
if err := server.Shutdown(shutdownCtx); err != nil {
|
||||
if err := server.Shutdown(ctx); err != nil {
|
||||
slog.Error("server shutdown error", "error", err)
|
||||
}
|
||||
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
wg.Wait()
|
||||
close(done)
|
||||
}()
|
||||
select {
|
||||
case <-done:
|
||||
slog.Info("all workers stopped")
|
||||
case <-time.After(15 * time.Second):
|
||||
slog.Warn("timed out waiting for workers to stop")
|
||||
}
|
||||
|
||||
db.ClosePool(dbPool)
|
||||
slog.Info("server stopped")
|
||||
}
|
||||
|
||||
func listActivePrinters(ctx context.Context, pool *pgxpool.Pool) []models.Printer {
|
||||
rows, err := pool.Query(ctx, `
|
||||
SELECT id, name, printer_type_id,
|
||||
manufacturer, model,
|
||||
moonraker_url, moonraker_api_key,
|
||||
mqtt_broker_host, mqtt_topic_prefix,
|
||||
mqtt_tls_enabled, is_active,
|
||||
created_at, updated_at
|
||||
FROM printers WHERE is_active = TRUE ORDER BY name
|
||||
`)
|
||||
if err != nil {
|
||||
slog.Warn("failed to query active printers", "error", err)
|
||||
return nil
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var printers []models.Printer
|
||||
for rows.Next() {
|
||||
var p models.Printer
|
||||
if err := rows.Scan(
|
||||
&p.ID, &p.Name, &p.PrinterTypeID,
|
||||
&p.Manufacturer, &p.Model,
|
||||
&p.MoonrakerURL, &p.MoonrakerAPIKey,
|
||||
&p.MQTTBrokerHost, &p.MQTTTopicPrefix,
|
||||
&p.MQTTTLSEnabled, &p.IsActive,
|
||||
&p.CreatedAt, &p.UpdatedAt,
|
||||
); err != nil {
|
||||
slog.Warn("failed to scan printer row", "error", err)
|
||||
continue
|
||||
}
|
||||
printers = append(printers, p)
|
||||
}
|
||||
return printers
|
||||
}
|
||||
|
||||
func startWorkerForPrinter(
|
||||
ctx context.Context,
|
||||
wg *sync.WaitGroup,
|
||||
cfg *config.Config,
|
||||
pool *pgxpool.Pool,
|
||||
printer models.Printer,
|
||||
pollInterval time.Duration,
|
||||
) {
|
||||
if printer.MoonrakerURL != nil && *printer.MoonrakerURL != "" {
|
||||
mc := clients.NewMoonrakerClient(*printer.MoonrakerURL)
|
||||
poller := workers.NewMoonrakerPoller(workers.MoonrakerPollerConfig{
|
||||
Client: mc,
|
||||
Pool: pool,
|
||||
PollInterval: pollInterval,
|
||||
PrinterID: printer.ID,
|
||||
PrinterName: printer.Name,
|
||||
})
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
poller.Run(ctx)
|
||||
}()
|
||||
}
|
||||
|
||||
if printer.MQTTBrokerHost != nil && *printer.MQTTBrokerHost != "" {
|
||||
topicPrefix := cfg.MQTTTopicPrefix
|
||||
if printer.MQTTTopicPrefix != nil && *printer.MQTTTopicPrefix != "" {
|
||||
topicPrefix = *printer.MQTTTopicPrefix
|
||||
}
|
||||
sub := workers.NewMQTTSubscriber(workers.MQTTSubscriberConfig{
|
||||
Pool: pool,
|
||||
PrinterID: printer.ID,
|
||||
PrinterName: printer.Name,
|
||||
})
|
||||
mqttClient := clients.NewMQTTClient(clients.MQTTConfig{
|
||||
Broker: *printer.MQTTBrokerHost,
|
||||
ClientID: cfg.MQTTClientID + "-p" + strconv.Itoa(printer.ID),
|
||||
TopicPrefix: topicPrefix,
|
||||
TLSCert: cfg.MQTTTLSCert,
|
||||
TLSKey: cfg.MQTTTLSKey,
|
||||
Handler: sub.HandleBambuReport,
|
||||
})
|
||||
sub.Client = mqttClient
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
if err := sub.Run(ctx); err != nil {
|
||||
slog.Error("mqtt subscriber error", "printer_id", printer.ID, "error", err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,9 @@
|
||||
module github.com/CubeCraft-Creations/Extrudex/backend
|
||||
|
||||
go 1.24.0
|
||||
|
||||
toolchain go1.24.2
|
||||
go 1.24
|
||||
|
||||
require (
|
||||
github.com/eclipse/paho.mqtt.golang v1.5.1
|
||||
github.com/go-chi/chi/v5 v5.2.0
|
||||
github.com/gorilla/websocket v1.5.3
|
||||
github.com/jackc/pgx/v5 v5.7.4
|
||||
github.com/kelseyhightower/envconfig v1.4.0
|
||||
)
|
||||
@@ -16,8 +12,7 @@ require (
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
||||
golang.org/x/crypto v0.42.0 // indirect
|
||||
golang.org/x/net v0.44.0 // indirect
|
||||
golang.org/x/sync v0.17.0 // indirect
|
||||
golang.org/x/text v0.29.0 // indirect
|
||||
golang.org/x/crypto v0.31.0 // indirect
|
||||
golang.org/x/sync v0.10.0 // indirect
|
||||
golang.org/x/text v0.21.0 // indirect
|
||||
)
|
||||
|
||||
@@ -1,12 +1,8 @@
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/eclipse/paho.mqtt.golang v1.5.1 h1:/VSOv3oDLlpqR2Epjn1Q7b2bSTplJIeV2ISgCl2W7nE=
|
||||
github.com/eclipse/paho.mqtt.golang v1.5.1/go.mod h1:1/yJCneuyOoCOzKSsOTUc0AJfpsItBGWvYpBLimhArU=
|
||||
github.com/go-chi/chi/v5 v5.2.0 h1:Aj1EtB0qR2Rdo2dG4O94RIU35w2lvQSj6BRA4+qwFL0=
|
||||
github.com/go-chi/chi/v5 v5.2.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
|
||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
|
||||
@@ -24,14 +20,12 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
golang.org/x/crypto v0.42.0 h1:chiH31gIWm57EkTXpwnqf8qeuMUi0yekh6mT2AvFlqI=
|
||||
golang.org/x/crypto v0.42.0/go.mod h1:4+rDnOTJhQCx2q7/j6rAN5XDw8kPjeaXEUR2eL94ix8=
|
||||
golang.org/x/net v0.44.0 h1:evd8IRDyfNBMBTTY5XRF1vaZlD+EmWx6x8PkhR04H/I=
|
||||
golang.org/x/net v0.44.0/go.mod h1:ECOoLqd5U3Lhyeyo/QDCEVQ4sNgYsqvCZ722XogGieY=
|
||||
golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug=
|
||||
golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
|
||||
golang.org/x/text v0.29.0 h1:1neNs90w9YzJ9BocxfsQNHKuAT4pkghyXc4nhZ6sJvk=
|
||||
golang.org/x/text v0.29.0/go.mod h1:7MhJOA9CD2qZyOKYazxdYMF85OwPdEr9jTtBpO7ydH4=
|
||||
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
|
||||
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
|
||||
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
|
||||
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
|
||||
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
|
||||
@@ -1,171 +0,0 @@
|
||||
// Package clients provides client implementations for printer integrations:
|
||||
// Moonraker REST + WebSocket (Klipper-based printers) and MQTT (Bambu Lab).
|
||||
package clients
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ── Moonraker response types ────────────────────────────────────────────────
|
||||
|
||||
// moonrakerRPC is the generic JSON-RPC wrapper Moonraker uses for responses.
|
||||
type moonrakerRPC struct {
|
||||
Result json.RawMessage `json:"result"`
|
||||
Error *moonrakerError `json:"error"`
|
||||
}
|
||||
|
||||
type moonrakerError struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// ── Public DTOs ─────────────────────────────────────────────────────────────
|
||||
|
||||
// MoonrakerPrinterInfo represents the /printer/info response.
|
||||
type MoonrakerPrinterInfo struct {
|
||||
State string `json:"state"`
|
||||
StateMessage string `json:"state_message"`
|
||||
KlippyReady bool `json:"klippy_ready"`
|
||||
}
|
||||
|
||||
// MoonrakerPrintStats represents the print_stats object from
|
||||
// /printer/objects/query?print_stats.
|
||||
type MoonrakerPrintStats struct {
|
||||
State string `json:"state"`
|
||||
Filename *string `json:"filename"`
|
||||
FilamentUsedMm float64 `json:"filament_used"`
|
||||
PrintDuration float64 `json:"print_duration"`
|
||||
Message *string `json:"message"`
|
||||
}
|
||||
|
||||
// MoonrakerPrintJob represents a single entry in /server/history/items.
|
||||
type MoonrakerPrintJob struct {
|
||||
JobID string `json:"job_id"`
|
||||
Filename string `json:"filename"`
|
||||
Status string `json:"status"`
|
||||
FilamentUsedMm float64 `json:"filament_used"`
|
||||
PrintDuration float64 `json:"print_duration"`
|
||||
TotalDuration float64 `json:"total_duration"`
|
||||
StartTime *float64 `json:"start_time"`
|
||||
EndTime *float64 `json:"end_time"`
|
||||
Metadata map[string]interface{} `json:"metadata"`
|
||||
}
|
||||
|
||||
// MoonrakerHistoryResponse wraps the /server/history/items response.
|
||||
type MoonrakerHistoryResponse struct {
|
||||
Items []MoonrakerPrintJob `json:"items"`
|
||||
TotalCount int `json:"count"`
|
||||
}
|
||||
|
||||
// ── Client ──────────────────────────────────────────────────────────────────
|
||||
|
||||
// MoonrakerClient is an HTTP client for the Moonraker REST API on
|
||||
// Klipper-based printers (e.g., Elegoo Centauri Carbon).
|
||||
type MoonrakerClient struct {
|
||||
baseURL string
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
// NewMoonrakerClient creates a MoonrakerClient that targets the given
|
||||
// base URL (e.g., "http://192.168.1.50:7125"). The internal HTTP client
|
||||
// uses a 15-second timeout.
|
||||
func NewMoonrakerClient(baseURL string) *MoonrakerClient {
|
||||
baseURL = strings.TrimRight(baseURL, "/")
|
||||
return &MoonrakerClient{
|
||||
baseURL: baseURL,
|
||||
httpClient: &http.Client{
|
||||
Timeout: 15 * time.Second,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// GetPrinterInfo calls GET /printer/info and returns the Klipper state.
|
||||
// Returns nil when the printer is unreachable or the response cannot be parsed.
|
||||
func (c *MoonrakerClient) GetPrinterInfo(ctx context.Context) (*MoonrakerPrinterInfo, error) {
|
||||
var info MoonrakerPrinterInfo
|
||||
if err := c.getJSON(ctx, "/printer/info", &info); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &info, nil
|
||||
}
|
||||
|
||||
// GetPrintStats calls GET /printer/objects/query?print_stats and returns
|
||||
// real-time print statistics including filament consumption.
|
||||
// Returns nil when no print is active or the printer is unreachable.
|
||||
func (c *MoonrakerClient) GetPrintStats(ctx context.Context) (*MoonrakerPrintStats, error) {
|
||||
var stats MoonrakerPrintStats
|
||||
// Moonraker wraps the object in status.print_stats
|
||||
var wrapper struct {
|
||||
Status struct {
|
||||
PrintStats MoonrakerPrintStats `json:"print_stats"`
|
||||
} `json:"status"`
|
||||
}
|
||||
if err := c.getJSON(ctx, "/printer/objects/query?print_stats", &wrapper); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
stats = wrapper.Status.PrintStats
|
||||
return &stats, nil
|
||||
}
|
||||
|
||||
// GetPrintHistory calls GET /server/history/items and returns recent print
|
||||
// jobs. limit controls the maximum number of items (clamped 1-100).
|
||||
func (c *MoonrakerClient) GetPrintHistory(ctx context.Context, limit int) (*MoonrakerHistoryResponse, error) {
|
||||
if limit < 1 {
|
||||
limit = 1
|
||||
}
|
||||
if limit > 100 {
|
||||
limit = 100
|
||||
}
|
||||
var history MoonrakerHistoryResponse
|
||||
if err := c.getJSON(ctx, fmt.Sprintf("/server/history/items?limit=%d", limit), &history); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &history, nil
|
||||
}
|
||||
|
||||
// ── Internal helpers ────────────────────────────────────────────────────────
|
||||
|
||||
func (c *MoonrakerClient) getJSON(ctx context.Context, path string, target interface{}) error {
|
||||
url := c.baseURL + path
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("moonraker: failed to build request: %w", err)
|
||||
}
|
||||
req.Header.Set("Accept", "application/json")
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("moonraker: request failed (%s): %w", url, err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("moonraker: failed to read body: %w", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
return fmt.Errorf("moonraker: %s returned HTTP %d: %s", url, resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
// Moonraker wraps responses in {"result": ...}
|
||||
var rpc moonrakerRPC
|
||||
if err := json.Unmarshal(body, &rpc); err != nil {
|
||||
return fmt.Errorf("moonraker: failed to parse response: %w", err)
|
||||
}
|
||||
if rpc.Error != nil && rpc.Error.Message != "" {
|
||||
return fmt.Errorf("moonraker: api error: %s", rpc.Error.Message)
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(rpc.Result, target); err != nil {
|
||||
return fmt.Errorf("moonraker: failed to unmarshal result: %w (raw: %s)", err, string(rpc.Result))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,229 +0,0 @@
|
||||
package clients
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
// ── WebSocket message types ─────────────────────────────────────────────────
|
||||
|
||||
// moonrakerWSMessage is a single JSON-RPC frame from the Moonraker WebSocket.
|
||||
type moonrakerWSMessage struct {
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
Method string `json:"method"`
|
||||
Params json.RawMessage `json:"params"`
|
||||
ID *int `json:"id"`
|
||||
}
|
||||
|
||||
// MoonrakerPrintEvent is the payload delivered by the "notify_status_update"
|
||||
// subscription when print_stats or display_status change.
|
||||
type MoonrakerPrintEvent struct {
|
||||
PrintStats *MoonrakerPrintStats `json:"print_stats"`
|
||||
DisplayStatus *MoonrakerDisplayStatus `json:"display_status"`
|
||||
}
|
||||
|
||||
// MoonrakerDisplayStatus carries progress and the LCD message.
|
||||
type MoonrakerDisplayStatus struct {
|
||||
Progress float64 `json:"progress"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// MoonrakerStatusHandler is called for every status update received from the
|
||||
// Moonraker WebSocket. It receives the parsed event and the raw JSON.
|
||||
type MoonrakerStatusHandler func(event MoonrakerPrintEvent) error
|
||||
|
||||
// ── WebSocket client ────────────────────────────────────────────────────────
|
||||
|
||||
// MoonrakerWSClient maintains a persistent WebSocket connection to the
|
||||
// Moonraker server and delivers parsed status updates to a handler.
|
||||
type MoonrakerWSClient struct {
|
||||
wsURL string
|
||||
handler MoonrakerStatusHandler
|
||||
dialer *websocket.Dialer
|
||||
|
||||
mu sync.Mutex
|
||||
conn *websocket.Conn
|
||||
done chan struct{}
|
||||
once sync.Once
|
||||
}
|
||||
|
||||
// NewMoonrakerWSClient creates a WebSocket client for the given Moonraker base
|
||||
// URL. The handler is invoked on every status update.
|
||||
func NewMoonrakerWSClient(baseURL string, handler MoonrakerStatusHandler) *MoonrakerWSClient {
|
||||
baseURL = strings.TrimRight(baseURL, "/")
|
||||
wsURL := strings.Replace(baseURL, "http://", "ws://", 1)
|
||||
wsURL = strings.Replace(wsURL, "https://", "wss://", 1)
|
||||
wsURL += "/websocket"
|
||||
|
||||
return &MoonrakerWSClient{
|
||||
wsURL: wsURL,
|
||||
handler: handler,
|
||||
dialer: &websocket.Dialer{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
HandshakeTimeout: 10 * time.Second,
|
||||
},
|
||||
done: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
// Connect establishes the WebSocket, subscribes to status updates, and
|
||||
// starts the read loop in a background goroutine. It retries on failure
|
||||
// with exponential backoff up to a 60-second cap.
|
||||
func (c *MoonrakerWSClient) Connect(ctx context.Context) {
|
||||
go c.run(ctx)
|
||||
}
|
||||
|
||||
// Shutdown gracefully closes the WebSocket and stops the read loop.
|
||||
func (c *MoonrakerWSClient) Shutdown() {
|
||||
c.once.Do(func() {
|
||||
close(c.done)
|
||||
})
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
if c.conn != nil {
|
||||
c.conn.Close()
|
||||
c.conn = nil
|
||||
}
|
||||
}
|
||||
|
||||
// run is the main connection loop with reconnect backoff.
|
||||
func (c *MoonrakerWSClient) run(ctx context.Context) {
|
||||
backoff := 1 * time.Second
|
||||
const maxBackoff = 60 * time.Second
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
slog.Info("moonraker ws: context cancelled, stopping")
|
||||
return
|
||||
case <-c.done:
|
||||
slog.Info("moonraker ws: shutdown requested")
|
||||
return
|
||||
default:
|
||||
}
|
||||
|
||||
if err := c.connectAndRead(ctx); err != nil {
|
||||
slog.Error("moonraker ws: connection error, retrying", "error", err, "backoff", backoff)
|
||||
}
|
||||
|
||||
// Exponential backoff.
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-c.done:
|
||||
return
|
||||
case <-time.After(backoff):
|
||||
}
|
||||
backoff *= 2
|
||||
if backoff > maxBackoff {
|
||||
backoff = maxBackoff
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *MoonrakerWSClient) connectAndRead(ctx context.Context) error {
|
||||
slog.Info("moonraker ws: connecting", "url", c.wsURL)
|
||||
|
||||
conn, _, err := c.dialer.DialContext(ctx, c.wsURL, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("dial failed: %w", err)
|
||||
}
|
||||
|
||||
c.mu.Lock()
|
||||
if c.conn != nil {
|
||||
c.conn.Close()
|
||||
}
|
||||
c.conn = conn
|
||||
c.mu.Unlock()
|
||||
|
||||
defer func() {
|
||||
c.mu.Lock()
|
||||
if c.conn == conn {
|
||||
c.conn = nil
|
||||
}
|
||||
c.mu.Unlock()
|
||||
conn.Close()
|
||||
}()
|
||||
|
||||
// Subscribe to status updates.
|
||||
subReq := map[string]interface{}{
|
||||
"jsonrpc": "2.0",
|
||||
"method": "printer.objects.subscribe",
|
||||
"params": map[string]interface{}{
|
||||
"objects": map[string]interface{}{
|
||||
"print_stats": nil,
|
||||
"display_status": nil,
|
||||
},
|
||||
},
|
||||
"id": 1,
|
||||
}
|
||||
if err := conn.WriteJSON(subReq); err != nil {
|
||||
return fmt.Errorf("subscribe failed: %w", err)
|
||||
}
|
||||
|
||||
slog.Info("moonraker ws: subscribed to status updates")
|
||||
|
||||
// Set read deadline to detect stale connections.
|
||||
// 120s is long enough to avoid false positives.
|
||||
pingPeriod := 60 * time.Second
|
||||
|
||||
for {
|
||||
// Set read deadline.
|
||||
if err := conn.SetReadDeadline(time.Now().Add(150 * time.Second)); err != nil {
|
||||
return fmt.Errorf("set read deadline: %w", err)
|
||||
}
|
||||
|
||||
_, raw, err := conn.ReadMessage()
|
||||
if err != nil {
|
||||
return fmt.Errorf("read message: %w", err)
|
||||
}
|
||||
|
||||
// Send periodic pings to keep the connection alive.
|
||||
go func() {
|
||||
time.Sleep(pingPeriod)
|
||||
c.mu.Lock()
|
||||
if c.conn == conn {
|
||||
c.conn.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(10*time.Second))
|
||||
}
|
||||
c.mu.Unlock()
|
||||
}()
|
||||
|
||||
var msg moonrakerWSMessage
|
||||
if err := json.Unmarshal(raw, &msg); err != nil {
|
||||
slog.Warn("moonraker ws: failed to parse message", "error", err)
|
||||
continue
|
||||
}
|
||||
|
||||
// Only process notify_status_update messages.
|
||||
if msg.Method != "notify_status_update" {
|
||||
continue
|
||||
}
|
||||
|
||||
var statusWrapper []MoonrakerPrintEvent
|
||||
if err := json.Unmarshal(msg.Params, &statusWrapper); err != nil {
|
||||
// Params might be an object, not an array.
|
||||
var singleEvent MoonrakerPrintEvent
|
||||
if err2 := json.Unmarshal(msg.Params, &singleEvent); err2 != nil {
|
||||
slog.Warn("moonraker ws: failed to unmarshal status params", "error", err2)
|
||||
continue
|
||||
}
|
||||
statusWrapper = []MoonrakerPrintEvent{singleEvent}
|
||||
}
|
||||
|
||||
for _, ev := range statusWrapper {
|
||||
if c.handler != nil {
|
||||
if err := c.handler(ev); err != nil {
|
||||
slog.Error("moonraker ws: handler error", "error", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,183 +0,0 @@
|
||||
package clients
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||
)
|
||||
|
||||
// ── Bambu Lab telemetry types ───────────────────────────────────────────────
|
||||
|
||||
// BambuPrintReport is the JSON payload published by Bambu Lab printers
|
||||
// on the MQTT report topic. The structure varies by printer model;
|
||||
// we extract the common fields needed for filament tracking.
|
||||
type BambuPrintReport struct {
|
||||
// Print holds the active print job data.
|
||||
Print BambuPrintData `json:"print"`
|
||||
|
||||
// VtTray contains AMS tray info; the extruded length is per-tray.
|
||||
VtTray *BambuVtTray `json:"vt_tray,omitempty"`
|
||||
}
|
||||
|
||||
// BambuPrintData carries the active print state from a Bambu report.
|
||||
type BambuPrintData struct {
|
||||
// GcodeFile is the filename being printed.
|
||||
GcodeFile string `json:"gcode_file"`
|
||||
// GcodeState describes the current print state:
|
||||
// "IDLE", "RUNNING", "PAUSE", "FINISH", "FAILED".
|
||||
GcodeState string `json:"gcode_state"`
|
||||
// McPercent is the progress as a percentage (0-100).
|
||||
McPercent int `json:"mc_percent"`
|
||||
// McRemainingTime is the estimated remaining time in minutes.
|
||||
McRemainingTime int `json:"mc_remaining_time"`
|
||||
}
|
||||
|
||||
// BambuVtTray holds AMS tray telemetry from Bambu printers.
|
||||
type BambuVtTray struct {
|
||||
ID string `json:"id"`
|
||||
TagUID string `json:"tag_uid"`
|
||||
TrayIDName string `json:"tray_id_name"`
|
||||
// TrayInfoIdx is the hex color code for the tray's filament.
|
||||
TrayInfoIdx string `json:"tray_info_idx"`
|
||||
// TrayColor is a hex color string like "FF0000FF".
|
||||
TrayColor string `json:"tray_color"`
|
||||
// Remain is the percentage of filament remaining on this tray (0-100).
|
||||
Remain int `json:"remain"`
|
||||
// K is a temperature coefficient.
|
||||
K float64 `json:"k"`
|
||||
// N is a second temperature coefficient.
|
||||
N float64 `json:"n"`
|
||||
}
|
||||
|
||||
// BambuReportHandler is called for each parsed Bambu telemetry message.
|
||||
type BambuReportHandler func(report BambuPrintReport) error
|
||||
|
||||
// ── MQTT client ─────────────────────────────────────────────────────────────
|
||||
|
||||
// MQTTClient wraps the Eclipse Paho MQTT client for Bambu Lab printer
|
||||
// telemetry with optional TLS support.
|
||||
type MQTTClient struct {
|
||||
broker string
|
||||
clientID string
|
||||
topicPrefix string
|
||||
tlsCert string
|
||||
tlsKey string
|
||||
handler BambuReportHandler
|
||||
|
||||
mu sync.Mutex
|
||||
client mqtt.Client
|
||||
}
|
||||
|
||||
// MQTTConfig holds the configuration for creating an MQTTClient.
|
||||
type MQTTConfig struct {
|
||||
Broker string // e.g., "ssl://192.168.1.50:8883"
|
||||
ClientID string // unique MQTT client id, defaults to "extrudex"
|
||||
TopicPrefix string // topic prefix, defaults to "device/+/report"
|
||||
TLSCert string // path to TLS client certificate (optional)
|
||||
TLSKey string // path to TLS client key (optional)
|
||||
Handler BambuReportHandler
|
||||
}
|
||||
|
||||
// NewMQTTClient creates a new MQTTClient. The connection is not established
|
||||
// until Connect is called.
|
||||
func NewMQTTClient(cfg MQTTConfig) *MQTTClient {
|
||||
if cfg.ClientID == "" {
|
||||
cfg.ClientID = "extrudex"
|
||||
}
|
||||
if cfg.TopicPrefix == "" {
|
||||
cfg.TopicPrefix = "device/+/report"
|
||||
}
|
||||
return &MQTTClient{
|
||||
broker: cfg.Broker,
|
||||
clientID: cfg.ClientID,
|
||||
topicPrefix: cfg.TopicPrefix,
|
||||
tlsCert: cfg.TLSCert,
|
||||
tlsKey: cfg.TLSKey,
|
||||
handler: cfg.Handler,
|
||||
}
|
||||
}
|
||||
|
||||
// Connect establishes the MQTT connection and subscribes to the configured
|
||||
// topic prefix. Returns an error if the initial connection fails.
|
||||
func (c *MQTTClient) Connect() error {
|
||||
opts := mqtt.NewClientOptions().
|
||||
AddBroker(c.broker).
|
||||
SetClientID(c.clientID).
|
||||
SetAutoReconnect(true).
|
||||
SetMaxReconnectInterval(30 * time.Second).
|
||||
SetKeepAlive(30 * time.Second).
|
||||
SetPingTimeout(10 * time.Second).
|
||||
SetConnectTimeout(15 * time.Second).
|
||||
SetOnConnectHandler(func(client mqtt.Client) {
|
||||
slog.Info("mqtt: connected", "broker", c.broker)
|
||||
// Subscribe on every reconnect.
|
||||
token := client.Subscribe(c.topicPrefix, 0, c.messageHandler)
|
||||
token.Wait()
|
||||
if err := token.Error(); err != nil {
|
||||
slog.Error("mqtt: subscribe failed on reconnect", "topic", c.topicPrefix, "error", err)
|
||||
} else {
|
||||
slog.Info("mqtt: subscribed", "topic", c.topicPrefix)
|
||||
}
|
||||
}).
|
||||
SetConnectionLostHandler(func(client mqtt.Client, err error) {
|
||||
slog.Warn("mqtt: connection lost", "error", err)
|
||||
})
|
||||
|
||||
// Configure TLS if cert and key are provided.
|
||||
if c.tlsCert != "" && c.tlsKey != "" {
|
||||
cert, err := tls.LoadX509KeyPair(c.tlsCert, c.tlsKey)
|
||||
if err != nil {
|
||||
return fmt.Errorf("mqtt: failed to load TLS cert/key: %w", err)
|
||||
}
|
||||
opts.SetTLSConfig(&tls.Config{
|
||||
Certificates: []tls.Certificate{cert},
|
||||
MinVersion: tls.VersionTLS12,
|
||||
})
|
||||
slog.Info("mqtt: TLS configured", "cert", c.tlsCert)
|
||||
}
|
||||
|
||||
c.client = mqtt.NewClient(opts)
|
||||
token := c.client.Connect()
|
||||
if !token.WaitTimeout(15 * time.Second) {
|
||||
return fmt.Errorf("mqtt: connect timed out to %s", c.broker)
|
||||
}
|
||||
if err := token.Error(); err != nil {
|
||||
return fmt.Errorf("mqtt: connect failed: %w", err)
|
||||
}
|
||||
|
||||
slog.Info("mqtt: initial connection established", "broker", c.broker)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Disconnect gracefully closes the MQTT connection.
|
||||
func (c *MQTTClient) Disconnect() {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
if c.client != nil && c.client.IsConnected() {
|
||||
c.client.Disconnect(2500) // wait up to 2.5s
|
||||
slog.Info("mqtt: disconnected")
|
||||
}
|
||||
}
|
||||
|
||||
// messageHandler is the MQTT callback invoked for every message received on
|
||||
// the subscribed topic.
|
||||
func (c *MQTTClient) messageHandler(_ mqtt.Client, msg mqtt.Message) {
|
||||
if c.handler == nil {
|
||||
return
|
||||
}
|
||||
|
||||
var report BambuPrintReport
|
||||
if err := json.Unmarshal(msg.Payload(), &report); err != nil {
|
||||
slog.Warn("mqtt: failed to parse bambu report", "topic", msg.Topic(), "error", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := c.handler(report); err != nil {
|
||||
slog.Error("mqtt: handler error", "topic", msg.Topic(), "error", err)
|
||||
}
|
||||
}
|
||||
@@ -12,17 +12,6 @@ type Config struct {
|
||||
Port string `envconfig:"port" default:"8080"`
|
||||
CorsOrigin string `envconfig:"cors_origin" default:"*"`
|
||||
LogLevel string `envconfig:"log_level" default:"info"`
|
||||
|
||||
// Moonraker integration.
|
||||
MoonrakerURL string `envconfig:"moonraker_url" default:"http://localhost:7125"`
|
||||
MoonrakerPollInterval string `envconfig:"moonraker_poll_interval" default:"10s"`
|
||||
|
||||
// MQTT (Bambu Lab) integration.
|
||||
MQTTBroker string `envconfig:"mqtt_broker" default:"localhost:1883"`
|
||||
MQTTTopicPrefix string `envconfig:"mqtt_topic_prefix" default:"device/+/report"`
|
||||
MQTTClientID string `envconfig:"mqtt_client_id" default:"extrudex"`
|
||||
MQTTTLSCert string `envconfig:"mqtt_tls_cert" default:""`
|
||||
MQTTTLSKey string `envconfig:"mqtt_tls_key" default:""`
|
||||
}
|
||||
|
||||
// Load reads configuration from environment variables and returns a populated Config.
|
||||
|
||||
34
backend/internal/handlers/material_finish_handler.go
Normal file
34
backend/internal/handlers/material_finish_handler.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"github.com/CubeCraft-Creations/Extrudex/backend/internal/dtos"
|
||||
"github.com/CubeCraft-Creations/Extrudex/backend/internal/repositories"
|
||||
)
|
||||
|
||||
// MaterialFinishHandler handles requests for material finish lookup data.
|
||||
type MaterialFinishHandler struct {
|
||||
repo *repositories.MaterialFinishRepository
|
||||
}
|
||||
|
||||
// NewMaterialFinishHandler creates a MaterialFinishHandler with the given repository.
|
||||
func NewMaterialFinishHandler(repo *repositories.MaterialFinishRepository) *MaterialFinishHandler {
|
||||
return &MaterialFinishHandler{repo: repo}
|
||||
}
|
||||
|
||||
// List handles GET /api/finishes — returns all material finishes.
|
||||
func (h *MaterialFinishHandler) List(w http.ResponseWriter, r *http.Request) {
|
||||
finishes, err := h.repo.GetAll(r.Context())
|
||||
if err != nil {
|
||||
slog.Error("failed to list finishes", "error", err)
|
||||
writeJSON(w, http.StatusInternalServerError, dtos.ErrorResponse{
|
||||
Error: "internal server error",
|
||||
Code: http.StatusInternalServerError,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, dtos.SingleResponse{Data: finishes})
|
||||
}
|
||||
34
backend/internal/handlers/material_modifier_handler.go
Normal file
34
backend/internal/handlers/material_modifier_handler.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"github.com/CubeCraft-Creations/Extrudex/backend/internal/dtos"
|
||||
"github.com/CubeCraft-Creations/Extrudex/backend/internal/repositories"
|
||||
)
|
||||
|
||||
// MaterialModifierHandler handles requests for material modifier lookup data.
|
||||
type MaterialModifierHandler struct {
|
||||
repo *repositories.MaterialModifierRepository
|
||||
}
|
||||
|
||||
// NewMaterialModifierHandler creates a MaterialModifierHandler with the given repository.
|
||||
func NewMaterialModifierHandler(repo *repositories.MaterialModifierRepository) *MaterialModifierHandler {
|
||||
return &MaterialModifierHandler{repo: repo}
|
||||
}
|
||||
|
||||
// List handles GET /api/modifiers — returns all material modifiers.
|
||||
func (h *MaterialModifierHandler) List(w http.ResponseWriter, r *http.Request) {
|
||||
modifiers, err := h.repo.GetAll(r.Context())
|
||||
if err != nil {
|
||||
slog.Error("failed to list modifiers", "error", err)
|
||||
writeJSON(w, http.StatusInternalServerError, dtos.ErrorResponse{
|
||||
Error: "internal server error",
|
||||
Code: http.StatusInternalServerError,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, dtos.SingleResponse{Data: modifiers})
|
||||
}
|
||||
50
backend/internal/repositories/material_finish_repository.go
Normal file
50
backend/internal/repositories/material_finish_repository.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/CubeCraft-Creations/Extrudex/backend/internal/models"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
// MaterialFinishRepository handles database queries for material finishes.
|
||||
type MaterialFinishRepository struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
// NewMaterialFinishRepository creates a MaterialFinishRepository backed by the given pool.
|
||||
func NewMaterialFinishRepository(pool *pgxpool.Pool) *MaterialFinishRepository {
|
||||
return &MaterialFinishRepository{pool: pool}
|
||||
}
|
||||
|
||||
// GetAll returns all material finishes ordered by name.
|
||||
func (r *MaterialFinishRepository) GetAll(ctx context.Context) ([]models.MaterialFinish, error) {
|
||||
rows, err := r.pool.Query(ctx, `
|
||||
SELECT id, name, description, created_at, updated_at
|
||||
FROM material_finishes
|
||||
ORDER BY name
|
||||
`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var finishes []models.MaterialFinish
|
||||
for rows.Next() {
|
||||
var f models.MaterialFinish
|
||||
if err := rows.Scan(
|
||||
&f.ID, &f.Name, &f.Description,
|
||||
&f.CreatedAt, &f.UpdatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
finishes = append(finishes, f)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if finishes == nil {
|
||||
finishes = []models.MaterialFinish{}
|
||||
}
|
||||
return finishes, nil
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/CubeCraft-Creations/Extrudex/backend/internal/models"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
// MaterialModifierRepository handles database queries for material modifiers.
|
||||
type MaterialModifierRepository struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
// NewMaterialModifierRepository creates a MaterialModifierRepository backed by the given pool.
|
||||
func NewMaterialModifierRepository(pool *pgxpool.Pool) *MaterialModifierRepository {
|
||||
return &MaterialModifierRepository{pool: pool}
|
||||
}
|
||||
|
||||
// GetAll returns all material modifiers ordered by name.
|
||||
func (r *MaterialModifierRepository) GetAll(ctx context.Context) ([]models.MaterialModifier, error) {
|
||||
rows, err := r.pool.Query(ctx, `
|
||||
SELECT id, name, description, created_at, updated_at
|
||||
FROM material_modifiers
|
||||
ORDER BY name
|
||||
`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var modifiers []models.MaterialModifier
|
||||
for rows.Next() {
|
||||
var m models.MaterialModifier
|
||||
if err := rows.Scan(
|
||||
&m.ID, &m.Name, &m.Description,
|
||||
&m.CreatedAt, &m.UpdatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
modifiers = append(modifiers, m)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if modifiers == nil {
|
||||
modifiers = []models.MaterialModifier{}
|
||||
}
|
||||
return modifiers, nil
|
||||
}
|
||||
@@ -45,6 +45,8 @@ func New(cfg *config.Config, dbPool *pgxpool.Pool, sseBC *sse.Broadcaster) chi.R
|
||||
|
||||
// ── Repositories ──────────────────────────────────────────────────────
|
||||
materialRepo := repositories.NewMaterialRepository(dbPool)
|
||||
finishRepo := repositories.NewMaterialFinishRepository(dbPool)
|
||||
modifierRepo := repositories.NewMaterialModifierRepository(dbPool)
|
||||
filamentRepo := repositories.NewFilamentRepository(dbPool)
|
||||
printerRepo := repositories.NewPrinterRepository(dbPool)
|
||||
printJobRepo := repositories.NewPrintJobRepository(dbPool)
|
||||
@@ -57,6 +59,8 @@ func New(cfg *config.Config, dbPool *pgxpool.Pool, sseBC *sse.Broadcaster) chi.R
|
||||
|
||||
// ── Handlers ──────────────────────────────────────────────────────────
|
||||
materialHandler := handlers.NewMaterialHandler(materialRepo)
|
||||
finishHandler := handlers.NewMaterialFinishHandler(finishRepo)
|
||||
modifierHandler := handlers.NewMaterialModifierHandler(modifierRepo)
|
||||
filamentHandler := handlers.NewFilamentHandler(filamentService)
|
||||
printerHandler := handlers.NewPrinterHandler(printerService)
|
||||
printJobHandler := handlers.NewPrintJobHandler(printJobService)
|
||||
@@ -66,6 +70,8 @@ func New(cfg *config.Config, dbPool *pgxpool.Pool, sseBC *sse.Broadcaster) chi.R
|
||||
r.Route("/api", func(r chi.Router) {
|
||||
r.Use(middleware.Timeout(60 * time.Second))
|
||||
r.Get("/materials", materialHandler.List)
|
||||
r.Get("/finishes", finishHandler.List)
|
||||
r.Get("/modifiers", modifierHandler.List)
|
||||
|
||||
r.Route("/filaments", func(r chi.Router) {
|
||||
r.Get("/", filamentHandler.List)
|
||||
|
||||
@@ -1,255 +0,0 @@
|
||||
package workers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/CubeCraft-Creations/Extrudex/backend/internal/clients"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
// ── deduplication ───────────────────────────────────────────────────────────
|
||||
|
||||
// jobTrack holds the last-seen filename and filament_used for dedup.
|
||||
type jobTrack struct {
|
||||
filename string
|
||||
filamentUsed float64
|
||||
}
|
||||
|
||||
// MoonrakerPoller periodically queries the Moonraker REST API for print stats
|
||||
// and logs filament usage to PostgreSQL. It deduplicates by tracking the
|
||||
// last-known filament_used value for the active job on this printer.
|
||||
type MoonrakerPoller struct {
|
||||
client *clients.MoonrakerClient
|
||||
pool *pgxpool.Pool
|
||||
pollInterval time.Duration
|
||||
printerID int
|
||||
printerName string
|
||||
|
||||
mu sync.Mutex
|
||||
track jobTrack
|
||||
}
|
||||
|
||||
// MoonrakerPollerConfig holds configuration for the Moonraker polling worker.
|
||||
type MoonrakerPollerConfig struct {
|
||||
Client *clients.MoonrakerClient
|
||||
Pool *pgxpool.Pool
|
||||
PollInterval time.Duration
|
||||
PrinterID int
|
||||
PrinterName string
|
||||
}
|
||||
|
||||
// NewMoonrakerPoller creates a new MoonrakerPoller worker.
|
||||
func NewMoonrakerPoller(cfg MoonrakerPollerConfig) *MoonrakerPoller {
|
||||
if cfg.PollInterval <= 0 {
|
||||
cfg.PollInterval = 10 * time.Second
|
||||
}
|
||||
return &MoonrakerPoller{
|
||||
client: cfg.Client,
|
||||
pool: cfg.Pool,
|
||||
pollInterval: cfg.PollInterval,
|
||||
printerID: cfg.PrinterID,
|
||||
printerName: cfg.PrinterName,
|
||||
}
|
||||
}
|
||||
|
||||
// Run starts the polling loop. It blocks until ctx is cancelled.
|
||||
func (w *MoonrakerPoller) Run(ctx context.Context) {
|
||||
slog.Info("moonraker poller: starting",
|
||||
"printer_id", w.printerID,
|
||||
"printer_name", w.printerName,
|
||||
"interval", w.pollInterval,
|
||||
)
|
||||
|
||||
ticker := time.NewTicker(w.pollInterval)
|
||||
defer ticker.Stop()
|
||||
|
||||
w.poll(ctx)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
slog.Info("moonraker poller: stopping", "printer_id", w.printerID)
|
||||
return
|
||||
case <-ticker.C:
|
||||
w.poll(ctx)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (w *MoonrakerPoller) poll(ctx context.Context) {
|
||||
stats, err := w.client.GetPrintStats(ctx)
|
||||
if err != nil {
|
||||
slog.Warn("moonraker poller: failed to get print stats",
|
||||
"printer_id", w.printerID, "error", err)
|
||||
return
|
||||
}
|
||||
|
||||
if stats.State == "" {
|
||||
return
|
||||
}
|
||||
|
||||
jobName := "unknown"
|
||||
if stats.Filename != nil {
|
||||
jobName = *stats.Filename
|
||||
}
|
||||
|
||||
// Compute delta under lock; release before I/O.
|
||||
w.mu.Lock()
|
||||
prevName := w.track.filename
|
||||
prevUsed := w.track.filamentUsed
|
||||
|
||||
if jobName != prevName {
|
||||
w.track.filename = jobName
|
||||
w.track.filamentUsed = 0
|
||||
prevUsed = 0
|
||||
}
|
||||
|
||||
deltaMM := stats.FilamentUsedMm - prevUsed
|
||||
totalMM := stats.FilamentUsedMm
|
||||
if deltaMM <= 0 && jobName == prevName {
|
||||
w.mu.Unlock()
|
||||
return
|
||||
}
|
||||
w.mu.Unlock()
|
||||
|
||||
slog.Info("moonraker poller: filament usage",
|
||||
"printer_id", w.printerID,
|
||||
"job", jobName,
|
||||
"delta_mm", deltaMM,
|
||||
"total_mm", totalMM,
|
||||
"state", stats.State,
|
||||
)
|
||||
|
||||
jobID, err := w.ensurePrintJob(ctx, jobName, stats.State)
|
||||
if err != nil {
|
||||
slog.Error("moonraker poller: failed to ensure print job",
|
||||
"printer_id", w.printerID, "error", err)
|
||||
return
|
||||
}
|
||||
|
||||
spoolID, density := lookupActiveSpool(ctx, w.pool, w.printerID)
|
||||
|
||||
if err := insertUsageLog(ctx, w.pool, jobID, spoolID, deltaMM, density); err != nil {
|
||||
slog.Error("moonraker poller: failed to log usage",
|
||||
"printer_id", w.printerID, "error", err)
|
||||
return
|
||||
}
|
||||
|
||||
w.mu.Lock()
|
||||
w.track.filamentUsed = totalMM
|
||||
w.mu.Unlock()
|
||||
}
|
||||
|
||||
func (w *MoonrakerPoller) ensurePrintJob(ctx context.Context, jobName, state string) (int, error) {
|
||||
var jobID int
|
||||
err := w.pool.QueryRow(ctx, `
|
||||
SELECT pj.id FROM print_jobs pj
|
||||
JOIN job_statuses js ON pj.job_status_id = js.id
|
||||
WHERE pj.printer_id = $1
|
||||
AND pj.job_name = $2
|
||||
AND pj.deleted_at IS NULL
|
||||
AND js.name IN ('printing', 'pending')
|
||||
ORDER BY pj.created_at DESC
|
||||
LIMIT 1
|
||||
`, w.printerID, jobName).Scan(&jobID)
|
||||
|
||||
if err == nil {
|
||||
_, _ = w.pool.Exec(ctx, `
|
||||
UPDATE print_jobs SET
|
||||
job_status_id = (SELECT id FROM job_statuses WHERE name = 'printing'),
|
||||
started_at = COALESCE(started_at, NOW()),
|
||||
updated_at = NOW()
|
||||
WHERE id = $1
|
||||
AND job_status_id = (SELECT id FROM job_statuses WHERE name = 'pending')
|
||||
`, jobID)
|
||||
return jobID, nil
|
||||
}
|
||||
|
||||
var statusID int
|
||||
err = w.pool.QueryRow(ctx, `SELECT id FROM job_statuses WHERE name = 'printing'`).Scan(&statusID)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("moonraker poller: missing 'printing' job status: %w", err)
|
||||
}
|
||||
|
||||
err = w.pool.QueryRow(ctx, `
|
||||
INSERT INTO print_jobs (printer_id, job_name, file_name, job_status_id, started_at)
|
||||
VALUES ($1, $2, $3, $4, NOW())
|
||||
RETURNING id
|
||||
`, w.printerID, jobName, jobName, statusID).Scan(&jobID)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("moonraker poller: failed to create print job: %w", err)
|
||||
}
|
||||
|
||||
slog.Info("moonraker poller: created print job", "job_id", jobID, "job_name", jobName)
|
||||
return jobID, nil
|
||||
}
|
||||
|
||||
// ── Package-level helpers (shared by both workers) ──────────────────────────
|
||||
|
||||
// lookupActiveSpool finds the most recently used spool for a given printer.
|
||||
func lookupActiveSpool(ctx context.Context, pool *pgxpool.Pool, printerID int) (int, float64) {
|
||||
type result struct {
|
||||
id int
|
||||
density float64
|
||||
}
|
||||
var res result
|
||||
|
||||
err := pool.QueryRow(ctx, `
|
||||
SELECT fs.id, COALESCE(mb.density_g_cm3, 1.24)
|
||||
FROM filament_spools fs
|
||||
JOIN material_bases mb ON fs.material_base_id = mb.id
|
||||
JOIN print_jobs pj ON pj.filament_spool_id = fs.id
|
||||
WHERE pj.printer_id = $1 AND fs.deleted_at IS NULL
|
||||
ORDER BY pj.created_at DESC LIMIT 1
|
||||
`, printerID).Scan(&res.id, &res.density)
|
||||
if err == nil {
|
||||
return res.id, res.density
|
||||
}
|
||||
|
||||
err = pool.QueryRow(ctx, `
|
||||
SELECT fs.id, COALESCE(mb.density_g_cm3, 1.24)
|
||||
FROM filament_spools fs
|
||||
JOIN material_bases mb ON fs.material_base_id = mb.id
|
||||
WHERE fs.deleted_at IS NULL
|
||||
ORDER BY fs.created_at DESC LIMIT 1
|
||||
`).Scan(&res.id, &res.density)
|
||||
if err == nil {
|
||||
return res.id, res.density
|
||||
}
|
||||
|
||||
return 1, 1.24
|
||||
}
|
||||
|
||||
// insertUsageLog inserts a usage_log entry and decrements the spool's remaining grams.
|
||||
func insertUsageLog(ctx context.Context, pool *pgxpool.Pool, jobID, spoolID int, deltaMM, densityGCm3 float64) error {
|
||||
const crossSectionCm2 = 0.02405 // π * (0.0875cm)² for 1.75mm filament
|
||||
gramsUsed := crossSectionCm2 * (deltaMM / 10.0) * densityGCm3
|
||||
|
||||
if gramsUsed <= 0 || deltaMM <= 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if _, err := pool.Exec(ctx, `
|
||||
INSERT INTO usage_logs (print_job_id, filament_spool_id, mm_extruded, grams_used, logged_at)
|
||||
VALUES ($1, $2, $3, $4, NOW())
|
||||
`, jobID, spoolID, deltaMM, gramsUsed); err != nil {
|
||||
return fmt.Errorf("usage_log insert failed: %w", err)
|
||||
}
|
||||
|
||||
_, _ = pool.Exec(ctx, `
|
||||
UPDATE filament_spools
|
||||
SET remaining_grams = GREATEST(remaining_grams - $2::int, 0),
|
||||
updated_at = NOW()
|
||||
WHERE id = $1
|
||||
`, spoolID, int(gramsUsed))
|
||||
|
||||
slog.Debug("moonraker poller: logged usage",
|
||||
"job_id", jobID, "spool_id", spoolID,
|
||||
"mm_extruded", deltaMM, "grams_used", gramsUsed,
|
||||
)
|
||||
return nil
|
||||
}
|
||||
@@ -1,170 +0,0 @@
|
||||
package workers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/CubeCraft-Creations/Extrudex/backend/internal/clients"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
// bambuJobState tracks the active print job detected via MQTT.
|
||||
type bambuJobState struct {
|
||||
gcodeFile string
|
||||
gcodeState string
|
||||
percent int
|
||||
}
|
||||
|
||||
// MQTTSubscriber listens to Bambu Lab MQTT telemetry topics and logs
|
||||
// filament usage events to PostgreSQL.
|
||||
type MQTTSubscriber struct {
|
||||
Client *clients.MQTTClient
|
||||
pool *pgxpool.Pool
|
||||
|
||||
printerID int
|
||||
printerName string
|
||||
|
||||
mu sync.Mutex
|
||||
state bambuJobState
|
||||
}
|
||||
|
||||
// MQTTSubscriberConfig holds configuration for the MQTT subscriber worker.
|
||||
type MQTTSubscriberConfig struct {
|
||||
Pool *pgxpool.Pool
|
||||
PrinterID int
|
||||
PrinterName string
|
||||
}
|
||||
|
||||
// NewMQTTSubscriber creates a new MQTTSubscriber worker. Set Client after
|
||||
// construction to wire the handler.
|
||||
func NewMQTTSubscriber(cfg MQTTSubscriberConfig) *MQTTSubscriber {
|
||||
return &MQTTSubscriber{
|
||||
pool: cfg.Pool,
|
||||
printerID: cfg.PrinterID,
|
||||
printerName: cfg.PrinterName,
|
||||
}
|
||||
}
|
||||
|
||||
// Run connects to MQTT and blocks until ctx is cancelled.
|
||||
func (w *MQTTSubscriber) Run(ctx context.Context) error {
|
||||
slog.Info("mqtt subscriber: starting",
|
||||
"printer_id", w.printerID,
|
||||
"printer_name", w.printerName,
|
||||
)
|
||||
|
||||
if w.Client == nil {
|
||||
return fmt.Errorf("mqtt subscriber: Client is nil")
|
||||
}
|
||||
|
||||
if err := w.Client.Connect(); err != nil {
|
||||
return fmt.Errorf("mqtt subscriber: connect failed: %w", err)
|
||||
}
|
||||
defer w.Client.Disconnect()
|
||||
|
||||
slog.Info("mqtt subscriber: connected", "printer_id", w.printerID)
|
||||
|
||||
<-ctx.Done()
|
||||
slog.Info("mqtt subscriber: shutting down", "printer_id", w.printerID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// HandleBambuReport is the MQTT callback for Bambu telemetry messages.
|
||||
func (w *MQTTSubscriber) HandleBambuReport(report clients.BambuPrintReport) error {
|
||||
w.mu.Lock()
|
||||
prev := w.state
|
||||
current := bambuJobState{
|
||||
gcodeFile: report.Print.GcodeFile,
|
||||
gcodeState: report.Print.GcodeState,
|
||||
percent: report.Print.McPercent,
|
||||
}
|
||||
w.state = current
|
||||
w.mu.Unlock()
|
||||
|
||||
if prev.gcodeState == current.gcodeState && prev.gcodeFile == current.gcodeFile {
|
||||
return nil
|
||||
}
|
||||
|
||||
slog.Info("mqtt subscriber: state change",
|
||||
"printer_id", w.printerID,
|
||||
"file", current.gcodeFile,
|
||||
"state", current.gcodeState,
|
||||
"percent", current.percent,
|
||||
)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
defer cancel()
|
||||
|
||||
switch current.gcodeState {
|
||||
case "RUNNING":
|
||||
return w.handleState(ctx, current, "printing")
|
||||
case "FINISH":
|
||||
return w.handleState(ctx, current, "completed")
|
||||
case "FAILED":
|
||||
return w.handleState(ctx, current, "failed")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *MQTTSubscriber) handleState(ctx context.Context, s bambuJobState, status string) error {
|
||||
jobID, err := w.ensurePrintJob(ctx, s.gcodeFile, status)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if status == "completed" || status == "failed" {
|
||||
_, _ = w.pool.Exec(ctx, `
|
||||
UPDATE print_jobs SET
|
||||
job_status_id = (SELECT id FROM job_statuses WHERE name = $2),
|
||||
completed_at = CASE WHEN $2 = 'completed' THEN NOW() ELSE completed_at END,
|
||||
updated_at = NOW()
|
||||
WHERE id = $1
|
||||
`, jobID, status)
|
||||
} else {
|
||||
_, _ = w.pool.Exec(ctx, `
|
||||
UPDATE print_jobs SET
|
||||
job_status_id = (SELECT id FROM job_statuses WHERE name = $2),
|
||||
started_at = COALESCE(started_at, NOW()),
|
||||
updated_at = NOW()
|
||||
WHERE id = $1
|
||||
`, jobID, status)
|
||||
}
|
||||
|
||||
slog.Info("mqtt subscriber: job updated",
|
||||
"printer_id", w.printerID, "job_id", jobID, "status", status)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *MQTTSubscriber) ensurePrintJob(ctx context.Context, filename, status string) (int, error) {
|
||||
var jobID int
|
||||
err := w.pool.QueryRow(ctx, `
|
||||
SELECT id FROM print_jobs
|
||||
WHERE printer_id = $1 AND file_name = $2 AND deleted_at IS NULL
|
||||
ORDER BY created_at DESC LIMIT 1
|
||||
`, w.printerID, filename).Scan(&jobID)
|
||||
|
||||
if err == nil {
|
||||
return jobID, nil
|
||||
}
|
||||
|
||||
var statusID int
|
||||
err = w.pool.QueryRow(ctx, `SELECT id FROM job_statuses WHERE name = $1`, status).Scan(&statusID)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("mqtt subscriber: unknown status '%s': %w", status, err)
|
||||
}
|
||||
|
||||
err = w.pool.QueryRow(ctx, `
|
||||
INSERT INTO print_jobs (printer_id, job_name, file_name, job_status_id, started_at)
|
||||
VALUES ($1, $2, $2, $3, NOW())
|
||||
RETURNING id
|
||||
`, w.printerID, filename, statusID).Scan(&jobID)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("mqtt subscriber: create print_job failed: %w", err)
|
||||
}
|
||||
|
||||
slog.Info("mqtt subscriber: created print job",
|
||||
"job_id", jobID, "file", filename, "status", status)
|
||||
return jobID, nil
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { BrowserRouter, Routes, Route } from 'react-router-dom'
|
||||
import Dashboard from './pages/Dashboard'
|
||||
import InventoryPage from './pages/InventoryPage'
|
||||
|
||||
const queryClient = new QueryClient()
|
||||
@@ -16,8 +15,7 @@ export default function App() {
|
||||
</header>
|
||||
<main className="p-4">
|
||||
<Routes>
|
||||
<Route path="/" element={<Dashboard />} />
|
||||
<Route path="/inventory" element={<InventoryPage />} />
|
||||
<Route path="/" element={<InventoryPage />} />
|
||||
</Routes>
|
||||
</main>
|
||||
</div>
|
||||
|
||||
360
frontend/src/components/FilamentForm.tsx
Normal file
360
frontend/src/components/FilamentForm.tsx
Normal file
@@ -0,0 +1,360 @@
|
||||
import { useState, useEffect, useMemo } from 'react'
|
||||
import { X, Save, AlertCircle } from 'lucide-react'
|
||||
import ColorSwatch from './ColorSwatch'
|
||||
import { createFilament, updateFilament, fetchMaterialBases, fetchMaterialFinishes, fetchMaterialModifiers } from '../services/filamentService'
|
||||
import type { FilamentSpool, MaterialBase, MaterialFinish, MaterialModifier } from '../types/filament'
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
|
||||
interface FilamentFormProps {
|
||||
mode: 'create' | 'edit'
|
||||
initialData?: FilamentSpool | null
|
||||
onClose: () => void
|
||||
onSuccess: () => void
|
||||
}
|
||||
|
||||
interface FormErrors {
|
||||
[key: string]: string
|
||||
}
|
||||
|
||||
export default function FilamentForm({ mode, initialData, onClose, onSuccess }: FilamentFormProps) {
|
||||
const [name, setName] = useState('')
|
||||
const [materialBaseId, setMaterialBaseId] = useState('')
|
||||
const [materialFinishId, setMaterialFinishId] = useState('1')
|
||||
const [materialModifierId, setMaterialModifierId] = useState('')
|
||||
const [colorHex, setColorHex] = useState('#3B82F6')
|
||||
const [brand, setBrand] = useState('')
|
||||
const [diameterMm, setDiameterMm] = useState('1.75')
|
||||
const [initialGrams, setInitialGrams] = useState('')
|
||||
const [remainingGrams, setRemainingGrams] = useState('')
|
||||
const [costUsd, setCostUsd] = useState('')
|
||||
const [lowStockThreshold, setLowStockThreshold] = useState('50')
|
||||
const [notes, setNotes] = useState('')
|
||||
const [barcode, setBarcode] = useState('')
|
||||
const [errors, setErrors] = useState<FormErrors>({})
|
||||
const [submitting, setSubmitting] = useState(false)
|
||||
const [submitError, setSubmitError] = useState<string | null>(null)
|
||||
|
||||
const { data: materials } = useQuery({
|
||||
queryKey: ['materials'],
|
||||
queryFn: fetchMaterialBases,
|
||||
staleTime: Infinity,
|
||||
})
|
||||
const { data: finishes } = useQuery({
|
||||
queryKey: ['finishes'],
|
||||
queryFn: fetchMaterialFinishes,
|
||||
staleTime: Infinity,
|
||||
})
|
||||
const { data: modifiers } = useQuery({
|
||||
queryKey: ['modifiers'],
|
||||
queryFn: fetchMaterialModifiers,
|
||||
staleTime: Infinity,
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
if (mode === 'edit' && initialData) {
|
||||
setName(initialData.name ?? '')
|
||||
setMaterialBaseId(String(initialData.material_base_id ?? ''))
|
||||
setMaterialFinishId(String(initialData.material_finish_id ?? '1'))
|
||||
setMaterialModifierId(initialData.material_modifier_id ? String(initialData.material_modifier_id) : '')
|
||||
setColorHex(initialData.color_hex ?? '#3B82F6')
|
||||
setBrand(initialData.brand ?? '')
|
||||
setDiameterMm(String(initialData.diameter_mm ?? 1.75))
|
||||
setInitialGrams(String(initialData.initial_grams ?? ''))
|
||||
setRemainingGrams(String(initialData.remaining_grams ?? ''))
|
||||
setCostUsd(initialData.cost_usd != null ? String(initialData.cost_usd) : '')
|
||||
setLowStockThreshold(String(initialData.low_stock_threshold_grams ?? 50))
|
||||
setNotes(initialData.notes ?? '')
|
||||
setBarcode(initialData.barcode ?? '')
|
||||
}
|
||||
}, [mode, initialData])
|
||||
|
||||
const colorHexValid = useMemo(() => {
|
||||
return /^#[0-9A-Fa-f]{6}$/.test(colorHex)
|
||||
}, [colorHex])
|
||||
|
||||
function validate(): FormErrors {
|
||||
const e: FormErrors = {}
|
||||
if (!name.trim()) e.name = 'Name is required'
|
||||
if (!materialBaseId) e.material_base_id = 'Material Base is required'
|
||||
if (!materialFinishId) e.material_finish_id = 'Material Finish is required'
|
||||
if (!colorHexValid) e.color_hex = 'Enter a valid hex color (e.g., #FF0000)'
|
||||
if (!initialGrams || Number(initialGrams) <= 0) e.initial_grams = 'Must be > 0'
|
||||
if (remainingGrams === '' || Number(remainingGrams) < 0) e.remaining_grams = 'Must be >= 0'
|
||||
if (Number(remainingGrams) > Number(initialGrams)) e.remaining_grams = 'Cannot exceed Initial Grams'
|
||||
if (costUsd && Number(costUsd) < 0) e.cost_usd = 'Must be >= 0'
|
||||
if (!diameterMm || Number(diameterMm) <= 0) e.diameter_mm = 'Must be > 0'
|
||||
if (!lowStockThreshold || Number(lowStockThreshold) < 0) e.low_stock_threshold_grams = 'Must be >= 0'
|
||||
return e
|
||||
}
|
||||
|
||||
async function handleSubmit(e: React.FormEvent) {
|
||||
e.preventDefault()
|
||||
setSubmitError(null)
|
||||
const validation = validate()
|
||||
if (Object.keys(validation).length > 0) {
|
||||
setErrors(validation)
|
||||
return
|
||||
}
|
||||
setErrors({})
|
||||
setSubmitting(true)
|
||||
|
||||
const payload: Record<string, unknown> = {
|
||||
name: name.trim(),
|
||||
material_base_id: Number(materialBaseId),
|
||||
material_finish_id: Number(materialFinishId),
|
||||
color_hex: colorHex,
|
||||
initial_grams: Number(initialGrams),
|
||||
remaining_grams: Number(remainingGrams),
|
||||
diameter_mm: Number(diameterMm),
|
||||
low_stock_threshold_grams: Number(lowStockThreshold),
|
||||
}
|
||||
|
||||
if (materialModifierId) payload.material_modifier_id = Number(materialModifierId)
|
||||
if (brand.trim()) payload.brand = brand.trim()
|
||||
if (costUsd) payload.cost_usd = Number(costUsd)
|
||||
if (notes.trim()) payload.notes = notes.trim()
|
||||
if (barcode.trim()) payload.barcode = barcode.trim()
|
||||
|
||||
try {
|
||||
if (mode === 'edit' && initialData) {
|
||||
await updateFilament(initialData.id, payload)
|
||||
} else {
|
||||
await createFilament(payload)
|
||||
}
|
||||
onSuccess()
|
||||
onClose()
|
||||
} catch (err: any) {
|
||||
setSubmitError(err?.response?.data?.error || 'Failed to save spool. Please try again.')
|
||||
} finally {
|
||||
setSubmitting(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="fixed inset-0 z-50 flex items-start justify-center sm:items-center bg-black/60 backdrop-blur-sm p-4 overflow-y-auto">
|
||||
<div className="w-full max-w-2xl rounded-xl bg-slate-800 border border-slate-700 shadow-2xl overflow-hidden my-4">
|
||||
{/* Header */}
|
||||
<div className="flex items-center justify-between px-6 py-4 border-b border-slate-700">
|
||||
<h3 className="text-lg font-semibold text-slate-100">
|
||||
{mode === 'edit' ? 'Edit Filament Spool' : 'Add Filament Spool'}
|
||||
</h3>
|
||||
<button
|
||||
onClick={onClose}
|
||||
className="p-1.5 rounded-lg text-slate-400 hover:text-slate-100 hover:bg-slate-700 transition-colors"
|
||||
type="button"
|
||||
aria-label="Close"
|
||||
>
|
||||
<X size={18} />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Error banner */}
|
||||
{submitError && (
|
||||
<div className="mx-6 mt-4 flex items-start gap-2 rounded-lg bg-red-900/30 border border-red-700 px-4 py-3 text-sm text-red-300">
|
||||
<AlertCircle size={16} className="shrink-0 mt-0.5" />
|
||||
<span>{submitError}</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Form */}
|
||||
<form onSubmit={handleSubmit} className="px-6 py-4 space-y-5">
|
||||
{/* Row 1: Name + Brand */}
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-slate-300 mb-1">Name <span className="text-red-400">*</span></label>
|
||||
<input
|
||||
type="text"
|
||||
value={name}
|
||||
onChange={e => setName(e.target.value)}
|
||||
className={`w-full rounded-lg bg-slate-900 border px-3 py-2.5 text-sm text-slate-100 focus:outline-none focus:ring-2 ${errors.name ? 'border-red-500 focus:ring-red-500' : 'border-slate-600 focus:ring-emerald-500'}`}
|
||||
placeholder="e.g. Sunlu PLA Silk Red"
|
||||
/>
|
||||
{errors.name && <p className="mt-1 text-xs text-red-400">{errors.name}</p>}
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-slate-300 mb-1">Brand</label>
|
||||
<input
|
||||
type="text"
|
||||
value={brand}
|
||||
onChange={e => setBrand(e.target.value)}
|
||||
className="w-full rounded-lg bg-slate-900 border border-slate-600 px-3 py-2.5 text-sm text-slate-100 focus:outline-none focus:ring-2 focus:ring-emerald-500"
|
||||
placeholder="e.g. Hatchbox"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Row 2: Material Base + Finish + Modifier */}
|
||||
<div className="grid grid-cols-1 sm:grid-cols-3 gap-4">
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-slate-300 mb-1">Material Base <span className="text-red-400">*</span></label>
|
||||
<select
|
||||
value={materialBaseId}
|
||||
onChange={e => setMaterialBaseId(e.target.value)}
|
||||
className={`w-full rounded-lg bg-slate-900 border px-3 py-2.5 text-sm text-slate-100 focus:outline-none focus:ring-2 ${errors.material_base_id ? 'border-red-500 focus:ring-red-500' : 'border-slate-600 focus:ring-emerald-500'}`}
|
||||
>
|
||||
<option value="">Select…</option>
|
||||
{materials?.map((m: MaterialBase) => (
|
||||
<option key={m.id} value={m.id}>{m.name}</option>
|
||||
))}
|
||||
</select>
|
||||
{errors.material_base_id && <p className="mt-1 text-xs text-red-400">{errors.material_base_id}</p>}
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-slate-300 mb-1">Finish <span className="text-red-400">*</span></label>
|
||||
<select
|
||||
value={materialFinishId}
|
||||
onChange={e => setMaterialFinishId(e.target.value)}
|
||||
className={`w-full rounded-lg bg-slate-900 border px-3 py-2.5 text-sm text-slate-100 focus:outline-none focus:ring-2 ${errors.material_finish_id ? 'border-red-500 focus:ring-red-500' : 'border-slate-600 focus:ring-emerald-500'}`}
|
||||
>
|
||||
{finishes?.map((f: MaterialFinish) => (
|
||||
<option key={f.id} value={f.id}>{f.name}</option>
|
||||
))}
|
||||
</select>
|
||||
{errors.material_finish_id && <p className="mt-1 text-xs text-red-400">{errors.material_finish_id}</p>}
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-slate-300 mb-1">Modifier</label>
|
||||
<select
|
||||
value={materialModifierId}
|
||||
onChange={e => setMaterialModifierId(e.target.value)}
|
||||
className="w-full rounded-lg bg-slate-900 border border-slate-600 px-3 py-2.5 text-sm text-slate-100 focus:outline-none focus:ring-2 focus:ring-emerald-500"
|
||||
>
|
||||
<option value="">None</option>
|
||||
{modifiers?.map((m: MaterialModifier) => (
|
||||
<option key={m.id} value={m.id}>{m.name}</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Row 3: Color + Diameter */}
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-slate-300 mb-1">Color <span className="text-red-400">*</span></label>
|
||||
<div className="flex items-center gap-3">
|
||||
<input
|
||||
type="color"
|
||||
value={colorHex}
|
||||
onChange={e => setColorHex(e.target.value)}
|
||||
className="h-10 w-14 rounded border border-slate-600 bg-slate-900 cursor-pointer"
|
||||
/>
|
||||
<div className="flex-1">
|
||||
<input
|
||||
type="text"
|
||||
value={colorHex}
|
||||
onChange={e => setColorHex(e.target.value)}
|
||||
className={`w-full rounded-lg bg-slate-900 border px-3 py-2.5 text-sm text-slate-100 font-mono uppercase focus:outline-none focus:ring-2 ${errors.color_hex ? 'border-red-500 focus:ring-red-500' : 'border-slate-600 focus:ring-emerald-500'}`}
|
||||
placeholder="#FF0000"
|
||||
maxLength={7}
|
||||
/>
|
||||
</div>
|
||||
<ColorSwatch colorHex={colorHex} size={32} />
|
||||
</div>
|
||||
{errors.color_hex && <p className="mt-1 text-xs text-red-400">{errors.color_hex}</p>}
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-slate-300 mb-1">Diameter (mm) <span className="text-red-400">*</span></label>
|
||||
<input
|
||||
type="number"
|
||||
step="0.01"
|
||||
value={diameterMm}
|
||||
onChange={e => setDiameterMm(e.target.value)}
|
||||
className={`w-full rounded-lg bg-slate-900 border px-3 py-2.5 text-sm text-slate-100 focus:outline-none focus:ring-2 ${errors.diameter_mm ? 'border-red-500 focus:ring-red-500' : 'border-slate-600 focus:ring-emerald-500'}`}
|
||||
/>
|
||||
{errors.diameter_mm && <p className="mt-1 text-xs text-red-400">{errors.diameter_mm}</p>}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Row 4: Grams */}
|
||||
<div className="grid grid-cols-1 sm:grid-cols-3 gap-4">
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-slate-300 mb-1">Initial Grams <span className="text-red-400">*</span></label>
|
||||
<input
|
||||
type="number"
|
||||
value={initialGrams}
|
||||
onChange={e => setInitialGrams(e.target.value)}
|
||||
className={`w-full rounded-lg bg-slate-900 border px-3 py-2.5 text-sm text-slate-100 focus:outline-none focus:ring-2 ${errors.initial_grams ? 'border-red-500 focus:ring-red-500' : 'border-slate-600 focus:ring-emerald-500'}`}
|
||||
/>
|
||||
{errors.initial_grams && <p className="mt-1 text-xs text-red-400">{errors.initial_grams}</p>}
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-slate-300 mb-1">Remaining Grams <span className="text-red-400">*</span></label>
|
||||
<input
|
||||
type="number"
|
||||
value={remainingGrams}
|
||||
onChange={e => setRemainingGrams(e.target.value)}
|
||||
className={`w-full rounded-lg bg-slate-900 border px-3 py-2.5 text-sm text-slate-100 focus:outline-none focus:ring-2 ${errors.remaining_grams ? 'border-red-500 focus:ring-red-500' : 'border-slate-600 focus:ring-emerald-500'}`}
|
||||
/>
|
||||
{errors.remaining_grams && <p className="mt-1 text-xs text-red-400">{errors.remaining_grams}</p>}
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-slate-300 mb-1">Cost (USD)</label>
|
||||
<input
|
||||
type="number"
|
||||
step="0.01"
|
||||
value={costUsd}
|
||||
onChange={e => setCostUsd(e.target.value)}
|
||||
className={`w-full rounded-lg bg-slate-900 border px-3 py-2.5 text-sm text-slate-100 focus:outline-none focus:ring-2 ${errors.cost_usd ? 'border-red-500 focus:ring-red-500' : 'border-slate-600 focus:ring-emerald-500'}`}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Row 5: Threshold + Barcode */}
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-slate-300 mb-1">Low Stock Threshold (g) <span className="text-red-400">*</span></label>
|
||||
<input
|
||||
type="number"
|
||||
value={lowStockThreshold}
|
||||
onChange={e => setLowStockThreshold(e.target.value)}
|
||||
className={`w-full rounded-lg bg-slate-900 border px-3 py-2.5 text-sm text-slate-100 focus:outline-none focus:ring-2 ${errors.low_stock_threshold_grams ? 'border-red-500 focus:ring-red-500' : 'border-slate-600 focus:ring-emerald-500'}`}
|
||||
/>
|
||||
{errors.low_stock_threshold_grams && <p className="mt-1 text-xs text-red-400">{errors.low_stock_threshold_grams}</p>}
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-slate-300 mb-1">Barcode</label>
|
||||
<input
|
||||
type="text"
|
||||
value={barcode}
|
||||
onChange={e => setBarcode(e.target.value)}
|
||||
className="w-full rounded-lg bg-slate-900 border border-slate-600 px-3 py-2.5 text-sm text-slate-100 focus:outline-none focus:ring-2 focus:ring-emerald-500"
|
||||
placeholder="e.g. 123456789012"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Notes */}
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-slate-300 mb-1">Notes</label>
|
||||
<textarea
|
||||
rows={3}
|
||||
value={notes}
|
||||
onChange={e => setNotes(e.target.value)}
|
||||
className="w-full rounded-lg bg-slate-900 border border-slate-600 px-3 py-2.5 text-sm text-slate-100 focus:outline-none focus:ring-2 focus:ring-emerald-500 resize-y"
|
||||
placeholder="Print temperature tips, storage notes, etc."
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Footer actions */}
|
||||
<div className="flex justify-end gap-3 pt-2">
|
||||
<button
|
||||
type="button"
|
||||
onClick={onClose}
|
||||
className="rounded-lg bg-slate-700 px-4 py-2.5 text-sm font-medium text-slate-200 hover:bg-slate-600 transition-colors"
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
<button
|
||||
type="submit"
|
||||
disabled={submitting}
|
||||
className="inline-flex items-center gap-2 rounded-lg bg-emerald-600 px-4 py-2.5 text-sm font-semibold text-white hover:bg-emerald-500 active:bg-emerald-700 disabled:opacity-50 transition-colors"
|
||||
>
|
||||
<Save size={16} />
|
||||
{submitting ? 'Saving…' : mode === 'edit' ? 'Update Spool' : 'Create Spool'}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
export default function LoadingSpinner() {
|
||||
return (
|
||||
<div className="flex flex-col items-center gap-3">
|
||||
<div className="w-10 h-10 border-4 border-emerald-500/30 border-t-emerald-400 rounded-full animate-spin" />
|
||||
<p className="text-slate-400 text-sm">Loading dashboard…</p>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
import type { PrintJob } from '../types'
|
||||
import { Clock, FileText } from 'lucide-react'
|
||||
|
||||
interface RecentPrintsProps {
|
||||
jobs: PrintJob[]
|
||||
}
|
||||
|
||||
export default function RecentPrints({ jobs }: RecentPrintsProps) {
|
||||
if (jobs.length === 0) {
|
||||
return (
|
||||
<div className="rounded-xl border border-slate-700 bg-slate-800 p-6 text-center text-slate-400">
|
||||
<FileText size={32} className="mx-auto mb-3 text-slate-500" />
|
||||
<p>No recent print jobs</p>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const statusColor = (status: string) => {
|
||||
switch (status.toLowerCase()) {
|
||||
case 'completed': return 'text-emerald-400 bg-emerald-500/10'
|
||||
case 'in_progress': return 'text-sky-400 bg-sky-500/10'
|
||||
case 'failed': return 'text-red-400 bg-red-500/10'
|
||||
default: return 'text-slate-400 bg-slate-500/10'
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="rounded-xl border border-slate-700 bg-slate-800 overflow-hidden">
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full text-left">
|
||||
<thead>
|
||||
<tr className="border-b border-slate-700">
|
||||
<th className="px-4 py-3 text-sm font-medium text-slate-400">Name</th>
|
||||
<th className="px-4 py-3 text-sm font-medium text-slate-400">Status</th>
|
||||
<th className="px-4 py-3 text-sm font-medium text-slate-400">Duration</th>
|
||||
<th className="px-4 py-3 text-sm font-medium text-slate-400">Filament</th>
|
||||
<th className="px-4 py-3 text-sm font-medium text-slate-400">Cost</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{jobs.map((job) => (
|
||||
<tr key={job.id} className="border-b border-slate-700/50 last:border-0 hover:bg-slate-700/30 transition-colors">
|
||||
<td className="px-4 py-3 text-sm text-slate-100 font-medium">{job.name}</td>
|
||||
<td className="px-4 py-3">
|
||||
<span className={`inline-flex items-center gap-1 px-2 py-1 rounded-full text-xs font-medium ${statusColor(job.status)}`}>
|
||||
{job.status}
|
||||
</span>
|
||||
</td>
|
||||
<td className="px-4 py-3 text-sm text-slate-300">
|
||||
<div className="flex items-center gap-1">
|
||||
<Clock size={14} />
|
||||
{formatDuration(job.duration_seconds)}
|
||||
</div>
|
||||
</td>
|
||||
<td className="px-4 py-3 text-sm text-slate-300">
|
||||
{job.filament_used_grams?.toFixed(1) ?? '-'} g
|
||||
</td>
|
||||
<td className="px-4 py-3 text-sm text-slate-300">
|
||||
${job.cost_usd?.toFixed(2) ?? '-'}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function formatDuration(seconds: number): string {
|
||||
if (!seconds) return '-'
|
||||
const hrs = Math.floor(seconds / 3600)
|
||||
const mins = Math.floor((seconds % 3600) / 60)
|
||||
if (hrs > 0) return `${hrs}h ${mins}m`
|
||||
return `${mins}m`
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
import type { LucideIcon } from 'lucide-react'
|
||||
|
||||
interface SummaryCardProps {
|
||||
title: string
|
||||
value: string | number
|
||||
icon: LucideIcon
|
||||
color: 'emerald' | 'amber' | 'sky' | 'violet'
|
||||
}
|
||||
|
||||
const colorMap = {
|
||||
emerald: { bg: 'bg-emerald-500/10', border: 'border-emerald-500/20', text: 'text-emerald-400', icon: 'text-emerald-400' },
|
||||
amber: { bg: 'bg-amber-500/10', border: 'border-amber-500/20', text: 'text-amber-400', icon: 'text-amber-400' },
|
||||
sky: { bg: 'bg-sky-500/10', border: 'border-sky-500/20', text: 'text-sky-400', icon: 'text-sky-400' },
|
||||
violet: { bg: 'bg-violet-500/10', border: 'border-violet-500/20', text: 'text-violet-400', icon: 'text-violet-400' },
|
||||
}
|
||||
|
||||
export default function SummaryCard({ title, value, icon: Icon, color }: SummaryCardProps) {
|
||||
const c = colorMap[color]
|
||||
|
||||
return (
|
||||
<div className={`rounded-xl border ${c.border} ${c.bg} p-5 flex items-start justify-between`}>
|
||||
<div>
|
||||
<p className="text-sm font-medium text-slate-400 mb-1">{title}</p>
|
||||
<p className={`text-3xl font-bold ${c.text}`}>{value}</p>
|
||||
</div>
|
||||
<div className={`p-2 rounded-lg ${c.bg}`}>
|
||||
<Icon size={24} className={c.icon} />
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -1,25 +1,10 @@
|
||||
import { StrictMode } from 'react'
|
||||
import { createRoot } from 'react-dom/client'
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { BrowserRouter } from 'react-router-dom'
|
||||
import './index.css'
|
||||
import App from './App'
|
||||
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: {
|
||||
staleTime: 30_000,
|
||||
refetchOnWindowFocus: false,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
createRoot(document.getElementById('root')!).render(
|
||||
<StrictMode>
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<BrowserRouter>
|
||||
<App />
|
||||
</BrowserRouter>
|
||||
</QueryClientProvider>
|
||||
<App />
|
||||
</StrictMode>,
|
||||
)
|
||||
|
||||
@@ -1,139 +0,0 @@
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { getFilaments, getPrintJobs } from '../services/api'
|
||||
import SummaryCard from '../components/SummaryCard'
|
||||
import RecentPrints from '../components/RecentPrints'
|
||||
import LoadingSpinner from '../components/LoadingSpinner'
|
||||
import { Package, AlertTriangle, Printer, DollarSign, Plus, List } from 'lucide-react'
|
||||
|
||||
export default function Dashboard() {
|
||||
const {
|
||||
data: filamentData,
|
||||
isLoading: filamentLoading,
|
||||
error: filamentError,
|
||||
} = useQuery({
|
||||
queryKey: ['filaments', 'count'],
|
||||
queryFn: () => getFilaments({ limit: 1 }),
|
||||
})
|
||||
|
||||
const {
|
||||
data: lowStockData,
|
||||
isLoading: lowStockLoading,
|
||||
error: lowStockError,
|
||||
} = useQuery({
|
||||
queryKey: ['filaments', 'lowStock'],
|
||||
queryFn: () => getFilaments({ low_stock: true, limit: 1 }),
|
||||
})
|
||||
|
||||
const {
|
||||
data: recentPrints,
|
||||
isLoading: printsLoading,
|
||||
error: printsError,
|
||||
} = useQuery({
|
||||
queryKey: ['printJobs', 'recent'],
|
||||
queryFn: () => getPrintJobs({ limit: 5 }),
|
||||
})
|
||||
|
||||
const {
|
||||
data: allPrints,
|
||||
isLoading: costLoading,
|
||||
error: costError,
|
||||
} = useQuery({
|
||||
queryKey: ['printJobs', 'all'],
|
||||
queryFn: () => getPrintJobs({ limit: 1000 }),
|
||||
})
|
||||
|
||||
const totalSpools = filamentData?.total ?? 0
|
||||
const lowStockCount = lowStockData?.total ?? 0
|
||||
const recentPrintJobs = recentPrints?.data ?? []
|
||||
|
||||
// Calculate cost this month from all print jobs
|
||||
const now = new Date()
|
||||
const currentMonth = now.getMonth()
|
||||
const currentYear = now.getFullYear()
|
||||
const monthlyCost =
|
||||
allPrints?.data
|
||||
.filter((job) => {
|
||||
const d = new Date(job.started_at)
|
||||
return d.getMonth() === currentMonth && d.getFullYear() === currentYear
|
||||
})
|
||||
.reduce((sum, job) => sum + (job.cost_usd ?? 0), 0) ?? 0
|
||||
|
||||
const isLoading = filamentLoading || lowStockLoading || printsLoading || costLoading
|
||||
const error = filamentError || lowStockError || printsError || costError
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className="min-h-screen flex items-center justify-center bg-slate-900">
|
||||
<LoadingSpinner />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
if (error) {
|
||||
return (
|
||||
<div className="min-h-screen flex items-center justify-center bg-slate-900">
|
||||
<div className="p-6 rounded-lg bg-red-900/30 border border-red-500 text-red-200 max-w-md">
|
||||
<h2 className="text-xl font-bold mb-2">Error</h2>
|
||||
<p className="text-sm">{(error as Error).message || 'Failed to load dashboard data.'}</p>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-slate-900 p-4 md:p-8">
|
||||
<div className="max-w-6xl mx-auto">
|
||||
<header className="mb-8">
|
||||
<h1 className="text-3xl font-bold text-emerald-400 mb-2">Dashboard</h1>
|
||||
<p className="text-slate-400">Overview of your Extrudex inventory and prints</p>
|
||||
</header>
|
||||
|
||||
{/* Summary Cards Grid */}
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4 mb-8">
|
||||
<SummaryCard
|
||||
title="Total Spools"
|
||||
value={totalSpools}
|
||||
icon={Package}
|
||||
color="emerald"
|
||||
/>
|
||||
<SummaryCard
|
||||
title="Low Stock"
|
||||
value={lowStockCount}
|
||||
icon={AlertTriangle}
|
||||
color={lowStockCount > 0 ? 'amber' : 'emerald'}
|
||||
/>
|
||||
<SummaryCard
|
||||
title="Recent Prints"
|
||||
value={recentPrintJobs.length}
|
||||
icon={Printer}
|
||||
color="sky"
|
||||
/>
|
||||
<SummaryCard
|
||||
title="Cost This Month"
|
||||
value={`$${monthlyCost.toFixed(2)}`}
|
||||
icon={DollarSign}
|
||||
color="violet"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Quick Actions */}
|
||||
<div className="flex flex-wrap gap-3 mb-8">
|
||||
<button className="flex items-center gap-2 px-4 py-2 rounded-lg bg-emerald-600 hover:bg-emerald-500 text-white font-medium transition-colors active:scale-95 touch-manipulation">
|
||||
<Plus size={18} />
|
||||
Add Spool
|
||||
</button>
|
||||
<button className="flex items-center gap-2 px-4 py-2 rounded-lg bg-slate-700 hover:bg-slate-600 text-slate-100 font-medium transition-colors active:scale-95 touch-manipulation">
|
||||
<List size={18} />
|
||||
View Inventory
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Recent Prints Section */}
|
||||
<section>
|
||||
<h2 className="text-xl font-semibold text-slate-100 mb-4">Recent Prints</h2>
|
||||
<RecentPrints jobs={recentPrintJobs} />
|
||||
</section>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import { useState, useMemo } from 'react'
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { Search, Filter, ChevronLeft, ChevronRight, Trash2, Pencil, Plus, AlertTriangle } from 'lucide-react'
|
||||
import ColorSwatch from '../components/ColorSwatch'
|
||||
import FilamentForm from '../components/FilamentForm'
|
||||
import { fetchFilaments, deleteFilament } from '../services/filamentService'
|
||||
import type { FilamentSpool, FilamentFilter } from '../types/filament'
|
||||
|
||||
@@ -19,6 +20,9 @@ export default function InventoryPage() {
|
||||
const [sortDir, setSortDir] = useState<SortDir>('asc')
|
||||
const [page, setPage] = useState(0)
|
||||
const [deleteId, setDeleteId] = useState<number | null>(null)
|
||||
const [formOpen, setFormOpen] = useState(false)
|
||||
const [formMode, setFormMode] = useState<'create' | 'edit'>('create')
|
||||
const [formInitial, setFormInitial] = useState<FilamentSpool | null>(null)
|
||||
|
||||
const filter: FilamentFilter = useMemo(() => ({
|
||||
material: material || undefined,
|
||||
@@ -79,7 +83,10 @@ export default function InventoryPage() {
|
||||
<h2 className="text-xl font-bold text-slate-100">Filament Inventory</h2>
|
||||
<p className="text-sm text-slate-400">{total} spool(s) total</p>
|
||||
</div>
|
||||
<button className="inline-flex items-center gap-2 rounded-lg bg-emerald-600 px-4 py-2 text-sm font-semibold text-white hover:bg-emerald-500 active:bg-emerald-700 transition-colors">
|
||||
<button
|
||||
onClick={() => { setFormMode('create'); setFormInitial(null); setFormOpen(true) }}
|
||||
className="inline-flex items-center gap-2 rounded-lg bg-emerald-600 px-4 py-2 text-sm font-semibold text-white hover:bg-emerald-500 active:bg-emerald-700 transition-colors"
|
||||
>
|
||||
<Plus size={16} /> Add Spool
|
||||
</button>
|
||||
</div>
|
||||
@@ -208,7 +215,11 @@ export default function InventoryPage() {
|
||||
</td>
|
||||
<td className="px-4 py-3 text-right">
|
||||
<div className="flex items-center justify-end gap-2">
|
||||
<button className="p-1.5 rounded hover:bg-slate-600 text-slate-400 hover:text-blue-400 transition-colors" title="Edit">
|
||||
<button
|
||||
onClick={() => { setFormMode('edit'); setFormInitial(spool); setFormOpen(true) }}
|
||||
className="p-1.5 rounded hover:bg-slate-600 text-slate-400 hover:text-blue-400 transition-colors"
|
||||
title="Edit"
|
||||
>
|
||||
<Pencil size={14} />
|
||||
</button>
|
||||
<button
|
||||
@@ -263,7 +274,10 @@ export default function InventoryPage() {
|
||||
</div>
|
||||
|
||||
<div className="flex items-center justify-end gap-2 pt-1">
|
||||
<button className="flex items-center gap-1 rounded-md bg-slate-700 px-3 py-1.5 text-xs font-medium text-slate-200 hover:bg-slate-600">
|
||||
<button
|
||||
onClick={() => { setFormMode('edit'); setFormInitial(spool); setFormOpen(true) }}
|
||||
className="flex items-center gap-1 rounded-md bg-slate-700 px-3 py-1.5 text-xs font-medium text-slate-200 hover:bg-slate-600"
|
||||
>
|
||||
<Pencil size={12} /> Edit
|
||||
</button>
|
||||
<button
|
||||
@@ -304,6 +318,16 @@ export default function InventoryPage() {
|
||||
</>
|
||||
)}
|
||||
|
||||
{/* Add/Edit Form Modal */}
|
||||
{formOpen && (
|
||||
<FilamentForm
|
||||
mode={formMode}
|
||||
initialData={formInitial}
|
||||
onClose={() => setFormOpen(false)}
|
||||
onSuccess={() => refetch()}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* Delete confirmation modal */}
|
||||
{deleteId !== null && (
|
||||
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-blur-sm p-4">
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
import axios from 'axios'
|
||||
import type { ListResponse, FilamentSpool, PrintJob } from '../types'
|
||||
|
||||
const api = axios.create({
|
||||
baseURL: '/api',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
})
|
||||
|
||||
export async function getFilaments(params?: { low_stock?: boolean; limit?: number; offset?: number }) {
|
||||
const res = await api.get<ListResponse<FilamentSpool>>('/filaments', { params })
|
||||
return res.data
|
||||
}
|
||||
|
||||
export async function getPrintJobs(params?: { limit?: number; offset?: number }) {
|
||||
const res = await api.get<ListResponse<PrintJob>>('/print-jobs', { params })
|
||||
return res.data
|
||||
}
|
||||
|
||||
export default api
|
||||
@@ -1,5 +1,5 @@
|
||||
import axios from 'axios'
|
||||
import type { FilamentSpool, ListResponse, FilamentFilter } from '../types/filament'
|
||||
import type { FilamentSpool, ListResponse, FilamentFilter, MaterialBase, MaterialFinish, MaterialModifier } from '../types/filament'
|
||||
|
||||
const API_BASE = '/api'
|
||||
|
||||
@@ -19,6 +19,31 @@ export async function fetchFilaments(filter: FilamentFilter): Promise<ListRespon
|
||||
return res.data
|
||||
}
|
||||
|
||||
export async function createFilament(payload: Partial<FilamentSpool>): Promise<FilamentSpool> {
|
||||
const res = await axios.post<{ data: FilamentSpool }>(`${API_BASE}/filaments`, payload)
|
||||
return res.data.data
|
||||
}
|
||||
|
||||
export async function updateFilament(id: number, payload: Partial<FilamentSpool>): Promise<FilamentSpool> {
|
||||
const res = await axios.put<{ data: FilamentSpool }>(`${API_BASE}/filaments/${id}`, payload)
|
||||
return res.data.data
|
||||
}
|
||||
|
||||
export async function deleteFilament(id: number): Promise<void> {
|
||||
await axios.delete(`${API_BASE}/filaments/${id}`)
|
||||
}
|
||||
|
||||
export async function fetchMaterialBases(): Promise<MaterialBase[]> {
|
||||
const res = await axios.get<{ data: MaterialBase[] }>(`${API_BASE}/materials`)
|
||||
return res.data.data
|
||||
}
|
||||
|
||||
export async function fetchMaterialFinishes(): Promise<MaterialFinish[]> {
|
||||
const res = await axios.get<{ data: MaterialFinish[] }>(`${API_BASE}/finishes`)
|
||||
return res.data.data
|
||||
}
|
||||
|
||||
export async function fetchMaterialModifiers(): Promise<MaterialModifier[]> {
|
||||
const res = await axios.get<{ data: MaterialModifier[] }>(`${API_BASE}/modifiers`)
|
||||
return res.data.data
|
||||
}
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
export interface FilamentSpool {
|
||||
id: number;
|
||||
name: string;
|
||||
material_base_id: number;
|
||||
material_finish_id: number;
|
||||
material_modifier_id: number;
|
||||
color_hex: string;
|
||||
brand: string;
|
||||
diameter_mm: number;
|
||||
initial_grams: number;
|
||||
remaining_grams: number;
|
||||
spool_weight_grams: number;
|
||||
cost_usd: number;
|
||||
low_stock_threshold_grams: number;
|
||||
notes: string;
|
||||
barcode: string;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
deleted_at: string | null;
|
||||
}
|
||||
|
||||
export interface PrintJob {
|
||||
id: number;
|
||||
name: string;
|
||||
printer_id: number;
|
||||
filament_id: number;
|
||||
status: string;
|
||||
duration_seconds: number;
|
||||
filament_used_grams: number;
|
||||
cost_usd: number;
|
||||
started_at: string;
|
||||
completed_at: string | null;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
export interface ListResponse<T> {
|
||||
data: T[];
|
||||
total: number;
|
||||
limit: number;
|
||||
offset: number;
|
||||
}
|
||||
@@ -15,7 +15,6 @@
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"ignoreDeprecations": "5.0",
|
||||
"baseUrl": ".",
|
||||
"paths": {
|
||||
"@/*": ["src/*"]
|
||||
|
||||
Reference in New Issue
Block a user