@@ -8,15 +8,19 @@ import de.tillhub.printengine.data.PrinterResult
8
8
import de.tillhub.printengine.data.PrinterSettings
9
9
import de.tillhub.printengine.data.PrinterState
10
10
import de.tillhub.printengine.data.doOnError
11
+ import de.tillhub.printengine.dispatcher.DispatcherProvider
12
+ import de.tillhub.printengine.dispatcher.DispatcherProviderImp
11
13
import de.tillhub.printengine.pax.PaxPrintService
12
14
import de.tillhub.printengine.sunmi.SunmiPrintService
13
15
import de.tillhub.printengine.verifone.VerifonePrintService
14
16
import kotlinx.coroutines.flow.StateFlow
17
+ import kotlinx.coroutines.withContext
15
18
import timber.log.Timber
16
19
17
20
class PrinterImpl (
18
21
private val printService : PrintService ,
19
- private val analytics : PrintAnalytics ?
22
+ private val analytics : PrintAnalytics ? ,
23
+ private val dispatcherProvider : DispatcherProvider = DispatcherProviderImp ()
20
24
) : Printer {
21
25
22
26
init {
@@ -34,51 +38,55 @@ class PrinterImpl(
34
38
override fun observePrinterState (): StateFlow <PrinterState > = printService.printerState
35
39
36
40
override suspend fun getPrinterInfo (): PrinterResult <PrinterInfo > =
37
- printService.withPrinterCatching {
38
- it.getPrinterInfo().let { info ->
39
- PrinterInfo (
40
- info.serialNumber,
41
- info.deviceModel,
42
- info.printerVersion,
43
- info.printerPaperSpec,
44
- info.printingFontType,
45
- info.printerHead,
46
- info.printedDistance,
47
- info.serviceVersion
48
- )
41
+ withContext(dispatcherProvider.iO()) {
42
+ printService.withPrinterCatching {
43
+ it.getPrinterInfo().let { info ->
44
+ PrinterInfo (
45
+ info.serialNumber,
46
+ info.deviceModel,
47
+ info.printerVersion,
48
+ info.printerPaperSpec,
49
+ info.printingFontType,
50
+ info.printerHead,
51
+ info.printedDistance,
52
+ info.serviceVersion
53
+ )
54
+ }
55
+ }.doOnError {
56
+ logWarning(" getting printer info" )
49
57
}
50
- }.doOnError {
51
- logWarning(" getting printer info" )
52
58
}
53
59
54
60
override suspend fun startPrintJob (job : PrintJob ): PrinterResult <Unit > =
55
- printService.withPrinterCatching { controller ->
56
- logInfo(
57
- """ receipt: START #################
61
+ withContext(dispatcherProvider.iO()) {
62
+ printService.withPrinterCatching { controller ->
63
+ logInfo(
64
+ """ receipt: START #################
58
65
|${job.description}
59
66
|receipt END #################
60
67
|""" .trimMargin()
61
- )
62
- if (settings.enabled && job.isNotEmpty) {
63
- controller.setIntensity(settings.printingIntensity)
64
- controller.setFontSize(controller.getPrinterInfo().printingFontType)
65
- job.commands.forEach { command ->
66
- when (command) {
67
- is PrintCommand .Barcode -> controller.printBarcode(command.barcode)
68
- is PrintCommand .Image -> controller.printImage(command.image)
69
- is PrintCommand .QrCode -> controller.printQr(command.code)
70
- is PrintCommand .RawData -> controller.sendRawData(command.data)
71
- is PrintCommand .Text -> controller.printText(command.text)
72
- PrintCommand .CutPaper -> controller.cutPaper()
73
- PrintCommand .FeedPaper -> controller.feedPaper()
68
+ )
69
+ if (settings.enabled && job.isNotEmpty) {
70
+ controller.setIntensity(settings.printingIntensity)
71
+ controller.setFontSize(controller.getPrinterInfo().printingFontType)
72
+ job.commands.forEach { command ->
73
+ when (command) {
74
+ is PrintCommand .Barcode -> controller.printBarcode(command.barcode)
75
+ is PrintCommand .Image -> controller.printImage(command.image)
76
+ is PrintCommand .QrCode -> controller.printQr(command.code)
77
+ is PrintCommand .RawData -> controller.sendRawData(command.data)
78
+ is PrintCommand .Text -> controller.printText(command.text)
79
+ PrintCommand .CutPaper -> controller.cutPaper()
80
+ PrintCommand .FeedPaper -> controller.feedPaper()
81
+ }
74
82
}
83
+ controller.start()
84
+ analytics?.logPrintReceipt(job.description)
75
85
}
76
- controller.start()
77
- analytics?.logPrintReceipt(job.description)
86
+ }.doOnError {
87
+ logWarning(" printing job '${job.description} '" )
88
+ analytics?.logErrorPrintReceipt(" printing text '${job.description} '" )
78
89
}
79
- }.doOnError {
80
- logWarning(" printing job '${job.description} '" )
81
- analytics?.logErrorPrintReceipt(" printing text '${job.description} '" )
82
90
}
83
91
84
92
private fun logInfo (message : String ) {
0 commit comments