@munchi_oy/react-native-epson-printer 1.0.6 → 1.0.8
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 +27 -0
- package/android/src/main/java/com/munchiepsonprinter/MunchiEpsonModule.java +221 -119
- package/dist/errors.d.ts +1 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/index.js +7 -7
- package/dist/index.mjs +7 -7
- package/dist/utils/errorUtils.d.ts.map +1 -1
- package/dist/version.d.ts +1 -1
- package/ios/Vendors/Epson/MunchiEpsonModule.swift +285 -249
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -30,6 +30,25 @@ This package provides:
|
|
|
30
30
|
|
|
31
31
|
This package does not include Star or Sunmi drivers.
|
|
32
32
|
|
|
33
|
+
## Runtime Contract
|
|
34
|
+
|
|
35
|
+
For both iOS and Android, the SDK aims to provide this behavior:
|
|
36
|
+
|
|
37
|
+
- Native printer work may take time, especially for Bluetooth connection, status checks, and image printing.
|
|
38
|
+
- Slow printer operations are acceptable; visible app hangs are not.
|
|
39
|
+
- Printer work must not block the app UI/main thread while running.
|
|
40
|
+
- Errors from native printer operations must reject back to JS and must not be swallowed.
|
|
41
|
+
- Image and logo printing may be more expensive than text-only receipts, but must still remain asynchronous from the app's point of view.
|
|
42
|
+
|
|
43
|
+
## Observed Performance Expectations
|
|
44
|
+
|
|
45
|
+
These are real-device expectations, not strict guarantees:
|
|
46
|
+
|
|
47
|
+
- iOS: around 3s end-to-end can be acceptable if the app UI remains responsive.
|
|
48
|
+
- Android: the first print may take around 5-6s, with later warm prints often dropping to around 2-3s.
|
|
49
|
+
- Actual timing depends on connection state, Bluetooth behavior, printer readiness, and whether the receipt includes images.
|
|
50
|
+
- Responsiveness and correct JS error propagation matter more than raw duration.
|
|
51
|
+
|
|
33
52
|
## Behavior Support (implemented)
|
|
34
53
|
|
|
35
54
|
| Behavior | Status | Details |
|
|
@@ -41,6 +60,7 @@ This package does not include Star or Sunmi drivers.
|
|
|
41
60
|
| Retry for transient busy/timeout states | Implemented | Native operations retry for retryable states (`BUSY`, `IN_USE`, timeout-family errors). |
|
|
42
61
|
| Print receive timeout guard | Implemented | Native print call fails with `PRINT_TIMEOUT` if printer callback does not return within 30s. |
|
|
43
62
|
| iOS stale-session recovery | Implemented | One-shot session recovery + reconnect + retry for stale/busy/offline connection scenarios. |
|
|
63
|
+
| Non-blocking native printer execution | Implemented | Native connect / print / status work is executed off the app UI thread on both iOS and Android. |
|
|
44
64
|
| Post-print auto-disconnect | Implemented | JS driver schedules disconnect after 5s idle after print completion. |
|
|
45
65
|
| Android warm reconnect cache | Implemented | Native Android keeps a short-lived connection cache (up to 120s) for faster reconnect. |
|
|
46
66
|
| Discovery cleanup/filtering | Implemented | Duplicate targets are deduped and Epson sub-devices such as `[local_display]` are filtered out. |
|
|
@@ -194,6 +214,13 @@ await printer.printEmbedded({
|
|
|
194
214
|
});
|
|
195
215
|
```
|
|
196
216
|
|
|
217
|
+
### Image printing expectations
|
|
218
|
+
|
|
219
|
+
- Image printing is supported.
|
|
220
|
+
- Image printing can take longer than text-only receipts because the image may need decode, resize, rasterization, and transport work.
|
|
221
|
+
- This extra work should not block the app UI.
|
|
222
|
+
- If image printing fails, the error should still reject back to JS like any other print failure.
|
|
223
|
+
|
|
197
224
|
### Cash drawer
|
|
198
225
|
|
|
199
226
|
```ts
|
|
@@ -44,6 +44,13 @@ import java.util.IdentityHashMap;
|
|
|
44
44
|
import java.util.Map;
|
|
45
45
|
import java.util.Set;
|
|
46
46
|
import java.util.UUID;
|
|
47
|
+
import java.util.concurrent.Callable;
|
|
48
|
+
import java.util.concurrent.ExecutionException;
|
|
49
|
+
import java.util.concurrent.ExecutorService;
|
|
50
|
+
import java.util.concurrent.Executors;
|
|
51
|
+
import java.util.concurrent.Future;
|
|
52
|
+
import java.util.concurrent.TimeUnit;
|
|
53
|
+
import java.util.concurrent.TimeoutException;
|
|
47
54
|
|
|
48
55
|
public class MunchiEpsonModule extends ReactContextBaseJavaModule implements ConnectionListener, StatusChangeListener, ReceiveListener, DiscoveryListener {
|
|
49
56
|
private static final String MODULE_NAME = "MunchiEpsonModule";
|
|
@@ -52,12 +59,19 @@ public class MunchiEpsonModule extends ReactContextBaseJavaModule implements Con
|
|
|
52
59
|
private static final String EVENT_PRINTER_RECEIVE = "onPrinterReceive";
|
|
53
60
|
|
|
54
61
|
private static final int DEFAULT_DISCOVERY_TIMEOUT_MS = 5000;
|
|
62
|
+
private static final int DEFAULT_CONNECT_TIMEOUT_MS = 5000;
|
|
55
63
|
private static final int PRINT_TIMEOUT_MS = 30000;
|
|
56
64
|
private static final int CONNECTION_CACHE_TTL_MS = 120000;
|
|
65
|
+
private static final int STATUS_OPERATION_TIMEOUT_MS = 3000;
|
|
66
|
+
private static final int CACHE_HEALTH_CHECK_TIMEOUT_MS = 800;
|
|
67
|
+
private static final int SEND_OPERATION_TIMEOUT_MS = 5000;
|
|
68
|
+
private static final String RECONNECT_REQUIRED_CODE = "PRINTER_RECONNECT_REQUIRED";
|
|
69
|
+
private static final String RECONNECT_REQUIRED_MESSAGE = "Printer connection was lost. Reconnect the printer in Android Bluetooth settings, then try again.";
|
|
57
70
|
|
|
58
71
|
private final ReactApplicationContext reactContext;
|
|
59
72
|
private final Handler mainHandler = new Handler(Looper.getMainLooper());
|
|
60
73
|
private final Object lock = new Object();
|
|
74
|
+
private final ExecutorService printerExecutor = Executors.newCachedThreadPool();
|
|
61
75
|
|
|
62
76
|
private final Map<String, EpsonSession> sessions = new HashMap<>();
|
|
63
77
|
private final Map<Printer, String> printerToSessionId = new IdentityHashMap<>();
|
|
@@ -167,6 +181,7 @@ public class MunchiEpsonModule extends ReactContextBaseJavaModule implements Con
|
|
|
167
181
|
public void connect(String sessionId, String target, double timeout, double model, double lang, Promise promise) {
|
|
168
182
|
int safeModel = normalizeModel((int) model);
|
|
169
183
|
int safeLang = normalizeLanguage((int) lang);
|
|
184
|
+
int safeTimeout = normalizeConnectTimeout((int) timeout);
|
|
170
185
|
|
|
171
186
|
EpsonSession session;
|
|
172
187
|
EpsonSession ownerSessionToReclaim = null;
|
|
@@ -210,69 +225,67 @@ public class MunchiEpsonModule extends ReactContextBaseJavaModule implements Con
|
|
|
210
225
|
|
|
211
226
|
teardownPrinter(session, true);
|
|
212
227
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
if (cachedConnection.model == safeModel
|
|
216
|
-
&& cachedConnection.lang == safeLang
|
|
217
|
-
&& isConnectionHealthy(cachedConnection.printer)) {
|
|
218
|
-
printer = cachedConnection.printer;
|
|
219
|
-
} else {
|
|
220
|
-
disconnectPrinterQuietly(cachedConnection.printer);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
228
|
+
printerExecutor.execute(() -> {
|
|
229
|
+
Printer printer = null;
|
|
223
230
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
+
try {
|
|
232
|
+
if (cachedConnection != null) {
|
|
233
|
+
if (cachedConnection.model == safeModel && cachedConnection.lang == safeLang) {
|
|
234
|
+
if (isBluetoothTarget(target)) {
|
|
235
|
+
disconnectPrinterQuietly(cachedConnection.printer);
|
|
236
|
+
} else if (isConnectionHealthy(cachedConnection.printer, CACHE_HEALTH_CHECK_TIMEOUT_MS)) {
|
|
237
|
+
printer = cachedConnection.printer;
|
|
238
|
+
} else {
|
|
239
|
+
disconnectPrinterQuietly(cachedConnection.printer);
|
|
240
|
+
}
|
|
241
|
+
} else {
|
|
242
|
+
disconnectPrinterQuietly(cachedConnection.printer);
|
|
243
|
+
}
|
|
231
244
|
}
|
|
232
245
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
246
|
+
if (printer == null) {
|
|
247
|
+
Printer freshPrinter = new Printer(safeModel, safeLang, reactContext.getApplicationContext());
|
|
248
|
+
printer = freshPrinter;
|
|
249
|
+
freshPrinter.setConnectionEventListener(this);
|
|
250
|
+
freshPrinter.setStatusChangeEventListener(this);
|
|
251
|
+
freshPrinter.setReceiveEventListener(this);
|
|
252
|
+
runPrinterCall(() -> {
|
|
253
|
+
freshPrinter.connect(target, safeTimeout);
|
|
254
|
+
return null;
|
|
255
|
+
}, safeTimeout);
|
|
256
|
+
freshPrinter.startMonitor();
|
|
257
|
+
}
|
|
243
258
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
} catch (Exception error) {
|
|
252
|
-
if (printer != null) {
|
|
253
|
-
disconnectPrinterQuietly(printer);
|
|
254
|
-
}
|
|
255
|
-
rejectWithEposError(promise, "CONNECT_ERROR", "Failed to connect", error);
|
|
256
|
-
return;
|
|
257
|
-
}
|
|
259
|
+
synchronized (lock) {
|
|
260
|
+
EpsonSession latestSession = sessions.get(sessionId);
|
|
261
|
+
if (latestSession == null) {
|
|
262
|
+
disconnectPrinterQuietly(printer);
|
|
263
|
+
promise.reject("SESSION_ERROR", "Session disposed while connecting");
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
258
266
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
267
|
+
latestSession.printer = printer;
|
|
268
|
+
latestSession.target = target;
|
|
269
|
+
latestSession.model = safeModel;
|
|
270
|
+
latestSession.lang = safeLang;
|
|
271
|
+
printerToSessionId.put(printer, sessionId);
|
|
272
|
+
targetToSessionId.put(target, sessionId);
|
|
273
|
+
}
|
|
266
274
|
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
targetToSessionId.put(target, sessionId);
|
|
273
|
-
}
|
|
275
|
+
promise.resolve(null);
|
|
276
|
+
} catch (Exception error) {
|
|
277
|
+
if (printer != null) {
|
|
278
|
+
disconnectPrinterQuietly(printer);
|
|
279
|
+
}
|
|
274
280
|
|
|
275
|
-
|
|
281
|
+
if (requiresReconnectInstruction(target, error)) {
|
|
282
|
+
promise.reject(RECONNECT_REQUIRED_CODE, RECONNECT_REQUIRED_MESSAGE);
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
rejectWithEposError(promise, "CONNECT_ERROR", "Failed to connect", error);
|
|
287
|
+
}
|
|
288
|
+
});
|
|
276
289
|
}
|
|
277
290
|
|
|
278
291
|
@ReactMethod
|
|
@@ -295,6 +308,7 @@ public class MunchiEpsonModule extends ReactContextBaseJavaModule implements Con
|
|
|
295
308
|
public void print(String sessionId, ReadableArray commands, Promise promise) {
|
|
296
309
|
EpsonSession session;
|
|
297
310
|
Printer printer;
|
|
311
|
+
String target;
|
|
298
312
|
synchronized (lock) {
|
|
299
313
|
session = sessions.get(sessionId);
|
|
300
314
|
if (session == null || session.printer == null) {
|
|
@@ -306,65 +320,80 @@ public class MunchiEpsonModule extends ReactContextBaseJavaModule implements Con
|
|
|
306
320
|
return;
|
|
307
321
|
}
|
|
308
322
|
printer = session.printer;
|
|
323
|
+
target = session.target;
|
|
309
324
|
}
|
|
310
325
|
|
|
311
|
-
if (!ensureBluetoothOperationReadiness(
|
|
312
|
-
return;
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
PrinterStatusInfo status;
|
|
316
|
-
try {
|
|
317
|
-
status = printer.getStatus();
|
|
318
|
-
} catch (Exception error) {
|
|
319
|
-
detachUnhealthyPrinter(sessionId, printer);
|
|
320
|
-
rejectWithEposError(promise, "PRINT_ERROR", "Printer is not reachable", error);
|
|
321
|
-
return;
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
if (!isStatusReady(status)) {
|
|
325
|
-
detachUnhealthyPrinter(sessionId, printer);
|
|
326
|
-
promise.reject("PRINTER_OFFLINE", "Printer is offline or unreachable");
|
|
326
|
+
if (!ensureBluetoothOperationReadiness(target, promise)) {
|
|
327
327
|
return;
|
|
328
328
|
}
|
|
329
329
|
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
330
|
+
printerExecutor.execute(() -> {
|
|
331
|
+
try {
|
|
332
|
+
PrinterStatusInfo status = runPrinterCall(printer::getStatus, STATUS_OPERATION_TIMEOUT_MS);
|
|
333
|
+
if (!isStatusReady(status)) {
|
|
334
|
+
detachUnhealthyPrinter(sessionId, printer);
|
|
335
|
+
promise.reject("PRINTER_OFFLINE", "Printer is offline or unreachable");
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
337
338
|
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
339
|
+
printer.clearCommandBuffer();
|
|
340
|
+
applyCommands(printer, commands);
|
|
341
|
+
} catch (Exception error) {
|
|
342
|
+
detachUnhealthyPrinter(sessionId, printer);
|
|
343
|
+
if (requiresReconnectInstruction(target, error)) {
|
|
344
|
+
promise.reject(RECONNECT_REQUIRED_CODE, RECONNECT_REQUIRED_MESSAGE);
|
|
345
|
+
} else if (error instanceof Epos2Exception) {
|
|
346
|
+
rejectWithEposError(promise, "PRINT_ERROR", "Printer is not reachable", error);
|
|
347
|
+
} else {
|
|
348
|
+
rejectWithEposError(promise, "PRINT_ERROR", "Failed to build print data", error);
|
|
349
|
+
}
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
344
352
|
|
|
345
|
-
try {
|
|
346
|
-
printer.sendData(Printer.PARAM_DEFAULT);
|
|
347
|
-
} catch (Exception error) {
|
|
348
|
-
detachUnhealthyPrinter(sessionId, printer);
|
|
349
|
-
Promise pending = null;
|
|
350
353
|
synchronized (lock) {
|
|
351
354
|
EpsonSession latestSession = sessions.get(sessionId);
|
|
352
|
-
if (latestSession !=
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
latestSession.printPromise = null;
|
|
356
|
-
latestSession.printTimeoutRunnable = null;
|
|
355
|
+
if (latestSession == null || latestSession.printer != printer) {
|
|
356
|
+
promise.reject("PRINT_ERROR", "Printer not connected");
|
|
357
|
+
return;
|
|
357
358
|
}
|
|
359
|
+
latestSession.printPromise = promise;
|
|
360
|
+
Runnable timeoutRunnable = () -> handlePrintTimeout(sessionId);
|
|
361
|
+
latestSession.printTimeoutRunnable = timeoutRunnable;
|
|
362
|
+
mainHandler.postDelayed(timeoutRunnable, PRINT_TIMEOUT_MS);
|
|
358
363
|
}
|
|
359
|
-
|
|
360
|
-
|
|
364
|
+
|
|
365
|
+
try {
|
|
366
|
+
runPrinterCall(() -> {
|
|
367
|
+
printer.sendData(Printer.PARAM_DEFAULT);
|
|
368
|
+
return null;
|
|
369
|
+
}, SEND_OPERATION_TIMEOUT_MS);
|
|
370
|
+
} catch (Exception error) {
|
|
371
|
+
detachUnhealthyPrinter(sessionId, printer);
|
|
372
|
+
Promise pending = null;
|
|
373
|
+
synchronized (lock) {
|
|
374
|
+
EpsonSession latestSession = sessions.get(sessionId);
|
|
375
|
+
if (latestSession != null) {
|
|
376
|
+
pending = latestSession.printPromise;
|
|
377
|
+
cancelPrintTimeoutLocked(latestSession);
|
|
378
|
+
latestSession.printPromise = null;
|
|
379
|
+
latestSession.printTimeoutRunnable = null;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
if (pending != null) {
|
|
383
|
+
if (requiresReconnectInstruction(target, error)) {
|
|
384
|
+
pending.reject(RECONNECT_REQUIRED_CODE, RECONNECT_REQUIRED_MESSAGE);
|
|
385
|
+
} else {
|
|
386
|
+
rejectWithEposError(pending, "PRINT_ERROR", "Failed to send data", error);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
361
389
|
}
|
|
362
|
-
}
|
|
390
|
+
});
|
|
363
391
|
}
|
|
364
392
|
|
|
365
393
|
@ReactMethod
|
|
366
394
|
public void getStatus(String sessionId, Promise promise) {
|
|
367
395
|
EpsonSession session;
|
|
396
|
+
Printer printer;
|
|
368
397
|
synchronized (lock) {
|
|
369
398
|
session = sessions.get(sessionId);
|
|
370
399
|
}
|
|
@@ -374,30 +403,39 @@ public class MunchiEpsonModule extends ReactContextBaseJavaModule implements Con
|
|
|
374
403
|
return;
|
|
375
404
|
}
|
|
376
405
|
|
|
406
|
+
printer = session.printer;
|
|
407
|
+
|
|
377
408
|
if (!ensureBluetoothOperationReadiness(session.target, promise)) {
|
|
378
409
|
return;
|
|
379
410
|
}
|
|
380
411
|
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
WritableMap statusMap = Arguments.createMap();
|
|
389
|
-
boolean paperEmpty = status.getPaper() == Printer.PAPER_EMPTY;
|
|
390
|
-
statusMap.putBoolean("online", isStatusReady(status));
|
|
391
|
-
statusMap.putBoolean("coverOpen", status.getCoverOpen() == Printer.TRUE);
|
|
392
|
-
statusMap.putBoolean("paperEmpty", paperEmpty);
|
|
393
|
-
statusMap.putString("paper", mapPaperStatus(status.getPaper()));
|
|
394
|
-
statusMap.putBoolean("drawerOpen", status.getDrawer() == Printer.DRAWER_HIGH);
|
|
395
|
-
statusMap.putString("errorStatus", mapErrorStatus(status.getErrorStatus()));
|
|
412
|
+
printerExecutor.execute(() -> {
|
|
413
|
+
try {
|
|
414
|
+
PrinterStatusInfo status = runPrinterCall(printer::getStatus, STATUS_OPERATION_TIMEOUT_MS);
|
|
415
|
+
if (status == null) {
|
|
416
|
+
promise.reject("STATUS_ERROR", "Failed to retrieve status");
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
396
419
|
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
420
|
+
WritableMap statusMap = Arguments.createMap();
|
|
421
|
+
boolean paperEmpty = status.getPaper() == Printer.PAPER_EMPTY;
|
|
422
|
+
statusMap.putBoolean("online", isStatusReady(status));
|
|
423
|
+
statusMap.putBoolean("coverOpen", status.getCoverOpen() == Printer.TRUE);
|
|
424
|
+
statusMap.putBoolean("paperEmpty", paperEmpty);
|
|
425
|
+
statusMap.putString("paper", mapPaperStatus(status.getPaper()));
|
|
426
|
+
statusMap.putBoolean("drawerOpen", status.getDrawer() == Printer.DRAWER_HIGH);
|
|
427
|
+
statusMap.putString("errorStatus", mapErrorStatus(status.getErrorStatus()));
|
|
428
|
+
|
|
429
|
+
promise.resolve(statusMap);
|
|
430
|
+
} catch (Exception error) {
|
|
431
|
+
detachUnhealthyPrinter(sessionId, printer);
|
|
432
|
+
if (requiresReconnectInstruction(session.target, error)) {
|
|
433
|
+
promise.reject(RECONNECT_REQUIRED_CODE, RECONNECT_REQUIRED_MESSAGE);
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
rejectWithEposError(promise, "STATUS_ERROR", "Failed to retrieve status", error);
|
|
437
|
+
}
|
|
438
|
+
});
|
|
401
439
|
}
|
|
402
440
|
|
|
403
441
|
@Override
|
|
@@ -534,6 +572,8 @@ public class MunchiEpsonModule extends ReactContextBaseJavaModule implements Con
|
|
|
534
572
|
clearDiscoveryStateLocked();
|
|
535
573
|
}
|
|
536
574
|
|
|
575
|
+
printerExecutor.shutdownNow();
|
|
576
|
+
|
|
537
577
|
super.invalidate();
|
|
538
578
|
}
|
|
539
579
|
|
|
@@ -624,6 +664,10 @@ public class MunchiEpsonModule extends ReactContextBaseJavaModule implements Con
|
|
|
624
664
|
}
|
|
625
665
|
|
|
626
666
|
if (disconnect && target != null && model >= 0 && lang >= 0) {
|
|
667
|
+
if (isBluetoothTarget(target)) {
|
|
668
|
+
disconnectPrinterQuietly(printer);
|
|
669
|
+
return;
|
|
670
|
+
}
|
|
627
671
|
synchronized (lock) {
|
|
628
672
|
if (!targetToSessionId.containsKey(target)) {
|
|
629
673
|
cacheConnectionLocked(target, model, lang, printer);
|
|
@@ -676,9 +720,9 @@ public class MunchiEpsonModule extends ReactContextBaseJavaModule implements Con
|
|
|
676
720
|
cachedConnections.clear();
|
|
677
721
|
}
|
|
678
722
|
|
|
679
|
-
private boolean isConnectionHealthy(Printer printer) {
|
|
723
|
+
private boolean isConnectionHealthy(Printer printer, int timeoutMs) {
|
|
680
724
|
try {
|
|
681
|
-
PrinterStatusInfo status = printer
|
|
725
|
+
PrinterStatusInfo status = runPrinterCall(printer::getStatus, timeoutMs);
|
|
682
726
|
return isStatusReady(status);
|
|
683
727
|
} catch (Exception ignored) {
|
|
684
728
|
return false;
|
|
@@ -1072,6 +1116,58 @@ public class MunchiEpsonModule extends ReactContextBaseJavaModule implements Con
|
|
|
1072
1116
|
|| locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
|
|
1073
1117
|
}
|
|
1074
1118
|
|
|
1119
|
+
private int normalizeConnectTimeout(int timeout) {
|
|
1120
|
+
if (timeout <= 0) {
|
|
1121
|
+
return DEFAULT_CONNECT_TIMEOUT_MS;
|
|
1122
|
+
}
|
|
1123
|
+
return timeout;
|
|
1124
|
+
}
|
|
1125
|
+
|
|
1126
|
+
private boolean requiresReconnectInstruction(String target, Exception error) {
|
|
1127
|
+
if (!isBluetoothTarget(target)) {
|
|
1128
|
+
return false;
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
if (error instanceof PrinterOperationTimeoutException) {
|
|
1132
|
+
return true;
|
|
1133
|
+
}
|
|
1134
|
+
|
|
1135
|
+
if (error instanceof Epos2Exception) {
|
|
1136
|
+
int status = ((Epos2Exception) error).getErrorStatus();
|
|
1137
|
+
return status == Epos2Exception.ERR_CONNECT
|
|
1138
|
+
|| status == Epos2Exception.ERR_TIMEOUT
|
|
1139
|
+
|| status == Epos2Exception.ERR_DISCONNECT
|
|
1140
|
+
|| status == Epos2Exception.ERR_PROCESSING
|
|
1141
|
+
|| status == Epos2Exception.ERR_FAILURE
|
|
1142
|
+
|| status == Epos2Exception.ERR_ILLEGAL;
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
String message = String.valueOf(error.getMessage()).toUpperCase();
|
|
1146
|
+
return message.contains("DISCONNECT")
|
|
1147
|
+
|| message.contains("OFFLINE")
|
|
1148
|
+
|| message.contains("NOT CONNECTED");
|
|
1149
|
+
}
|
|
1150
|
+
|
|
1151
|
+
private <T> T runPrinterCall(Callable<T> callable, int timeoutMs) throws Exception {
|
|
1152
|
+
Future<T> future = printerExecutor.submit(callable);
|
|
1153
|
+
try {
|
|
1154
|
+
return future.get(timeoutMs, TimeUnit.MILLISECONDS);
|
|
1155
|
+
} catch (TimeoutException error) {
|
|
1156
|
+
future.cancel(true);
|
|
1157
|
+
throw new PrinterOperationTimeoutException(timeoutMs, error);
|
|
1158
|
+
} catch (InterruptedException error) {
|
|
1159
|
+
future.cancel(true);
|
|
1160
|
+
Thread.currentThread().interrupt();
|
|
1161
|
+
throw error;
|
|
1162
|
+
} catch (ExecutionException error) {
|
|
1163
|
+
Throwable cause = error.getCause();
|
|
1164
|
+
if (cause instanceof Exception) {
|
|
1165
|
+
throw (Exception) cause;
|
|
1166
|
+
}
|
|
1167
|
+
throw new Exception(cause);
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1075
1171
|
private int normalizeLanguage(int lang) {
|
|
1076
1172
|
if (lang >= 0 && lang <= 7) {
|
|
1077
1173
|
return lang;
|
|
@@ -1226,4 +1322,10 @@ public class MunchiEpsonModule extends ReactContextBaseJavaModule implements Con
|
|
|
1226
1322
|
this.printer = printer;
|
|
1227
1323
|
}
|
|
1228
1324
|
}
|
|
1325
|
+
|
|
1326
|
+
private static final class PrinterOperationTimeoutException extends Exception {
|
|
1327
|
+
PrinterOperationTimeoutException(int timeoutMs, Throwable cause) {
|
|
1328
|
+
super("Printer operation timed out after " + timeoutMs + "ms", cause);
|
|
1329
|
+
}
|
|
1330
|
+
}
|
|
1229
1331
|
}
|
package/dist/errors.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export declare enum PrinterErrorCode {
|
|
|
7
7
|
UNKNOWN = "UNKNOWN"
|
|
8
8
|
}
|
|
9
9
|
export declare enum PrinterTranslationCode {
|
|
10
|
+
ERR_PRINTER_RECONNECT_REQUIRED = "printer.error.reconnect_required",
|
|
10
11
|
ERR_PRINTER_OFFLINE = "printer.error.offline",
|
|
11
12
|
ERR_PRINTER_BUSY = "printer.error.busy",
|
|
12
13
|
ERR_PRINTER_COVER_OPEN = "printer.error.cover_open",
|
package/dist/errors.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,oBAAY,gBAAgB;IAC1B,iBAAiB,sBAAsB;IACvC,YAAY,iBAAiB;IAC7B,gBAAgB,qBAAqB;IACrC,OAAO,YAAY;IACnB,qBAAqB,0BAA0B;IAC/C,OAAO,YAAY;CACpB;AAED,oBAAY,sBAAsB;IAChC,mBAAmB,0BAA0B;IAC7C,gBAAgB,uBAAuB;IACvC,sBAAsB,6BAA6B;IACnD,uBAAuB,8BAA8B;IACrD,sBAAsB,qCAAqC;IAC3D,WAAW,0BAA0B;CACtC;AAED,qBAAa,kBAAmB,SAAQ,KAAK;IAElC,IAAI,EAAE,gBAAgB;IACtB,eAAe,EAAE,sBAAsB;IAEvC,aAAa,CAAC,EAAE,OAAO;gBAHvB,IAAI,EAAE,gBAAgB,EACtB,eAAe,EAAE,sBAAsB,EAC9C,OAAO,EAAE,MAAM,EACR,aAAa,CAAC,EAAE,OAAO,YAAA;CAQjC"}
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,oBAAY,gBAAgB;IAC1B,iBAAiB,sBAAsB;IACvC,YAAY,iBAAiB;IAC7B,gBAAgB,qBAAqB;IACrC,OAAO,YAAY;IACnB,qBAAqB,0BAA0B;IAC/C,OAAO,YAAY;CACpB;AAED,oBAAY,sBAAsB;IAChC,8BAA8B,qCAAqC;IACnE,mBAAmB,0BAA0B;IAC7C,gBAAgB,uBAAuB;IACvC,sBAAsB,6BAA6B;IACnD,uBAAuB,8BAA8B;IACrD,sBAAsB,qCAAqC;IAC3D,WAAW,0BAA0B;CACtC;AAED,qBAAa,kBAAmB,SAAQ,KAAK;IAElC,IAAI,EAAE,gBAAgB;IACtB,eAAe,EAAE,sBAAsB;IAEvC,aAAa,CAAC,EAAE,OAAO;gBAHvB,IAAI,EAAE,gBAAgB,EACtB,eAAe,EAAE,sBAAsB,EAC9C,OAAO,EAAE,MAAM,EACR,aAAa,CAAC,EAAE,OAAO,YAAA;CAQjC"}
|