examples/Print-Automation.ps1

<#
.SYNOPSIS
    Automated printing workflow examples.

.DESCRIPTION
    Demonstrates how to automate print workflows including preheating,
    waiting for conditions, and monitoring print progress.
#>


Import-Module KlippyCLI

#region Preheat and Start Print

# Preheat sequence
function Start-PrintWithPreheat {
    param(
        [string]$FileName,
        [int]$ExtruderTemp = 200,
        [int]$BedTemp = 60
    )

    Write-Host "Starting preheat sequence..." -ForegroundColor Cyan

    # Start heating
    Invoke-KlippyGcode -Gcode "M104 S$ExtruderTemp", "M140 S$BedTemp"

    # Wait for bed temperature
    Write-Host "Waiting for bed to reach ${BedTemp}C..."
    Wait-KlippyHeaterTemperature -Heater "heater_bed" -Temperature $BedTemp -Tolerance 2

    # Wait for extruder temperature
    Write-Host "Waiting for extruder to reach ${ExtruderTemp}C..."
    Wait-KlippyHeaterTemperature -Heater "extruder" -Temperature $ExtruderTemp -Tolerance 2

    # Home and start
    Write-Host "Homing..."
    Invoke-KlippyHome
    Wait-KlippyHomed

    Write-Host "Starting print: $FileName" -ForegroundColor Green
    Start-KlippyPrint -FileName $FileName
}

# Usage
Start-PrintWithPreheat -FileName "benchy.gcode" -ExtruderTemp 210 -BedTemp 65

#endregion

#region Monitor Print to Completion

function Watch-PrintProgress {
    param(
        [int]$IntervalSeconds = 30
    )

    $printing = $true
    while ($printing) {
        $job = Get-KlippyPrintJob

        if ($job.State -eq "printing") {
            $progress = [math]::Round($job.Progress * 100, 1)
            $elapsed = $job.PrintTime.ToString("hh\:mm\:ss")
            Write-Host "[$elapsed] Progress: $progress% - $($job.FileName)" -ForegroundColor Cyan
        }
        elseif ($job.State -in @("complete", "cancelled", "error", "standby")) {
            $printing = $false
            Write-Host "Print finished with state: $($job.State)" -ForegroundColor Yellow
        }

        if ($printing) {
            Start-Sleep -Seconds $IntervalSeconds
        }
    }
}

# Start print and monitor
Start-KlippyPrint -FileName "part.gcode"
Watch-PrintProgress -IntervalSeconds 60

#endregion

#region Wait for Print Completion

# Simple wait for print to finish
Start-KlippyPrint -FileName "part.gcode"
Wait-KlippyPrintFinished -Timeout 86400  # 24 hour timeout
Write-Host "Print complete!" -ForegroundColor Green

# Cooldown after print
Invoke-KlippyGcode -Gcode "M104 S0", "M140 S0"  # Turn off heaters
Invoke-KlippyGcode -Gcode "M106 S255"  # Part cooling fan full
Start-Sleep -Seconds 60
Invoke-KlippyGcode -Gcode "M106 S0"  # Fan off

#endregion

#region Batch Processing with Job Queue

# Queue multiple parts for sequential printing
$files = @(
    "part1.gcode",
    "part2.gcode",
    "part3.gcode"
)

# Clear existing queue
Remove-KlippyJobQueue -All -Force

# Add files to queue
Add-KlippyJobQueue -FileName $files
Write-Host "Queued $($files.Count) files for printing"

# View queue
Get-KlippyJobQueue | Format-Table FileName, TimeAdded

# Start the queue (starts first print)
# Note: Klipper will automatically proceed to next job when each completes

#endregion

#region Multi-Printer Batch

# Print same file on multiple printers
$printers = Get-KlippyPrinter

foreach ($printer in $printers) {
    Write-Host "Starting print on $($printer.PrinterName)..."

    # Check if printer is ready
    $status = Get-KlippyStatus -PrinterName $printer.PrinterName
    if ($status.State -eq "ready") {
        Start-KlippyPrint -PrinterName $printer.PrinterName -FileName "common_part.gcode"
    }
    else {
        Write-Warning "$($printer.PrinterName) is not ready (state: $($status.State))"
    }
}

#endregion