@connexup/react-native-xprinter 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (145) hide show
  1. package/LICENSE +20 -0
  2. package/NativeXPrinter.podspec +23 -0
  3. package/README.md +24 -0
  4. package/android/build.gradle +73 -0
  5. package/android/gradle.properties +5 -0
  6. package/android/libs/printer-lib-3.4.7.aar +0 -0
  7. package/android/src/main/AndroidManifest.xml +2 -0
  8. package/android/src/main/java/com/connexup/xprinter/Errors.kt +11 -0
  9. package/android/src/main/java/com/connexup/xprinter/Util.kt +106 -0
  10. package/android/src/main/java/com/connexup/xprinter/XPrinterPackage.kt +77 -0
  11. package/android/src/main/java/com/connexup/xprinter/esc/Convert.kt +211 -0
  12. package/android/src/main/java/com/connexup/xprinter/esc/XPrinterESCModule.kt +1995 -0
  13. package/android/src/main/java/com/connexup/xprinter/printer/Convert.kt +46 -0
  14. package/android/src/main/java/com/connexup/xprinter/printer/DeviceConnection.kt +14 -0
  15. package/android/src/main/java/com/connexup/xprinter/printer/Printers.kt +153 -0
  16. package/android/src/main/java/com/connexup/xprinter/printer/XPrinterPrinterModule.kt +295 -0
  17. package/android/src/main/java/com/connexup/xprinter/tspl/Convert.kt +151 -0
  18. package/android/src/main/java/com/connexup/xprinter/tspl/XPrinterTSPLModule.kt +1408 -0
  19. package/android/src/main/java/com/connexup/xprinter/utils/XPrinterUtilsModule.kt +54 -0
  20. package/ios/ESC/ESCConvert.swift +283 -0
  21. package/ios/ESC/Models/RCTESCPrintBarCodeRequest.swift +23 -0
  22. package/ios/ESC/Models/RCTESCPrintBitmapRequest.swift +23 -0
  23. package/ios/ESC/Models/RCTESCPrintPDF417Request.swift +27 -0
  24. package/ios/ESC/Models/RCTESCPrintQRCodeRequest.swift +19 -0
  25. package/ios/ESC/Models/RCTESCPrintTableBarcode.swift +23 -0
  26. package/ios/ESC/Models/RCTESCPrintTableRequest.swift +19 -0
  27. package/ios/ESC/Models/RCTESCPrintTableRow.swift +19 -0
  28. package/ios/ESC/Models/RCTESCPrintTextRequest.swift +21 -0
  29. package/ios/ESC/Models/RCTESCPrintTextSizeRequest.swift +17 -0
  30. package/ios/ESC/Models/RCTESCSelectBitmapModelRequest.swift +17 -0
  31. package/ios/ESC/Models/RCTESCSetNetAllRequest.swift +19 -0
  32. package/ios/ESC/Models/RCTESCSetPrintAreaRequest.swift +19 -0
  33. package/ios/ESC/Models/RCTESCSetTextStyleRequest.swift +17 -0
  34. package/ios/ESC/Models/RCTESCWifiConfigRequest.swift +23 -0
  35. package/ios/ESC/NativeXPrinterESC.swift +1339 -0
  36. package/ios/ESC/RCTNativeXPrinterESC.h +10 -0
  37. package/ios/ESC/RCTNativeXPrinterESC.mm +541 -0
  38. package/ios/Errors.swift +11 -0
  39. package/ios/NativeErrorInfo.swift +21 -0
  40. package/ios/Printer/Connecter.swift +214 -0
  41. package/ios/Printer/Models/RCTDeviceConnectStatusChangeEvent.swift +19 -0
  42. package/ios/Printer/Models/RCTNetDevice.swift +21 -0
  43. package/ios/Printer/Models/RCTUSBDevice.swift +17 -0
  44. package/ios/Printer/Models/RCTUpdateUdpNetConfigRequest.swift +21 -0
  45. package/ios/Printer/NativeXPrinterPrinter.swift +209 -0
  46. package/ios/Printer/Printers.swift +59 -0
  47. package/ios/Printer/RCTNativeXPrinterPrinter.h +12 -0
  48. package/ios/Printer/RCTNativeXPrinterPrinter.mm +123 -0
  49. package/ios/PrinterSDK/Headers/CPCLCommand.h +324 -0
  50. package/ios/PrinterSDK/Headers/KDS_Log.h +47 -0
  51. package/ios/PrinterSDK/Headers/LabelDocument.h +32 -0
  52. package/ios/PrinterSDK/Headers/LabelImageTranster.h +19 -0
  53. package/ios/PrinterSDK/Headers/POSBLEManager.h +164 -0
  54. package/ios/PrinterSDK/Headers/POSCommand.h +760 -0
  55. package/ios/PrinterSDK/Headers/POSImageTranster.h +34 -0
  56. package/ios/PrinterSDK/Headers/POSPrinter.h +47 -0
  57. package/ios/PrinterSDK/Headers/POSPrinterSDK.h +13 -0
  58. package/ios/PrinterSDK/Headers/POSWIFIManager.h +162 -0
  59. package/ios/PrinterSDK/Headers/PTable.h +50 -0
  60. package/ios/PrinterSDK/Headers/PrinterProfile.h +28 -0
  61. package/ios/PrinterSDK/Headers/TSCBLEManager.h +142 -0
  62. package/ios/PrinterSDK/Headers/TSCCommand.h +701 -0
  63. package/ios/PrinterSDK/Headers/TSCPrinterSDK.h +15 -0
  64. package/ios/PrinterSDK/Headers/TSCWIFIManager.h +99 -0
  65. package/ios/PrinterSDK/Headers/WIFIConnecter.h +131 -0
  66. package/ios/PrinterSDK/Headers/ZPLCommand.h +372 -0
  67. package/ios/PrinterSDK/libPrinterSDK.a +0 -0
  68. package/ios/TSPL/Models/RCTTSPLBarRequest.swift +19 -0
  69. package/ios/TSPL/Models/RCTTSPLBarcodeRequest.swift +29 -0
  70. package/ios/TSPL/Models/RCTTSPLBitmapCompressionRequest.swift +25 -0
  71. package/ios/TSPL/Models/RCTTSPLBitmapRequest.swift +25 -0
  72. package/ios/TSPL/Models/RCTTSPLBoxRequest.swift +21 -0
  73. package/ios/TSPL/Models/RCTTSPLEraseRequest.swift +19 -0
  74. package/ios/TSPL/Models/RCTTSPLPDFBase64Request.swift +21 -0
  75. package/ios/TSPL/Models/RCTTSPLPutBMPRequest.swift +17 -0
  76. package/ios/TSPL/Models/RCTTSPLQRCodeRequest.swift +29 -0
  77. package/ios/TSPL/Models/RCTTSPLReverseRequest.swift +19 -0
  78. package/ios/TSPL/Models/RCTTSPLTextRequest.swift +25 -0
  79. package/ios/TSPL/NativeXPrinterTSPL.swift +831 -0
  80. package/ios/TSPL/RCTNativeXPrinterTSPL.h +10 -0
  81. package/ios/TSPL/RCTNativeXPrinterTSPL.mm +445 -0
  82. package/ios/TSPL/TSPLConvert.swift +140 -0
  83. package/ios/Utils/Models/RCTCheckIsPureWhiteImageRequest.swift +16 -0
  84. package/ios/Utils/NativeXPrinterUtils.swift +17 -0
  85. package/ios/Utils/RCTNativeXPrinterUtils.h +10 -0
  86. package/ios/Utils/RCTNativeXPrinterUtils.mm +38 -0
  87. package/ios/Utils.swift +126 -0
  88. package/lib/module/esc/NativeXPrinterESC.js +218 -0
  89. package/lib/module/esc/NativeXPrinterESC.js.map +1 -0
  90. package/lib/module/esc/index.js +324 -0
  91. package/lib/module/esc/index.js.map +1 -0
  92. package/lib/module/index.js +8 -0
  93. package/lib/module/index.js.map +1 -0
  94. package/lib/module/package.json +1 -0
  95. package/lib/module/printer/NativeXPrinterPrinter.js +30 -0
  96. package/lib/module/printer/NativeXPrinterPrinter.js.map +1 -0
  97. package/lib/module/printer/index.js +110 -0
  98. package/lib/module/printer/index.js.map +1 -0
  99. package/lib/module/tspl/NativeXPrinterTSPL.js +143 -0
  100. package/lib/module/tspl/NativeXPrinterTSPL.js.map +1 -0
  101. package/lib/module/tspl/index.js +347 -0
  102. package/lib/module/tspl/index.js.map +1 -0
  103. package/lib/module/type.js +12 -0
  104. package/lib/module/type.js.map +1 -0
  105. package/lib/module/util.js +4 -0
  106. package/lib/module/util.js.map +1 -0
  107. package/lib/module/utils/NativeXPrinterUtils.js +5 -0
  108. package/lib/module/utils/NativeXPrinterUtils.js.map +1 -0
  109. package/lib/module/utils/index.js +17 -0
  110. package/lib/module/utils/index.js.map +1 -0
  111. package/lib/typescript/package.json +1 -0
  112. package/lib/typescript/src/esc/NativeXPrinterESC.d.ts +343 -0
  113. package/lib/typescript/src/esc/NativeXPrinterESC.d.ts.map +1 -0
  114. package/lib/typescript/src/esc/index.d.ts +122 -0
  115. package/lib/typescript/src/esc/index.d.ts.map +1 -0
  116. package/lib/typescript/src/index.d.ts +6 -0
  117. package/lib/typescript/src/index.d.ts.map +1 -0
  118. package/lib/typescript/src/printer/NativeXPrinterPrinter.d.ts +61 -0
  119. package/lib/typescript/src/printer/NativeXPrinterPrinter.d.ts.map +1 -0
  120. package/lib/typescript/src/printer/index.d.ts +32 -0
  121. package/lib/typescript/src/printer/index.d.ts.map +1 -0
  122. package/lib/typescript/src/tspl/NativeXPrinterTSPL.d.ts +261 -0
  123. package/lib/typescript/src/tspl/NativeXPrinterTSPL.d.ts.map +1 -0
  124. package/lib/typescript/src/tspl/index.d.ts +128 -0
  125. package/lib/typescript/src/tspl/index.d.ts.map +1 -0
  126. package/lib/typescript/src/type.d.ts +9 -0
  127. package/lib/typescript/src/type.d.ts.map +1 -0
  128. package/lib/typescript/src/util.d.ts +2 -0
  129. package/lib/typescript/src/util.d.ts.map +1 -0
  130. package/lib/typescript/src/utils/NativeXPrinterUtils.d.ts +10 -0
  131. package/lib/typescript/src/utils/NativeXPrinterUtils.d.ts.map +1 -0
  132. package/lib/typescript/src/utils/index.d.ts +9 -0
  133. package/lib/typescript/src/utils/index.d.ts.map +1 -0
  134. package/package.json +178 -0
  135. package/src/esc/NativeXPrinterESC.ts +516 -0
  136. package/src/esc/index.ts +489 -0
  137. package/src/index.ts +5 -0
  138. package/src/printer/NativeXPrinterPrinter.ts +84 -0
  139. package/src/printer/index.ts +160 -0
  140. package/src/tspl/NativeXPrinterTSPL.ts +388 -0
  141. package/src/tspl/index.ts +535 -0
  142. package/src/type.ts +11 -0
  143. package/src/util.ts +1 -0
  144. package/src/utils/NativeXPrinterUtils.ts +14 -0
  145. package/src/utils/index.ts +27 -0
@@ -0,0 +1,1995 @@
1
+ package com.connexup.xprinter.esc
2
+
3
+ import android.graphics.Bitmap
4
+ import android.graphics.BitmapFactory
5
+ import com.connexup.xprinter.COMMON_ERROR
6
+ import com.connexup.xprinter.NOT_YET_IMPLEMENTED_ERROR
7
+ import com.connexup.xprinter.NativeXPrinterESCSpec
8
+ import com.connexup.xprinter.PARAMS_ERROR
9
+ import com.connexup.xprinter.PRINTER_NOT_FOUND_ERROR
10
+ import com.connexup.xprinter.loadAndScaleBitmap
11
+ import com.connexup.xprinter.parseAddressData
12
+ import com.connexup.xprinter.printer.Printers
13
+ import com.facebook.react.bridge.Promise
14
+ import com.facebook.react.bridge.ReactApplicationContext
15
+ import com.facebook.react.bridge.ReadableMap
16
+ import com.facebook.react.bridge.WritableNativeArray
17
+ import com.facebook.react.bridge.WritableNativeMap
18
+ import kotlinx.coroutines.CoroutineScope
19
+ import kotlinx.coroutines.Dispatchers
20
+ import kotlinx.coroutines.SupervisorJob
21
+ import kotlinx.coroutines.launch
22
+ import kotlinx.coroutines.withContext
23
+ import net.posprinter.POSConst.SPACE_DEFAULT
24
+ import net.posprinter.model.PTable
25
+ import net.posprinter.model.TableBarcode
26
+ import java.nio.charset.Charset
27
+
28
+ private const val DEFAULT_LINE_SPACE = -1
29
+
30
+ /**
31
+ * Description:
32
+ * @author lukechen@chancetop.com
33
+ * @date 2026/2/9
34
+ */
35
+ class XPrinterESCModule(reactContext: ReactApplicationContext) :
36
+ NativeXPrinterESCSpec(reactContext) {
37
+
38
+ private val scope: CoroutineScope by lazy {
39
+ CoroutineScope(
40
+ SupervisorJob() + Dispatchers.Main
41
+ )
42
+ }
43
+
44
+ private val printers = Printers.escPrinters
45
+
46
+ override fun setCharSet(
47
+ connectInfo: String,
48
+ charSet: String,
49
+ promise: Promise
50
+ ) {
51
+ scope.launch {
52
+ val printer = printers[connectInfo]
53
+ if (printer == null) {
54
+ promise.reject(
55
+ PRINTER_NOT_FOUND_ERROR,
56
+ "not found printer:${connectInfo}"
57
+ )
58
+ return@launch
59
+ }
60
+ val convertCharSet = charSet.toCharSet()
61
+ if (convertCharSet == null) {
62
+ promise.reject(
63
+ PARAMS_ERROR,
64
+ "charSet is error"
65
+ )
66
+ return@launch
67
+ }
68
+
69
+ try {
70
+ withContext(Dispatchers.IO) {
71
+ printer.setCharSet(convertCharSet)
72
+ }
73
+ promise.resolve(null)
74
+ } catch (e: Exception) {
75
+ promise.reject(
76
+ COMMON_ERROR,
77
+ e.message,
78
+ e
79
+ )
80
+ }
81
+ }
82
+ }
83
+
84
+ override fun selectInternationCharacterSets(
85
+ connectInfo: String,
86
+ n: String,
87
+ promise: Promise
88
+ ) {
89
+ promise.reject(NOT_YET_IMPLEMENTED_ERROR, "Not yet implemented")
90
+ }
91
+
92
+ override fun isConnect(connectInfo: String, promise: Promise) {
93
+ promise.reject(NOT_YET_IMPLEMENTED_ERROR, "Not yet implemented")
94
+ }
95
+
96
+ override fun initializePrinter(
97
+ connectInfo: String,
98
+ promise: Promise
99
+ ) {
100
+ scope.launch {
101
+ val printer = printers[connectInfo]
102
+ if (printer == null) {
103
+ promise.reject(
104
+ PRINTER_NOT_FOUND_ERROR,
105
+ "not found printer:${connectInfo}"
106
+ )
107
+ return@launch
108
+ }
109
+
110
+ try {
111
+ withContext(Dispatchers.IO) {
112
+ printer.initializePrinter()
113
+ }
114
+ promise.resolve(null)
115
+ } catch (e: Exception) {
116
+ promise.reject(
117
+ COMMON_ERROR,
118
+ e.message,
119
+ e
120
+ )
121
+ }
122
+ }
123
+ }
124
+
125
+ override fun printString(
126
+ connectInfo: String,
127
+ data: String,
128
+ promise: Promise
129
+ ) {
130
+ scope.launch {
131
+ val printer = printers[connectInfo]
132
+ if (printer == null) {
133
+ promise.reject(
134
+ PRINTER_NOT_FOUND_ERROR,
135
+ "not found printer:${connectInfo}"
136
+ )
137
+ return@launch
138
+ }
139
+
140
+ try {
141
+ withContext(Dispatchers.IO) {
142
+ printer.printString(data)
143
+ }
144
+ promise.resolve(null)
145
+ } catch (e: Exception) {
146
+ promise.reject(
147
+ COMMON_ERROR,
148
+ e.message,
149
+ e
150
+ )
151
+ }
152
+ }
153
+ }
154
+
155
+ override fun printTextSize(
156
+ connectInfo: String,
157
+ payload: ReadableMap,
158
+ promise: Promise
159
+ ) {
160
+ scope.launch {
161
+ val printer = printers[connectInfo]
162
+ if (printer == null) {
163
+ promise.reject(
164
+ PRINTER_NOT_FOUND_ERROR,
165
+ "not found printer:${connectInfo}"
166
+ )
167
+ return@launch
168
+ }
169
+
170
+ val data = payload.getString("data")
171
+ if (data == null) {
172
+ promise.reject(
173
+ PARAMS_ERROR,
174
+ "data is null"
175
+ )
176
+ return@launch
177
+ }
178
+
179
+ val textWidth = payload.getString("textWidth")?.toESCTextWidth()
180
+ if (textWidth == null) {
181
+ promise.reject(
182
+ PARAMS_ERROR,
183
+ "textWidth is error"
184
+ )
185
+ return@launch
186
+ }
187
+ val textHeight = payload.getString("textHeight")?.toESCTextHeight()
188
+ if (textHeight == null) {
189
+ promise.reject(
190
+ PARAMS_ERROR,
191
+ "textHeight is error"
192
+ )
193
+ return@launch
194
+ }
195
+
196
+
197
+ try {
198
+ withContext(Dispatchers.IO) {
199
+ printer.printTextSize(data, textWidth or textHeight)
200
+ }
201
+ promise.resolve(null)
202
+ } catch (e: Exception) {
203
+ promise.reject(
204
+ COMMON_ERROR,
205
+ e.message,
206
+ e,
207
+ )
208
+ }
209
+ }
210
+ }
211
+
212
+ override fun printTextAttribute(
213
+ connectInfo: String,
214
+ data: String,
215
+ attribute: String,
216
+ promise: Promise
217
+ ) {
218
+ scope.launch {
219
+ val printer = printers[connectInfo]
220
+ if (printer == null) {
221
+ promise.reject(
222
+ PRINTER_NOT_FOUND_ERROR,
223
+ "not found printer:${connectInfo}"
224
+ )
225
+ return@launch
226
+ }
227
+ val textAttribute = attribute.toESCTextAttribute()
228
+ if (textAttribute == null) {
229
+ promise.reject(
230
+ PARAMS_ERROR,
231
+ "attribute is error"
232
+ )
233
+ return@launch
234
+ }
235
+
236
+ try {
237
+ withContext(Dispatchers.IO) {
238
+ printer.printTextAttribute(data, textAttribute)
239
+ }
240
+ promise.resolve(null)
241
+ } catch (e: Exception) {
242
+ promise.reject(
243
+ COMMON_ERROR,
244
+ e.message,
245
+ e
246
+ )
247
+ }
248
+ }
249
+ }
250
+
251
+ override fun printTextAlignment(
252
+ connectInfo: String,
253
+ data: String,
254
+ alignment: String,
255
+ promise: Promise
256
+ ) {
257
+ scope.launch {
258
+ val printer = printers[connectInfo]
259
+ if (printer == null) {
260
+ promise.reject(
261
+ PRINTER_NOT_FOUND_ERROR,
262
+ "not found printer:${connectInfo}"
263
+ )
264
+ return@launch
265
+ }
266
+ val textAlign = alignment.toESCAlignment()
267
+ if (textAlign == null) {
268
+ promise.reject(
269
+ PARAMS_ERROR,
270
+ "alignment is error"
271
+ )
272
+ return@launch
273
+ }
274
+
275
+ try {
276
+ withContext(Dispatchers.IO) {
277
+ printer.printTextAlignment(data, textAlign)
278
+ }
279
+ promise.resolve(null)
280
+ } catch (e: Exception) {
281
+ promise.reject(
282
+ COMMON_ERROR,
283
+ e.message,
284
+ e,
285
+ )
286
+ }
287
+ }
288
+ }
289
+
290
+ override fun printText(
291
+ connectInfo: String,
292
+ payload: ReadableMap,
293
+ promise: Promise
294
+ ) {
295
+ scope.launch {
296
+ val printer = printers[connectInfo]
297
+ if (printer == null) {
298
+ promise.reject(
299
+ PRINTER_NOT_FOUND_ERROR,
300
+ "not found printer:${connectInfo}"
301
+ )
302
+ return@launch
303
+ }
304
+
305
+ val data = payload.getString("data")
306
+ if (data == null) {
307
+ promise.reject(
308
+ PARAMS_ERROR,
309
+ "data is null"
310
+ )
311
+ return@launch
312
+ }
313
+ val alignment = payload.getString("alignment")?.toESCAlignment()
314
+ if (alignment == null) {
315
+ promise.reject(
316
+ PARAMS_ERROR,
317
+ "alignment is error"
318
+ )
319
+ return@launch
320
+ }
321
+
322
+ val attribute = payload.getString("attribute")?.toESCTextAttribute()
323
+ if (attribute == null) {
324
+ promise.reject(
325
+ PARAMS_ERROR,
326
+ "attribute is error"
327
+ )
328
+ return@launch
329
+ }
330
+ val textWidth = payload.getString("textWidth")?.toESCTextWidth()
331
+ if (textWidth == null) {
332
+ promise.reject(
333
+ PARAMS_ERROR,
334
+ "textWidth is error"
335
+ )
336
+ return@launch
337
+ }
338
+ val textHeight = payload.getString("textHeight")?.toESCTextHeight()
339
+ if (textHeight == null) {
340
+ promise.reject(
341
+ PARAMS_ERROR,
342
+ "textHeight is error"
343
+ )
344
+ return@launch
345
+ }
346
+
347
+ try {
348
+ withContext(Dispatchers.IO) {
349
+ printer.printText(data, alignment, attribute, textWidth or textHeight)
350
+ }
351
+ promise.resolve(null)
352
+ } catch (e: Exception) {
353
+ promise.reject(
354
+ COMMON_ERROR,
355
+ e.message,
356
+ e
357
+ )
358
+ }
359
+ }
360
+ }
361
+
362
+ override fun printBitmap(
363
+ connectInfo: String,
364
+ payload: ReadableMap,
365
+ promise: Promise
366
+ ) {
367
+ scope.launch {
368
+ val printer = printers[connectInfo]
369
+ if (printer == null) {
370
+ promise.reject(
371
+ PRINTER_NOT_FOUND_ERROR,
372
+ "not found printer:${connectInfo}"
373
+ )
374
+ return@launch
375
+ }
376
+
377
+ val bitmapPath = payload.getString("bitmapPath")
378
+ val bitmapContent = payload.getString("bitmapContent")
379
+ if (bitmapPath == null && bitmapContent == null) {
380
+ promise.reject(
381
+ PARAMS_ERROR,
382
+ "bitmapPath is null and bitmapContent is null"
383
+ )
384
+ return@launch
385
+ }
386
+
387
+ val alignment = payload.getString("alignment")?.toESCAlignment()
388
+ if (alignment == null) {
389
+ promise.reject(
390
+ PARAMS_ERROR,
391
+ "alignment is error"
392
+ )
393
+ return@launch
394
+ }
395
+ val width = payload.getInt("width")
396
+ val model = payload.getString("model")?.toESCBmpModel()
397
+ if (model == null) {
398
+ promise.reject(
399
+ PARAMS_ERROR,
400
+ "model is error"
401
+ )
402
+ return@launch
403
+ }
404
+
405
+ var bitmap: Bitmap? = null
406
+ try {
407
+ withContext(Dispatchers.IO) {
408
+ bitmap = loadAndScaleBitmap(
409
+ bitmapContent = bitmapContent,
410
+ bitmapPath = bitmapPath,
411
+ width = width
412
+ )
413
+ printer.printBitmap(bitmap, alignment, width, model)
414
+ }
415
+ promise.resolve(null)
416
+ } catch (e: Exception) {
417
+ promise.reject(
418
+ COMMON_ERROR,
419
+ e.message,
420
+ e
421
+ )
422
+ }
423
+ scope.launch(Dispatchers.IO) {
424
+ try {
425
+ if (bitmap !== null && !bitmap.isRecycled) {
426
+ bitmap.recycle()
427
+ }
428
+ } catch (_: Exception) {
429
+ }
430
+ }
431
+ }
432
+ }
433
+
434
+ override fun compressionPrintBitmap(
435
+ connectInfo: String,
436
+ payload: ReadableMap,
437
+ promise: Promise
438
+ ) {
439
+ scope.launch {
440
+ val printer = printers[connectInfo]
441
+ if (printer == null) {
442
+ promise.reject(
443
+ PRINTER_NOT_FOUND_ERROR,
444
+ "not found printer:${connectInfo}"
445
+ )
446
+ return@launch
447
+ }
448
+
449
+ val bitmapPath = payload.getString("bitmapPath")
450
+ val bitmapContent = payload.getString("bitmapContent")
451
+ if (bitmapPath == null && bitmapContent == null) {
452
+ promise.reject(
453
+ PARAMS_ERROR,
454
+ "bitmapPath is null and bitmapContent is null"
455
+ )
456
+ return@launch
457
+ }
458
+
459
+ val alignment = payload.getString("alignment")?.toESCAlignment()
460
+ if (alignment == null) {
461
+ promise.reject(
462
+ PARAMS_ERROR,
463
+ "alignment is error"
464
+ )
465
+ return@launch
466
+ }
467
+ val width = payload.getInt("width")
468
+ val model = payload.getString("model")?.toESCBmpModel()
469
+ if (model == null) {
470
+ promise.reject(
471
+ PARAMS_ERROR,
472
+ "model is error"
473
+ )
474
+ return@launch
475
+ }
476
+
477
+ var bitmap: Bitmap? = null
478
+ try {
479
+ withContext(Dispatchers.IO) {
480
+ bitmap = loadAndScaleBitmap(
481
+ bitmapContent = bitmapContent,
482
+ bitmapPath = bitmapPath,
483
+ width = width
484
+ )
485
+ printer.printBitmapCompress(
486
+ bitmap,
487
+ alignment,
488
+ width,
489
+ model
490
+ )
491
+ }
492
+ promise.resolve(null)
493
+ } catch (e: Exception) {
494
+ promise.reject(
495
+ COMMON_ERROR,
496
+ e.message,
497
+ e,
498
+ )
499
+ }
500
+ scope.launch(Dispatchers.IO) {
501
+ try {
502
+ if (bitmap !== null && !bitmap.isRecycled) {
503
+ bitmap.recycle()
504
+ }
505
+ } catch (_: Exception) {
506
+ }
507
+ }
508
+ }
509
+ }
510
+
511
+ override fun printBarCode(
512
+ connectInfo: String,
513
+ payload: ReadableMap,
514
+ promise: Promise
515
+ ) {
516
+ scope.launch {
517
+ val printer = printers[connectInfo]
518
+ if (printer == null) {
519
+ promise.reject(
520
+ PRINTER_NOT_FOUND_ERROR,
521
+ "not found printer:${connectInfo}"
522
+ )
523
+ return@launch
524
+ }
525
+
526
+ val data = payload.getString("data")
527
+ if (data == null) {
528
+ promise.reject(
529
+ PARAMS_ERROR,
530
+ "data is null"
531
+ )
532
+ return@launch
533
+ }
534
+ val codeType = payload.getString("codeType")?.toESCBarcodeType()
535
+ if (codeType == null) {
536
+ promise.reject(
537
+ PARAMS_ERROR,
538
+ "codeType is error"
539
+ )
540
+ return@launch
541
+ }
542
+ val width = payload.getInt("width")
543
+ val height = payload.getInt("height")
544
+ val alignment = payload.getString("alignment")?.toESCAlignment()
545
+ if (alignment == null) {
546
+ promise.reject(
547
+ PARAMS_ERROR,
548
+ "alignment is error"
549
+ )
550
+ return@launch
551
+ }
552
+ val textPosition = payload.getString("textPosition")?.toESCTextPosition()
553
+ if (textPosition == null) {
554
+ promise.reject(
555
+ PARAMS_ERROR,
556
+ "textPosition is error"
557
+ )
558
+ return@launch
559
+ }
560
+
561
+ try {
562
+ withContext(Dispatchers.IO) {
563
+ printer.printBarCode(data, codeType, width, height, alignment, textPosition)
564
+ }
565
+ promise.resolve(null)
566
+ } catch (e: Exception) {
567
+ promise.reject(
568
+ COMMON_ERROR,
569
+ e.message,
570
+ e
571
+ )
572
+ }
573
+ }
574
+ }
575
+
576
+ override fun lineFeed(
577
+ connectInfo: String,
578
+ lineCount: Double,
579
+ promise: Promise
580
+ ) {
581
+ scope.launch {
582
+ val printer = printers[connectInfo]
583
+ if (printer == null) {
584
+ promise.reject(
585
+ PRINTER_NOT_FOUND_ERROR,
586
+ "not found printer:${connectInfo}"
587
+ )
588
+ return@launch
589
+ }
590
+
591
+ try {
592
+ withContext(Dispatchers.IO) {
593
+ printer.feedLine(lineCount.toInt())
594
+ }
595
+ promise.resolve(null)
596
+ } catch (e: Exception) {
597
+ promise.reject(
598
+ COMMON_ERROR,
599
+ e.message,
600
+ e
601
+ )
602
+ }
603
+ }
604
+ }
605
+
606
+ override fun feedDot(
607
+ connectInfo: String,
608
+ dotCount: Double,
609
+ promise: Promise
610
+ ) {
611
+ scope.launch {
612
+ val printer = printers[connectInfo]
613
+ if (printer == null) {
614
+ promise.reject(
615
+ PRINTER_NOT_FOUND_ERROR,
616
+ "not found printer:${connectInfo}"
617
+ )
618
+ return@launch
619
+ }
620
+
621
+ try {
622
+ withContext(Dispatchers.IO) {
623
+ printer.feedDot(dotCount.toInt())
624
+ }
625
+ promise.resolve(null)
626
+ } catch (e: Exception) {
627
+ promise.reject(
628
+ COMMON_ERROR,
629
+ e.message,
630
+ e
631
+ )
632
+ }
633
+ }
634
+ }
635
+
636
+ override fun printerCheck(
637
+ connectInfo: String,
638
+ type: String,
639
+ timeout: Double,
640
+ promise: Promise
641
+ ) {
642
+ scope.launch {
643
+ val printer = printers[connectInfo]
644
+ if (printer == null) {
645
+ promise.reject(
646
+ PRINTER_NOT_FOUND_ERROR,
647
+ "not found printer:${connectInfo}"
648
+ )
649
+ return@launch
650
+ }
651
+ val checkType = type.toESCPrinterCheckType()
652
+ if (checkType == null) {
653
+ promise.reject(
654
+ PARAMS_ERROR,
655
+ "type is error"
656
+ )
657
+ return@launch
658
+ }
659
+
660
+ try {
661
+ withContext(Dispatchers.IO) {
662
+ printer.printerCheck(checkType, timeout.toInt()) { data ->
663
+ scope.launch {
664
+ val result = WritableNativeMap()
665
+
666
+ val debugArray = WritableNativeArray()
667
+ if (data.isNotEmpty()) {
668
+ data.forEach {
669
+ debugArray.pushInt(it.toInt())
670
+ }
671
+ result.putString(
672
+ "status",
673
+ data[0].toInt().toESCPrinterCheckStatus()
674
+ )
675
+ } else {
676
+ result.putString("status", (-4).toESCPrinterCheckStatus())
677
+ }
678
+ result.putArray("debugArray", debugArray)
679
+ promise.resolve(result)
680
+ }
681
+ }
682
+ }
683
+ } catch (e: Exception) {
684
+ promise.reject(
685
+ COMMON_ERROR,
686
+ e.message,
687
+ e
688
+ )
689
+ }
690
+ }
691
+ }
692
+
693
+ override fun printerStatus(
694
+ connectInfo: String,
695
+ promise: Promise
696
+ ) {
697
+ scope.launch {
698
+ val printer = printers[connectInfo]
699
+ if (printer == null) {
700
+ promise.reject(
701
+ PRINTER_NOT_FOUND_ERROR,
702
+ "not found printer:${connectInfo}"
703
+ )
704
+ return@launch
705
+ }
706
+
707
+ try {
708
+ withContext(Dispatchers.IO) {
709
+ printer.printerStatus { status ->
710
+ scope.launch {
711
+ val result = WritableNativeMap()
712
+ result.putString("status", status.toESCPrinterStatus())
713
+ result.putInt("debugCode", status)
714
+ promise.resolve(result)
715
+ }
716
+ }
717
+ }
718
+ } catch (e: Exception) {
719
+ promise.reject(
720
+ COMMON_ERROR,
721
+ e.message,
722
+ e
723
+ )
724
+ }
725
+ }
726
+ }
727
+
728
+ override fun printerStatusII(
729
+ connectInfo: String,
730
+ promise: Promise
731
+ ) {
732
+ scope.launch {
733
+ val printer = printers[connectInfo]
734
+ if (printer == null) {
735
+ promise.reject(
736
+ PRINTER_NOT_FOUND_ERROR,
737
+ "not found printer:${connectInfo}"
738
+ )
739
+ return@launch
740
+ }
741
+
742
+ try {
743
+ withContext(Dispatchers.IO) {
744
+ printer.printerStatusII { status ->
745
+ scope.launch {
746
+ val result = WritableNativeMap()
747
+ result.putString("status", status.toESCPrinterStatusII())
748
+ result.putInt("debugCode", status)
749
+ promise.resolve(result)
750
+ }
751
+ }
752
+ }
753
+ } catch (e: Exception) {
754
+ promise.reject(
755
+ COMMON_ERROR,
756
+ e,
757
+ )
758
+ }
759
+ }
760
+ }
761
+
762
+ override fun printQRCode(
763
+ connectInfo: String,
764
+ payload: ReadableMap,
765
+ promise: Promise
766
+ ) {
767
+ scope.launch {
768
+ val printer = printers[connectInfo]
769
+ if (printer == null) {
770
+ promise.reject(
771
+ PRINTER_NOT_FOUND_ERROR,
772
+ "not found printer:${connectInfo}"
773
+ )
774
+ return@launch
775
+ }
776
+
777
+ val data = payload.getString("data")
778
+ if (data == null) {
779
+ promise.reject(
780
+ PARAMS_ERROR,
781
+ "data is null"
782
+ )
783
+ return@launch
784
+ }
785
+ val moduleSize = payload.getInt("moduleSize")
786
+ val ecLevel = payload.getString("ecLevel")?.toESCECLevel()
787
+ if (ecLevel == null) {
788
+ promise.reject(
789
+ PARAMS_ERROR,
790
+ "ecLevel is error"
791
+ )
792
+ return@launch
793
+ }
794
+ val alignment = payload.getString("alignment")?.toESCAlignment()
795
+ if (alignment == null) {
796
+ promise.reject(
797
+ PARAMS_ERROR,
798
+ "alignment is error"
799
+ )
800
+ return@launch
801
+ }
802
+
803
+ try {
804
+ withContext(Dispatchers.IO) {
805
+ printer.printQRCode(data, moduleSize, ecLevel, alignment)
806
+ }
807
+ promise.resolve(null)
808
+ } catch (e: Exception) {
809
+ promise.reject(
810
+ COMMON_ERROR,
811
+ e.message,
812
+ e
813
+ )
814
+ }
815
+ }
816
+ }
817
+
818
+ override fun cutPaper(
819
+ connectInfo: String,
820
+ model: String,
821
+ promise: Promise
822
+ ) {
823
+ scope.launch {
824
+ val printer = printers[connectInfo]
825
+ if (printer == null) {
826
+ promise.reject(
827
+ PRINTER_NOT_FOUND_ERROR,
828
+ "not found printer:${connectInfo}"
829
+ )
830
+ return@launch
831
+ }
832
+ val cutModel = model.toESCCutModel()
833
+ if (cutModel == null) {
834
+ promise.reject(
835
+ PARAMS_ERROR,
836
+ "model is error"
837
+ )
838
+ return@launch
839
+ }
840
+
841
+ try {
842
+ withContext(Dispatchers.IO) {
843
+ printer.cutPaper(cutModel)
844
+ }
845
+ promise.resolve(null)
846
+ } catch (e: Exception) {
847
+ promise.reject(
848
+ COMMON_ERROR,
849
+ e.message,
850
+ e
851
+ )
852
+ }
853
+ }
854
+ }
855
+
856
+ override fun cutHalfAndFeed(
857
+ connectInfo: String,
858
+ distance: Double,
859
+ promise: Promise
860
+ ) {
861
+ scope.launch {
862
+ val printer = printers[connectInfo]
863
+ if (printer == null) {
864
+ promise.reject(
865
+ PRINTER_NOT_FOUND_ERROR,
866
+ "not found printer:${connectInfo}"
867
+ )
868
+ return@launch
869
+ }
870
+
871
+ try {
872
+ withContext(Dispatchers.IO) {
873
+ printer.cutHalfAndFeed(distance.toInt())
874
+ }
875
+ promise.resolve(null)
876
+ } catch (e: Exception) {
877
+ promise.reject(
878
+ COMMON_ERROR,
879
+ e.message,
880
+ e
881
+ )
882
+ }
883
+ }
884
+ }
885
+
886
+ override fun setPrintArea(
887
+ connectInfo: String,
888
+ payload: ReadableMap,
889
+ promise: Promise
890
+ ) {
891
+ scope.launch {
892
+ val printer = printers[connectInfo]
893
+ if (printer == null) {
894
+ promise.reject(
895
+ PRINTER_NOT_FOUND_ERROR,
896
+ "not found printer:${connectInfo}"
897
+ )
898
+ return@launch
899
+ }
900
+ val x = payload.getInt("x")
901
+ val y = payload.getInt("y")
902
+ val width = payload.getInt("width")
903
+ val height = payload.getInt("height")
904
+
905
+ try {
906
+ withContext(Dispatchers.IO) {
907
+ printer.setPrintArea(x, y, width, height)
908
+ }
909
+ promise.resolve(null)
910
+ } catch (e: Exception) {
911
+ promise.reject(
912
+ COMMON_ERROR,
913
+ e.message,
914
+ e
915
+ )
916
+ }
917
+ }
918
+ }
919
+
920
+ override fun setPageModel(
921
+ connectInfo: String,
922
+ isOpen: Boolean,
923
+ promise: Promise
924
+ ) {
925
+ scope.launch {
926
+ val printer = printers[connectInfo]
927
+ if (printer == null) {
928
+ promise.reject(
929
+ PRINTER_NOT_FOUND_ERROR,
930
+ "not found printer:${connectInfo}"
931
+ )
932
+ return@launch
933
+ }
934
+
935
+ try {
936
+ withContext(Dispatchers.IO) {
937
+ printer.setPageModel(isOpen)
938
+ }
939
+ promise.resolve(null)
940
+ } catch (e: Exception) {
941
+ promise.reject(
942
+ COMMON_ERROR,
943
+ e.message,
944
+ e
945
+ )
946
+ }
947
+ }
948
+ }
949
+
950
+ override fun printPageModelData(
951
+ connectInfo: String,
952
+ promise: Promise
953
+ ) {
954
+ scope.launch {
955
+ val printer = printers[connectInfo]
956
+ if (printer == null) {
957
+ promise.reject(
958
+ PRINTER_NOT_FOUND_ERROR,
959
+ "not found printer:${connectInfo}"
960
+ )
961
+ return@launch
962
+ }
963
+
964
+ try {
965
+ withContext(Dispatchers.IO) {
966
+ printer.printPageModelData()
967
+ }
968
+ promise.resolve(null)
969
+ } catch (e: Exception) {
970
+ promise.reject(
971
+ COMMON_ERROR,
972
+ e,
973
+ )
974
+ }
975
+ }
976
+ }
977
+
978
+ override fun setPrintDirection(
979
+ connectInfo: String,
980
+ direction: String,
981
+ promise: Promise
982
+ ) {
983
+ scope.launch {
984
+ val printer = printers[connectInfo]
985
+ if (printer == null) {
986
+ promise.reject(
987
+ PRINTER_NOT_FOUND_ERROR,
988
+ "not found printer:${connectInfo}"
989
+ )
990
+ return@launch
991
+ }
992
+ val printDirection = direction.toESCDirection()
993
+ if (printDirection == null) {
994
+ promise.reject(
995
+ PARAMS_ERROR,
996
+ "direction is error"
997
+ )
998
+ return@launch
999
+ }
1000
+
1001
+ try {
1002
+ withContext(Dispatchers.IO) {
1003
+ printer.setPrintDirection(printDirection)
1004
+ }
1005
+ promise.resolve(null)
1006
+ } catch (e: Exception) {
1007
+ promise.reject(
1008
+ COMMON_ERROR,
1009
+ e.message,
1010
+ e
1011
+ )
1012
+ }
1013
+ }
1014
+ }
1015
+
1016
+ override fun setAbsoluteHorizontal(
1017
+ connectInfo: String,
1018
+ position: Double,
1019
+ promise: Promise
1020
+ ) {
1021
+ scope.launch {
1022
+ val printer = printers[connectInfo]
1023
+ if (printer == null) {
1024
+ promise.reject(
1025
+ PRINTER_NOT_FOUND_ERROR,
1026
+ "not found printer:${connectInfo}"
1027
+ )
1028
+ return@launch
1029
+ }
1030
+
1031
+ try {
1032
+ withContext(Dispatchers.IO) {
1033
+ printer.setAbsoluteHorizontal(position.toInt())
1034
+ }
1035
+ promise.resolve(null)
1036
+ } catch (e: Exception) {
1037
+ promise.reject(
1038
+ COMMON_ERROR,
1039
+ e.message,
1040
+ e
1041
+ )
1042
+ }
1043
+ }
1044
+ }
1045
+
1046
+ override fun setRelativeHorizontal(
1047
+ connectInfo: String,
1048
+ position: Double,
1049
+ promise: Promise
1050
+ ) {
1051
+ scope.launch {
1052
+ val printer = printers[connectInfo]
1053
+ if (printer == null) {
1054
+ promise.reject(
1055
+ PRINTER_NOT_FOUND_ERROR,
1056
+ "not found printer:${connectInfo}"
1057
+ )
1058
+ return@launch
1059
+ }
1060
+
1061
+ try {
1062
+ withContext(Dispatchers.IO) {
1063
+ printer.setRelativeHorizontal(position.toInt())
1064
+ }
1065
+ promise.resolve(null)
1066
+ } catch (e: Exception) {
1067
+ promise.reject(
1068
+ COMMON_ERROR,
1069
+ e,
1070
+ )
1071
+ }
1072
+ }
1073
+ }
1074
+
1075
+ override fun setAbsoluteVertical(
1076
+ connectInfo: String,
1077
+ position: Double,
1078
+ promise: Promise
1079
+ ) {
1080
+ scope.launch {
1081
+ val printer = printers[connectInfo]
1082
+ if (printer == null) {
1083
+ promise.reject(
1084
+ PRINTER_NOT_FOUND_ERROR,
1085
+ "not found printer:${connectInfo}"
1086
+ )
1087
+ return@launch
1088
+ }
1089
+
1090
+ try {
1091
+ withContext(Dispatchers.IO) {
1092
+ printer.setAbsoluteVertical(position.toInt())
1093
+ }
1094
+ promise.resolve(null)
1095
+ } catch (e: Exception) {
1096
+ promise.reject(
1097
+ COMMON_ERROR,
1098
+ e.message,
1099
+ e
1100
+ )
1101
+ }
1102
+ }
1103
+ }
1104
+
1105
+ override fun setRelativeVertical(
1106
+ connectInfo: String,
1107
+ position: Double,
1108
+ promise: Promise
1109
+ ) {
1110
+ scope.launch {
1111
+ val printer = printers[connectInfo]
1112
+ if (printer == null) {
1113
+ promise.reject(
1114
+ PRINTER_NOT_FOUND_ERROR,
1115
+ "not found printer:${connectInfo}"
1116
+ )
1117
+ return@launch
1118
+ }
1119
+
1120
+ try {
1121
+ withContext(Dispatchers.IO) {
1122
+ printer.setRelativeVertical(position.toInt())
1123
+ }
1124
+ promise.resolve(null)
1125
+ } catch (e: Exception) {
1126
+ promise.reject(
1127
+ COMMON_ERROR,
1128
+ e.message,
1129
+ e
1130
+ )
1131
+ }
1132
+ }
1133
+ }
1134
+
1135
+ override fun setTextStyle(
1136
+ connectInfo: String,
1137
+ payload: ReadableMap,
1138
+ promise: Promise
1139
+ ) {
1140
+ scope.launch {
1141
+ val printer = printers[connectInfo]
1142
+ if (printer == null) {
1143
+ promise.reject(
1144
+ PRINTER_NOT_FOUND_ERROR,
1145
+ "not found printer:${connectInfo}"
1146
+ )
1147
+ return@launch
1148
+ }
1149
+ val attribute = payload.getString("attribute")?.toESCTextAttribute()
1150
+ if (attribute == null) {
1151
+ promise.reject(
1152
+ PARAMS_ERROR,
1153
+ "attribute is error"
1154
+ )
1155
+ return@launch
1156
+ }
1157
+ val textWidth = payload.getString("textWidth")?.toESCTextWidth()
1158
+ if (textWidth == null) {
1159
+ promise.reject(
1160
+ PARAMS_ERROR,
1161
+ "textWidth is error"
1162
+ )
1163
+ return@launch
1164
+ }
1165
+ val textHeight = payload.getString("textHeight")?.toESCTextHeight()
1166
+ if (textHeight == null) {
1167
+ promise.reject(
1168
+ PARAMS_ERROR,
1169
+ "textHeight is error"
1170
+ )
1171
+ return@launch
1172
+ }
1173
+
1174
+ try {
1175
+ withContext(Dispatchers.IO) {
1176
+ printer.setTextStyle(attribute, textWidth or textHeight)
1177
+ }
1178
+ promise.resolve(null)
1179
+ } catch (e: Exception) {
1180
+ promise.reject(
1181
+ COMMON_ERROR,
1182
+ e.message,
1183
+ e
1184
+ )
1185
+ }
1186
+ }
1187
+ }
1188
+
1189
+ override fun setAlignment(
1190
+ connectInfo: String,
1191
+ alignment: String,
1192
+ promise: Promise
1193
+ ) {
1194
+ scope.launch {
1195
+ val printer = printers[connectInfo]
1196
+ if (printer == null) {
1197
+ promise.reject(
1198
+ PRINTER_NOT_FOUND_ERROR,
1199
+ "not found printer:${connectInfo}"
1200
+ )
1201
+ return@launch
1202
+ }
1203
+ val printAlignment = alignment.toESCAlignment()
1204
+ if (printAlignment == null) {
1205
+ promise.reject(
1206
+ PARAMS_ERROR,
1207
+ "alignment is error"
1208
+ )
1209
+ return@launch
1210
+ }
1211
+
1212
+ try {
1213
+ withContext(Dispatchers.IO) {
1214
+ printer.setAlignment(printAlignment)
1215
+ }
1216
+ promise.resolve(null)
1217
+ } catch (e: Exception) {
1218
+ promise.reject(
1219
+ COMMON_ERROR,
1220
+ e,
1221
+ )
1222
+ }
1223
+ }
1224
+ }
1225
+
1226
+ override fun downloadNVImage(
1227
+ connectInfo: String,
1228
+ imagePaths: String,
1229
+ imageWidth: Double,
1230
+ promise: Promise
1231
+ ) {
1232
+ scope.launch {
1233
+ val printer = printers[connectInfo]
1234
+ if (printer == null) {
1235
+ promise.reject(
1236
+ PRINTER_NOT_FOUND_ERROR,
1237
+ "not found printer:${connectInfo}"
1238
+ )
1239
+ return@launch
1240
+ }
1241
+
1242
+ try {
1243
+ withContext(Dispatchers.IO) {
1244
+ printer.downloadNVImage(imagePaths, imageWidth.toInt())
1245
+ }
1246
+ promise.resolve(null)
1247
+ } catch (e: Exception) {
1248
+ promise.reject(
1249
+ COMMON_ERROR,
1250
+ e.message,
1251
+ e
1252
+ )
1253
+ }
1254
+ }
1255
+ }
1256
+
1257
+ override fun clearNVImage(
1258
+ connectInfo: String,
1259
+ promise: Promise
1260
+ ) {
1261
+ scope.launch {
1262
+ val printer = printers[connectInfo]
1263
+ if (printer == null) {
1264
+ promise.reject(
1265
+ PRINTER_NOT_FOUND_ERROR,
1266
+ "not found printer:${connectInfo}"
1267
+ )
1268
+ return@launch
1269
+ }
1270
+
1271
+ try {
1272
+ withContext(Dispatchers.IO) {
1273
+ printer.clearNVImage()
1274
+ }
1275
+ promise.resolve(null)
1276
+ } catch (e: Exception) {
1277
+ promise.reject(
1278
+ COMMON_ERROR,
1279
+ e.message,
1280
+ e
1281
+ )
1282
+ }
1283
+ }
1284
+ }
1285
+
1286
+ override fun printNVImage(
1287
+ connectInfo: String,
1288
+ index: Double,
1289
+ model: String,
1290
+ promise: Promise
1291
+ ) {
1292
+ scope.launch {
1293
+ val printer = printers[connectInfo]
1294
+ if (printer == null) {
1295
+ promise.reject(
1296
+ PRINTER_NOT_FOUND_ERROR,
1297
+ "not found printer:${connectInfo}"
1298
+ )
1299
+ return@launch
1300
+ }
1301
+ val bmpModel = model.toESCBmpModel()
1302
+ if (bmpModel == null) {
1303
+ promise.reject(
1304
+ PARAMS_ERROR,
1305
+ "model is error"
1306
+ )
1307
+ return@launch
1308
+ }
1309
+
1310
+ try {
1311
+ withContext(Dispatchers.IO) {
1312
+ printer.printNVImage(index.toInt(), bmpModel)
1313
+ }
1314
+ promise.resolve(null)
1315
+ } catch (e: Exception) {
1316
+ promise.reject(
1317
+ COMMON_ERROR,
1318
+ e.message,
1319
+ e
1320
+ )
1321
+ }
1322
+ }
1323
+ }
1324
+
1325
+ override fun selectBitmapModel(
1326
+ connectInfo: String,
1327
+ payload: ReadableMap,
1328
+ promise: Promise
1329
+ ) {
1330
+ scope.launch {
1331
+ val printer = printers[connectInfo]
1332
+ if (printer == null) {
1333
+ promise.reject(
1334
+ PRINTER_NOT_FOUND_ERROR,
1335
+ "not found printer:${connectInfo}"
1336
+ )
1337
+ return@launch
1338
+ }
1339
+ val width = payload.getInt("width")
1340
+ val bitmapPath = payload.getString("bitmapPath")
1341
+ if (bitmapPath == null) {
1342
+ promise.reject(
1343
+ PARAMS_ERROR,
1344
+ "bitmapPath is null"
1345
+ )
1346
+ return@launch
1347
+ }
1348
+ val model = payload.getString("model")?.toESCSelectBitmapModel()
1349
+ if (model == null) {
1350
+ promise.reject(
1351
+ PARAMS_ERROR,
1352
+ "model is null"
1353
+ )
1354
+ return@launch
1355
+ }
1356
+
1357
+ try {
1358
+ withContext(Dispatchers.IO) {
1359
+ val bitmap = BitmapFactory.decodeFile(bitmapPath)
1360
+ printer.selectBitmapModel(model, width, bitmap)
1361
+ }
1362
+ promise.resolve(null)
1363
+ // if (!bitmap.isRecycled){
1364
+ // bitmap.recycle()
1365
+ // }
1366
+ } catch (e: Exception) {
1367
+ promise.reject(
1368
+ COMMON_ERROR,
1369
+ e.message,
1370
+ e,
1371
+ )
1372
+ }
1373
+ }
1374
+ }
1375
+
1376
+ override fun setLineSpacing(
1377
+ connectInfo: String,
1378
+ space: Double,
1379
+ promise: Promise
1380
+ ) {
1381
+ scope.launch {
1382
+ val printer = printers[connectInfo]
1383
+ if (printer == null) {
1384
+ promise.reject(
1385
+ PRINTER_NOT_FOUND_ERROR,
1386
+ "not found printer:${connectInfo}"
1387
+ )
1388
+ return@launch
1389
+ }
1390
+
1391
+ try {
1392
+ withContext(Dispatchers.IO) {
1393
+ printer.setLineSpacing(if (space.toInt() == DEFAULT_LINE_SPACE) SPACE_DEFAULT else space.toInt())
1394
+ }
1395
+ promise.resolve(null)
1396
+ } catch (e: Exception) {
1397
+ promise.reject(
1398
+ COMMON_ERROR,
1399
+ e.message,
1400
+ e
1401
+ )
1402
+ }
1403
+ }
1404
+ }
1405
+
1406
+ override fun setTurnUpsideDownMode(
1407
+ connectInfo: String,
1408
+ on: Boolean,
1409
+ promise: Promise
1410
+ ) {
1411
+ scope.launch {
1412
+ val printer = printers[connectInfo]
1413
+ if (printer == null) {
1414
+ promise.reject(
1415
+ PRINTER_NOT_FOUND_ERROR,
1416
+ "not found printer:${connectInfo}"
1417
+ )
1418
+ return@launch
1419
+ }
1420
+
1421
+ try {
1422
+ withContext(Dispatchers.IO) {
1423
+ printer.setTurnUpsideDownMode(on)
1424
+ }
1425
+ promise.resolve(null)
1426
+ } catch (e: Exception) {
1427
+ promise.reject(
1428
+ COMMON_ERROR,
1429
+ e.message,
1430
+ e
1431
+ )
1432
+ }
1433
+ }
1434
+ }
1435
+
1436
+ override fun selectCodePage(
1437
+ connectInfo: String,
1438
+ page: Double,
1439
+ promise: Promise
1440
+ ) {
1441
+ scope.launch {
1442
+ val printer = printers[connectInfo]
1443
+ if (printer == null) {
1444
+ promise.reject(
1445
+ PRINTER_NOT_FOUND_ERROR,
1446
+ "not found printer:${connectInfo}"
1447
+ )
1448
+ return@launch
1449
+ }
1450
+
1451
+ try {
1452
+ withContext(Dispatchers.IO) {
1453
+ printer.selectCodePage(page.toInt())
1454
+ }
1455
+ promise.resolve(null)
1456
+ } catch (e: Exception) {
1457
+ promise.reject(
1458
+ COMMON_ERROR,
1459
+ e.message,
1460
+ e,
1461
+ )
1462
+ }
1463
+ }
1464
+ }
1465
+
1466
+ override fun selectCharacterFont(
1467
+ connectInfo: String,
1468
+ font: String,
1469
+ promise: Promise
1470
+ ) {
1471
+ scope.launch {
1472
+ val printer = printers[connectInfo]
1473
+ if (printer == null) {
1474
+ promise.reject(
1475
+ PRINTER_NOT_FOUND_ERROR,
1476
+ "not found printer:${connectInfo}"
1477
+ )
1478
+ return@launch
1479
+ }
1480
+ val fontType = font.toESCFontType()
1481
+ if (fontType == null) {
1482
+ promise.reject(
1483
+ PARAMS_ERROR,
1484
+ "font is error"
1485
+ )
1486
+ return@launch
1487
+ }
1488
+
1489
+ try {
1490
+ withContext(Dispatchers.IO) {
1491
+ printer.selectCharacterFont(fontType)
1492
+ }
1493
+ promise.resolve(null)
1494
+ } catch (e: Exception) {
1495
+ promise.reject(
1496
+ COMMON_ERROR,
1497
+ e.message,
1498
+ e,
1499
+ )
1500
+ }
1501
+ }
1502
+ }
1503
+
1504
+ override fun printPDF417(
1505
+ connectInfo: String,
1506
+ payload: ReadableMap,
1507
+ promise: Promise
1508
+ ) {
1509
+ scope.launch {
1510
+ val printer = printers[connectInfo]
1511
+ if (printer == null) {
1512
+ promise.reject(
1513
+ PRINTER_NOT_FOUND_ERROR,
1514
+ "not found printer:${connectInfo}"
1515
+ )
1516
+ return@launch
1517
+ }
1518
+ val data = payload.getString("data")
1519
+ if (data == null) {
1520
+ promise.reject(
1521
+ PARAMS_ERROR,
1522
+ "data is null"
1523
+ )
1524
+ return@launch
1525
+ }
1526
+ val cellWidth = payload.getInt("cellWidth")
1527
+ val cellHeightRatio = payload.getInt("cellHeightRatio")
1528
+ val numberOfColumns = payload.getInt("numberOfColumns")
1529
+ val numberOfRows = payload.getInt("numberOfRows")
1530
+ val eclType = payload.getInt("eclType")
1531
+ val eclValue = payload.getInt("eclValue")
1532
+ val alignment = payload.getString("alignment")?.toESCAlignment()
1533
+ if (alignment == null) {
1534
+ promise.reject(
1535
+ PARAMS_ERROR,
1536
+ "alignment is error"
1537
+ )
1538
+ return@launch
1539
+ }
1540
+
1541
+ try {
1542
+ withContext(Dispatchers.IO) {
1543
+ printer.printPDF417(
1544
+ data,
1545
+ cellWidth,
1546
+ cellHeightRatio,
1547
+ numberOfColumns,
1548
+ numberOfRows,
1549
+ eclType,
1550
+ eclValue,
1551
+ alignment
1552
+ )
1553
+ }
1554
+ promise.resolve(null)
1555
+ } catch (e: Exception) {
1556
+ promise.reject(
1557
+ COMMON_ERROR,
1558
+ e.message,
1559
+ e,
1560
+ )
1561
+ }
1562
+ }
1563
+ }
1564
+
1565
+ override fun printTable(
1566
+ connectInfo: String,
1567
+ payload: ReadableMap,
1568
+ promise: Promise
1569
+ ) {
1570
+ scope.launch {
1571
+ val printer = printers[connectInfo]
1572
+ if (printer == null) {
1573
+ promise.reject(
1574
+ PRINTER_NOT_FOUND_ERROR,
1575
+ "not found printer:${connectInfo}"
1576
+ )
1577
+ return@launch
1578
+ }
1579
+
1580
+ val titles = payload.getArray("titles")
1581
+ val numberOfSingleBytesPerCol =
1582
+ payload.getArray("numberOfSingleBytesPerCol")
1583
+ val align = payload.getArray("align")
1584
+ val titleList = arrayListOf<String>()
1585
+ val numberOfSingleBytesPerColList = arrayListOf<Int>()
1586
+ val alignList = arrayListOf<Int>()
1587
+ if (titles != null) {
1588
+ for (i in 0 until titles.size()) {
1589
+ titleList.add(titles.getString(i) ?: "")
1590
+ }
1591
+ }
1592
+ if (numberOfSingleBytesPerCol != null) {
1593
+ for (i in 0 until numberOfSingleBytesPerCol.size()) {
1594
+ numberOfSingleBytesPerColList.add(numberOfSingleBytesPerCol.getInt(i))
1595
+ }
1596
+ }
1597
+ if (align != null) {
1598
+ for (i in 0 until align.size()) {
1599
+ alignList.add(align.getInt(i))
1600
+ }
1601
+ }
1602
+ val rows = payload.getArray("rows")
1603
+
1604
+ try {
1605
+ val table = PTable(
1606
+ titleList.toTypedArray(),
1607
+ numberOfSingleBytesPerColList.toTypedArray(),
1608
+ alignList.toTypedArray(),
1609
+ )
1610
+ if (rows != null) {
1611
+ for (i in 0 until rows.size()) {
1612
+ val rowMap = rows.getMap(i)
1613
+ val rowTitle = rowMap?.getString("title")
1614
+ val rowData = rowMap?.getString("data")
1615
+ val rowRemark = rowMap?.getString("remark")
1616
+ val rowDataList = arrayListOf<String>()
1617
+ if (rowData != null) {
1618
+ for (j in 0 until rows.size()) {
1619
+ rowDataList.add(rows.getString(j) ?: "")
1620
+ }
1621
+ }
1622
+ val rowBarcode = rowMap?.getMap("barcode")
1623
+ var rowTableBarcode: TableBarcode? = null
1624
+ if (rowBarcode != null) {
1625
+ val rowCodeType = rowBarcode.getString("codeType")?.toESCBarcodeType()
1626
+ if (rowCodeType == null) {
1627
+ promise.reject(
1628
+ PARAMS_ERROR,
1629
+ "codeType is error"
1630
+ )
1631
+ return@launch
1632
+ }
1633
+ val rowAlignment = rowBarcode.getString("alignment")?.toESCAlignment()
1634
+ if (rowAlignment == null) {
1635
+ promise.reject(
1636
+ PARAMS_ERROR,
1637
+ "alignment is error"
1638
+ )
1639
+ return@launch
1640
+ }
1641
+ val rowTextPosition =
1642
+ rowBarcode.getString("textPosition")?.toESCTextPosition()
1643
+ if (rowTextPosition == null) {
1644
+ promise.reject(
1645
+ PARAMS_ERROR,
1646
+ "textPosition is error"
1647
+ )
1648
+ return@launch
1649
+ }
1650
+
1651
+ rowTableBarcode = TableBarcode(
1652
+ rowBarcode.getString("data"),
1653
+ rowCodeType,
1654
+ rowBarcode.getInt("width"),
1655
+ rowBarcode.getInt("height"),
1656
+ rowAlignment,
1657
+ rowTextPosition,
1658
+ )
1659
+ }
1660
+ table.addRow(
1661
+ rowTitle,
1662
+ rowDataList.toTypedArray(),
1663
+ rowRemark,
1664
+ rowTableBarcode
1665
+ )
1666
+ }
1667
+ }
1668
+ withContext(Dispatchers.IO) {
1669
+ printer.printTable(table)
1670
+ }
1671
+ promise.resolve(null)
1672
+ } catch (e: Exception) {
1673
+ promise.reject(
1674
+ COMMON_ERROR,
1675
+ e.message,
1676
+ e,
1677
+ )
1678
+ }
1679
+ }
1680
+ }
1681
+
1682
+ override fun wifiConfig(
1683
+ connectInfo: String,
1684
+ payload: ReadableMap,
1685
+ promise: Promise
1686
+ ) {
1687
+ scope.launch {
1688
+ val printer = printers[connectInfo]
1689
+ if (printer == null) {
1690
+ promise.reject(
1691
+ PRINTER_NOT_FOUND_ERROR,
1692
+ "not found printer:${connectInfo}"
1693
+ )
1694
+ return@launch
1695
+ }
1696
+ val ip = payload.getString("ip")?.let { parseAddressData(it) }
1697
+ if (ip == null) {
1698
+ promise.reject(
1699
+ PARAMS_ERROR,
1700
+ "ip is null"
1701
+ )
1702
+ return@launch
1703
+ }
1704
+ val mask = payload.getString("mask")?.let { parseAddressData(it) }
1705
+ if (mask == null) {
1706
+ promise.reject(
1707
+ PARAMS_ERROR,
1708
+ "mask is null"
1709
+ )
1710
+ return@launch
1711
+ }
1712
+ val gateway = payload.getString("gateway")?.let { parseAddressData(it) }
1713
+ if (gateway == null) {
1714
+ promise.reject(
1715
+ PARAMS_ERROR,
1716
+ "gateway is null"
1717
+ )
1718
+ return@launch
1719
+ }
1720
+ val ssid = payload.getString("ssid")
1721
+ if (ssid == null) {
1722
+ promise.reject(
1723
+ PARAMS_ERROR,
1724
+ "ssid is null"
1725
+ )
1726
+ return@launch
1727
+ }
1728
+ val password = payload.getString("password")
1729
+ if (password == null) {
1730
+ promise.reject(
1731
+ PARAMS_ERROR,
1732
+ "password is null"
1733
+ )
1734
+ return@launch
1735
+ }
1736
+ val encrypt = payload.getString("encrypt")?.toESCWifiEncrypt()
1737
+ if (encrypt == null) {
1738
+ promise.reject(
1739
+ PARAMS_ERROR,
1740
+ "encrypt is error"
1741
+ )
1742
+ return@launch
1743
+ }
1744
+
1745
+ try {
1746
+ withContext(Dispatchers.IO) {
1747
+ printer.wifiConfig(
1748
+ ip,
1749
+ mask,
1750
+ gateway,
1751
+ ssid,
1752
+ password,
1753
+ encrypt
1754
+ )
1755
+ }
1756
+ promise.resolve(null)
1757
+ } catch (e: Exception) {
1758
+ promise.reject(
1759
+ COMMON_ERROR,
1760
+ e.message,
1761
+ e,
1762
+ )
1763
+ }
1764
+ }
1765
+ }
1766
+
1767
+ override fun setIp(
1768
+ connectInfo: String,
1769
+ ip: String,
1770
+ promise: Promise
1771
+ ) {
1772
+ scope.launch {
1773
+ val printer = printers[connectInfo]
1774
+ if (printer == null) {
1775
+ promise.reject(
1776
+ PRINTER_NOT_FOUND_ERROR,
1777
+ "not found printer:${connectInfo}"
1778
+ )
1779
+ return@launch
1780
+ }
1781
+
1782
+ val ipBytes = parseAddressData(ip)
1783
+ if (ipBytes == null) {
1784
+ promise.reject(
1785
+ PARAMS_ERROR,
1786
+ "ip is error"
1787
+ )
1788
+ return@launch
1789
+ }
1790
+
1791
+ try {
1792
+ withContext(Dispatchers.IO) {
1793
+ printer.setIp(ipBytes)
1794
+ }
1795
+ promise.resolve(null)
1796
+ } catch (e: Exception) {
1797
+ promise.reject(
1798
+ COMMON_ERROR,
1799
+ e.message,
1800
+ e,
1801
+ )
1802
+ }
1803
+ }
1804
+ }
1805
+
1806
+ override fun setMask(
1807
+ connectInfo: String,
1808
+ mask: String,
1809
+ promise: Promise
1810
+ ) {
1811
+ scope.launch {
1812
+ val printer = printers[connectInfo]
1813
+ if (printer == null) {
1814
+ promise.reject(
1815
+ PRINTER_NOT_FOUND_ERROR,
1816
+ "not found printer:${connectInfo}"
1817
+ )
1818
+ return@launch
1819
+ }
1820
+
1821
+ try {
1822
+ withContext(Dispatchers.IO) {
1823
+ printer.setMask(mask.toByteArray())
1824
+ }
1825
+ promise.resolve(null)
1826
+ } catch (e: Exception) {
1827
+ promise.reject(
1828
+ COMMON_ERROR,
1829
+ e.message,
1830
+ e,
1831
+ )
1832
+ }
1833
+ }
1834
+ }
1835
+
1836
+ override fun setGateway(
1837
+ connectInfo: String,
1838
+ gateway: String,
1839
+ promise: Promise
1840
+ ) {
1841
+ scope.launch {
1842
+ val printer = printers[connectInfo]
1843
+ if (printer == null) {
1844
+ promise.reject(
1845
+ PRINTER_NOT_FOUND_ERROR,
1846
+ "not found printer:${connectInfo}"
1847
+ )
1848
+ return@launch
1849
+ }
1850
+
1851
+ try {
1852
+ printer.setGateway(gateway.toByteArray())
1853
+ promise.resolve(null)
1854
+ } catch (e: Exception) {
1855
+ promise.reject(
1856
+ COMMON_ERROR,
1857
+ e.message,
1858
+ e,
1859
+ )
1860
+ }
1861
+ }
1862
+ }
1863
+
1864
+ override fun setNetAll(
1865
+ connectInfo: String,
1866
+ payload: ReadableMap,
1867
+ promise: Promise
1868
+ ) {
1869
+ scope.launch {
1870
+ val printer = printers[connectInfo]
1871
+ if (printer == null) {
1872
+ promise.reject(
1873
+ PRINTER_NOT_FOUND_ERROR,
1874
+ "not found printer:${connectInfo}"
1875
+ )
1876
+ return@launch
1877
+ }
1878
+ val ip = payload.getString("ip")?.let { parseAddressData(it) }
1879
+ if (ip == null) {
1880
+ promise.reject(
1881
+ PARAMS_ERROR,
1882
+ "ip is null"
1883
+ )
1884
+ return@launch
1885
+ }
1886
+ val mask = payload.getString("mask")?.let { parseAddressData(it) }
1887
+ if (mask == null) {
1888
+ promise.reject(
1889
+ PARAMS_ERROR,
1890
+ "mask is null"
1891
+ )
1892
+ return@launch
1893
+ }
1894
+ val gateway = payload.getString("gateway")?.let { parseAddressData(it) }
1895
+ if (gateway == null) {
1896
+ promise.reject(
1897
+ PARAMS_ERROR,
1898
+ "gateway is null"
1899
+ )
1900
+ return@launch
1901
+ }
1902
+ val dhcpIsOpen = payload.getBoolean("dhcpIsOpen")
1903
+
1904
+ try {
1905
+ withContext(Dispatchers.IO) {
1906
+ printer.setNetAll(
1907
+ ip,
1908
+ mask,
1909
+ gateway,
1910
+ dhcpIsOpen
1911
+ )
1912
+ }
1913
+ promise.resolve(null)
1914
+ } catch (e: Exception) {
1915
+ promise.reject(
1916
+ COMMON_ERROR,
1917
+ e.message,
1918
+ e,
1919
+ )
1920
+ }
1921
+ }
1922
+ }
1923
+
1924
+ override fun setBluetooth(
1925
+ connectInfo: String,
1926
+ name: String,
1927
+ pin: String,
1928
+ promise: Promise
1929
+ ) {
1930
+ scope.launch {
1931
+ val printer = printers[connectInfo]
1932
+ if (printer == null) {
1933
+ promise.reject(
1934
+ PRINTER_NOT_FOUND_ERROR,
1935
+ "not found printer:${connectInfo}"
1936
+ )
1937
+ return@launch
1938
+ }
1939
+
1940
+ try {
1941
+ withContext(Dispatchers.IO) {
1942
+ printer.setBluetooth(
1943
+ name,
1944
+ pin
1945
+ )
1946
+ }
1947
+ promise.resolve(null)
1948
+ } catch (e: Exception) {
1949
+ promise.reject(
1950
+ COMMON_ERROR,
1951
+ e.message,
1952
+ e,
1953
+ )
1954
+ }
1955
+ }
1956
+ }
1957
+
1958
+ override fun getSerialNumber(
1959
+ connectInfo: String,
1960
+ promise: Promise
1961
+ ) {
1962
+ scope.launch {
1963
+ val printer = printers[connectInfo]
1964
+ if (printer == null) {
1965
+ promise.reject(
1966
+ PRINTER_NOT_FOUND_ERROR,
1967
+ "not found printer:${connectInfo}"
1968
+ )
1969
+ return@launch
1970
+ }
1971
+
1972
+ try {
1973
+ withContext(Dispatchers.IO) {
1974
+ printer.getSerialNumber {
1975
+ scope.launch {
1976
+ val result = WritableNativeMap()
1977
+ result.putString("data", it.toString(Charset.defaultCharset()))
1978
+ promise.resolve(result)
1979
+ }
1980
+ }
1981
+ }
1982
+ } catch (e: Exception) {
1983
+ promise.reject(
1984
+ COMMON_ERROR,
1985
+ e.message,
1986
+ e,
1987
+ )
1988
+ }
1989
+ }
1990
+ }
1991
+
1992
+ companion object {
1993
+ const val NAME = NativeXPrinterESCSpec.NAME
1994
+ }
1995
+ }