Merge remote-tracking branch 'origin/dev' into agent/dex/CUB-33-moonraker-usage-polling
Some checks failed
Dev Build / build-test (pull_request) Failing after 1m5s
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 3s

# Conflicts:
#	backend/Domain/Interfaces/IMoonrakerClient.cs
#	backend/Infrastructure/Services/MoonrakerClient.cs
This commit is contained in:
2026-04-27 20:22:36 -04:00
16 changed files with 1001 additions and 260 deletions

View File

@@ -1,20 +1,16 @@
using System.Globalization;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text.Json;
using Extrudex.Domain.DTOs.Moonraker;
using Extrudex.Domain.Interfaces;
using Microsoft.Extensions.Logging;
namespace Extrudex.Infrastructure.Services;
namespace Extrudex.Infrastructure.Configuration;
/// <summary>
/// HTTP client for communicating with the Moonraker REST API on
/// Klipper-based printers (e.g., Elegoo Centauri Carbon).
///
/// Moonraker endpoints used:
/// - GET /api/objects?print_stats — current print stats including filament used
/// - GET /api/objects?history — job history with filament usage per job
///
/// Authentication: optional X-Api-Key header when API key is configured.
/// HTTP client for communicating with Moonraker REST API endpoints
/// on Klipper-based printers (e.g., Elegoo Centauri Carbon).
/// Provides strongly-typed methods for server discovery, printer status,
/// print job history, and real-time telemetry.
/// </summary>
public class MoonrakerClient : IMoonrakerClient
{
@@ -22,9 +18,9 @@ public class MoonrakerClient : IMoonrakerClient
private readonly ILogger<MoonrakerClient> _logger;
/// <summary>
/// Creates a new MoonrakerClient with the specified HTTP client and logger.
/// Creates a new MoonrakerClient with the configured HTTP client and logger.
/// </summary>
/// <param name="httpClient">The HTTP client used for API calls.</param>
/// <param name="httpClient">The HTTP client for making requests to Moonraker endpoints.</param>
/// <param name="logger">Logger for diagnostic output.</param>
public MoonrakerClient(HttpClient httpClient, ILogger<MoonrakerClient> logger)
{
@@ -33,271 +29,419 @@ public class MoonrakerClient : IMoonrakerClient
}
/// <inheritdoc />
public async Task<string> GetPrinterStatusAsync(
public async Task<MoonrakerServerInfo?> GetServerInfoAsync(
string hostnameOrIp,
int port,
string? apiKey = null,
string? apiKey,
CancellationToken cancellationToken = default)
{
var baseUrl = BuildBaseUrl(hostnameOrIp, port);
var requestUrl = $"{baseUrl}/api/objects?print_stats";
using var request = new HttpRequestMessage(HttpMethod.Get, requestUrl);
ApplyApiKey(request, apiKey);
try
{
using var request = CreateRequest(HttpMethod.Get, $"{baseUrl}/server/info", apiKey);
using var response = await _httpClient.SendAsync(request, cancellationToken);
response.EnsureSuccessStatusCode();
var json = await response.Content.ReadAsStringAsync(cancellationToken);
using var doc = JsonDocument.Parse(json);
var json = await response.Content.ReadFromJsonAsync<JsonElement>(cancellationToken: cancellationToken);
// Moonraker returns: { "result": { "print_stats": { "state": "idle", ... } } }
var state = doc.RootElement
.GetProperty("result")
.GetProperty("print_stats")
.GetProperty("state")
.GetString();
var serverInfo = new MoonrakerServerInfo();
return state ?? "unknown";
if (json.TryGetProperty("result", out var result))
{
if (result.TryGetProperty("hostname", out var hostname))
serverInfo.Hostname = hostname.GetString() ?? string.Empty;
if (result.TryGetProperty("software_version", out var version))
serverInfo.SoftwareVersion = version.GetString() ?? string.Empty;
if (result.TryGetProperty("cpu_info", out var cpuInfo))
serverInfo.CpuInfo = cpuInfo.GetString() ?? string.Empty;
if (result.TryGetProperty("klippy_connected", out var klippyConnected))
serverInfo.KlippyConnected = klippyConnected.GetBoolean();
if (result.TryGetProperty("klippy_state", out var klippyState))
serverInfo.KlippyState = klippyState.GetString() ?? string.Empty;
if (result.TryGetProperty("api_key_required", out var apiKeyRequired))
serverInfo.ApiKeyRequired = apiKeyRequired.GetBoolean();
if (result.TryGetProperty("plugins", out var plugins))
serverInfo.Plugins = plugins.EnumerateArray()
.Select(p => p.GetString() ?? string.Empty)
.Where(s => !string.IsNullOrEmpty(s))
.ToList();
}
_logger.LogDebug(
"Retrieved server info from Moonraker at {Host}:{Port} — version {Version}, klippy {State}",
hostnameOrIp, port, serverInfo.SoftwareVersion, serverInfo.KlippyState);
return serverInfo;
}
catch (HttpRequestException ex)
{
_logger.LogWarning(ex,
"Moonraker printer status request failed for {Host}:{Port}",
"Failed to retrieve server info from Moonraker at {Host}:{Port}",
hostnameOrIp, port);
return "offline";
return null;
}
catch (JsonException ex)
{
_logger.LogWarning(ex,
"Malformed Moonraker response from {Host}:{Port}",
"Failed to parse Moonraker server info response from {Host}:{Port}",
hostnameOrIp, port);
return "error";
return null;
}
}
/// <inheritdoc />
public async Task<MoonrakerFilamentUsage?> GetFilamentUsageAsync(
public async Task<bool> IsReachableAsync(
string hostnameOrIp,
int port,
string? apiKey = null,
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);
// Fetch current print_stats (has live filament_used for active/recent job)
PrintStatsResult? printStats = null;
try
{
printStats = await FetchPrintStatsAsync(baseUrl, apiKey, cancellationToken);
}
catch (HttpRequestException ex)
{
_logger.LogWarning(ex,
"Moonraker print_stats request failed for {Host}:{Port}",
hostnameOrIp, port);
return null;
}
catch (JsonException ex)
{
_logger.LogWarning(ex,
"Malformed Moonraker print_stats response from {Host}:{Port}",
hostnameOrIp, port);
return null;
}
using var request = CreateRequest(HttpMethod.Get, $"{baseUrl}/printer/info", apiKey);
using var response = await _httpClient.SendAsync(request, cancellationToken);
response.EnsureSuccessStatusCode();
if (printStats is null)
return null;
var json = await response.Content.ReadFromJsonAsync<JsonElement>(cancellationToken: cancellationToken);
// Attempt to enrich with history data for timing info
HistoryResult? history = null;
try
{
history = await FetchHistoryAsync(baseUrl, apiKey, cancellationToken);
}
catch (HttpRequestException ex)
{
_logger.LogWarning(ex,
"Moonraker history request failed for {Host}:{Port}",
hostnameOrIp, port);
}
catch (JsonException ex)
{
_logger.LogWarning(ex,
"Malformed Moonraker history response from {Host}:{Port}",
hostnameOrIp, port);
}
var printerInfo = new MoonrakerPrinterInfo();
DateTime? startedAt = null;
DateTime? completedAt = null;
double? printDurationSeconds = null;
if (history is not null)
{
startedAt = history.StartTime;
completedAt = history.EndTime;
printDurationSeconds = history.PrintDuration;
}
return new MoonrakerFilamentUsage
{
MmExtruded = printStats.FilamentUsedMm ?? 0m,
GcodeFileName = printStats.FileName,
PrintState = printStats.State ?? "unknown",
PrintDurationSeconds = printDurationSeconds,
StartedAt = startedAt,
CompletedAt = completedAt
};
}
/// <summary>
/// Fetches and parses print_stats from the Moonraker API.
/// </summary>
private async Task<PrintStatsResult?> FetchPrintStatsAsync(
string baseUrl,
string? apiKey,
CancellationToken cancellationToken)
{
var requestUrl = $"{baseUrl}/api/objects?print_stats";
using var request = new HttpRequestMessage(HttpMethod.Get, requestUrl);
ApplyApiKey(request, apiKey);
using var response = await _httpClient.SendAsync(request, cancellationToken);
response.EnsureSuccessStatusCode();
var json = await response.Content.ReadAsStringAsync(cancellationToken);
using var doc = JsonDocument.Parse(json);
if (!doc.RootElement.TryGetProperty("result", out var result) ||
!result.TryGetProperty("print_stats", out var stats))
{
_logger.LogWarning("Moonraker response missing 'print_stats' object");
return null;
}
return new PrintStatsResult
{
State = stats.TryGetProperty("state", out var stateEl) ? stateEl.GetString() : null,
FilamentUsedMm = stats.TryGetProperty("filament_used", out var filamentEl) &&
filamentEl.ValueKind == JsonValueKind.Number
? filamentEl.GetDecimal() : (decimal?)null,
FileName = stats.TryGetProperty("filename", out var fileEl) ? fileEl.GetString() : null
};
}
/// <summary>
/// Fetches and parses history (last job) from the Moonraker API.
/// </summary>
private async Task<HistoryResult?> FetchHistoryAsync(
string baseUrl,
string? apiKey,
CancellationToken cancellationToken)
{
var requestUrl = $"{baseUrl}/api/objects?history";
using var request = new HttpRequestMessage(HttpMethod.Get, requestUrl);
ApplyApiKey(request, apiKey);
using var response = await _httpClient.SendAsync(request, cancellationToken);
response.EnsureSuccessStatusCode();
var json = await response.Content.ReadAsStringAsync(cancellationToken);
using var doc = JsonDocument.Parse(json);
if (!doc.RootElement.TryGetProperty("result", out var result) ||
!result.TryGetProperty("history", out var history))
{
_logger.LogWarning("Moonraker response missing 'history' object");
return null;
}
// Try last_job first, then job
JsonElement jobEl;
if (!history.TryGetProperty("last_job", out jobEl) &&
!history.TryGetProperty("job", out jobEl))
{
_logger.LogDebug("Moonraker history has no 'last_job' or 'job' entry");
return null;
}
return new HistoryResult
{
StartTime = ParseDateTimeProperty(jobEl, "start_time"),
EndTime = ParseDateTimeProperty(jobEl, "end_time"),
PrintDuration = jobEl.TryGetProperty("print_duration", out var durEl) &&
durEl.ValueKind == JsonValueKind.Number
? durEl.GetDouble() : (double?)null
};
}
/// <summary>
/// Parses a Moonraker timestamp property (Unix epoch seconds or ISO 8601 string).
/// </summary>
private static DateTime? ParseDateTimeProperty(JsonElement element, string propertyName)
{
if (!element.TryGetProperty(propertyName, out var prop))
return null;
// Moonraker returns Unix epoch seconds as a number
if (prop.ValueKind == JsonValueKind.Number)
{
var epochSeconds = prop.GetDouble();
return DateTime.UnixEpoch.AddSeconds(epochSeconds);
}
// Fallback: try parsing as ISO 8601 string
if (prop.ValueKind == JsonValueKind.String)
{
var str = prop.GetString();
if (str is not null &&
DateTime.TryParse(str, CultureInfo.InvariantCulture,
DateTimeStyles.AssumeUniversal, out var dt))
if (json.TryGetProperty("result", out var result))
{
return dt.ToUniversalTime();
if (result.TryGetProperty("state", out var state))
printerInfo.State = state.GetString() ?? string.Empty;
if (result.TryGetProperty("state_message", out var stateMessage))
printerInfo.StateMessage = stateMessage.GetString() ?? string.Empty;
if (result.TryGetProperty("klippy_ready", out var klippyReady))
printerInfo.KlippyReady = klippyReady.GetBoolean();
}
_logger.LogDebug(
"Retrieved printer info from Moonraker at {Host}:{Port} — state: {State}",
hostnameOrIp, port, printerInfo.State);
return printerInfo;
}
catch (HttpRequestException ex)
{
_logger.LogWarning(ex,
"Failed to retrieve printer info from Moonraker at {Host}:{Port}",
hostnameOrIp, port);
return null;
}
catch (JsonException ex)
{
_logger.LogWarning(ex,
"Failed to parse Moonraker printer info response from {Host}:{Port}",
hostnameOrIp, port);
return null;
}
}
/// <inheritdoc />
public async Task<MoonrakerHistoryResponse> GetPrintHistoryAsync(
string hostnameOrIp,
int port,
string? apiKey,
int limit = 50,
CancellationToken cancellationToken = default)
{
var baseUrl = BuildBaseUrl(hostnameOrIp, port);
var historyResponse = new MoonrakerHistoryResponse();
try
{
using var request = CreateRequest(
HttpMethod.Get,
$"{baseUrl}/server/history/items?limit={limit}",
apiKey);
using var response = await _httpClient.SendAsync(request, cancellationToken);
response.EnsureSuccessStatusCode();
var json = await response.Content.ReadFromJsonAsync<JsonElement>(cancellationToken: cancellationToken);
if (json.TryGetProperty("result", out var result))
{
if (result.TryGetProperty("count", out var count))
historyResponse.TotalCount = count.GetInt32();
if (result.TryGetProperty("items", out var items))
{
foreach (var item in items.EnumerateArray())
{
var job = MapPrintJob(item);
historyResponse.Items.Add(job);
}
}
}
_logger.LogDebug(
"Retrieved {JobCount} print history items from Moonraker at {Host}:{Port}",
historyResponse.Items.Count, hostnameOrIp, port);
}
catch (HttpRequestException ex)
{
_logger.LogWarning(ex,
"Failed to retrieve print history from Moonraker at {Host}:{Port}",
hostnameOrIp, port);
}
catch (JsonException ex)
{
_logger.LogWarning(ex,
"Failed to parse Moonraker history response from {Host}:{Port}",
hostnameOrIp, port);
}
return null;
return historyResponse;
}
/// <inheritdoc />
public async Task<MoonrakerPrintStats?> GetPrintStatsAsync(
string hostnameOrIp,
int port,
string? apiKey,
CancellationToken cancellationToken = default)
{
var baseUrl = BuildBaseUrl(hostnameOrIp, port);
try
{
using var request = CreateRequest(
HttpMethod.Get,
$"{baseUrl}/printer/objects/query?print_stats",
apiKey);
using var response = await _httpClient.SendAsync(request, cancellationToken);
response.EnsureSuccessStatusCode();
var json = await response.Content.ReadFromJsonAsync<JsonElement>(cancellationToken: cancellationToken);
if (json.TryGetProperty("result", out var result)
&& result.TryGetProperty("status", out var status)
&& status.TryGetProperty("print_stats", out var printStats))
{
var stats = MapPrintStats(printStats);
_logger.LogDebug(
"Retrieved print stats from Moonraker at {Host}:{Port} — state: {State}, filament: {FilamentMm}mm",
hostnameOrIp, port, stats.State, stats.FilamentUsedMm);
return stats;
}
_logger.LogWarning(
"Moonraker print_stats not found in response from {Host}:{Port}",
hostnameOrIp, port);
return null;
}
catch (HttpRequestException ex)
{
_logger.LogWarning(ex,
"Failed to retrieve print stats from Moonraker at {Host}:{Port}",
hostnameOrIp, port);
return null;
}
catch (JsonException ex)
{
_logger.LogWarning(ex,
"Failed to parse Moonraker print stats response from {Host}:{Port}",
hostnameOrIp, port);
return null;
}
}
/// <inheritdoc />
public async Task<MoonrakerDisplayStatus?> GetDisplayStatusAsync(
string hostnameOrIp,
int port,
string? apiKey,
CancellationToken cancellationToken = default)
{
var baseUrl = BuildBaseUrl(hostnameOrIp, port);
try
{
using var request = CreateRequest(
HttpMethod.Get,
$"{baseUrl}/printer/objects/query?display_status",
apiKey);
using var response = await _httpClient.SendAsync(request, cancellationToken);
response.EnsureSuccessStatusCode();
var json = await response.Content.ReadFromJsonAsync<JsonElement>(cancellationToken: cancellationToken);
if (json.TryGetProperty("result", out var result)
&& result.TryGetProperty("status", out var status)
&& status.TryGetProperty("display_status", out var displayStatus))
{
var ds = new MoonrakerDisplayStatus();
if (displayStatus.TryGetProperty("progress", out var progress))
ds.Progress = progress.GetDecimal();
if (displayStatus.TryGetProperty("message", out var message))
ds.Message = message.GetString() ?? string.Empty;
_logger.LogDebug(
"Retrieved display status from Moonraker at {Host}:{Port} — progress: {Progress:P0}",
hostnameOrIp, port, ds.Progress);
return ds;
}
_logger.LogWarning(
"Moonraker display_status not found in response from {Host}:{Port}",
hostnameOrIp, port);
return null;
}
catch (HttpRequestException ex)
{
_logger.LogWarning(ex,
"Failed to retrieve display status from Moonraker at {Host}:{Port}",
hostnameOrIp, port);
return null;
}
catch (JsonException ex)
{
_logger.LogWarning(ex,
"Failed to parse Moonraker display status response from {Host}:{Port}",
hostnameOrIp, port);
return null;
}
}
/// <inheritdoc />
public async Task<Dictionary<string, decimal>> GetFilamentUsageAsync(
string hostnameOrIp,
int port,
string? apiKey,
CancellationToken cancellationToken = default)
{
// Delegate to the typed GetPrintHistoryAsync and extract metrics
var history = await GetPrintHistoryAsync(hostnameOrIp, port, apiKey, limit: 1, cancellationToken);
var result = new Dictionary<string, decimal>();
if (history.Items.Count > 0)
{
var latestJob = history.Items[0];
result["mm_extruded"] = latestJob.FilamentUsedMm;
result["print_duration_seconds"] = latestJob.PrintDurationSeconds;
}
_logger.LogDebug(
"Retrieved filament usage from Moonraker at {Host}:{Port}: {MetricCount} metrics",
hostnameOrIp, port, result.Count);
return result;
}
/// <summary>
/// Builds the base URL for Moonraker API calls.
/// Builds the base URL for Moonraker API calls from hostname and port.
/// </summary>
private static string BuildBaseUrl(string hostnameOrIp, int port) =>
$"http://{hostnameOrIp}:{port}";
private static string BuildBaseUrl(string hostnameOrIp, int port)
{
return $"http://{hostnameOrIp}:{port}";
}
/// <summary>
/// Applies the Moonraker API key to the request header if provided.
/// Creates an HttpRequestMessage with the optional API key header.
/// </summary>
private static void ApplyApiKey(HttpRequestMessage request, string? apiKey)
private static HttpRequestMessage CreateRequest(HttpMethod method, string url, string? apiKey)
{
if (!string.IsNullOrWhiteSpace(apiKey))
var request = new HttpRequestMessage(method, url);
if (!string.IsNullOrEmpty(apiKey))
{
request.Headers.Add("X-Api-Key", apiKey);
}
return request;
}
/// <summary>
/// Parsed result from Moonraker's print_stats object.
/// Extracted immediately from the JSON response to avoid JsonDocument disposal issues.
/// Maps a JSON element representing a Moonraker print job history item
/// to a <see cref="MoonrakerPrintJob"/> DTO.
/// </summary>
private sealed class PrintStatsResult
private static MoonrakerPrintJob MapPrintJob(JsonElement item)
{
public string? State { get; set; }
public decimal? FilamentUsedMm { get; set; }
public string? FileName { get; set; }
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>
/// Parsed result from Moonraker's history/last_job object.
/// Maps a JSON element representing Moonraker print_stats
/// to a <see cref="MoonrakerPrintStats"/> DTO.
/// </summary>
private sealed class HistoryResult
private static MoonrakerPrintStats MapPrintStats(JsonElement printStats)
{
public DateTime? StartTime { get; set; }
public DateTime? EndTime { get; set; }
public double? PrintDuration { get; set; }
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;
}
}