Crate printer_event_handler

Crate printer_event_handler 

Source
Expand description

§Printer Event Handler

A cross-platform printer status monitoring library for Windows and Linux systems. This library provides functionality to query printer status, monitor printer events, and track printer state changes using platform-specific backends:

  • Windows: WMI (Windows Management Instrumentation) with complete Win32_Printer support
    • PrinterStatus (current, values 1-7): Other, Unknown, Idle, Printing, Warmup, StoppedPrinting, Offline
    • PrinterState (.NET PrintQueueStatus flags): Processing (16384), Printing (1024), Offline (128), etc.
    • All 12 DetectedErrorState values (0-11): NoError, NoPaper, Jammed, ServiceRequested, etc.
  • Linux: CUPS (Common Unix Printing System) with basic status detection

§.NET PrintQueueStatus Flags

The PrinterState property uses .NET PrintQueueStatus bitwise flags. These flags can be combined, and our implementation uses priority-based parsing to select the most significant state.

NameValueDescription
None0Status is not specified
Paused1The print queue is paused
Error2The printer cannot print due to an error condition
PendingDeletion4The print queue is deleting a print job
PaperJam8The paper in the printer is jammed
PaperOut16The printer does not have, or is out of, the type of paper needed for the current print job
ManualFeed32The printer is waiting for a user to place print media in the manual feed bin
PaperProblem64The paper in the printer is causing an unspecified error condition
Offline128The printer is offline
IOActive256The printer is exchanging data with the print server
Busy512The printer is busy
Printing1024The device is printing
OutputBinFull2048The printer’s output bin is full
NotAvailable4096Status information is unavailable
Waiting8192The printer is waiting for a print job
Processing16384The device is doing some kind of work, which need not be printing if the device also sets Printing
Initializing32768The device is in the process of initialization
WarmingUp65536The device is warming up
TonerLow131072The device is low on toner
NoToner262144The device has no toner
PagePunt524288The device cannot print the current page
UserInterventionRequired1048576The device needs user intervention
OutOfMemory2097152The device is out of memory
DoorOpen4194304A door on the device is open
ServerUnknown8388608Status is unknown
PowerSave16777216The device is in power save mode

Reference: Microsoft .NET PrintQueueStatus

§DetectedErrorState Values

The DetectedErrorState property indicates specific error conditions on the printer.

NameValueDescription
Unknown0The error state is unknown
Other1An error other than those listed below
No Error2The printer is functioning normally
Low Paper3The printer is low on paper
No Paper4The printer is out of paper
Low Toner5The printer is low on toner or ink
No Toner6The printer is out of toner or ink
Door Open7A door or cover on the printer is open
Jammed8Paper is jammed in the printer
Offline9The printer is offline
Service Requested10The printer requires service or maintenance
Output Bin Full11The printer’s output bin is full

Reference: Microsoft Win32_Printer DetectedErrorState

§Features

  • Comprehensive Windows support - Full Win32_Printer coverage per Microsoft documentation
  • Cross-platform support (Windows and Linux)
  • Real-time monitoring - Query all printers on the system
  • Status change detection - Monitor specific printers for status changes
  • Async/await support with Tokio
  • Detailed status information - PrinterStatus (current) + PrinterState (.NET flags) and 11 error states
  • Platform-specific backends with unified API

§Example

use printer_event_handler::{PrinterMonitor, PrinterError, MonitorableProperty};

#[tokio::main]
async fn main() -> Result<(), PrinterError> {
    let monitor = PrinterMonitor::new().await?;
     
    // List all printers with complete WMI information
    let printers = monitor.list_printers().await?;
    for printer in printers {
        println!("Printer: {}", printer.name());
        println!("Status: {}", printer.status_description());
        println!("Offline: {}", printer.is_offline());
         
        // Access raw WMI data
        if let Some(code) = printer.printer_status_code() {
            println!("  PrinterStatus: {} ({})", code,
                printer.printer_status_description().unwrap_or("Unknown"));
        }
         
        if let Some(status) = printer.wmi_status() {
            println!("  WMI Status: {}", status);
        }
    }
     
    // Monitor specific property changes with type safety
    monitor.monitor_property("HP LaserJet", MonitorableProperty::IsOffline, 60000, |change| {
        println!("Offline status changed: {}", change.description());
    }).await?;
     
    Ok(())
}

Re-exports§

pub use error::PrinterError;
pub use monitor::MonitorableProperty;
pub use monitor::PrinterMonitor;
pub use printer::ErrorState;
pub use printer::Printer;
pub use printer::PrinterChanges;
pub use printer::PrinterState;
pub use printer::PrinterStatus;
pub use printer::PropertyChange;

Modules§

backend
error
monitor
printer

Structs§

CancellationToken
A token which can be used to signal a cancellation request to one or more tasks.

Type Aliases§

Result
Result type used throughout the library