diff --git a/android/src/main/java/com/reactnativeescposprinter/EscPosPrinterModule.java b/android/src/main/java/com/reactnativeescposprinter/EscPosPrinterModule.java index a3c8581..979f95d 100644 --- a/android/src/main/java/com/reactnativeescposprinter/EscPosPrinterModule.java +++ b/android/src/main/java/com/reactnativeescposprinter/EscPosPrinterModule.java @@ -440,46 +440,46 @@ private void performMonitoring(int inteval) { public void run() { if(mIsMonitoring) { - tasksQueue.submit(new Callable() { - @Override - public String call() { - PrinterStatusInfo statusInfo = null; - try { - connectPrinter(); - statusInfo = mPrinter.getStatus(); - WritableMap msg = EscPosPrinterErrorManager.makeStatusMassage(statusInfo); - - reactContext - .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) - .emit("onMonitorStatusUpdate", msg); - disconnectPrinter(); - return null; - } catch(Epos2Exception e) { - int errorStatus = ((Epos2Exception) e).getErrorStatus(); - - if (errorStatus != Epos2Exception.ERR_PROCESSING && errorStatus != Epos2Exception.ERR_ILLEGAL) { - - WritableMap msg = EscPosPrinterErrorManager.getOfflineStatusMessage(); - + tasksQueue.submit(new Callable() { + @Override + public String call() { + try { + WritableMap msg = getMPrinterStatus(); reactContext - .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) - .emit("onMonitorStatusUpdate", msg); - } - return null; - } finally { - handler.postDelayed(monitor, inteval); - } - - } - }); - } + .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) + .emit("onMonitorStatusUpdate", msg); + } catch (Epos2Exception e) {} finally { + handler.postDelayed(monitor, inteval); + } + return null; + } + }); + } } }; - monitor.run(); } + public WritableMap getMPrinterStatus() throws Epos2Exception { + try { + connectPrinter(); + PrinterStatusInfo statusInfo = mPrinter.getStatus(); + WritableMap msg = EscPosPrinterErrorManager.makeStatusMassage(statusInfo); + disconnectPrinter(); + return msg; + } catch (Epos2Exception e) { + int errorStatus = ((Epos2Exception) e).getErrorStatus(); + + if (errorStatus != Epos2Exception.ERR_PROCESSING && errorStatus != Epos2Exception.ERR_ILLEGAL) { + + WritableMap msg = EscPosPrinterErrorManager.getOfflineStatusMessage(); + return msg; + } + throw e; + } + } + @ReactMethod public void startMonitorPrinter(int interval, Promise promise) { @@ -515,6 +515,21 @@ public void stopMonitorPrinter(Promise promise) { promise.resolve(successString); } + @ReactMethod + public void getPrinterStatus(Promise promise) { + tasksQueue.submit(new Runnable() { + @Override + public void run() { + try { + WritableMap msg = getMPrinterStatus(); + promise.resolve(msg); + } catch (Epos2Exception e) { + promise.reject("Failed to get status"); + } + } + }); + } + @ReactMethod public void printBuffer(ReadableArray printBuffer, final ReadableMap paramsMap, Promise promise) { tasksQueue.submit(new Runnable() { diff --git a/ios/EscPosPrinter.m b/ios/EscPosPrinter.m index 709d840..afab4ac 100644 --- a/ios/EscPosPrinter.m +++ b/ios/EscPosPrinter.m @@ -199,6 +199,17 @@ + (BOOL)requiresMainQueueSetup [self disconnectPrinter]; } +RCT_EXPORT_METHOD(getPrinterStatus: (RCTPromiseResolveBlock)resolve + withRejecter:(RCTPromiseRejectBlock)reject) +{ + + [self getMPrinterStatus:^(NSDictionary *result) { + resolve(result); + } onError:^(NSString *error) { + reject(@"event_failure",error, nil); + }]; +} + RCT_EXPORT_METHOD(startMonitorPrinter:(int) interval withResolver: (RCTPromiseResolveBlock)resolve withRejecter:(RCTPromiseRejectBlock)reject) @@ -421,45 +432,49 @@ - (void)onSetPrinterSetting:(int)code { // nothing to do } -- (void)performMonitoring: (NSTimer*)timer { - int interval = [timer.userInfo intValue]; - +- (void)getMPrinterStatus:(void(^)(NSDictionary *))onSuccess + onError: (void(^)(NSString *))onError +{ __block Epos2PrinterStatusInfo *info; __block int result = EPOS2_SUCCESS; - if(self->isMonitoring_) { - [self->tasksQueue addOperationWithBlock: ^{ - result = [self connectPrinter]; - - if (result != EPOS2_SUCCESS) { - if(result != EPOS2_ERR_ILLEGAL && result != EPOS2_ERR_PROCESSING) { - NSDictionary *msg = [ErrorManager getOfflineStatusMessage]; - @try { - [self sendEventWithName:@"onMonitorStatusUpdate" body: msg]; - } @catch(NSException *e) { - } + [self->tasksQueue addOperationWithBlock: ^{ + result = [self connectPrinter]; - } + if (result != EPOS2_SUCCESS) { + if(result != EPOS2_ERR_ILLEGAL && result != EPOS2_ERR_PROCESSING) { + NSDictionary *msg = [ErrorManager getOfflineStatusMessage]; + onSuccess(msg); } else { - info = [self->printer getStatus]; - - [[NSOperationQueue mainQueue] addOperationWithBlock:^{ - NSDictionary *msg = [ErrorManager makeStatusMessage: info]; - if(msg != nil){ - @try { - [self sendEventWithName:@"onMonitorStatusUpdate" body: msg]; - } @catch(NSException *e) { - } - } - }]; - [self disconnectPrinter]; + onError(@"Get status failed"); } + } else { + info = [self->printer getStatus]; + + [[NSOperationQueue mainQueue] addOperationWithBlock:^{ + NSDictionary *msg = [ErrorManager makeStatusMessage: info]; + if(msg != nil) { + onSuccess(msg); + } else { + onError(@"Get status failed"); + } + }]; + [self disconnectPrinter]; + } + }]; +} + +- (void)performMonitoring: (NSTimer*)timer { + int interval = [timer.userInfo intValue]; - [[NSOperationQueue mainQueue] addOperationWithBlock:^{ - [NSTimer scheduledTimerWithTimeInterval: (int)interval target:self selector: @selector(performMonitoring:) userInfo: @(interval) repeats:NO]; - }]; + if(self->isMonitoring_) { + [self getMPrinterStatus:^(NSDictionary *msg) { + [self sendEventWithName:@"onMonitorStatusUpdate" body: msg]; - }]; + [[NSOperationQueue mainQueue] addOperationWithBlock:^{ + [NSTimer scheduledTimerWithTimeInterval: (int)interval target:self selector: @selector(performMonitoring:) userInfo: @(interval) repeats:NO]; + }]; + } onError:^(NSString *error) {}]; } } diff --git a/src/index.tsx b/src/index.tsx index ff95ee3..4a40302 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -9,6 +9,7 @@ import type { IPrinterInitParams, PrinterSeriesName, IMonitorStatus, + IPrinterStatus, } from './types'; import { PRINTER_SERIES, @@ -118,6 +119,10 @@ const _default = { EscPosPrinter.disconnect(); }, + getPrinterStatus(): Promise { + return EscPosPrinter.getPrinterStatus(); + }, + startMonitorPrinter(interval: number = 5) { return EscPosPrinter.startMonitorPrinter(Math.max(5, Math.floor(interval))); }, @@ -145,6 +150,7 @@ export type { EventListenerCallback, IPrinter, PrinterSeriesName, + IPrinterStatus, }; export default _default; diff --git a/src/types.ts b/src/types.ts index 21dd6a6..c46b062 100644 --- a/src/types.ts +++ b/src/types.ts @@ -197,3 +197,5 @@ export interface ISpaceBetweenParams { gapSymbol?: string; noTrim?: boolean; } + +export type IPrinterStatus = IMonitorStatus;