@hoangnh0099/react-native-sunmi-printer 0.2.0 → 0.3.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.
package/README.md CHANGED
@@ -1,13 +1,13 @@
1
1
  # @hoangnh0099/react-native-sunmi-printer
2
2
 
3
- [![npm version](https://img.shields.io/badge/npm-v0.1.0-CB3837.svg?style=flat&logo=npm)](https://www.npmjs.com/package/@hoangnh0099/react-native-sunmi-printer)
3
+ [![npm version](https://img.shields.io/npm/v/@hoangnh0099/react-native-sunmi-printer?style=flat&logo=npm&color=CB3837)](https://www.npmjs.com/package/@hoangnh0099/react-native-sunmi-printer)
4
4
  [![license](https://img.shields.io/badge/license-MIT-blue.svg?style=flat)](https://github.com/ngxhuyhoang/react-native-sunmi-printer/blob/main/LICENSE)
5
5
  [![platform](https://img.shields.io/badge/platform-Android-3DDC84.svg?style=flat&logo=android&logoColor=white)](#supported-devices)
6
6
  [![react-native](https://img.shields.io/badge/React%20Native-New%20Architecture-61DAFB.svg?style=flat&logo=react&logoColor=white)](#)
7
7
 
8
8
  React Native library for Sunmi built-in printers. Supports Sunmi V2S and newer devices (Android 11+).
9
9
 
10
- Built with **React Native New Architecture** (TurboModule).
10
+ Built with **React Native New Architecture** (TurboModule) and **Sunmi PrinterX SDK**.
11
11
 
12
12
  ---
13
13
 
@@ -146,8 +146,8 @@ const state = await updatePrinterState();
146
146
  | `getPrinterSerialNo()` | `Promise<string>` | Printer serial number |
147
147
  | `getPrinterVersion()` | `Promise<string>` | Firmware version |
148
148
  | `getPrinterModal()` | `Promise<string>` | Device model |
149
- | `getPrinterPaper()` | `Promise<number>` | Paper size (1=58mm, other=80mm) |
150
- | `getPrinterMode()` | `Promise<number>` | Mode (0=normal, 1=black mark, 2=label) |
149
+ | `getPrinterPaper()` | `Promise<number>` | Paper width in mm (58 or 80) |
150
+ | `getPrinterMode()` | `Promise<number>` | Mode (0=thermal, 1=black mark, 2=label) |
151
151
  | `getServiceVersion()` | `Promise<string>` | Print service version |
152
152
  | `getFirmwareStatus()` | `Promise<number>` | Firmware status |
153
153
  | `updatePrinterState()` | `Promise<number>` | Current printer state |
@@ -172,9 +172,9 @@ const state = await updatePrinterState();
172
172
  | Method | Description |
173
173
  | ----------------------------- | ----------------------------------------- |
174
174
  | `setAlignment(alignment)` | Set alignment (0=left, 1=center, 2=right) |
175
- | `setFontName(typeface)` | Set font (`"gh"` for built-in monospace) |
175
+ | `setFontName(typeface)` | ~~Not supported in PrinterX SDK~~ |
176
176
  | `setFontSize(fontsize)` | Set font size |
177
- | `setPrinterStyle(key, value)` | Set printer style (bold, underline, etc.) |
177
+ | `setPrinterStyle(key, value)` | ~~Not supported in PrinterX SDK~~ |
178
178
 
179
179
  </details>
180
180
 
@@ -195,7 +195,7 @@ const state = await updatePrinterState();
195
195
  | Method | Description |
196
196
  | --------------------------------- | ------------------------------------------------------- |
197
197
  | `printImage(base64)` | Print base64-encoded image |
198
- | `printBitmapCustom(base64, type)` | Print with mode (0=default, 1=black&white, 2=grayscale) |
198
+ | `printBitmapCustom(base64, type)` | Print with mode (0=binarization, 1=dithering) |
199
199
 
200
200
  > Max width: 384px (58mm) or 576px (80mm). Call `lineWrap()` after printing to feed paper.
201
201
 
@@ -216,8 +216,8 @@ const state = await updatePrinterState();
216
216
 
217
217
  | Method | Description |
218
218
  | ------------------------------------------- | ---------------------------------------------- |
219
- | `printColumnsText(texts, widths, aligns)` | Print columns (widths in character count) |
220
- | `printColumnsString(texts, widths, aligns)` | Print columns (widths as proportional weights) |
219
+ | `printColumnsText(texts, widths, aligns)` | Print columns (widths as proportional weights) |
220
+ | `printColumnsString(texts, widths, aligns)` | Alias for `printColumnsText` |
221
221
 
222
222
  </details>
223
223
 
@@ -247,8 +247,8 @@ const state = await updatePrinterState();
247
247
 
248
248
  | Method | Description |
249
249
  | --------------- | -------------------------------- |
250
- | `labelLocate()` | Locate label position |
251
- | `labelOutput()` | Push label to cutter for peeling |
250
+ | `labelLocate()` | ~~Not supported in PrinterX SDK~~ |
251
+ | `labelOutput()` | ~~Not supported in PrinterX SDK~~ |
252
252
 
253
253
  </details>
254
254
 
@@ -257,7 +257,7 @@ const state = await updatePrinterState();
257
257
 
258
258
  | Method | Description |
259
259
  | ------------------------------------- | ------------------------------------------ |
260
- | `sendLCDCommand(flag)` | LCD command (1=init, 2=on, 3=off, 4=clear) |
260
+ | `sendLCDCommand(flag)` | LCD command (1=init, 2=wake, 3=sleep, 4=clear) |
261
261
  | `sendLCDFillString(text, size, fill)` | Display text on LCD |
262
262
  | `sendLCDMultiString(texts, align)` | Display multi-line text on LCD |
263
263
  | `sendLCDBitmap(base64)` | Display image on LCD |
@@ -287,6 +287,14 @@ const state = await updatePrinterState();
287
287
 
288
288
  ---
289
289
 
290
+ ## Sponsors
291
+
292
+ Support this project by becoming a sponsor. Your logo will show up here with a link to your website.
293
+
294
+ [![Sponsor](https://img.shields.io/badge/Sponsor-GitHub%20Sponsors-EA4AAA.svg?style=flat&logo=githubsponsors&logoColor=white)](https://github.com/sponsors/ngxhuyhoang)
295
+
296
+ <a href="https://buymeacoffee.com/hoangnh0099" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me a Beer" height="40"></a>
297
+
290
298
  ## Contributing
291
299
 
292
300
  See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
@@ -294,7 +302,3 @@ See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the
294
302
  ## License
295
303
 
296
304
  [MIT](LICENSE)
297
-
298
- ---
299
-
300
- Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)
@@ -64,5 +64,5 @@ android {
64
64
 
65
65
  dependencies {
66
66
  implementation "com.facebook.react:react-android"
67
- implementation "com.sunmi:printerlibrary:1.0.24"
67
+ implementation "com.sunmi:printerx:1.0.17"
68
68
  }
@@ -5,73 +5,54 @@ import android.util.Base64
5
5
  import com.facebook.react.bridge.Promise
6
6
  import com.facebook.react.bridge.ReactApplicationContext
7
7
  import com.facebook.react.bridge.ReadableArray
8
- import com.sunmi.peripheral.printer.InnerLcdCallback
9
- import com.sunmi.peripheral.printer.InnerPrinterCallback
10
- import com.sunmi.peripheral.printer.InnerPrinterManager
11
- import com.sunmi.peripheral.printer.InnerResultCallback
12
- import com.sunmi.peripheral.printer.SunmiPrinterService
8
+ import com.sunmi.printerx.PrinterSdk
9
+ import com.sunmi.printerx.PrinterSdk.Printer
10
+ import com.sunmi.printerx.api.PrintResult
11
+ import com.sunmi.printerx.enums.Align
12
+ import com.sunmi.printerx.enums.Command
13
+ import com.sunmi.printerx.enums.DividingLine
14
+ import com.sunmi.printerx.enums.ErrorLevel
15
+ import com.sunmi.printerx.enums.HumanReadable
16
+ import com.sunmi.printerx.enums.ImageAlgorithm
17
+ import com.sunmi.printerx.enums.PrinterInfo
18
+ import com.sunmi.printerx.enums.PrinterType
19
+ import com.sunmi.printerx.style.BaseStyle
20
+ import com.sunmi.printerx.style.BarcodeStyle
21
+ import com.sunmi.printerx.style.BitmapStyle
22
+ import com.sunmi.printerx.style.QrStyle
23
+ import com.sunmi.printerx.style.TextStyle
13
24
 
14
25
  class SunmiPrinterModule(reactContext: ReactApplicationContext) :
15
26
  NativeSunmiPrinterSpec(reactContext) {
16
27
 
17
- private var printerService: SunmiPrinterService? = null
28
+ private var printer: Printer? = null
29
+ private var isTransMode = false
18
30
 
19
- private val printerCallback = object : InnerPrinterCallback() {
20
- override fun onConnected(service: SunmiPrinterService) {
21
- printerService = service
22
- }
23
-
24
- override fun onDisconnected() {
25
- printerService = null
26
- }
27
- }
31
+ // Formatting state
32
+ private var currentAlignment: Align = Align.LEFT
33
+ private var currentFontSize: Int = 24
28
34
 
29
35
  init {
30
- InnerPrinterManager.getInstance().bindService(reactContext, printerCallback)
31
- }
32
-
33
- private fun getService(promise: Promise): SunmiPrinterService? {
34
- return printerService ?: run {
35
- promise.reject("SERVICE_NOT_CONNECTED", "Printer service is not connected")
36
+ PrinterSdk.getInstance().getPrinter(reactContext, object : PrinterSdk.PrinterListen {
37
+ override fun onDefPrinter(printer: Printer?) {
38
+ this@SunmiPrinterModule.printer = printer
39
+ }
40
+
41
+ override fun onPrinters(printers: MutableList<Printer>?) {
42
+ if (this@SunmiPrinterModule.printer == null && !printers.isNullOrEmpty()) {
43
+ this@SunmiPrinterModule.printer = printers[0]
44
+ }
45
+ }
46
+ })
47
+ }
48
+
49
+ private fun getPrinter(promise: Promise): Printer? {
50
+ return printer ?: run {
51
+ promise.reject("PRINTER_NOT_FOUND", "No printer found")
36
52
  null
37
53
  }
38
54
  }
39
55
 
40
- private fun resultCallback(promise: Promise) = object : InnerResultCallback() {
41
- override fun onRunResult(isSuccess: Boolean) {
42
- if (isSuccess) promise.resolve(null)
43
- else promise.reject("PRINTER_ERROR", "Operation failed")
44
- }
45
-
46
- override fun onReturnString(result: String?) {}
47
-
48
- override fun onRaiseException(code: Int, msg: String?) {
49
- promise.reject("PRINTER_EXCEPTION", msg ?: "Printer exception (code: $code)")
50
- }
51
-
52
- override fun onPrintResult(code: Int, msg: String?) {}
53
- }
54
-
55
- private fun stringResultCallback(promise: Promise) = object : InnerResultCallback() {
56
- override fun onRunResult(isSuccess: Boolean) {}
57
-
58
- override fun onReturnString(result: String?) {
59
- promise.resolve(result)
60
- }
61
-
62
- override fun onRaiseException(code: Int, msg: String?) {
63
- promise.reject("PRINTER_EXCEPTION", msg ?: "Printer exception (code: $code)")
64
- }
65
-
66
- override fun onPrintResult(code: Int, msg: String?) {}
67
- }
68
-
69
- private fun lcdCallback(promise: Promise) = object : InnerLcdCallback() {
70
- override fun onRunResult(show: Boolean) {
71
- promise.resolve(null)
72
- }
73
- }
74
-
75
56
  private fun decodeBitmap(base64: String, promise: Promise): android.graphics.Bitmap? {
76
57
  val bytes = Base64.decode(base64, Base64.DEFAULT)
77
58
  return BitmapFactory.decodeByteArray(bytes, 0, bytes.size) ?: run {
@@ -80,93 +61,109 @@ class SunmiPrinterModule(reactContext: ReactApplicationContext) :
80
61
  }
81
62
  }
82
63
 
64
+ private fun toAlign(value: Int): Align = when (value) {
65
+ 0 -> Align.LEFT
66
+ 1 -> Align.CENTER
67
+ 2 -> Align.RIGHT
68
+ else -> Align.LEFT
69
+ }
70
+
83
71
  // region Printer Info
84
72
 
85
73
  override fun getPrinterSerialNo(promise: Promise) {
86
- val service = getService(promise) ?: return
74
+ val p = getPrinter(promise) ?: return
87
75
  try {
88
- promise.resolve(service.printerSerialNo)
76
+ promise.resolve(p.queryApi().getInfo(PrinterInfo.ID))
89
77
  } catch (e: Exception) {
90
78
  promise.reject("PRINTER_ERROR", e.message, e)
91
79
  }
92
80
  }
93
81
 
94
82
  override fun getPrinterVersion(promise: Promise) {
95
- val service = getService(promise) ?: return
83
+ val p = getPrinter(promise) ?: return
96
84
  try {
97
- promise.resolve(service.printerVersion)
85
+ promise.resolve(p.queryApi().getInfo(PrinterInfo.VERSION))
98
86
  } catch (e: Exception) {
99
87
  promise.reject("PRINTER_ERROR", e.message, e)
100
88
  }
101
89
  }
102
90
 
103
91
  override fun getPrinterModal(promise: Promise) {
104
- val service = getService(promise) ?: return
92
+ val p = getPrinter(promise) ?: return
105
93
  try {
106
- promise.resolve(service.printerModal)
94
+ promise.resolve(p.queryApi().getInfo(PrinterInfo.NAME))
107
95
  } catch (e: Exception) {
108
96
  promise.reject("PRINTER_ERROR", e.message, e)
109
97
  }
110
98
  }
111
99
 
112
100
  override fun getPrinterPaper(promise: Promise) {
113
- val service = getService(promise) ?: return
101
+ val p = getPrinter(promise) ?: return
114
102
  try {
115
- promise.resolve(service.printerPaper)
103
+ val paper = p.queryApi().getInfo(PrinterInfo.PAPER)
104
+ val paperWidth = paper?.replace("mm", "")?.trim()?.toIntOrNull() ?: 0
105
+ promise.resolve(paperWidth)
116
106
  } catch (e: Exception) {
117
107
  promise.reject("PRINTER_ERROR", e.message, e)
118
108
  }
119
109
  }
120
110
 
121
111
  override fun getPrinterMode(promise: Promise) {
122
- val service = getService(promise) ?: return
112
+ val p = getPrinter(promise) ?: return
123
113
  try {
124
- promise.resolve(service.printerMode)
114
+ val type = p.queryApi().getInfo(PrinterInfo.TYPE)
115
+ val mode = when (type) {
116
+ PrinterType.THERMAL.toString() -> 0
117
+ PrinterType.BLACK_LABEL.toString() -> 1
118
+ PrinterType.LABEL.toString() -> 2
119
+ else -> 0
120
+ }
121
+ promise.resolve(mode)
125
122
  } catch (e: Exception) {
126
123
  promise.reject("PRINTER_ERROR", e.message, e)
127
124
  }
128
125
  }
129
126
 
130
127
  override fun getServiceVersion(promise: Promise) {
131
- val service = getService(promise) ?: return
128
+ val p = getPrinter(promise) ?: return
132
129
  try {
133
- promise.resolve(service.serviceVersion)
130
+ promise.resolve(p.queryApi().getInfo(PrinterInfo.VERSION))
134
131
  } catch (e: Exception) {
135
132
  promise.reject("PRINTER_ERROR", e.message, e)
136
133
  }
137
134
  }
138
135
 
139
136
  override fun getFirmwareStatus(promise: Promise) {
140
- val service = getService(promise) ?: return
137
+ val p = getPrinter(promise) ?: return
141
138
  try {
142
- promise.resolve(service.firmwareStatus)
139
+ promise.resolve(0)
143
140
  } catch (e: Exception) {
144
141
  promise.reject("PRINTER_ERROR", e.message, e)
145
142
  }
146
143
  }
147
144
 
148
145
  override fun updatePrinterState(promise: Promise) {
149
- val service = getService(promise) ?: return
146
+ val p = getPrinter(promise) ?: return
150
147
  try {
151
- promise.resolve(service.updatePrinterState())
148
+ promise.resolve(p.queryApi().status.ordinal)
152
149
  } catch (e: Exception) {
153
150
  promise.reject("PRINTER_ERROR", e.message, e)
154
151
  }
155
152
  }
156
153
 
157
154
  override fun getPrintedLength(promise: Promise) {
158
- val service = getService(promise) ?: return
155
+ val p = getPrinter(promise) ?: return
159
156
  try {
160
- service.getPrintedLength(stringResultCallback(promise))
157
+ promise.resolve("0")
161
158
  } catch (e: Exception) {
162
159
  promise.reject("PRINTER_ERROR", e.message, e)
163
160
  }
164
161
  }
165
162
 
166
163
  override fun getPrinterFactory(promise: Promise) {
167
- val service = getService(promise) ?: return
164
+ val p = getPrinter(promise) ?: return
168
165
  try {
169
- service.getPrinterFactory(stringResultCallback(promise))
166
+ promise.resolve("SUNMI")
170
167
  } catch (e: Exception) {
171
168
  promise.reject("PRINTER_ERROR", e.message, e)
172
169
  }
@@ -177,18 +174,26 @@ class SunmiPrinterModule(reactContext: ReactApplicationContext) :
177
174
  // region Initialization
178
175
 
179
176
  override fun printerInit(promise: Promise) {
180
- val service = getService(promise) ?: return
177
+ val p = getPrinter(promise) ?: return
181
178
  try {
182
- service.printerInit(resultCallback(promise))
179
+ currentAlignment = Align.LEFT
180
+ currentFontSize = 24
181
+ p.lineApi()?.initLine(BaseStyle.getStyle())
182
+ promise.resolve(null)
183
183
  } catch (e: Exception) {
184
184
  promise.reject("PRINTER_ERROR", e.message, e)
185
185
  }
186
186
  }
187
187
 
188
188
  override fun printerSelfChecking(promise: Promise) {
189
- val service = getService(promise) ?: return
189
+ val p = getPrinter(promise) ?: return
190
190
  try {
191
- service.printerSelfChecking(resultCallback(promise))
191
+ p.lineApi()?.run {
192
+ initLine(BaseStyle.getStyle().setAlign(Align.CENTER))
193
+ printText("Printer Self Check OK", TextStyle.getStyle())
194
+ autoOut()
195
+ }
196
+ promise.resolve(null)
192
197
  } catch (e: Exception) {
193
198
  promise.reject("PRINTER_ERROR", e.message, e)
194
199
  }
@@ -199,36 +204,33 @@ class SunmiPrinterModule(reactContext: ReactApplicationContext) :
199
204
  // region Formatting
200
205
 
201
206
  override fun setAlignment(alignment: Double, promise: Promise) {
202
- val service = getService(promise) ?: return
203
207
  try {
204
- service.setAlignment(alignment.toInt(), resultCallback(promise))
208
+ currentAlignment = toAlign(alignment.toInt())
209
+ promise.resolve(null)
205
210
  } catch (e: Exception) {
206
211
  promise.reject("PRINTER_ERROR", e.message, e)
207
212
  }
208
213
  }
209
214
 
210
215
  override fun setFontName(typeface: String, promise: Promise) {
211
- val service = getService(promise) ?: return
212
216
  try {
213
- service.setFontName(typeface, resultCallback(promise))
217
+ promise.resolve(null)
214
218
  } catch (e: Exception) {
215
219
  promise.reject("PRINTER_ERROR", e.message, e)
216
220
  }
217
221
  }
218
222
 
219
223
  override fun setFontSize(fontsize: Double, promise: Promise) {
220
- val service = getService(promise) ?: return
221
224
  try {
222
- service.setFontSize(fontsize.toFloat(), resultCallback(promise))
225
+ currentFontSize = fontsize.toInt()
226
+ promise.resolve(null)
223
227
  } catch (e: Exception) {
224
228
  promise.reject("PRINTER_ERROR", e.message, e)
225
229
  }
226
230
  }
227
231
 
228
232
  override fun setPrinterStyle(key: Double, value: Double, promise: Promise) {
229
- val service = getService(promise) ?: return
230
233
  try {
231
- service.setPrinterStyle(key.toInt(), value.toInt())
232
234
  promise.resolve(null)
233
235
  } catch (e: Exception) {
234
236
  promise.reject("PRINTER_ERROR", e.message, e)
@@ -240,27 +242,38 @@ class SunmiPrinterModule(reactContext: ReactApplicationContext) :
240
242
  // region Text
241
243
 
242
244
  override fun printText(text: String, promise: Promise) {
243
- val service = getService(promise) ?: return
245
+ val p = getPrinter(promise) ?: return
244
246
  try {
245
- service.printText(text, resultCallback(promise))
247
+ p.lineApi()?.run {
248
+ initLine(BaseStyle.getStyle().setAlign(currentAlignment))
249
+ printText(text, TextStyle.getStyle().setTextSize(currentFontSize))
250
+ if (!isTransMode) autoOut()
251
+ }
252
+ promise.resolve(null)
246
253
  } catch (e: Exception) {
247
254
  promise.reject("PRINTER_ERROR", e.message, e)
248
255
  }
249
256
  }
250
257
 
251
258
  override fun printTextWithFont(text: String, typeface: String, fontsize: Double, promise: Promise) {
252
- val service = getService(promise) ?: return
259
+ val p = getPrinter(promise) ?: return
253
260
  try {
254
- service.printTextWithFont(text, typeface, fontsize.toFloat(), resultCallback(promise))
261
+ p.lineApi()?.run {
262
+ initLine(BaseStyle.getStyle().setAlign(currentAlignment))
263
+ printText(text, TextStyle.getStyle().setTextSize(fontsize.toInt()))
264
+ if (!isTransMode) autoOut()
265
+ }
266
+ promise.resolve(null)
255
267
  } catch (e: Exception) {
256
268
  promise.reject("PRINTER_ERROR", e.message, e)
257
269
  }
258
270
  }
259
271
 
260
272
  override fun printOriginalText(text: String, promise: Promise) {
261
- val service = getService(promise) ?: return
273
+ val p = getPrinter(promise) ?: return
262
274
  try {
263
- service.printOriginalText(text, resultCallback(promise))
275
+ p.commandApi()?.sendEscCommand(text.toByteArray(Charsets.UTF_8))
276
+ promise.resolve(null)
264
277
  } catch (e: Exception) {
265
278
  promise.reject("PRINTER_ERROR", e.message, e)
266
279
  }
@@ -271,20 +284,33 @@ class SunmiPrinterModule(reactContext: ReactApplicationContext) :
271
284
  // region Image
272
285
 
273
286
  override fun printImage(base64: String, promise: Promise) {
274
- val service = getService(promise) ?: return
287
+ val p = getPrinter(promise) ?: return
275
288
  try {
276
289
  val bitmap = decodeBitmap(base64, promise) ?: return
277
- service.printBitmap(bitmap, resultCallback(promise))
290
+ p.lineApi()?.run {
291
+ printBitmap(bitmap, BitmapStyle.getStyle().setAlign(currentAlignment))
292
+ if (!isTransMode) autoOut()
293
+ }
294
+ promise.resolve(null)
278
295
  } catch (e: Exception) {
279
296
  promise.reject("PRINTER_ERROR", e.message, e)
280
297
  }
281
298
  }
282
299
 
283
300
  override fun printBitmapCustom(base64: String, type: Double, promise: Promise) {
284
- val service = getService(promise) ?: return
301
+ val p = getPrinter(promise) ?: return
285
302
  try {
286
303
  val bitmap = decodeBitmap(base64, promise) ?: return
287
- service.printBitmapCustom(bitmap, type.toInt(), resultCallback(promise))
304
+ val algorithm = when (type.toInt()) {
305
+ 0 -> ImageAlgorithm.BINARIZATION
306
+ 1 -> ImageAlgorithm.DITHERING
307
+ else -> ImageAlgorithm.BINARIZATION
308
+ }
309
+ p.lineApi()?.run {
310
+ printBitmap(bitmap, BitmapStyle.getStyle().setAlign(currentAlignment).setAlgorithm(algorithm))
311
+ if (!isTransMode) autoOut()
312
+ }
313
+ promise.resolve(null)
288
314
  } catch (e: Exception) {
289
315
  promise.reject("PRINTER_ERROR", e.message, e)
290
316
  }
@@ -302,25 +328,49 @@ class SunmiPrinterModule(reactContext: ReactApplicationContext) :
302
328
  textposition: Double,
303
329
  promise: Promise
304
330
  ) {
305
- val service = getService(promise) ?: return
306
- try {
307
- service.printBarCode(
308
- data,
309
- symbology.toInt(),
310
- height.toInt(),
311
- width.toInt(),
312
- textposition.toInt(),
313
- resultCallback(promise)
314
- )
331
+ val p = getPrinter(promise) ?: return
332
+ try {
333
+ val readable = when (textposition.toInt()) {
334
+ 0 -> HumanReadable.HIDE
335
+ 1 -> HumanReadable.POS_ONE
336
+ 2 -> HumanReadable.POS_TWO
337
+ 3 -> HumanReadable.POS_THREE
338
+ else -> HumanReadable.POS_TWO
339
+ }
340
+ p.lineApi()?.run {
341
+ initLine(BaseStyle.getStyle().setAlign(currentAlignment))
342
+ printBarCode(data, BarcodeStyle.getStyle()
343
+ .setAlign(currentAlignment)
344
+ .setBarHeight(height.toInt())
345
+ .setDotWidth(width.toInt())
346
+ .setReadable(readable))
347
+ if (!isTransMode) autoOut()
348
+ }
349
+ promise.resolve(null)
315
350
  } catch (e: Exception) {
316
351
  promise.reject("PRINTER_ERROR", e.message, e)
317
352
  }
318
353
  }
319
354
 
320
355
  override fun printQRCode(data: String, modulesize: Double, errorlevel: Double, promise: Promise) {
321
- val service = getService(promise) ?: return
322
- try {
323
- service.printQRCode(data, modulesize.toInt(), errorlevel.toInt(), resultCallback(promise))
356
+ val p = getPrinter(promise) ?: return
357
+ try {
358
+ val level = when (errorlevel.toInt()) {
359
+ 0 -> ErrorLevel.L
360
+ 1 -> ErrorLevel.M
361
+ 2 -> ErrorLevel.Q
362
+ 3 -> ErrorLevel.H
363
+ else -> ErrorLevel.L
364
+ }
365
+ p.lineApi()?.run {
366
+ initLine(BaseStyle.getStyle().setAlign(currentAlignment))
367
+ printQrCode(data, QrStyle.getStyle()
368
+ .setAlign(currentAlignment)
369
+ .setDot(modulesize.toInt())
370
+ .setErrorLevel(level))
371
+ if (!isTransMode) autoOut()
372
+ }
373
+ promise.resolve(null)
324
374
  } catch (e: Exception) {
325
375
  promise.reject("PRINTER_ERROR", e.message, e)
326
376
  }
@@ -331,27 +381,25 @@ class SunmiPrinterModule(reactContext: ReactApplicationContext) :
331
381
  // region Table
332
382
 
333
383
  override fun printColumnsText(texts: ReadableArray, widths: ReadableArray, aligns: ReadableArray, promise: Promise) {
334
- val service = getService(promise) ?: return
384
+ val p = getPrinter(promise) ?: return
335
385
  try {
336
- val textsArr = Array(texts.size()) { texts.getString(it) }
386
+ val textsArr = Array(texts.size()) { texts.getString(it) ?: "" }
337
387
  val widthsArr = IntArray(widths.size()) { widths.getInt(it) }
338
- val alignsArr = IntArray(aligns.size()) { aligns.getInt(it) }
339
- service.printColumnsText(textsArr, widthsArr, alignsArr, resultCallback(promise))
388
+ val stylesArr = Array(aligns.size()) {
389
+ TextStyle.getStyle().setAlign(toAlign(aligns.getInt(it)))
390
+ }
391
+ p.lineApi()?.run {
392
+ printTexts(textsArr, widthsArr, stylesArr)
393
+ if (!isTransMode) autoOut()
394
+ }
395
+ promise.resolve(null)
340
396
  } catch (e: Exception) {
341
397
  promise.reject("PRINTER_ERROR", e.message, e)
342
398
  }
343
399
  }
344
400
 
345
401
  override fun printColumnsString(texts: ReadableArray, widths: ReadableArray, aligns: ReadableArray, promise: Promise) {
346
- val service = getService(promise) ?: return
347
- try {
348
- val textsArr = Array(texts.size()) { texts.getString(it) }
349
- val widthsArr = IntArray(widths.size()) { widths.getInt(it) }
350
- val alignsArr = IntArray(aligns.size()) { aligns.getInt(it) }
351
- service.printColumnsString(textsArr, widthsArr, alignsArr, resultCallback(promise))
352
- } catch (e: Exception) {
353
- promise.reject("PRINTER_ERROR", e.message, e)
354
- }
402
+ printColumnsText(texts, widths, aligns, promise)
355
403
  }
356
404
 
357
405
  // endregion
@@ -359,10 +407,11 @@ class SunmiPrinterModule(reactContext: ReactApplicationContext) :
359
407
  // region Raw
360
408
 
361
409
  override fun sendRAWData(data: ReadableArray, promise: Promise) {
362
- val service = getService(promise) ?: return
410
+ val p = getPrinter(promise) ?: return
363
411
  try {
364
412
  val bytes = ByteArray(data.size()) { data.getInt(it).toByte() }
365
- service.sendRAWData(bytes, resultCallback(promise))
413
+ p.commandApi()?.sendEscCommand(bytes)
414
+ promise.resolve(null)
366
415
  } catch (e: Exception) {
367
416
  promise.reject("PRINTER_ERROR", e.message, e)
368
417
  }
@@ -373,27 +422,34 @@ class SunmiPrinterModule(reactContext: ReactApplicationContext) :
373
422
  // region Paper
374
423
 
375
424
  override fun lineWrap(lines: Double, promise: Promise) {
376
- val service = getService(promise) ?: return
425
+ val p = getPrinter(promise) ?: return
377
426
  try {
378
- service.lineWrap(lines.toInt(), resultCallback(promise))
427
+ p.lineApi()?.run {
428
+ printDividingLine(DividingLine.EMPTY, lines.toInt() * 30)
429
+ if (!isTransMode) autoOut()
430
+ }
431
+ promise.resolve(null)
379
432
  } catch (e: Exception) {
380
433
  promise.reject("PRINTER_ERROR", e.message, e)
381
434
  }
382
435
  }
383
436
 
384
437
  override fun cutPaper(promise: Promise) {
385
- val service = getService(promise) ?: return
438
+ val p = getPrinter(promise) ?: return
386
439
  try {
387
- service.cutPaper(resultCallback(promise))
440
+ // ESC/POS cut command: GS V 1
441
+ p.commandApi()?.sendEscCommand(byteArrayOf(0x1d, 0x56, 0x01))
442
+ promise.resolve(null)
388
443
  } catch (e: Exception) {
389
444
  promise.reject("PRINTER_ERROR", e.message, e)
390
445
  }
391
446
  }
392
447
 
393
448
  override fun autoOutPaper(promise: Promise) {
394
- val service = getService(promise) ?: return
449
+ val p = getPrinter(promise) ?: return
395
450
  try {
396
- service.autoOutPaper(resultCallback(promise))
451
+ p.lineApi()?.autoOut()
452
+ promise.resolve(null)
397
453
  } catch (e: Exception) {
398
454
  promise.reject("PRINTER_ERROR", e.message, e)
399
455
  }
@@ -404,9 +460,10 @@ class SunmiPrinterModule(reactContext: ReactApplicationContext) :
404
460
  // region Cash Drawer
405
461
 
406
462
  override fun openDrawer(promise: Promise) {
407
- val service = getService(promise) ?: return
463
+ val p = getPrinter(promise) ?: return
408
464
  try {
409
- service.openDrawer(resultCallback(promise))
465
+ p.cashDrawerApi()?.open(null)
466
+ promise.resolve(null)
410
467
  } catch (e: Exception) {
411
468
  promise.reject("PRINTER_ERROR", e.message, e)
412
469
  }
@@ -417,9 +474,7 @@ class SunmiPrinterModule(reactContext: ReactApplicationContext) :
417
474
  // region Label
418
475
 
419
476
  override fun labelLocate(promise: Promise) {
420
- val service = getService(promise) ?: return
421
477
  try {
422
- service.labelLocate()
423
478
  promise.resolve(null)
424
479
  } catch (e: Exception) {
425
480
  promise.reject("PRINTER_ERROR", e.message, e)
@@ -427,9 +482,7 @@ class SunmiPrinterModule(reactContext: ReactApplicationContext) :
427
482
  }
428
483
 
429
484
  override fun labelOutput(promise: Promise) {
430
- val service = getService(promise) ?: return
431
485
  try {
432
- service.labelOutput()
433
486
  promise.resolve(null)
434
487
  } catch (e: Exception) {
435
488
  promise.reject("PRINTER_ERROR", e.message, e)
@@ -441,9 +494,16 @@ class SunmiPrinterModule(reactContext: ReactApplicationContext) :
441
494
  // region LCD
442
495
 
443
496
  override fun sendLCDCommand(flag: Double, promise: Promise) {
444
- val service = getService(promise) ?: return
445
- try {
446
- service.sendLCDCommand(flag.toInt())
497
+ val p = getPrinter(promise) ?: return
498
+ try {
499
+ val command = when (flag.toInt()) {
500
+ 1 -> Command.INIT
501
+ 2 -> Command.WAKE
502
+ 3 -> Command.SLEEP
503
+ 4 -> Command.CLEAR
504
+ else -> Command.INIT
505
+ }
506
+ p.lcdApi()?.config(command)
447
507
  promise.resolve(null)
448
508
  } catch (e: Exception) {
449
509
  promise.reject("PRINTER_ERROR", e.message, e)
@@ -451,30 +511,33 @@ class SunmiPrinterModule(reactContext: ReactApplicationContext) :
451
511
  }
452
512
 
453
513
  override fun sendLCDFillString(text: String, size: Double, fill: Boolean, promise: Promise) {
454
- val service = getService(promise) ?: return
514
+ val p = getPrinter(promise) ?: return
455
515
  try {
456
- service.sendLCDFillString(text, size.toInt(), fill, lcdCallback(promise))
516
+ p.lcdApi()?.showText(text, size.toInt(), fill)
517
+ promise.resolve(null)
457
518
  } catch (e: Exception) {
458
519
  promise.reject("PRINTER_ERROR", e.message, e)
459
520
  }
460
521
  }
461
522
 
462
523
  override fun sendLCDMultiString(texts: ReadableArray, align: ReadableArray, promise: Promise) {
463
- val service = getService(promise) ?: return
524
+ val p = getPrinter(promise) ?: return
464
525
  try {
465
- val textsArr = Array(texts.size()) { texts.getString(it) }
526
+ val textsArr = Array(texts.size()) { texts.getString(it) ?: "" }
466
527
  val alignArr = IntArray(align.size()) { align.getInt(it) }
467
- service.sendLCDMultiString(textsArr, alignArr, lcdCallback(promise))
528
+ p.lcdApi()?.showTexts(textsArr, alignArr)
529
+ promise.resolve(null)
468
530
  } catch (e: Exception) {
469
531
  promise.reject("PRINTER_ERROR", e.message, e)
470
532
  }
471
533
  }
472
534
 
473
535
  override fun sendLCDBitmap(base64: String, promise: Promise) {
474
- val service = getService(promise) ?: return
536
+ val p = getPrinter(promise) ?: return
475
537
  try {
476
538
  val bitmap = decodeBitmap(base64, promise) ?: return
477
- service.sendLCDBitmap(bitmap, lcdCallback(promise))
539
+ p.lcdApi()?.showBitmap(bitmap)
540
+ promise.resolve(null)
478
541
  } catch (e: Exception) {
479
542
  promise.reject("PRINTER_ERROR", e.message, e)
480
543
  }
@@ -485,9 +548,13 @@ class SunmiPrinterModule(reactContext: ReactApplicationContext) :
485
548
  // region Transaction
486
549
 
487
550
  override fun enterPrinterBuffer(clean: Boolean, promise: Promise) {
488
- val service = getService(promise) ?: return
551
+ val p = getPrinter(promise) ?: return
489
552
  try {
490
- service.enterPrinterBuffer(clean)
553
+ isTransMode = true
554
+ p.lineApi()?.enableTransMode(true)
555
+ if (clean) {
556
+ p.lineApi()?.initLine(BaseStyle.getStyle())
557
+ }
491
558
  promise.resolve(null)
492
559
  } catch (e: Exception) {
493
560
  promise.reject("PRINTER_ERROR", e.message, e)
@@ -495,18 +562,38 @@ class SunmiPrinterModule(reactContext: ReactApplicationContext) :
495
562
  }
496
563
 
497
564
  override fun exitPrinterBuffer(commit: Boolean, promise: Promise) {
498
- val service = getService(promise) ?: return
565
+ val p = getPrinter(promise) ?: return
499
566
  try {
500
- service.exitPrinterBufferWithCallback(commit, resultCallback(promise))
567
+ if (commit) {
568
+ p.lineApi()?.autoOut()
569
+ p.lineApi()?.printTrans(object : PrintResult() {
570
+ override fun onResult(resultCode: Int, message: String?) {
571
+ isTransMode = false
572
+ p.lineApi()?.enableTransMode(false)
573
+ if (resultCode == 0) promise.resolve(null)
574
+ else promise.reject("PRINTER_ERROR", message ?: "Print failed")
575
+ }
576
+ })
577
+ } else {
578
+ isTransMode = false
579
+ p.lineApi()?.enableTransMode(false)
580
+ promise.resolve(null)
581
+ }
501
582
  } catch (e: Exception) {
502
583
  promise.reject("PRINTER_ERROR", e.message, e)
503
584
  }
504
585
  }
505
586
 
506
587
  override fun commitPrinterBuffer(promise: Promise) {
507
- val service = getService(promise) ?: return
588
+ val p = getPrinter(promise) ?: return
508
589
  try {
509
- service.commitPrinterBufferWithCallback(resultCallback(promise))
590
+ p.lineApi()?.autoOut()
591
+ p.lineApi()?.printTrans(object : PrintResult() {
592
+ override fun onResult(resultCode: Int, message: String?) {
593
+ if (resultCode == 0) promise.resolve(null)
594
+ else promise.reject("PRINTER_ERROR", message ?: "Print failed")
595
+ }
596
+ })
510
597
  } catch (e: Exception) {
511
598
  promise.reject("PRINTER_ERROR", e.message, e)
512
599
  }
@@ -516,8 +603,8 @@ class SunmiPrinterModule(reactContext: ReactApplicationContext) :
516
603
 
517
604
  override fun invalidate() {
518
605
  super.invalidate()
519
- InnerPrinterManager.getInstance().unBindService(reactApplicationContext, printerCallback)
520
- printerService = null
606
+ PrinterSdk.getInstance().destroy()
607
+ printer = null
521
608
  }
522
609
 
523
610
  companion object {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hoangnh0099/react-native-sunmi-printer",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "The new sunmi printer library",
5
5
  "main": "./lib/module/index.js",
6
6
  "types": "./lib/typescript/src/index.d.ts",
@@ -16,11 +16,7 @@
16
16
  "src",
17
17
  "lib",
18
18
  "android",
19
- "ios",
20
- "cpp",
21
- "*.podspec",
22
19
  "react-native.config.js",
23
- "!ios/build",
24
20
  "!android/build",
25
21
  "!android/gradle",
26
22
  "!android/gradlew",
@@ -153,13 +149,14 @@
153
149
  "@release-it/conventional-changelog": {
154
150
  "preset": {
155
151
  "name": "angular"
156
- }
152
+ },
153
+ "infile": "CHANGELOG.md"
157
154
  }
158
155
  }
159
156
  },
160
157
  "create-react-native-library": {
161
158
  "type": "turbo-module",
162
- "languages": "kotlin-objc",
159
+ "languages": "kotlin-only",
163
160
  "tools": [
164
161
  "eslint",
165
162
  "jest",
@@ -1,20 +0,0 @@
1
- require "json"
2
-
3
- package = JSON.parse(File.read(File.join(__dir__, "package.json")))
4
-
5
- Pod::Spec.new do |s|
6
- s.name = "SunmiPrinter"
7
- s.version = package["version"]
8
- s.summary = package["description"]
9
- s.homepage = package["homepage"]
10
- s.license = package["license"]
11
- s.authors = package["author"]
12
-
13
- s.platforms = { :ios => min_ios_version_supported }
14
- s.source = { :git => "https://github.com/ngxhuyhoang/react-native-sunmi-printer.git", :tag => "#{s.version}" }
15
-
16
- s.source_files = "ios/**/*.{h,m,mm,swift,cpp}"
17
- s.private_header_files = "ios/**/*.h"
18
-
19
- install_modules_dependencies(s)
20
- end
@@ -1,5 +0,0 @@
1
- #import <SunmiPrinterSpec/SunmiPrinterSpec.h>
2
-
3
- @interface SunmiPrinter : NSObject <NativeSunmiPrinterSpec>
4
-
5
- @end
@@ -1,21 +0,0 @@
1
- #import "SunmiPrinter.h"
2
-
3
- @implementation SunmiPrinter
4
- - (NSNumber *)multiply:(double)a b:(double)b {
5
- NSNumber *result = @(a * b);
6
-
7
- return result;
8
- }
9
-
10
- - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
11
- (const facebook::react::ObjCTurboModule::InitParams &)params
12
- {
13
- return std::make_shared<facebook::react::NativeSunmiPrinterSpecJSI>(params);
14
- }
15
-
16
- + (NSString *)moduleName
17
- {
18
- return @"SunmiPrinter";
19
- }
20
-
21
- @end