functions/public/Get-KlippyStatus.ps1
|
function Get-KlippyStatus { <# .SYNOPSIS Gets the current status of a Klipper printer. .DESCRIPTION Retrieves comprehensive status information including printer state, temperatures, print progress, and key object states. .PARAMETER Id The unique identifier of the printer. .PARAMETER PrinterName The friendly name of the printer. .PARAMETER InputObject A printer object from pipeline input. .PARAMETER Full Include all available printer objects in the status. .EXAMPLE Get-KlippyStatus Gets status of the default printer. .EXAMPLE Get-KlippyStatus -PrinterName "voronv2" Gets status of the specified printer. .EXAMPLE Get-KlippyPrinter | Get-KlippyStatus Gets status of all registered printers. .OUTPUTS PSCustomObject with printer status information. #> [CmdletBinding(DefaultParameterSetName = 'Default')] [OutputType([PSCustomObject])] param( [Parameter(Mandatory = $true, ParameterSetName = 'ById')] [ValidateNotNullOrEmpty()] [string]$Id, [Parameter(Mandatory = $true, ParameterSetName = 'ByName', Position = 0)] [ValidateNotNullOrEmpty()] [string]$PrinterName, [Parameter(Mandatory = $true, ParameterSetName = 'ByObject', ValueFromPipeline = $true)] [PSCustomObject]$InputObject, [Parameter()] [switch]$Full ) process { # Resolve printer $resolveParams = @{} switch ($PSCmdlet.ParameterSetName) { 'ById' { $resolveParams['Id'] = $Id } 'ByName' { $resolveParams['PrinterName'] = $PrinterName } 'ByObject' { $resolveParams['InputObject'] = $InputObject } } $printer = Resolve-KlippyPrinterTarget @resolveParams # Define objects to query $objects = @{ print_stats = $null display_status = $null heater_bed = $null extruder = $null toolhead = $null gcode_move = $null fan = $null idle_timeout = $null } if ($Full) { # Query all available objects $objects = $null } try { # Build query parameters $queryObjects = @{} if ($objects) { foreach ($key in $objects.Keys) { $queryObjects[$key] = $objects[$key] } } # Use printer/objects/query endpoint $endpoint = "printer/objects/query" $queryParams = @{} if ($objects) { # Build query string for objects $objectList = ($objects.Keys | ForEach-Object { $_ }) -join '&' $endpoint = "printer/objects/query?$($objects.Keys -join '&')" } $response = Invoke-KlippyJsonRpc -Printer $printer -Method $endpoint # Build a friendly status object $status = $response.Status # Create summary object $result = [PSCustomObject]@{ PSTypeName = 'KlippyCLI.PrinterStatus' PrinterId = $printer.Id PrinterName = $printer.PrinterName State = $status.PrintStats.State StateMessage = $status.IdleTimeout.State # Print info Filename = $status.PrintStats.Filename PrintDuration = [TimeSpan]::FromSeconds([int]($status.PrintStats.PrintDuration ?? 0)) TotalDuration = [TimeSpan]::FromSeconds([int]($status.PrintStats.TotalDuration ?? 0)) FilamentUsed = [math]::Round(($status.PrintStats.FilamentUsed ?? 0) / 1000, 2) # Convert to meters Progress = [math]::Round(($status.DisplayStatus.Progress ?? 0) * 100, 1) # Temperatures ExtruderTemp = [math]::Round($status.Extruder.Temperature ?? 0, 1) ExtruderTarget = [math]::Round($status.Extruder.Target ?? 0, 1) BedTemp = [math]::Round($status.HeaterBed.Temperature ?? 0, 1) BedTarget = [math]::Round($status.HeaterBed.Target ?? 0, 1) # Position Position = $status.Toolhead.Position Homed = $status.Toolhead.HomedAxes # Fan FanSpeed = [math]::Round(($status.Fan.Speed ?? 0) * 100, 0) # Speed SpeedFactor = [math]::Round(($status.GcodeMove.SpeedFactor ?? 1) * 100, 0) ExtrudeFactor = [math]::Round(($status.GcodeMove.ExtrudeFactor ?? 1) * 100, 0) # Raw status for advanced use RawStatus = if ($Full) { $status } else { $null } } return $result } catch { Write-Error "Failed to get status for '$($printer.PrinterName)': $_" } } } |