merge(dev): Re-apply changes after conflict resolution
This commit is contained in:
@@ -1,15 +1,20 @@
|
||||
using System.Net.Http.Json;
|
||||
using System.Globalization;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text.Json;
|
||||
using Extrudex.Domain.Interfaces;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Extrudex.Infrastructure.Configuration;
|
||||
namespace Extrudex.Infrastructure.Services;
|
||||
|
||||
/// <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.
|
||||
/// HTTP client for communicating with the Moonraker REST API on
|
||||
/// Klipper-based printers (e.g., Elegoo Centauri Carbon).
|
||||
///
|
||||
/// Moonraker endpoints used:
|
||||
/// - GET /api/objects?print_stats — current print stats including filament used
|
||||
/// - GET /api/objects?history — job history with filament usage per job
|
||||
///
|
||||
/// Authentication: optional X-Api-Key header when API key is configured.
|
||||
/// </summary>
|
||||
public class MoonrakerClient : IMoonrakerClient
|
||||
{
|
||||
@@ -17,9 +22,9 @@ public class MoonrakerClient : IMoonrakerClient
|
||||
private readonly ILogger<MoonrakerClient> _logger;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new MoonrakerClient with the configured HTTP client and logger.
|
||||
/// Creates a new MoonrakerClient with the specified HTTP client and logger.
|
||||
/// </summary>
|
||||
/// <param name="httpClient">The HTTP client for making requests to Moonraker endpoints.</param>
|
||||
/// <param name="httpClient">The HTTP client used for API calls.</param>
|
||||
/// <param name="logger">Logger for diagnostic output.</param>
|
||||
public MoonrakerClient(HttpClient httpClient, ILogger<MoonrakerClient> logger)
|
||||
{
|
||||
@@ -28,103 +33,271 @@ public class MoonrakerClient : IMoonrakerClient
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<Dictionary<string, decimal>> GetFilamentUsageAsync(
|
||||
public async Task<string> GetPrinterStatusAsync(
|
||||
string hostnameOrIp,
|
||||
int port,
|
||||
string? apiKey,
|
||||
string? apiKey = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var baseUrl = BuildBaseUrl(hostnameOrIp, port);
|
||||
var result = new Dictionary<string, decimal>();
|
||||
var requestUrl = $"{baseUrl}/api/objects?print_stats";
|
||||
|
||||
using var request = new HttpRequestMessage(HttpMethod.Get, requestUrl);
|
||||
ApplyApiKey(request, apiKey);
|
||||
|
||||
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 response = await _httpClient.SendAsync(request, cancellationToken);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var json = await response.Content.ReadFromJsonAsync<JsonElement>(cancellationToken: cancellationToken);
|
||||
var json = await response.Content.ReadAsStringAsync(cancellationToken);
|
||||
using var doc = JsonDocument.Parse(json);
|
||||
|
||||
// Extract filament usage from the response
|
||||
// Moonraker returns job history with filament_used and similar fields
|
||||
if (json.TryGetProperty("result", out var resultElement))
|
||||
{
|
||||
if (resultElement.TryGetProperty("items", out var items) && items.GetArrayLength() > 0)
|
||||
{
|
||||
var job = items[0];
|
||||
// Moonraker returns: { "result": { "print_stats": { "state": "idle", ... } } }
|
||||
var state = doc.RootElement
|
||||
.GetProperty("result")
|
||||
.GetProperty("print_stats")
|
||||
.GetProperty("state")
|
||||
.GetString();
|
||||
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_logger.LogDebug(
|
||||
"Retrieved filament usage from Moonraker at {Host}:{Port}: {MetricCount} metrics",
|
||||
hostnameOrIp, port, result.Count);
|
||||
return state ?? "unknown";
|
||||
}
|
||||
catch (HttpRequestException ex)
|
||||
{
|
||||
_logger.LogWarning(ex,
|
||||
"Failed to retrieve filament usage from Moonraker at {Host}:{Port}",
|
||||
"Moonraker printer status request failed for {Host}:{Port}",
|
||||
hostnameOrIp, port);
|
||||
return "offline";
|
||||
}
|
||||
catch (JsonException ex)
|
||||
{
|
||||
_logger.LogWarning(ex,
|
||||
"Malformed Moonraker response from {Host}:{Port}",
|
||||
hostnameOrIp, port);
|
||||
return "error";
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<MoonrakerFilamentUsage?> GetFilamentUsageAsync(
|
||||
string hostnameOrIp,
|
||||
int port,
|
||||
string? apiKey = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var baseUrl = BuildBaseUrl(hostnameOrIp, port);
|
||||
|
||||
// Fetch current print_stats (has live filament_used for active/recent job)
|
||||
PrintStatsResult? printStats = null;
|
||||
try
|
||||
{
|
||||
printStats = await FetchPrintStatsAsync(baseUrl, apiKey, cancellationToken);
|
||||
}
|
||||
catch (HttpRequestException ex)
|
||||
{
|
||||
_logger.LogWarning(ex,
|
||||
"Moonraker print_stats request failed for {Host}:{Port}",
|
||||
hostnameOrIp, port);
|
||||
return null;
|
||||
}
|
||||
catch (JsonException ex)
|
||||
{
|
||||
_logger.LogWarning(ex,
|
||||
"Malformed Moonraker print_stats response from {Host}:{Port}",
|
||||
hostnameOrIp, port);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (printStats is null)
|
||||
return null;
|
||||
|
||||
// Attempt to enrich with history data for timing info
|
||||
HistoryResult? history = null;
|
||||
try
|
||||
{
|
||||
history = await FetchHistoryAsync(baseUrl, apiKey, cancellationToken);
|
||||
}
|
||||
catch (HttpRequestException ex)
|
||||
{
|
||||
_logger.LogWarning(ex,
|
||||
"Moonraker history request failed for {Host}:{Port}",
|
||||
hostnameOrIp, port);
|
||||
}
|
||||
catch (JsonException ex)
|
||||
{
|
||||
_logger.LogWarning(ex,
|
||||
"Failed to parse Moonraker response from {Host}:{Port}",
|
||||
"Malformed Moonraker history response from {Host}:{Port}",
|
||||
hostnameOrIp, port);
|
||||
}
|
||||
|
||||
return result;
|
||||
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
|
||||
};
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<bool> IsReachableAsync(
|
||||
string hostnameOrIp,
|
||||
int port,
|
||||
/// <summary>
|
||||
/// Fetches and parses print_stats from the Moonraker API.
|
||||
/// </summary>
|
||||
private async Task<PrintStatsResult?> FetchPrintStatsAsync(
|
||||
string baseUrl,
|
||||
string? apiKey,
|
||||
CancellationToken cancellationToken = default)
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var baseUrl = BuildBaseUrl(hostnameOrIp, port);
|
||||
var requestUrl = $"{baseUrl}/api/objects?print_stats";
|
||||
|
||||
try
|
||||
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))
|
||||
{
|
||||
using var request = new HttpRequestMessage(HttpMethod.Get, $"{baseUrl}/server/info");
|
||||
if (!string.IsNullOrEmpty(apiKey))
|
||||
{
|
||||
request.Headers.Add("X-Api-Key", apiKey);
|
||||
}
|
||||
|
||||
using var response = await _httpClient.SendAsync(request, cancellationToken);
|
||||
return response.IsSuccessStatusCode;
|
||||
_logger.LogWarning("Moonraker response missing 'print_stats' object");
|
||||
return null;
|
||||
}
|
||||
catch (HttpRequestException)
|
||||
|
||||
return new PrintStatsResult
|
||||
{
|
||||
_logger.LogDebug("Moonraker at {Host}:{Port} is not reachable", hostnameOrIp, port);
|
||||
return false;
|
||||
State = stats.TryGetProperty("state", out var stateEl) ? stateEl.GetString() : null,
|
||||
FilamentUsedMm = stats.TryGetProperty("filament_used", out var filamentEl) &&
|
||||
filamentEl.ValueKind == JsonValueKind.Number
|
||||
? filamentEl.GetDecimal() : (decimal?)null,
|
||||
FileName = stats.TryGetProperty("filename", out var fileEl) ? fileEl.GetString() : null
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fetches and parses history (last job) from the Moonraker API.
|
||||
/// </summary>
|
||||
private async Task<HistoryResult?> FetchHistoryAsync(
|
||||
string baseUrl,
|
||||
string? apiKey,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var requestUrl = $"{baseUrl}/api/objects?history";
|
||||
|
||||
using var request = new HttpRequestMessage(HttpMethod.Get, requestUrl);
|
||||
ApplyApiKey(request, apiKey);
|
||||
|
||||
using var response = await _httpClient.SendAsync(request, cancellationToken);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var json = await response.Content.ReadAsStringAsync(cancellationToken);
|
||||
using var doc = JsonDocument.Parse(json);
|
||||
|
||||
if (!doc.RootElement.TryGetProperty("result", out var result) ||
|
||||
!result.TryGetProperty("history", out var history))
|
||||
{
|
||||
_logger.LogWarning("Moonraker response missing 'history' object");
|
||||
return null;
|
||||
}
|
||||
|
||||
// Try last_job first, then job
|
||||
JsonElement jobEl;
|
||||
if (!history.TryGetProperty("last_job", out jobEl) &&
|
||||
!history.TryGetProperty("job", out jobEl))
|
||||
{
|
||||
_logger.LogDebug("Moonraker history has no 'last_job' or 'job' entry");
|
||||
return null;
|
||||
}
|
||||
|
||||
return new HistoryResult
|
||||
{
|
||||
StartTime = ParseDateTimeProperty(jobEl, "start_time"),
|
||||
EndTime = ParseDateTimeProperty(jobEl, "end_time"),
|
||||
PrintDuration = jobEl.TryGetProperty("print_duration", out var durEl) &&
|
||||
durEl.ValueKind == JsonValueKind.Number
|
||||
? durEl.GetDouble() : (double?)null
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parses a Moonraker timestamp property (Unix epoch seconds or ISO 8601 string).
|
||||
/// </summary>
|
||||
private static DateTime? ParseDateTimeProperty(JsonElement element, string propertyName)
|
||||
{
|
||||
if (!element.TryGetProperty(propertyName, out var prop))
|
||||
return null;
|
||||
|
||||
// Moonraker returns Unix epoch seconds as a number
|
||||
if (prop.ValueKind == JsonValueKind.Number)
|
||||
{
|
||||
var epochSeconds = prop.GetDouble();
|
||||
return DateTime.UnixEpoch.AddSeconds(epochSeconds);
|
||||
}
|
||||
|
||||
// Fallback: try parsing as ISO 8601 string
|
||||
if (prop.ValueKind == JsonValueKind.String)
|
||||
{
|
||||
var str = prop.GetString();
|
||||
if (str is not null &&
|
||||
DateTime.TryParse(str, CultureInfo.InvariantCulture,
|
||||
DateTimeStyles.AssumeUniversal, out var dt))
|
||||
{
|
||||
return dt.ToUniversalTime();
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds the base URL for Moonraker API calls.
|
||||
/// </summary>
|
||||
private static string BuildBaseUrl(string hostnameOrIp, int port) =>
|
||||
$"http://{hostnameOrIp}:{port}";
|
||||
|
||||
/// <summary>
|
||||
/// Applies the Moonraker API key to the request header if provided.
|
||||
/// </summary>
|
||||
private static void ApplyApiKey(HttpRequestMessage request, string? apiKey)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(apiKey))
|
||||
{
|
||||
request.Headers.Add("X-Api-Key", apiKey);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds the base URL for Moonraker API calls from hostname and port.
|
||||
/// Parsed result from Moonraker's print_stats object.
|
||||
/// Extracted immediately from the JSON response to avoid JsonDocument disposal issues.
|
||||
/// </summary>
|
||||
private static string BuildBaseUrl(string hostnameOrIp, int port)
|
||||
private sealed class PrintStatsResult
|
||||
{
|
||||
return $"http://{hostnameOrIp}:{port}";
|
||||
public string? State { get; set; }
|
||||
public decimal? FilamentUsedMm { get; set; }
|
||||
public string? FileName { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parsed result from Moonraker's history/last_job object.
|
||||
/// </summary>
|
||||
private sealed class HistoryResult
|
||||
{
|
||||
public DateTime? StartTime { get; set; }
|
||||
public DateTime? EndTime { get; set; }
|
||||
public double? PrintDuration { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user