@@ -17,6 +17,8 @@ import de.gmuth.ipp.iana.IppRegistrationsSection2
17
17
import java.io.*
18
18
import java.net.URI
19
19
import java.time.Duration
20
+ import java.time.Instant
21
+ import java.time.Instant.now
20
22
import java.util.logging.Level
21
23
import java.util.logging.Level.*
22
24
import java.util.logging.Logger
@@ -236,22 +238,29 @@ class IppPrinter(
236
238
237
239
// -------------------------------------------------------
238
240
239
- fun isIdle () = state == Idle
240
- fun isStopped () = state == Stopped
241
- fun isProcessing () = state == Processing
241
+ private fun stateIs (updateStateAttributes : Boolean , expectedState : PrinterState ): Boolean {
242
+ if (updateStateAttributes) updateStateAttributes()
243
+ return state == expectedState
244
+ }
245
+
246
+ fun isIdle (updateStateAttributes : Boolean = false) = stateIs(updateStateAttributes, Idle )
247
+ fun isStopped (updateStateAttributes : Boolean = false) = stateIs(updateStateAttributes, Stopped )
248
+ fun isProcessing (updateStateAttributes : Boolean = false) = stateIs(updateStateAttributes, Processing )
249
+
242
250
fun isPaused () = stateReasons.contains(" paused" )
243
251
fun isOffline () = stateReasons.contains(" offline-report" ) // reported by CUPS
244
- fun isDuplexSupported () = sidesSupported.any { it.startsWith(" two-sided" ) }
245
- fun supportsOperations (vararg operations : IppOperation ) = operationsSupported.containsAll(operations.toList())
246
- fun supportsVersion (version : String ) = versionsSupported.contains(version)
247
- fun isCups () = attributes.contains(" cups-version" )
248
252
fun isTonerLow () = stateReasons.contains(" toner-low" )
249
253
fun isTonerEmpty () = stateReasons.any { it.contains(" toner-empty" ) } // toner-empty-error
250
254
fun isMediaJam () = stateReasons.contains(" media-jam" )
251
255
fun isMediaLow () = stateReasons.contains(" media-low" )
252
256
fun isMediaEmpty () = stateReasons.any { it.contains(" media-empty" ) } // media-empty-report
253
257
fun isMediaNeeded () = stateReasons.contains(" media-needed" )
254
258
259
+ fun isDuplexSupported () = sidesSupported.any { it.startsWith(" two-sided" ) }
260
+ fun supportsOperations (vararg operations : IppOperation ) = operationsSupported.containsAll(operations.toList())
261
+ fun supportsVersion (version : String ) = versionsSupported.contains(version)
262
+ fun isCups () = attributes.contains(" cups-version" )
263
+
255
264
fun isMediaSizeSupported (size : MediaSize ) = mediaSizeSupported.supports(size)
256
265
257
266
fun isMediaSizeReady (size : MediaSize ) = mediaColReady
@@ -315,17 +324,33 @@ class IppPrinter(
315
324
316
325
fun getPrinterAttributesOrNull (requestedAttributes : Collection <String >? = null) =
317
326
exchange(ippRequest(GetPrinterAttributes , requestedAttributes))
318
- .attributesGroups.find { it.tag == Printer }
327
+ .attributesGroups.singleOrNull { it.tag == Printer }
319
328
320
329
fun getPrinterAttributesOrNull (vararg requestedAttributes : String ) =
321
330
getPrinterAttributesOrNull(requestedAttributes.toList())
322
331
332
+ private lateinit var stateAttributesLastUpdated: Instant
333
+
323
334
fun updateAttributes (requestedAttributes : List <String >? = null) =
324
- getPrinterAttributesOrNull(requestedAttributes)?.let { attributes.put(it) }
335
+ getPrinterAttributesOrNull(requestedAttributes)?.let {
336
+ attributes.put(it)
337
+ stateAttributesLastUpdated = now()
338
+ }
325
339
326
340
fun updateAttributes (vararg requestedAttributes : String ) =
327
341
updateAttributes(requestedAttributes.toList())
328
342
343
+ fun updateStateAttributes () = updateAttributes(
344
+ " printer-state" , " printer-state-reasons" , " printer-state-message" ,
345
+ " printer-is-accepting-jobs" , " media-ready"
346
+ )
347
+
348
+ var stateAttributesExpireAfter: Duration ? = null
349
+
350
+ fun stateAttributesExpired () =
351
+ if (stateAttributesExpireAfter == null ) false // never expire
352
+ else now().isAfter(stateAttributesLastUpdated.plus(stateAttributesExpireAfter))
353
+
329
354
// -------------
330
355
// Validate-Job
331
356
// -------------
0 commit comments