@munchi_oy/react-native-label-printer 2.0.0 → 2.0.1

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.
@@ -246,10 +246,11 @@ RCT_REMAP_METHOD(printLabel,
246
246
  CGFloat titlePointSize = [fontSizes[@"title"] doubleValue];
247
247
  CGFloat bodyPointSize = [fontSizes[@"body"] doubleValue];
248
248
 
249
- NSInteger maxAttempts = 3;
250
- NSTimeInterval retryDelay = 0.35;
249
+ NSInteger maxAttempts = 4;
250
+ NSTimeInterval retryDelay = 1.0;
251
251
  NSMutableArray *attemptLogs = [NSMutableArray array];
252
252
  NSDictionary *finalSuccessResult = nil;
253
+ NSMutableDictionary *lastFailure = nil;
253
254
 
254
255
  for (NSInteger attempt = 1; attempt <= maxAttempts; attempt++) {
255
256
  BRLMChannel *channel = nil;
@@ -258,44 +259,123 @@ RCT_REMAP_METHOD(printLabel,
258
259
  @try {
259
260
  channel = [[BRLMChannel alloc] initWithWifiIPAddress:ipAddress];
260
261
  if (channel == nil) {
261
- [attemptLogs addObject:[self attemptErrorWithAttempt:attempt stage:@"channel_create" reason:@"Failed to create channel"]];
262
- if (attempt < maxAttempts) [NSThread sleepForTimeInterval:retryDelay];
262
+ lastFailure = [self attemptErrorWithAttempt:attempt
263
+ stage:@"channel_create"
264
+ reason:@"Failed to create channel"];
265
+ [attemptLogs addObject:lastFailure];
266
+
267
+ if (attempt < maxAttempts) {
268
+ [NSThread sleepForTimeInterval:retryDelay];
269
+ }
263
270
  continue;
264
271
  }
265
272
 
266
273
  BRLMPrinterDriverGenerateResult *openResult = [BRLMPrinterDriverGenerator openChannel:channel];
267
274
 
275
+ NSInteger openChannelCode = 0;
276
+ NSString *openChannelError = @"";
277
+ if (openResult != nil && openResult.error != nil) {
278
+ openChannelCode = openResult.error.code;
279
+ openChannelError = [openResult.error description] ?: @"";
280
+ }
281
+
268
282
  if (openResult == nil || openResult.driver == nil) {
269
- [attemptLogs addObject:[self attemptErrorWithAttempt:attempt
270
- stage:@"open_channel"
271
- reason:@"Failed to open driver"
272
- extras:@{ @"openChannelError": openResult.error ? [openResult.error description] : @"" }]];
273
- if (attempt < maxAttempts) [NSThread sleepForTimeInterval:retryDelay];
283
+ lastFailure = [self attemptErrorWithAttempt:attempt
284
+ stage:@"open_channel"
285
+ reason:@"Failed to open driver"
286
+ extras:@{
287
+ @"openChannelCode": @(openChannelCode),
288
+ @"openChannelError": openChannelError
289
+ }];
290
+ [attemptLogs addObject:lastFailure];
291
+
292
+
293
+ if (attempt < maxAttempts) {
294
+ @try {
295
+ BRLMChannel *warmupChannel = [[BRLMChannel alloc] initWithWifiIPAddress:ipAddress];
296
+ if (warmupChannel != nil) {
297
+ BRLMPrinterDriverGenerateResult *warmupResult = [BRLMPrinterDriverGenerator openChannel:warmupChannel];
298
+ if (warmupResult != nil && warmupResult.driver != nil) {
299
+ if ([warmupResult.driver respondsToSelector:@selector(closeChannel)]) {
300
+ [warmupResult.driver closeChannel];
301
+ }
302
+ }
303
+ }
304
+ } @catch (NSException *warmupException) {
305
+
306
+ }
307
+
308
+ [NSThread sleepForTimeInterval:retryDelay];
309
+ }
274
310
  continue;
275
311
  }
276
312
 
277
313
  driver = openResult.driver;
278
314
 
279
-
315
+
316
+ [NSThread sleepForTimeInterval:0.8];
317
+
280
318
  BRLMQLPrintSettings *settings = [[BRLMQLPrintSettings alloc] initDefaultPrintSettingsWithPrinterModel:BRLMPrinterModelQL_810W];
281
319
 
282
320
  if (settings == nil) {
283
321
  if ([driver respondsToSelector:@selector(closeChannel)]) {
284
322
  [driver closeChannel];
285
323
  }
286
- [attemptLogs addObject:[self attemptErrorWithAttempt:attempt stage:@"settings_create" reason:@"Failed to create QL print settings"]];
287
- if (attempt < maxAttempts) [NSThread sleepForTimeInterval:retryDelay];
324
+
325
+ lastFailure = [self attemptErrorWithAttempt:attempt
326
+ stage:@"settings_create"
327
+ reason:@"Failed to create QL print settings"];
328
+ [attemptLogs addObject:lastFailure];
329
+
330
+ if (attempt < maxAttempts) {
331
+ [NSThread sleepForTimeInterval:retryDelay];
332
+ }
288
333
  continue;
289
334
  }
290
335
 
291
- BRLMGetPrinterStatusResult *statusResult = [driver getPrinterStatus];
292
- BRLMMediaInfo *mediaInfo = statusResult.status.mediaInfo;
336
+ BRLMGetPrinterStatusResult *statusResult = nil;
337
+ BRLMMediaInfo *mediaInfo = nil;
338
+ BOOL printerReady = NO;
293
339
 
294
- bool succeeded = false;
295
- if (mediaInfo != nil) {
296
- settings.labelSize = [mediaInfo getQLLabelSize:&succeeded];
340
+ for (NSInteger statusTry = 1; statusTry <= 3; statusTry++) {
341
+ statusResult = [driver getPrinterStatus];
342
+
343
+ if (statusResult != nil && statusResult.status != nil) {
344
+ mediaInfo = statusResult.status.mediaInfo;
345
+ if (mediaInfo != nil) {
346
+ printerReady = YES;
347
+ break;
348
+ }
349
+ }
350
+
351
+ if (statusTry < 3) {
352
+ [NSThread sleepForTimeInterval:0.7];
353
+ }
354
+ }
355
+
356
+ if (!printerReady) {
357
+ if ([driver respondsToSelector:@selector(closeChannel)]) {
358
+ [driver closeChannel];
359
+ }
360
+
361
+ lastFailure = [self attemptErrorWithAttempt:attempt
362
+ stage:@"get_status"
363
+ reason:@"Printer status/media not ready after reconnect"
364
+ extras:@{
365
+ @"statusAvailable": @(statusResult != nil),
366
+ @"statusObjectAvailable": @((statusResult != nil && statusResult.status != nil))
367
+ }];
368
+ [attemptLogs addObject:lastFailure];
369
+
370
+ if (attempt < maxAttempts) {
371
+ [NSThread sleepForTimeInterval:retryDelay];
372
+ }
373
+ continue;
297
374
  }
298
375
 
376
+ bool succeeded = false;
377
+ settings.labelSize = [mediaInfo getQLLabelSize:&succeeded];
378
+
299
379
  NSDictionary *mediaExtras = @{
300
380
  @"mediaType": mediaInfo ? @(mediaInfo.mediaType) : [NSNull null],
301
381
  @"width_mm": mediaInfo ? @(mediaInfo.width_mm) : [NSNull null],
@@ -307,11 +387,16 @@ RCT_REMAP_METHOD(printLabel,
307
387
  if ([driver respondsToSelector:@selector(closeChannel)]) {
308
388
  [driver closeChannel];
309
389
  }
310
- [attemptLogs addObject:[self attemptErrorWithAttempt:attempt
311
- stage:@"media_to_label_size"
312
- reason:@"Failed to determine QL label size from loaded media"
313
- extras:mediaExtras]];
314
- if (attempt < maxAttempts) [NSThread sleepForTimeInterval:retryDelay];
390
+
391
+ lastFailure = [self attemptErrorWithAttempt:attempt
392
+ stage:@"media_to_label_size"
393
+ reason:@"Failed to determine QL label size from loaded media"
394
+ extras:mediaExtras];
395
+ [attemptLogs addObject:lastFailure];
396
+
397
+ if (attempt < maxAttempts) {
398
+ [NSThread sleepForTimeInterval:retryDelay];
399
+ }
315
400
  continue;
316
401
  }
317
402
 
@@ -391,12 +476,18 @@ RCT_REMAP_METHOD(printLabel,
391
476
 
392
477
  if (printError != nil && printError.code != BRLMPrintErrorCodeNoError) {
393
478
  NSMutableDictionary *printExtras = [mediaExtras mutableCopy];
479
+ printExtras[@"printErrorCode"] = @(printError.code);
394
480
  printExtras[@"printError"] = [printError description] ?: @"";
395
- [attemptLogs addObject:[self attemptErrorWithAttempt:attempt
396
- stage:@"print"
397
- reason:@"Printer returned print error"
398
- extras:printExtras]];
399
- if (attempt < maxAttempts) [NSThread sleepForTimeInterval:retryDelay];
481
+
482
+ lastFailure = [self attemptErrorWithAttempt:attempt
483
+ stage:@"print"
484
+ reason:@"Printer returned print error"
485
+ extras:printExtras];
486
+ [attemptLogs addObject:lastFailure];
487
+
488
+ if (attempt < maxAttempts) {
489
+ [NSThread sleepForTimeInterval:retryDelay];
490
+ }
400
491
  continue;
401
492
  }
402
493
 
@@ -424,10 +515,15 @@ RCT_REMAP_METHOD(printLabel,
424
515
  if (driver && [driver respondsToSelector:@selector(closeChannel)]) {
425
516
  [driver closeChannel];
426
517
  }
427
- [attemptLogs addObject:[self attemptErrorWithAttempt:attempt
428
- stage:@"exception"
429
- reason:innerException.reason ?: @"Unknown native exception"]];
430
- if (attempt < maxAttempts) [NSThread sleepForTimeInterval:retryDelay];
518
+
519
+ lastFailure = [self attemptErrorWithAttempt:attempt
520
+ stage:@"exception"
521
+ reason:innerException.reason ?: @"Unknown native exception"];
522
+ [attemptLogs addObject:lastFailure];
523
+
524
+ if (attempt < maxAttempts) {
525
+ [NSThread sleepForTimeInterval:retryDelay];
526
+ }
431
527
  }
432
528
  }
433
529
 
@@ -436,13 +532,21 @@ RCT_REMAP_METHOD(printLabel,
436
532
  return;
437
533
  }
438
534
 
439
- NSMutableDictionary *exhaustedResult = [self errorWithStage:@"retry_exhausted" reason:@"Print failed after 3 attempts"];
535
+ NSMutableDictionary *exhaustedResult = [self errorWithStage:@"retry_exhausted"
536
+ reason:@"Print failed after all recovery attempts"];
440
537
  exhaustedResult[@"attemptLogs"] = attemptLogs;
441
538
  exhaustedResult[@"fontSize"] = fontSizeValue ?: @"Medium";
442
539
  exhaustedResult[@"fontStyle"] = fontStyleValue ?: @"Normal";
443
540
  exhaustedResult[@"title"] = title ?: @"";
444
541
  exhaustedResult[@"hideTitle"] = @(hideTitle);
445
542
  exhaustedResult[@"copies"] = @(copies);
543
+
544
+ if (lastFailure != nil) {
545
+ exhaustedResult[@"lastFailureStage"] = lastFailure[@"stage"] ?: @"unknown";
546
+ exhaustedResult[@"lastFailureReason"] = lastFailure[@"reason"] ?: @"Unknown error";
547
+ exhaustedResult[@"lastFailureDetails"] = lastFailure;
548
+ }
549
+
446
550
  resolve(exhaustedResult);
447
551
 
448
552
  } @catch (NSException *exception) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@munchi_oy/react-native-label-printer",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "React Native bridge for label printers",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",