functions/public/Get-KlippyOutputPin.ps1

function Get-KlippyOutputPin {
    <#
    .SYNOPSIS
        Gets output pin status from a Klipper printer.

    .DESCRIPTION
        Retrieves the status of output pins configured on the printer,
        including their current value (digital or PWM).

    .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 Name
        Filter by specific pin name. Supports wildcards.

    .EXAMPLE
        Get-KlippyOutputPin
        Gets all output pins from the default printer.

    .EXAMPLE
        Get-KlippyOutputPin -PrinterName "voronv2"
        Gets all output pins from the specified printer.

    .EXAMPLE
        Get-KlippyOutputPin -Name "*led*"
        Gets pins with "led" in the name.

    .OUTPUTS
        KlippyCLI.OutputPin objects.
    #>

    [CmdletBinding(DefaultParameterSetName = 'Default')]
    [OutputType([PSCustomObject])]
    param(
        [Parameter(ParameterSetName = 'ById')]
        [ValidateNotNullOrEmpty()]
        [string]$Id,

        [Parameter(ParameterSetName = 'ByName', Position = 0)]
        [ValidateNotNullOrEmpty()]
        [string]$PrinterName,

        [Parameter(ParameterSetName = 'ByObject', ValueFromPipeline = $true)]
        [PSCustomObject]$InputObject,

        [Parameter()]
        [string]$Name
    )

    process {
        # Resolve printer
        $resolveParams = @{}
        switch ($PSCmdlet.ParameterSetName) {
            'ById' { $resolveParams['Id'] = $Id }
            'ByName' { $resolveParams['PrinterName'] = $PrinterName }
            'ByObject' { $resolveParams['InputObject'] = $InputObject }
        }

        $printer = Resolve-KlippyPrinterTarget @resolveParams

        try {
            # Get list of available objects
            $allObjects = Invoke-KlippyJsonRpc -Printer $printer -Method "printer/objects/list"

            # Find output_pin objects
            $pinObjects = foreach ($obj in $allObjects.Objects) {
                if ($obj.StartsWith('output_pin ')) {
                    $pinName = ($obj -split ' ', 2)[1]
                    [PSCustomObject]@{
                        ObjectName = $obj
                        PinName    = $pinName
                    }
                }
            }

            # Apply name filter
            if ($Name) {
                $pinObjects = $pinObjects | Where-Object { $_.PinName -like $Name }
            }

            if (-not $pinObjects) {
                Write-Verbose "No output pins found matching the specified criteria."
                return
            }

            # Build query string
            $queryParts = $pinObjects | ForEach-Object { [System.Uri]::EscapeDataString($_.ObjectName) }
            $endpoint = "printer/objects/query?" + ($queryParts -join '&')

            $response = Invoke-KlippyJsonRpc -Printer $printer -Method $endpoint -NoNormalize

            # Process results
            foreach ($pin in $pinObjects) {
                $data = $response.status.($pin.ObjectName)
                if ($data) {
                    [PSCustomObject]@{
                        PSTypeName  = 'KlippyCLI.OutputPin'
                        PrinterId   = $printer.Id
                        PrinterName = $printer.PrinterName
                        Name        = $pin.PinName
                        Value       = $data.value
                    }
                }
            }
        }
        catch {
            Write-Error "Failed to get output pins from '$($printer.PrinterName)': $_"
        }
    }
}