@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,46 @@
1
+ package com.connexup.xprinter.printer
2
+
3
+ import android.hardware.usb.UsbDevice
4
+ import com.facebook.react.bridge.ReadableMap
5
+ import com.facebook.react.bridge.WritableNativeMap
6
+ import net.posprinter.POSConnect
7
+
8
+ /**
9
+ * Description:
10
+ * @author lukechen@chancetop.com
11
+ * @date 2025/1/26
12
+ */
13
+
14
+ const val CONNECT_STATUS_DISCONNECT = -999
15
+
16
+ fun String.toDeviceType(): Int? {
17
+ return when (this) {
18
+ "USB" -> POSConnect.DEVICE_TYPE_USB
19
+ "BLUETOOTH" -> POSConnect.DEVICE_TYPE_SERIAL
20
+ "ETHERNET" -> POSConnect.DEVICE_TYPE_ETHERNET
21
+ "SERIAL" -> POSConnect.DEVICE_TYPE_BLUETOOTH
22
+ else -> null
23
+ }
24
+ }
25
+
26
+ fun Int.toConnectionConnectStatus(): String {
27
+ return when (this) {
28
+ POSConnect.CONNECT_SUCCESS -> "CONNECT_SUCCESS"
29
+ POSConnect.CONNECT_FAIL -> "CONNECT_FAIL"
30
+ POSConnect.SEND_FAIL -> "SEND_FAIL"
31
+ POSConnect.CONNECT_INTERRUPT -> "CONNECT_INTERRUPT"
32
+ POSConnect.USB_ATTACHED -> "USB_ATTACHED"
33
+ POSConnect.USB_DETACHED -> "USB_DETACHED"
34
+ POSConnect.BLUETOOTH_INTERRUPT -> "BLUETOOTH_INTERRUPT"
35
+ else -> "UNKNOWN"
36
+ }
37
+ }
38
+
39
+ val UsbDevice.readableMap: ReadableMap
40
+ get() {
41
+ val result = WritableNativeMap()
42
+ result.putInt("deviceId", this.deviceId)
43
+ result.putString("deviceName", this.deviceName)
44
+ result.putString("manufacturerName", this.manufacturerName)
45
+ return result
46
+ }
@@ -0,0 +1,14 @@
1
+ package com.connexup.xprinter.printer
2
+
3
+ import net.posprinter.IDeviceConnection
4
+
5
+ /**
6
+ * Description:
7
+ * @author lukechen@chancetop.com
8
+ * @date 2026/2/6
9
+ */
10
+
11
+ data class DeviceConnection(
12
+ val connection: IDeviceConnection,
13
+ val sync: Boolean
14
+ )
@@ -0,0 +1,153 @@
1
+ package com.connexup.xprinter.printer
2
+
3
+ import net.posprinter.POSPrinter
4
+ import net.posprinter.TSPLPrinter
5
+
6
+ /**
7
+ * Description:
8
+ * @author lukechen@chancetop.com
9
+ * @date 2025/1/21
10
+ */
11
+
12
+
13
+ enum class PCL {
14
+ ESC,
15
+ TSPL,
16
+ // CPCL,
17
+ // ZPL
18
+ }
19
+
20
+ fun String.toPCLType(): PCL? {
21
+ return when (this) {
22
+ "ESC" -> PCL.ESC
23
+ "TSPL" -> PCL.TSPL
24
+ // "CPCL" -> PCL.CPCL
25
+ // "ZPL" -> PCL.ZPL
26
+ else -> null
27
+ }
28
+ }
29
+
30
+ object Printers {
31
+
32
+ private val _connections: MutableMap<String, DeviceConnection> by lazy { hashMapOf() }
33
+ val connections: Map<String, DeviceConnection> get() = _connections
34
+
35
+ private val _escPrinters: MutableMap<String, POSPrinter> by lazy {
36
+ hashMapOf()
37
+ }
38
+ val escPrinters: Map<String, POSPrinter> get() = _escPrinters
39
+
40
+ private val _tsplPrinters: MutableMap<String, TSPLPrinter> by lazy {
41
+ hashMapOf()
42
+ }
43
+ val tsplPrinters: Map<String, TSPLPrinter> get() = _tsplPrinters
44
+
45
+ // private val _cpclPrinters: MutableMap<String, CPCLPrinter> by lazy {
46
+ // hashMapOf()
47
+ // }
48
+ // val cpclPrinters: Map<String, CPCLPrinter> get() = _cpclPrinters
49
+ //
50
+ // private val _zplPrinters: MutableMap<String, ZPLPrinter> by lazy {
51
+ // hashMapOf()
52
+ // }
53
+ // val zplPrinters: Map<String, ZPLPrinter> get() = _zplPrinters
54
+
55
+ private val allPrinters by lazy {
56
+ // arrayOf(_escPrinters, _tsplPrinters, _cpclPrinters, _zplPrinters)
57
+ arrayOf(_escPrinters, _tsplPrinters)
58
+ }
59
+
60
+ fun cleanup() {
61
+ val allConnections = connections.entries
62
+ allPrinters.forEach {
63
+ it.clear()
64
+ }
65
+ _connections.clear()
66
+ allConnections.forEach {
67
+ try {
68
+ it.value.connection.close()
69
+ } catch (_: Exception) {
70
+ }
71
+ }
72
+ }
73
+
74
+ fun setConnection(connectInfo: String, connection: DeviceConnection) {
75
+ _connections[connectInfo] = connection
76
+ }
77
+
78
+ fun removeConnection(connectInfo: String) {
79
+ removePrinter(connectInfo)
80
+ _connections.remove(connectInfo)
81
+ }
82
+
83
+ fun setupPrinter(connectInfo: String, pcl: PCL) {
84
+ removePrinter(connectInfo, pcl)
85
+ addPrinter(connectInfo, pcl)
86
+ }
87
+
88
+ fun getPrinterPCL(connectInfo: String): PCL? {
89
+ if (!escPrinters.containsKey(connectInfo)) {
90
+ return PCL.ESC
91
+ }
92
+ if (!tsplPrinters.containsKey(connectInfo)) {
93
+ return PCL.TSPL
94
+ }
95
+ return null
96
+ }
97
+
98
+ private fun addPrinter(
99
+ connectInfo: String,
100
+ pcl: PCL,
101
+ ) {
102
+
103
+ val connection = connections[connectInfo] ?: return
104
+ when (pcl) {
105
+ PCL.ESC -> {
106
+ if (!escPrinters.containsKey(connectInfo)) {
107
+ _escPrinters[connectInfo] = POSPrinter(connection.connection)
108
+ }
109
+ }
110
+
111
+ PCL.TSPL -> {
112
+ if (!tsplPrinters.containsKey(connectInfo)) {
113
+ _tsplPrinters[connectInfo] = TSPLPrinter(connection.connection)
114
+ }
115
+ }
116
+
117
+ // PCL.CPCL -> {
118
+ // if (!cpclPrinters.containsKey(connectInfo)) {
119
+ // _cpclPrinters[connectInfo] = CPCLPrinter(connection)
120
+ // }
121
+ // }
122
+ //
123
+ // PCL.ZPL -> {
124
+ // if (!zplPrinters.containsKey(connectInfo)) {
125
+ // _zplPrinters[connectInfo] = ZPLPrinter(connection)
126
+ // }
127
+ // }
128
+ }
129
+ }
130
+
131
+ private fun removePrinter(
132
+ connectInfo: String,
133
+ skipPCL: PCL? = null,
134
+ ) {
135
+ val printers = if (skipPCL == null) {
136
+ null
137
+ } else {
138
+ getPrintersByPCL(skipPCL)
139
+ }
140
+ allPrinters.forEach {
141
+ if (it != printers) {
142
+ it.remove(connectInfo)
143
+ }
144
+ }
145
+ }
146
+
147
+ private fun getPrintersByPCL(pcl: PCL) = when (pcl) {
148
+ PCL.ESC -> escPrinters
149
+ PCL.TSPL -> tsplPrinters
150
+ // PCL.CPCL -> cpclPrinters
151
+ // PCL.ZPL -> zplPrinters
152
+ }
153
+ }
@@ -0,0 +1,295 @@
1
+ package com.connexup.xprinter.printer
2
+
3
+ import com.connexup.xprinter.COMMON_ERROR
4
+ import com.connexup.xprinter.NativeXPrinterPrinterSpec
5
+ import com.connexup.xprinter.PARAMS_ERROR
6
+ import com.connexup.xprinter.PRINTER_NOT_FOUND_ERROR
7
+ import com.connexup.xprinter.parseAddressData
8
+ import com.facebook.react.bridge.Promise
9
+ import com.facebook.react.bridge.ReactApplicationContext
10
+ import com.facebook.react.bridge.ReadableMap
11
+ import com.facebook.react.bridge.WritableMap
12
+ import com.facebook.react.bridge.WritableNativeArray
13
+ import com.facebook.react.bridge.WritableNativeMap
14
+ import kotlinx.coroutines.CoroutineScope
15
+ import kotlinx.coroutines.Dispatchers
16
+ import kotlinx.coroutines.SupervisorJob
17
+ import kotlinx.coroutines.delay
18
+ import kotlinx.coroutines.launch
19
+ import kotlinx.coroutines.withContext
20
+ import net.posprinter.POSConnect
21
+ import net.posprinter.POSPrinter
22
+ import net.posprinter.model.UdpDevice
23
+
24
+ /**
25
+ * Description:
26
+ * @author lukechen@chancetop.com
27
+ * @date 2026/2/6
28
+ */
29
+ class XPrinterPrinterModule(reactContext: ReactApplicationContext) :
30
+ NativeXPrinterPrinterSpec(reactContext) {
31
+
32
+ private val scope: CoroutineScope by lazy {
33
+ CoroutineScope(
34
+ SupervisorJob() + Dispatchers.Main
35
+ )
36
+ }
37
+
38
+ override fun initialize() {
39
+ super.initialize()
40
+ POSConnect.init(reactApplicationContext)
41
+ }
42
+
43
+ override fun invalidate() {
44
+ super.invalidate()
45
+ Printers.cleanup()
46
+ }
47
+
48
+
49
+ override fun getUsbDevices(promise: Promise) {
50
+ scope.launch {
51
+ val context = reactApplicationContextIfActiveOrWarn
52
+ if (context == null) {
53
+ promise.reject(
54
+ COMMON_ERROR, "context is null"
55
+ )
56
+ return@launch
57
+ }
58
+ val result = WritableNativeMap()
59
+ val data = WritableNativeArray()
60
+ try {
61
+ val devices = withContext(Dispatchers.IO) {
62
+ POSConnect.getUsbDevice(context)
63
+ }
64
+ if (devices !== null) {
65
+ devices.forEach {
66
+ data.pushMap(it.readableMap)
67
+ }
68
+ }
69
+ result.putArray("data", data)
70
+ promise.resolve(result)
71
+ } catch (e: Exception) {
72
+ promise.reject(
73
+ COMMON_ERROR, e.message, e
74
+ )
75
+ }
76
+ }
77
+ }
78
+
79
+ override fun searchNetDevice(promise: Promise) {
80
+ scope.launch {
81
+ try {
82
+ withContext(Dispatchers.IO) {
83
+ POSPrinter.searchNetDevice { device ->
84
+ scope.launch {
85
+ sendSearchNetDeviceEvent(device)
86
+ }
87
+ }
88
+ }
89
+ promise.resolve(null)
90
+ } catch (e: Exception) {
91
+ promise.reject(COMMON_ERROR, e.message, e)
92
+ }
93
+ }
94
+ }
95
+
96
+ override fun udpNetConfig(
97
+ request: ReadableMap,
98
+ promise: Promise
99
+ ) {
100
+ scope.launch {
101
+ try {
102
+ val macAddress = request.getString("macAddress")?.let { parseAddressData(it) }
103
+ if (macAddress == null) {
104
+ promise.reject(PARAMS_ERROR, "macAddress is error")
105
+ return@launch
106
+ }
107
+ val ipAddress = request.getString("ipAddress")?.let { parseAddressData(it) }
108
+ if (ipAddress == null) {
109
+ promise.reject(PARAMS_ERROR, "ipAddress is error")
110
+ return@launch
111
+ }
112
+ val mask = request.getString("mask")?.let { parseAddressData(it) }
113
+ if (mask == null) {
114
+ promise.reject(PARAMS_ERROR, "mask is error")
115
+ return@launch
116
+ }
117
+ val gateway = request.getString("gateway")?.let { parseAddressData(it) }
118
+ if (gateway == null) {
119
+ promise.reject(PARAMS_ERROR, "gateway is error")
120
+ return@launch
121
+ }
122
+ val dhcp = request.getBoolean("dhcp")
123
+ withContext(Dispatchers.IO) {
124
+ POSPrinter.udpNetConfig(macAddress, ipAddress, mask, gateway, dhcp)
125
+ }
126
+ promise.resolve(null)
127
+ } catch (e: Exception) {
128
+ promise.reject(COMMON_ERROR, e.message, e)
129
+ }
130
+ }
131
+ }
132
+
133
+ override fun connect(
134
+ connectInfo: String,
135
+ deviceType: String,
136
+ sync: Boolean,
137
+ promise: Promise
138
+ ) {
139
+ scope.launch {
140
+ val convertDeviceType = deviceType.toDeviceType()
141
+ if (convertDeviceType == null) {
142
+ promise.reject(PARAMS_ERROR, "deviceType error")
143
+ return@launch
144
+ }
145
+ try {
146
+ val device = POSConnect.createDevice(convertDeviceType)
147
+ val connection = DeviceConnection(device, sync)
148
+ if (sync) {
149
+ val isConnect = withContext(Dispatchers.IO) {
150
+ device.connectSync(connectInfo) { code, connectInfo, msg ->
151
+ scope.launch {
152
+ if (code == POSConnect.CONNECT_SUCCESS) {
153
+ Printers.setConnection(connectInfo, connection)
154
+ }
155
+ sendDeviceConnectStatusChangeEvent(code, connectInfo, msg)
156
+ }
157
+ }
158
+ }
159
+ if (!isConnect) {
160
+ promise.reject(COMMON_ERROR, "connect fail")
161
+ return@launch
162
+ }
163
+ } else {
164
+ device.connect(connectInfo) { code, connectInfo, msg ->
165
+ if (code == POSConnect.CONNECT_SUCCESS) {
166
+ Printers.setConnection(connectInfo, connection)
167
+ }
168
+ sendDeviceConnectStatusChangeEvent(code, connectInfo, msg)
169
+ }
170
+ }
171
+ promise.resolve(null)
172
+ } catch (e: Exception) {
173
+ promise.reject(COMMON_ERROR, e.message, e)
174
+ }
175
+ }
176
+ }
177
+
178
+ override fun disconnect(
179
+ connectInfo: String, promise: Promise
180
+ ) {
181
+ scope.launch {
182
+ try {
183
+ val device = Printers.connections[connectInfo]
184
+ if (device == null) {
185
+ promise.resolve(null)
186
+ return@launch
187
+ }
188
+ var error: Exception? = null
189
+ withContext(Dispatchers.IO) {
190
+ if (device.sync) {
191
+ try {
192
+ device.connection.closeSync()
193
+ delay(12)
194
+ } catch (e: Exception) {
195
+ error = e
196
+ }
197
+ } else {
198
+ device.connection.close()
199
+ }
200
+ }
201
+ Printers.removeConnection(connectInfo)
202
+ sendDeviceConnectStatusChangeEvent(
203
+ CONNECT_STATUS_DISCONNECT,
204
+ connectInfo,
205
+ null
206
+ )
207
+ if (error != null) {
208
+ throw error
209
+ }
210
+ promise.resolve(null)
211
+ } catch (e: Exception) {
212
+ promise.reject(COMMON_ERROR, e.message, e)
213
+ }
214
+ }
215
+ }
216
+
217
+ override fun isConnect(connectInfo: String, promise: Promise) {
218
+ scope.launch {
219
+ val device = Printers.connections[connectInfo]
220
+ if (device == null) {
221
+ promise.resolve(false)
222
+ return@launch
223
+ }
224
+ val isConnect = withContext(Dispatchers.IO) {
225
+ device.connection.isConnect
226
+ }
227
+ promise.resolve(isConnect)
228
+ }
229
+ }
230
+
231
+ override fun getPrinterPCL(
232
+ connectInfo: String,
233
+ promise: Promise
234
+ ) {
235
+ scope.launch {
236
+ promise.resolve(Printers.getPrinterPCL(connectInfo)?.name)
237
+ }
238
+ }
239
+
240
+ override fun setPrinterPCL(
241
+ connectInfo: String,
242
+ pcl: String,
243
+ promise: Promise
244
+ ) {
245
+ scope.launch {
246
+ val device = Printers.connections[connectInfo]
247
+ if (device == null) {
248
+ promise.reject(
249
+ PRINTER_NOT_FOUND_ERROR,
250
+ "not found device connectInfo:${connectInfo}"
251
+ )
252
+ return@launch
253
+ }
254
+ val pclType = pcl.toPCLType()
255
+ if (pclType == null) {
256
+ promise.reject(
257
+ PARAMS_ERROR,
258
+ "pclType is error"
259
+ )
260
+ return@launch
261
+ }
262
+ Printers.setupPrinter(connectInfo, pclType)
263
+ promise.resolve(null)
264
+ }
265
+ }
266
+
267
+ private fun sendDeviceConnectStatusChangeEvent(
268
+ code: Int,
269
+ connectInfo: String,
270
+ msg: String?
271
+ ) {
272
+ val result: WritableMap = WritableNativeMap()
273
+ result.putString("code", code.toConnectionConnectStatus())
274
+ result.putInt("debugCode", code)
275
+ result.putString("connectInfo", connectInfo)
276
+ result.putString("msg", msg)
277
+ emitOnDeviceConnectStatusChange(result)
278
+ }
279
+
280
+ private fun sendSearchNetDeviceEvent(
281
+ device: UdpDevice
282
+ ) {
283
+ val result: WritableMap = WritableNativeMap()
284
+ result.putString("macAddress", device.macStr)
285
+ result.putString("ipAddress", device.ipStr)
286
+ result.putString("mask", device.maskStr)
287
+ result.putString("gateway", device.gatewayStr)
288
+ result.putBoolean("dhcp", device.isDhcp)
289
+ emitOnSearchNetDeviceFound(result)
290
+ }
291
+
292
+ companion object {
293
+ const val NAME = NativeXPrinterPrinterSpec.NAME
294
+ }
295
+ }
@@ -0,0 +1,151 @@
1
+ package com.connexup.xprinter.tspl
2
+
3
+ import net.posprinter.TSPLConst
4
+ import net.posprinter.model.AlgorithmType
5
+
6
+ /**
7
+ * Description:
8
+ * @author lukechen@chancetop.com
9
+ * @date 2025/7/10
10
+ */
11
+
12
+ fun String.toTSPLDirection(): Int? {
13
+ return when (this) {
14
+ "DIRECTION_FORWARD" -> TSPLConst.DIRECTION_FORWARD
15
+ "DIRECTION_REVERSE" -> TSPLConst.DIRECTION_REVERSE
16
+ else -> null
17
+ }
18
+ }
19
+
20
+ fun String.toTSPLBarcodeType(): String? {
21
+ return when (this) {
22
+ "CODE_TYPE_128" -> TSPLConst.CODE_TYPE_128
23
+ "CODE_TYPE_128M" -> TSPLConst.CODE_TYPE_128M
24
+ "CODE_TYPE_EAN128" -> TSPLConst.CODE_TYPE_EAN128
25
+ "CODE_TYPE_25" -> TSPLConst.CODE_TYPE_25
26
+ "CODE_TYPE_25C" -> TSPLConst.CODE_TYPE_25C
27
+ "CODE_TYPE_39" -> TSPLConst.CODE_TYPE_39
28
+ "CODE_TYPE_39C" -> TSPLConst.CODE_TYPE_39C
29
+ "CODE_TYPE_93" -> TSPLConst.CODE_TYPE_93
30
+ "CODE_TYPE_EAN13" -> TSPLConst.CODE_TYPE_EAN13
31
+ "CODE_TYPE_EAN13_2" -> TSPLConst.CODE_TYPE_EAN13_2
32
+ "CODE_TYPE_EAN13_5" -> TSPLConst.CODE_TYPE_EAN13_5
33
+ "CODE_TYPE_EAN8" -> TSPLConst.CODE_TYPE_EAN8
34
+ "CODE_TYPE_EAN8_2" -> TSPLConst.CODE_TYPE_EAN8_2
35
+ "CODE_TYPE_EAN8_5" -> TSPLConst.CODE_TYPE_EAN8_5
36
+ "CODE_TYPE_CODA" -> TSPLConst.CODE_TYPE_CODA
37
+ "CODE_TYPE_POST" -> TSPLConst.CODE_TYPE_POST
38
+ "CODE_TYPE_UPCA" -> TSPLConst.CODE_TYPE_UPCA
39
+ "CODE_TYPE_UPCA_2" -> TSPLConst.CODE_TYPE_UPCA_2
40
+ "CODE_TYPE_UPCA_5" -> TSPLConst.CODE_TYPE_UPCA_5
41
+ "CODE_TYPE_UPCE" -> TSPLConst.CODE_TYPE_UPCE
42
+ "CODE_TYPE_UPCE_2" -> TSPLConst.CODE_TYPE_UPCE_2
43
+ "CODE_TYPE_UPCE_5" -> TSPLConst.CODE_TYPE_UPCE_5
44
+ "CODE_TYPE_CPOST" -> TSPLConst.CODE_TYPE_CPOST
45
+ "CODE_TYPE_MSI" -> TSPLConst.CODE_TYPE_MSI
46
+ "CODE_TYPE_MSIC" -> TSPLConst.CODE_TYPE_MSIC
47
+ "CODE_TYPE_PLESSEY" -> TSPLConst.CODE_TYPE_PLESSEY
48
+ "CODE_TYPE_ITF14" -> TSPLConst.CODE_TYPE_ITF14
49
+ "CODE_TYPE_EAN14" -> TSPLConst.CODE_TYPE_EAN14
50
+ "CODE_TYPE_11" -> TSPLConst.CODE_TYPE_11
51
+ "CODE_TYPE_TELEPEN" -> TSPLConst.CODE_TYPE_TELEPEN
52
+ "CODE_TYPE_TELEPENN" -> TSPLConst.CODE_TYPE_TELEPENN
53
+ "CODE_TYPE_PLANET" -> TSPLConst.CODE_TYPE_PLANET
54
+ "CODE_TYPE_CODE49" -> TSPLConst.CODE_TYPE_CODE49
55
+ "CODE_TYPE_DPI" -> TSPLConst.CODE_TYPE_DPI
56
+ "CODE_TYPE_DPL" -> TSPLConst.CODE_TYPE_DPL
57
+ else -> null
58
+ }
59
+ }
60
+
61
+ fun String.toTSPLReadable(): Int? {
62
+ return when (this) {
63
+ "READABLE_NONE" -> TSPLConst.READABLE_NONE
64
+ "READABLE_LEFT" -> TSPLConst.READABLE_LEFT
65
+ "READABLE_CENTER" -> TSPLConst.READABLE_CENTER
66
+ "READABLE_RIGHT" -> TSPLConst.READABLE_RIGHT
67
+ else -> null
68
+ }
69
+ }
70
+
71
+ fun String.toTSPLRotation(): Int? {
72
+ return when (this) {
73
+ "ROTATION_0" -> TSPLConst.ROTATION_0
74
+ "ROTATION_90" -> TSPLConst.ROTATION_90
75
+ "ROTATION_180" -> TSPLConst.ROTATION_180
76
+ "ROTATION_270" -> TSPLConst.ROTATION_270
77
+ else -> null
78
+ }
79
+ }
80
+
81
+ fun String.toTSPLFont(): String? {
82
+ return when (this) {
83
+ "FNT_8_12" -> TSPLConst.FNT_8_12
84
+ "FNT_12_20" -> TSPLConst.FNT_12_20
85
+ "FNT_16_24" -> TSPLConst.FNT_16_24
86
+ "FNT_24_32" -> TSPLConst.FNT_24_32
87
+ "FNT_32_48" -> TSPLConst.FNT_32_48
88
+ "FNT_14_19" -> TSPLConst.FNT_14_19
89
+ "FNT_14_25" -> TSPLConst.FNT_14_25
90
+ "FNT_21_27" -> TSPLConst.FNT_21_27
91
+ "FNT_SIMPLIFIED_CHINESE" -> TSPLConst.FNT_SIMPLIFIED_CHINESE
92
+ "FNT_TRADITIONAL_CHINESE" -> TSPLConst.FNT_TRADITIONAL_CHINESE
93
+ "FNT_KOREAN" -> TSPLConst.FNT_KOREAN
94
+ else -> null
95
+ }
96
+ }
97
+
98
+ fun String.toTSPLECLevel(): String? {
99
+ return when (this) {
100
+ "EC_LEVEL_L" -> TSPLConst.EC_LEVEL_L
101
+ "EC_LEVEL_M" -> TSPLConst.EC_LEVEL_M
102
+ "EC_LEVEL_Q" -> TSPLConst.EC_LEVEL_Q
103
+ "EC_LEVEL_H" -> TSPLConst.EC_LEVEL_H
104
+ else -> null
105
+ }
106
+ }
107
+
108
+ fun String.toTSPLQrcodeMode(): String? {
109
+ return when (this) {
110
+ "QRCODE_MODE_AUTO" -> TSPLConst.QRCODE_MODE_AUTO
111
+ "QRCODE_MODE_MANUAL" -> TSPLConst.QRCODE_MODE_MANUAL
112
+ else -> null
113
+ }
114
+ }
115
+
116
+ fun String.toTSPLQrcodeModel(): String? {
117
+ return when (this) {
118
+ "QRCODE_MODEL_M1" -> TSPLConst.QRCODE_MODEL_M1
119
+ "QRCODE_MODEL_M2" -> TSPLConst.QRCODE_MODEL_M2
120
+ else -> null
121
+ }
122
+ }
123
+
124
+ fun String.toTSPLBitmapMode(): Int? {
125
+ return when (this) {
126
+ "BMP_MODE_OVERWRITE" -> TSPLConst.BMP_MODE_OVERWRITE
127
+ "BMP_MODE_OR" -> TSPLConst.BMP_MODE_OR
128
+ "BMP_MODE_XOR" -> TSPLConst.BMP_MODE_XOR
129
+ else -> null
130
+ }
131
+ }
132
+
133
+ fun String.toTSPLBitmapCompressionMode(): Int? {
134
+ return when (this) {
135
+ "BMP_MODE_OVERWRITE_C" -> TSPLConst.BMP_MODE_OVERWRITE_C
136
+ "BMP_MODE_OR_C" -> TSPLConst.BMP_MODE_OR_C
137
+ "BMP_MODE_XOR_C" -> TSPLConst.BMP_MODE_XOR_C
138
+ else -> null
139
+ }
140
+ }
141
+
142
+ fun String.toTSPLBitmapAlgorithmType(): AlgorithmType? {
143
+ return when (this) {
144
+ "THRESHOLD" -> AlgorithmType.Threshold
145
+ "DITHERING" -> AlgorithmType.Dithering
146
+ "NONE" -> AlgorithmType.None
147
+ "DIFFUSION" -> AlgorithmType.Diffusion
148
+ "HALFTONE" -> AlgorithmType.Halftone
149
+ else -> null
150
+ }
151
+ }