@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,214 @@
1
+ //
2
+ // BluetoothConnecter.swift
3
+
4
+ //
5
+ // Created by chancetio on 2025/1/23.
6
+ //
7
+
8
+ enum ConnectType {
9
+ case ETHERNET
10
+ case BLUETOOTH
11
+ }
12
+
13
+ extension String {
14
+ func toConnectType() -> ConnectType? {
15
+ switch self {
16
+ case "BLUETOOTH":
17
+ return ConnectType.BLUETOOTH
18
+ case "ETHERNET":
19
+ return ConnectType.ETHERNET
20
+ default:
21
+ return nil
22
+ }
23
+ }
24
+ }
25
+
26
+ enum PCL: String, CaseIterable {
27
+ case ESC
28
+ case TSPL
29
+ // case CPCL
30
+ // case ZPL
31
+ }
32
+
33
+ extension String {
34
+ func toPCLType() -> PCL? {
35
+ return PCL.allCases.first { $0.rawValue == self }
36
+ }
37
+ }
38
+
39
+ class Connecter: WIFIConnecter {
40
+ let type: ConnectType
41
+ private lazy var bleManage = POSBLEManager()
42
+
43
+ weak var bluetoothDelegate: POSBLEManagerDelegate? {
44
+ didSet {
45
+ if bluetoothDelegate != nil {
46
+ bleManage.delegate = self
47
+ } else {
48
+ bleManage.delegate = nil
49
+ }
50
+ }
51
+ }
52
+
53
+ var pcl: PCL?
54
+
55
+ // var charSet:String.Encoding = .utf8
56
+ var charSet: String.Encoding = .init(rawValue: CFStringConvertEncodingToNSStringEncoding(CFStringEncoding(CFStringEncodings.GB_18030_2000.rawValue)))
57
+
58
+ private var pendingConnectInfo: String?
59
+
60
+ init(type: ConnectType) {
61
+ self.type = type
62
+ }
63
+
64
+ deinit {
65
+ delegate = nil
66
+ bluetoothDelegate = nil
67
+ }
68
+
69
+ func connectByBluetooth(connectInfo: String) {
70
+ pendingConnectInfo = connectInfo
71
+ bleManage.startScan()
72
+ }
73
+
74
+ func startBluetoothScan() {
75
+ pendingConnectInfo = nil
76
+ bleManage.startScan()
77
+ }
78
+
79
+ func isConnect() -> Bool {
80
+ if type == .BLUETOOTH {
81
+ return bleManage.printerIsConnect()
82
+ } else {
83
+ return printerCheckWithMac()
84
+ }
85
+ }
86
+
87
+ override func disconnect() {
88
+ if type == .BLUETOOTH {
89
+ bleManage.disconnectRootPeripheral()
90
+ } else {
91
+ super.disconnect()
92
+ }
93
+ }
94
+
95
+ override func writeCommand(with data: Data) {
96
+ if type == .BLUETOOTH {
97
+ bleManage.writeCommand(with: data)
98
+ } else {
99
+ super.writeCommand(with: data)
100
+ }
101
+ }
102
+
103
+ override func writeCommand(with data: Data, writeCallBack writeBlock: @escaping WIFIConnecterWriteBlock) {
104
+ if type == .BLUETOOTH {
105
+ bleManage.writeCommand(with: data, writeCallBack: { [weak self] _, error in
106
+ guard self != nil else { return }
107
+ writeBlock(error == nil, error)
108
+ })
109
+ } else {
110
+ super.writeCommand(with: data, writeCallBack: writeBlock)
111
+ }
112
+ }
113
+
114
+ // TODO: improve
115
+ override func writeCommand(with data: Data!, receiveCallBack receiveBlock: WIFIConnecterReceiveBlock!) {
116
+ if type == .BLUETOOTH {
117
+ bleManage.writeCommand(with: data, receiveCallBack: { [weak self] _, _ in
118
+ guard self != nil else { return }
119
+ receiveBlock(nil)
120
+ })
121
+ } else {
122
+ super.writeCommand(with: data, receiveCallBack: receiveBlock)
123
+ }
124
+ }
125
+
126
+ // TODO: improve
127
+ override func writeCommand(with data: Data!, subpackageLength: Int32, writeCallBack writeBlock: WIFIConnecterWriteBlock!) {
128
+ if type == .BLUETOOTH {
129
+ writeBlock(false, NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey: "unsupported method"]))
130
+ } else {
131
+ super.writeCommand(with: data, subpackageLength: subpackageLength, writeCallBack: writeBlock)
132
+ }
133
+ }
134
+
135
+ func printerCheck(type checkType: Int32, callBack: @escaping (Data?) -> Void) {
136
+ if type == .BLUETOOTH {
137
+ bleManage.printerCheck(checkType) { data in
138
+ callBack(data)
139
+ }
140
+ } else {
141
+ callBack(nil)
142
+ }
143
+ }
144
+
145
+ override func printerStatus(_ statusBlock: POSWiFiPrinterStatusBlock!) {
146
+ if type == .BLUETOOTH {
147
+ bleManage.printerStatus { data in
148
+ statusBlock(data)
149
+ }
150
+ } else {
151
+ super.printerStatus(statusBlock)
152
+ }
153
+ }
154
+
155
+ override func labelPrinterStatus(_ statusBlock: POSWiFiPrinterStatusBlock!) {
156
+ if type == .BLUETOOTH {
157
+ bleManage.printerStatus { data in
158
+ statusBlock(data)
159
+ }
160
+ } else {
161
+ super.labelPrinterStatus(statusBlock)
162
+ }
163
+ }
164
+
165
+ override func printerSN(snBlock: POSWiFiPrinterSNBlock!) {
166
+ if type == .BLUETOOTH {
167
+ bleManage.printerSN { data in
168
+ snBlock(data)
169
+ }
170
+ } else {
171
+ super.printerSN(snBlock: snBlock)
172
+ }
173
+ }
174
+
175
+ func setBluetooth(name: String, pin: String) {
176
+ bleManage.setBluetoothNameAndKeyWith(name, btKey: pin)
177
+ }
178
+ }
179
+
180
+ extension Connecter: POSBLEManagerDelegate {
181
+ func poSbleUpdatePeripheralList(_ peripherals: [Any], rssiList: [Any]) {
182
+ bluetoothDelegate?.poSbleUpdatePeripheralList?(peripherals, rssiList: rssiList)
183
+ for peripheral in peripherals {
184
+ if let peripheral = peripheral as? CBPeripheral, peripheral.identifier.uuidString == pendingConnectInfo {
185
+ pendingConnectInfo = nil
186
+ bleManage.connectDevice(peripheral)
187
+ }
188
+ }
189
+ }
190
+
191
+ func poSbleConnect(_ peripheral: CBPeripheral!) {
192
+ bluetoothDelegate?.poSbleConnect?(peripheral)
193
+ }
194
+
195
+ func poSbleFail(toConnect peripheral: CBPeripheral, error: any Error) {
196
+ bluetoothDelegate?.poSbleFail?(toConnect: peripheral, error: error)
197
+ }
198
+
199
+ func poSbleDisconnectPeripheral(_ peripheral: CBPeripheral, error: any Error) {
200
+ bluetoothDelegate?.poSbleDisconnectPeripheral?(peripheral, error: error)
201
+ }
202
+
203
+ func poSbleWriteValue(for character: CBCharacteristic, error: any Error) {
204
+ bluetoothDelegate?.poSbleWriteValue?(for: character, error: error)
205
+ }
206
+
207
+ func poSbleReceiveValue(for characteristic: CBCharacteristic, error: any Error) {
208
+ bluetoothDelegate?.poSbleReceiveValue?(for: characteristic, error: error)
209
+ }
210
+
211
+ func poSbleCentralManagerDidUpdateState(_ state: Int) {
212
+ bluetoothDelegate?.poSbleCentralManagerDidUpdateState?(state)
213
+ }
214
+ }
@@ -0,0 +1,19 @@
1
+ @objcMembers
2
+ public class RCTDeviceConnectStatusChangeEvent: NSObject {
3
+ public let code: String
4
+ public let connectInfo: String
5
+ public let msg: String?
6
+ public let debugCode: Double?
7
+
8
+ public init(code: String, connectInfo: String, msg: String?, debugCode: Double?) {
9
+ self.code = code
10
+ self.connectInfo = connectInfo
11
+ self.msg = msg
12
+ self.debugCode = debugCode
13
+ super.init()
14
+ }
15
+
16
+ override public var description: String {
17
+ "RCTDeviceConnectStatusChangeEvent(code=\(String(describing: code)), connectInfo=\(String(describing: connectInfo)), msg=\(String(describing: msg)), debugCode=\(String(describing: debugCode)))"
18
+ }
19
+ }
@@ -0,0 +1,21 @@
1
+ @objcMembers
2
+ public class RCTNetDevice: NSObject {
3
+ public let macAddress: String
4
+ public let ipAddress: String
5
+ public let mask: String
6
+ public let gateway: String
7
+ public let dhcp: Bool
8
+
9
+ public init(macAddress: String, ipAddress: String, mask: String, gateway: String, dhcp: Bool) {
10
+ self.macAddress = macAddress
11
+ self.ipAddress = ipAddress
12
+ self.mask = mask
13
+ self.gateway = gateway
14
+ self.dhcp = dhcp
15
+ super.init()
16
+ }
17
+
18
+ override public var description: String {
19
+ "RCTNetDevice(macAddress=\(String(describing: macAddress)), ipAddress=\(String(describing: ipAddress)), mask=\(String(describing: mask)), gateway=\(String(describing: gateway)), dhcp=\(String(describing: dhcp)))"
20
+ }
21
+ }
@@ -0,0 +1,17 @@
1
+ @objcMembers
2
+ public class RCTUSBDevice: NSObject {
3
+ public let deviceId: Double
4
+ public let deviceName: String
5
+ public let manufacturerName: String?
6
+
7
+ public init(deviceId: Double, deviceName: String, manufacturerName: String?) {
8
+ self.deviceId = deviceId
9
+ self.deviceName = deviceName
10
+ self.manufacturerName = manufacturerName
11
+ super.init()
12
+ }
13
+
14
+ override public var description: String {
15
+ "RCTUSBDevice(deviceId=\(String(describing: deviceId)), deviceName=\(String(describing: deviceName)), manufacturerName=\(String(describing: manufacturerName)))"
16
+ }
17
+ }
@@ -0,0 +1,21 @@
1
+ @objcMembers
2
+ public class RCTUpdateUdpNetConfigRequest: NSObject {
3
+ public let macAddress: String
4
+ public let ipAddress: String
5
+ public let mask: String
6
+ public let gateway: String
7
+ public let dhcp: Bool
8
+
9
+ public init(macAddress: String, ipAddress: String, mask: String, gateway: String, dhcp: Bool) {
10
+ self.macAddress = macAddress
11
+ self.ipAddress = ipAddress
12
+ self.mask = mask
13
+ self.gateway = gateway
14
+ self.dhcp = dhcp
15
+ super.init()
16
+ }
17
+
18
+ override public var description: String {
19
+ "RCTUpdateUdpNetConfigRequest(macAddress=\(String(describing: macAddress)), ipAddress=\(String(describing: ipAddress)), mask=\(String(describing: mask)), gateway=\(String(describing: gateway)), dhcp=\(String(describing: dhcp)))"
20
+ }
21
+ }
@@ -0,0 +1,209 @@
1
+ import Foundation
2
+
3
+ let CONNECT_STATUS_CONNECT_SUCCESS = "CONNECT_SUCCESS"
4
+ let CONNECT_STATUS_CONNECT_FAIL = "CONNECT_FAIL"
5
+ let CONNECT_STATUS_DISCONNECT = "DISCONNECT"
6
+ let CONNECT_STATUS_SEND_FAIL = "SEND_FAIL"
7
+ let CONNECT_STATUS_CONNECT_INTERRUPT = "CONNECT_INTERRUPT"
8
+ let CONNECT_STATUS_USB_ATTACHED = "USB_ATTACHED"
9
+ let CONNECT_STATUS_USB_DETACHED = "USB_DETACHED"
10
+ let CONNECT_STATUS_BLUETOOTH_INTERRUPT = "BLUETOOTH_INTERRUPT"
11
+ let CONNECT_STATUS_UNKNOWN = "UNKNOWN"
12
+
13
+ @objc public protocol RCTNativeXPrinterPrinterEventEmitter {
14
+ func onDeviceConnectStatusChange(_ value: [String: Any])
15
+ func onSearchNetDeviceFound(_ value: [String: Any])
16
+ }
17
+
18
+ @objcMembers
19
+ public class NativeXPrinterPrinter: NSObject {
20
+ private lazy var observer = NativeXPrinterPrinterObserver(owner: self)
21
+ public weak var emitter: RCTNativeXPrinterPrinterEventEmitter?
22
+
23
+ deinit {
24
+ invalidate()
25
+ }
26
+
27
+ public func invalidate() {
28
+ Printers.shared.clear()
29
+ }
30
+
31
+ public func searchNetDevice(completion: @escaping (_ error: NativeErrorInfo?) -> Void) {
32
+ DispatchQueue.main.async {
33
+ if POSWIFIManager.sharedInstance().createUdpSocket() {
34
+ POSWIFIManager.sharedInstance().sendFindCmd { [weak self] device in
35
+ guard let self = self else {
36
+ return
37
+ }
38
+ guard let device = device else {
39
+ return
40
+ }
41
+ self.sendSearchNetDeviceEvent(device)
42
+ }
43
+ completion(nil)
44
+ } else {
45
+ completion(NativeErrorInfo(code: PrinterErrors.COMMON_ERROR, message: "createUdpSocket", error: nil))
46
+ }
47
+ }
48
+ }
49
+
50
+ public func udpNetConfig(params: RCTUpdateUdpNetConfigRequest, completion: @escaping (_ error: NativeErrorInfo?) -> Void) {
51
+ DispatchQueue.main.async {
52
+ POSWIFIManager.sharedInstance().setIPConfigWithIP(params.ipAddress, mask: params.mask, gateway: params.gateway, dhcp: params.dhcp)
53
+ completion(nil)
54
+ }
55
+ }
56
+
57
+ public func connect(connectInfo: String, deviceType: String, sync: Bool, completion: @escaping (_ error: NativeErrorInfo?) -> Void) {
58
+ DispatchQueue.main.async {
59
+ guard let deviceType = deviceType.toConnectType() else {
60
+ completion(NativeErrorInfo(code: PrinterErrors.PARAMS_ERROR, message: "deviceType unsupported", error: nil))
61
+ return
62
+ }
63
+
64
+ if deviceType == .ETHERNET {
65
+ let connecter = Connecter(type: .ETHERNET)
66
+ connecter.delegate = self.observer
67
+ Printers.shared.setPendingPrinter(connectInfo: connectInfo, printer: connecter)
68
+ connecter.connect(withHost: connectInfo, port: 9100)
69
+ completion(nil)
70
+ } else if deviceType == .BLUETOOTH {
71
+ let connecter = Connecter(type: .BLUETOOTH)
72
+ connecter.bluetoothDelegate = self.observer
73
+ Printers.shared.setPendingPrinter(connectInfo: connectInfo, printer: connecter)
74
+ connecter.connectByBluetooth(connectInfo: connectInfo)
75
+ completion(nil)
76
+ } else {
77
+ completion(NativeErrorInfo(code: PrinterErrors.PARAMS_ERROR, message: "deviceType unsupported", error: nil))
78
+ }
79
+ }
80
+ }
81
+
82
+ public func disconnect(connectInfo: String, completion: @escaping (_ error: NativeErrorInfo?) -> Void) {
83
+ DispatchQueue.main.async {
84
+ guard let printer = Printers.shared.getPrinter(connectInfo: connectInfo) else {
85
+ completion(
86
+ NativeErrorInfo(
87
+ code: PrinterErrors.PRINTER_NOT_FOUND_ERROR,
88
+ message: "not found device connectInfo:\(connectInfo)",
89
+ error: nil
90
+ )
91
+ )
92
+ return
93
+ }
94
+ Printers.shared.removePrinter(connectInfo: connectInfo)
95
+ self.sendDeviceConnectStatusChangeEvent(code: CONNECT_STATUS_DISCONNECT, connectInfo: connectInfo, msg: nil)
96
+ printer.disconnect()
97
+ completion(nil)
98
+ }
99
+ }
100
+
101
+ public func getPrinterPCL(connectInfo: String, resolve: @escaping (_ value: String?) -> Void, reject: @escaping (_ error: NativeErrorInfo) -> Void)
102
+ {
103
+ DispatchQueue.main.async {
104
+ resolve(Printers.shared.getPrinterPCL(connectInfo: connectInfo)?.rawValue)
105
+ }
106
+ }
107
+
108
+ public func isConnect(connectInfo: String, resolve: @escaping (_ value: Bool) -> Void, reject: @escaping (_ error: NativeErrorInfo) -> Void)
109
+ {
110
+ DispatchQueue.main.async {
111
+ var status = false
112
+ if let printer = Printers.shared.getPrinter(connectInfo: connectInfo) {
113
+ status = printer.isConnect()
114
+ } else {
115
+ status = false
116
+ }
117
+ resolve(status)
118
+ }
119
+ }
120
+
121
+ public func setPrinterPCL(connectInfo: String, pcl: String, completion: @escaping (_ error: NativeErrorInfo?) -> Void) {
122
+ DispatchQueue.main.async {
123
+ guard let pcl = pcl.toPCLType() else {
124
+ completion(
125
+ NativeErrorInfo(
126
+ code: PrinterErrors.PARAMS_ERROR,
127
+ message: "unsupported pcl type",
128
+ error: nil
129
+ )
130
+ )
131
+ return
132
+ }
133
+ guard let printer = Printers.shared.getPrinter(connectInfo: connectInfo) else {
134
+ completion(
135
+ NativeErrorInfo(
136
+ code: PrinterErrors.PRINTER_NOT_FOUND_ERROR,
137
+ message: "not found device connectInfo:\(connectInfo)",
138
+ error: nil
139
+ )
140
+ )
141
+ return
142
+ }
143
+
144
+ printer.pcl = pcl
145
+ completion(nil)
146
+ }
147
+ }
148
+
149
+ public func getUsbDevices(resolve: @escaping (_ value: [[String: Any]]) -> Void, reject: @escaping (_ error: NativeErrorInfo) -> Void) {
150
+ DispatchQueue.main.async {
151
+ reject(NativeErrorInfo(code: PrinterErrors.NOT_YET_IMPLEMENTED_ERROR, message: "Not yet implemented", error: nil))
152
+ }
153
+ }
154
+
155
+ internal func sendSearchNetDeviceEvent(_ device: PrinterProfile) {
156
+ var result: [String: Any] = [:]
157
+ result["macAddress"] = device.printerName
158
+ result["ipAddress"] = device.getIPString()
159
+ result["mask"] = device.getMaskString()
160
+ result["gateway"] = device.getGatewayString()
161
+ result["dhcp"] = device.getDHCP() != 0
162
+ self.emitter?.onSearchNetDeviceFound(result)
163
+ }
164
+
165
+ internal func sendDeviceConnectStatusChangeEvent(code: String, connectInfo: String, msg: String?) {
166
+ var result: [String: Any] = [:]
167
+ result["code"] = code
168
+ result["connectInfo"] = connectInfo
169
+ result["msg"] = msg
170
+ self.emitter?.onDeviceConnectStatusChange(result)
171
+ }
172
+ }
173
+
174
+ class NativeXPrinterPrinterObserver: NSObject {
175
+ weak var owner: NativeXPrinterPrinter?
176
+
177
+ init(owner: NativeXPrinterPrinter) {
178
+ self.owner = owner
179
+ }
180
+ }
181
+
182
+ extension NativeXPrinterPrinterObserver: WIFIConnecterDelegate {
183
+ public func wifiPOSConnected(toHost ip: String, port: UInt16, mac: String) {
184
+ Printers.shared.onPrinterConnected(ip: ip, mac: mac)
185
+ self.owner?.sendDeviceConnectStatusChangeEvent(code: CONNECT_STATUS_CONNECT_SUCCESS, connectInfo: ip, msg: nil)
186
+ }
187
+
188
+ func wifiPOSDisconnectWithError(_ error: (any Error)?, mac: String, ip: String) {
189
+ Printers.shared.onPrinterDisconnectWithError(ip: ip, mac: mac)
190
+ self.owner?.sendDeviceConnectStatusChangeEvent(code: CONNECT_STATUS_CONNECT_FAIL, connectInfo: ip, msg: error?.localizedDescription)
191
+ }
192
+ }
193
+
194
+ extension NativeXPrinterPrinterObserver: POSBLEManagerDelegate {
195
+ func poSbleConnect(_ peripheral: CBPeripheral!) {
196
+ Printers.shared.onPrinterConnected(peripheral: peripheral)
197
+ self.owner?.sendDeviceConnectStatusChangeEvent(code: CONNECT_STATUS_CONNECT_SUCCESS, connectInfo: peripheral.identifier.uuidString, msg: nil)
198
+ }
199
+
200
+ func poSbleFail(toConnect peripheral: CBPeripheral, error: (any Error)?) {
201
+ Printers.shared.onPrinterDisconnectWithError(peripheral: peripheral)
202
+ self.owner?.sendDeviceConnectStatusChangeEvent(code: CONNECT_STATUS_CONNECT_FAIL, connectInfo: peripheral.identifier.uuidString, msg: error?.localizedDescription)
203
+ }
204
+
205
+ func poSbleDisconnectPeripheral(_ peripheral: CBPeripheral, error: (any Error)?) {
206
+ Printers.shared.onPrinterDisconnectWithError(peripheral: peripheral)
207
+ self.owner?.sendDeviceConnectStatusChangeEvent(code: CONNECT_STATUS_DISCONNECT, connectInfo: peripheral.identifier.uuidString, msg: error?.localizedDescription)
208
+ }
209
+ }
@@ -0,0 +1,59 @@
1
+ //
2
+ // Printers.swift
3
+ //
4
+ // Created by chancetio on 2025/1/23.
5
+ //
6
+
7
+ class Printers {
8
+ static let shared = Printers()
9
+
10
+ private var printers: [String: Connecter] = [:]
11
+
12
+ private var pendingConnecter: [String: Connecter] = [:]
13
+
14
+ private init() {}
15
+
16
+ func clear(){
17
+ printers.removeAll()
18
+ pendingConnecter.removeAll()
19
+ }
20
+
21
+ func setPendingPrinter(connectInfo: String, printer:Connecter){
22
+ self.pendingConnecter[connectInfo] = printer
23
+ }
24
+
25
+ func removePrinter(connectInfo: String){
26
+ self.pendingConnecter.removeValue(forKey: connectInfo)
27
+ self.printers.removeValue(forKey: connectInfo)
28
+ }
29
+
30
+ func getPrinter(connectInfo: String) -> Connecter? {
31
+ return self.printers[connectInfo]
32
+ }
33
+
34
+ func getPrinterPCL(connectInfo: String) -> PCL? {
35
+ return self.printers[connectInfo]?.pcl
36
+ }
37
+
38
+ func onPrinterConnected(ip: String, mac:String){
39
+ if let printer = self.pendingConnecter.removeValue(forKey: ip) {
40
+ printers[ip] = printer
41
+ }
42
+ if let printer = self.pendingConnecter.removeValue(forKey: mac) {
43
+ printers[ip] = printer
44
+ }
45
+ }
46
+
47
+ func onPrinterDisconnectWithError(ip: String, mac:String){
48
+ self.pendingConnecter.removeValue(forKey: ip)
49
+ self.pendingConnecter.removeValue(forKey: mac)
50
+ }
51
+
52
+ func onPrinterConnected(peripheral: CBPeripheral){
53
+ self.pendingConnecter.removeValue(forKey: peripheral.identifier.uuidString)
54
+ }
55
+
56
+ func onPrinterDisconnectWithError(peripheral: CBPeripheral){
57
+ self.pendingConnecter.removeValue(forKey: peripheral.identifier.uuidString)
58
+ }
59
+ }
@@ -0,0 +1,12 @@
1
+ #import <Foundation/Foundation.h>
2
+ #import <XPrinterSpec/XPrinterSpec.h>
3
+ #import <React/RCTInitializing.h>
4
+ #import <React/RCTInvalidating.h>
5
+
6
+ NS_ASSUME_NONNULL_BEGIN
7
+
8
+ @interface RCTNativeXPrinterPrinter : NativeXPrinterPrinterSpecBase <NativeXPrinterPrinterSpec, RCTInvalidating>
9
+
10
+ @end
11
+
12
+ NS_ASSUME_NONNULL_END
@@ -0,0 +1,123 @@
1
+ #import "RCTNativeXPrinterPrinter.h"
2
+ #import "NativeXPrinter-Swift.h"
3
+
4
+ @interface RCTNativeXPrinterPrinter () <RCTNativeXPrinterPrinterEventEmitter>
5
+ @end
6
+
7
+ @implementation RCTNativeXPrinterPrinter {
8
+ NativeXPrinterPrinter* printer;
9
+ }
10
+
11
+ - (id) init {
12
+ if (self = [super init]) {
13
+ printer = [NativeXPrinterPrinter new];
14
+ printer.emitter = self;
15
+ }
16
+ return self;
17
+ }
18
+
19
+ - (void)dealloc {
20
+
21
+ }
22
+
23
+ - (void)invalidate {
24
+ [printer invalidate];
25
+ }
26
+
27
+
28
+ - (void)connect:(nonnull NSString *)connectInfo deviceType:(nonnull NSString *)deviceType sync:(BOOL)sync resolve:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject {
29
+ [printer connectWithConnectInfo:connectInfo deviceType:deviceType sync:sync completion:^(NativeErrorInfo * _Nullable errorInfo) {
30
+ if (errorInfo) {
31
+ reject(errorInfo.code, errorInfo.message, errorInfo.error);
32
+ }else{
33
+ resolve(nil);
34
+ }
35
+ }];
36
+ }
37
+
38
+ - (void)disconnect:(nonnull NSString *)connectInfo resolve:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject {
39
+ [printer disconnectWithConnectInfo:connectInfo completion:^(NativeErrorInfo * _Nullable errorInfo) {
40
+ if (errorInfo) {
41
+ reject(errorInfo.code, errorInfo.message, errorInfo.error);
42
+ }else{
43
+ resolve(nil);
44
+ }
45
+ }];
46
+ }
47
+
48
+ - (void)getPrinterPCL:(nonnull NSString *)connectInfo resolve:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject {
49
+ [printer getPrinterPCLWithConnectInfo:connectInfo resolve:^(NSString * _Nullable value) {
50
+ resolve(value);
51
+ } reject:^(NativeErrorInfo * _Nonnull errorInfo) {
52
+ reject(errorInfo.code, errorInfo.message, errorInfo.error);
53
+ }];
54
+ }
55
+
56
+ - (void)getUsbDevices:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject {
57
+ [printer getUsbDevicesWithResolve:^(NSArray<NSDictionary<NSString *,id> *> * _Nonnull value) {
58
+ resolve(value);
59
+ } reject:^(NativeErrorInfo * _Nonnull errorInfo) {
60
+ reject(errorInfo.code, errorInfo.message, errorInfo.error);
61
+ }];
62
+ }
63
+
64
+ - (void)isConnect:(nonnull NSString *)connectInfo resolve:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject {
65
+ [printer isConnectWithConnectInfo:connectInfo resolve:^(BOOL value) {
66
+ resolve(@(value));
67
+ } reject:^(NativeErrorInfo * _Nonnull errorInfo) {
68
+ reject(errorInfo.code, errorInfo.message, errorInfo.error);
69
+ }];
70
+ }
71
+
72
+ - (void)searchNetDevice:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject {
73
+ [printer searchNetDeviceWithCompletion:^(NativeErrorInfo * _Nullable errorInfo) {
74
+ if (errorInfo) {
75
+ reject(errorInfo.code, errorInfo.message, errorInfo.error);
76
+ }else{
77
+ resolve(nil);
78
+ }
79
+ }];
80
+ }
81
+
82
+ - (void)setPrinterPCL:(nonnull NSString *)connectInfo pcl:(nonnull NSString *)pcl resolve:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject {
83
+ [printer setPrinterPCLWithConnectInfo:connectInfo pcl:pcl completion:^(NativeErrorInfo * _Nullable errorInfo) {
84
+ if (errorInfo) {
85
+ reject(errorInfo.code, errorInfo.message, errorInfo.error);
86
+ }else{
87
+ resolve(nil);
88
+ }
89
+ }];
90
+ }
91
+
92
+ - (void)udpNetConfig:(JS::NativeXPrinterPrinter::UpdateUdpNetConfigRequest &)request resolve:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject {
93
+ // <#code#>
94
+ }
95
+
96
+ - (void)onDeviceConnectStatusChange:(NSDictionary<NSString *,id> * _Nonnull)value {
97
+ try {
98
+ [self emitOnDeviceConnectStatusChange:value];
99
+ } catch (const std::bad_function_call &e) {
100
+ NSLog(@"[XPrinter] std::bad_function_call in emitOnDeviceConnectStatusChange: %s", e.what());
101
+ }
102
+ }
103
+
104
+ - (void)onSearchNetDeviceFound:(NSDictionary<NSString *,id> * _Nonnull)value {
105
+ try {
106
+ [self emitOnSearchNetDeviceFound:value];
107
+ } catch (const std::bad_function_call &e) {
108
+ NSLog(@"[XPrinter] std::bad_function_call in emitOnSearchNetDeviceFound: %s", e.what());
109
+ }
110
+ }
111
+
112
+ - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
113
+ (const facebook::react::ObjCTurboModule::InitParams &)params
114
+ {
115
+ return std::make_shared<facebook::react::NativeXPrinterPrinterSpecJSI>(params);
116
+ }
117
+
118
+ + (NSString *)moduleName
119
+ {
120
+ return @"NativeXPrinterPrinter";
121
+ }
122
+
123
+ @end