CUB-10: Create IMoonrakerClient interface and DTOs
Some checks failed
Dev Build / build-test (pull_request) Failing after 58s
Dev Build / deploy-dev (pull_request) Has been skipped
Dev Build / notify-success (pull_request) Has been skipped
Dev Build / notify-failure (pull_request) Successful in 6s

- Expanded IMoonrakerClient interface with 6 strongly-typed methods:
  - GetServerInfoAsync (Moonraker /server/info)
  - IsReachableAsync (connectivity check)
  - GetPrinterInfoAsync (Moonraker /printer/info)
  - GetPrintHistoryAsync (Moonraker /server/history/items)
  - GetPrintStatsAsync (Moonraker /printer/objects/query?print_stats)
  - GetDisplayStatusAsync (Moonraker /printer/objects/query?display_status)
  - GetFilamentUsageAsync (retained for backward compatibility)

- Created Domain/DTOs/Moonraker/ with 7 DTOs:
  - MoonrakerServerInfo, MoonrakerPrinterInfo, MoonrakerPrintJob
  - MoonrakerHistoryResponse, MoonrakerPrintStats
  - MoonrakerDisplayStatus, MoonrakerRequest

- Updated MoonrakerClient implementation to support all new methods
  with proper JSON parsing and mapping helpers

- Full XML doc comments on all public members
This commit is contained in:
2026-04-27 18:42:47 -04:00
parent aa182af979
commit 51bfb6d115
9 changed files with 685 additions and 50 deletions

View 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;
}

View 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; }
}

View 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();
}

View 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; }
}

View 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; }
}

View 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; }
}

View 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; } = [];
}

View File

@@ -1,23 +1,26 @@
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).
/// Used to retrieve filament usage data, print job status, and
/// remaining spool weight from the printer.
/// Provides strongly-typed methods for server discovery, printer status,
/// print job history, and real-time telemetry.
/// </summary>
public interface IMoonrakerClient
{
/// <summary>
/// Fetches the current filament usage data from the Moonraker server.
/// Returns a dictionary of usage metrics reported by the printer.
/// 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>A dictionary of usage metric names to their decimal values.</returns>
Task<Dictionary<string, decimal>> GetFilamentUsageAsync(
/// <returns>Server info if reachable; <c>null</c> if unreachable.</returns>
Task<MoonrakerServerInfo?> GetServerInfoAsync(
string hostnameOrIp,
int port,
string? apiKey,
@@ -25,6 +28,8 @@ public interface IMoonrakerClient
/// <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>
@@ -36,4 +41,91 @@ public interface IMoonrakerClient
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);
}

View File

@@ -1,15 +1,16 @@
using System.Net.Http.Json;
using System.Text.Json;
using Extrudex.Domain.DTOs.Moonraker;
using Extrudex.Domain.Interfaces;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Extrudex.Infrastructure.Configuration;
/// <summary>
/// HTTP client for communicating with Moonraker REST API endpoints
/// on Klipper-based printers (e.g., Elegoo Centauri Carbon).
/// Retrieves filament usage data and printer status information.
/// Provides strongly-typed methods for server discovery, printer status,
/// print job history, and real-time telemetry.
/// </summary>
public class MoonrakerClient : IMoonrakerClient
{
@@ -28,69 +29,65 @@ public class MoonrakerClient : IMoonrakerClient
}
/// <inheritdoc />
public async Task<Dictionary<string, decimal>> GetFilamentUsageAsync(
public async Task<MoonrakerServerInfo?> GetServerInfoAsync(
string hostnameOrIp,
int port,
string? apiKey,
CancellationToken cancellationToken = default)
{
var baseUrl = BuildBaseUrl(hostnameOrIp, port);
var result = new Dictionary<string, decimal>();
try
{
// Query Moonraker server info endpoint for filament usage data
using var request = new HttpRequestMessage(HttpMethod.Get, $"{baseUrl}/server/history/items?limit=1");
if (!string.IsNullOrEmpty(apiKey))
{
request.Headers.Add("X-Api-Key", apiKey);
}
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);
// Extract filament usage from the response
// Moonraker returns job history with filament_used and similar fields
if (json.TryGetProperty("result", out var resultElement))
var serverInfo = new MoonrakerServerInfo();
if (json.TryGetProperty("result", out var result))
{
if (resultElement.TryGetProperty("items", out var items) && items.GetArrayLength() > 0)
{
var job = items[0];
// Moonraker tracks filament_used in millimeters
if (job.TryGetProperty("filament_used", out var filamentUsed))
{
result["mm_extruded"] = filamentUsed.GetDecimal();
}
// Total duration in seconds
if (job.TryGetProperty("print_duration", out var duration))
{
result["print_duration_seconds"] = duration.GetDecimal();
}
}
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 filament usage from Moonraker at {Host}:{Port}: {MetricCount} metrics",
hostnameOrIp, port, result.Count);
"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 filament usage from Moonraker at {Host}:{Port}",
"Failed to retrieve server info from Moonraker at {Host}:{Port}",
hostnameOrIp, port);
return null;
}
catch (JsonException ex)
{
_logger.LogWarning(ex,
"Failed to parse Moonraker response from {Host}:{Port}",
"Failed to parse Moonraker server info response from {Host}:{Port}",
hostnameOrIp, port);
return null;
}
return result;
}
/// <inheritdoc />
@@ -99,25 +96,258 @@ public class MoonrakerClient : IMoonrakerClient
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 = new HttpRequestMessage(HttpMethod.Get, $"{baseUrl}/server/info");
if (!string.IsNullOrEmpty(apiKey))
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))
{
request.Headers.Add("X-Api-Key", apiKey);
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();
}
using var response = await _httpClient.SendAsync(request, cancellationToken);
return response.IsSuccessStatusCode;
_logger.LogDebug(
"Retrieved printer info from Moonraker at {Host}:{Port} — state: {State}",
hostnameOrIp, port, printerInfo.State);
return printerInfo;
}
catch (HttpRequestException)
catch (HttpRequestException ex)
{
_logger.LogDebug("Moonraker at {Host}:{Port} is not reachable", hostnameOrIp, port);
return false;
_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>
@@ -127,4 +357,91 @@ public class MoonrakerClient : IMoonrakerClient
{
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;
}
}