Skip to content

Commit

Permalink
refactor getPrinterModel in order to make request-id start with 001
Browse files Browse the repository at this point in the history
  • Loading branch information
gmuth committed Sep 19, 2024
1 parent 40ac10a commit f524c18
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/main/kotlin/de/gmuth/ipp/client/IppInspector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@ object IppInspector {
const val pdfA4 = "blank_A4.pdf"

fun inspect(printerUri: URI, cancelJob: Boolean = true) =
IppPrinter(printerUri).inspect(cancelJob)
IppPrinter(printerUri, getPrinterAttributesOnInit = false).inspect(cancelJob)

private fun IppPrinter.getPrinterModel() = StringBuilder().run {
// use another IppPrinter instance to leave request-id-counter untouched
with(IppPrinter(printerUri, getPrinterAttributesOnInit = false)) {
updateAttributes("cups-version", "printer-make-and-model")
if (isCups()) append("CUPS-")
append(makeAndModel.text.replace("\\s+".toRegex(), "_"))
}
toString()
}

/**
* Exchange a few IPP requests and save the IPP responses returned by the printer.
Expand All @@ -30,17 +40,16 @@ object IppInspector {
private fun IppPrinter.inspect(cancelJob: Boolean) {
logger.info { "Inspect printer $printerUri" }

val printerModel = with(StringBuilder()) {
if (isCups()) append("CUPS-")
append(makeAndModel.text.replace("\\s+".toRegex(), "_"))
toString()
}
val printerModel = getPrinterModel()
logger.info { "Printer model: $printerModel" }

ippClient.saveMessages = true
ippClient.saveMessagesDirectory = File(directory, printerModel).createDirectoryIfNotExists()
workDirectory = ippClient.saveMessagesDirectory

logger.info { "> Get printer attributes" }
updateAttributes()

attributes.run {
// Media
if (containsKey("media-supported")) logger.info { "Media supported: $mediaSupported" }
Expand Down Expand Up @@ -72,9 +81,6 @@ object IppInspector {

private fun IppPrinter.runInspectionWorkflow(pdfResource: String, cancelJob: Boolean) {

logger.info { "> Get printer attributes" }
getPrinterAttributes()

if (supportsOperations(CupsGetPPD)) {
logger.info { "> CUPS Get PPD" }
savePPD(file = File(workDirectory, "0-${name.text}.ppd"))
Expand Down
19 changes: 19 additions & 0 deletions src/test/kotlin/de/gmuth/ipp/client/inspectPrinter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package de.gmuth.ipp.client

import de.gmuth.ipp.core.IppException
import de.gmuth.log.Logging
import java.net.URI
import java.util.logging.Level
import java.util.logging.Logger

fun main() {
Logging.configure()
val logger = Logger.getLogger("main")

val printerURI = URI.create("ipp://xero.local")
try {
IppInspector.inspect(printerURI, cancelJob = true)
} catch (ippException: IppException) {
logger.log(Level.SEVERE, "Failed to inspect printer $printerURI", ippException)
}
}

0 comments on commit f524c18

Please sign in to comment.