Skip to content

Commit

Permalink
Merge pull request #134 from wix-playground/main
Browse files Browse the repository at this point in the history
Feature - New Method: `getPrinterStatus`
  • Loading branch information
tr3v3r authored Feb 5, 2024
2 parents bd46205 + 01c396a commit 87866da
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -440,46 +440,46 @@ private void performMonitoring(int inteval) {
public void run() {

if(mIsMonitoring) {
tasksQueue.submit(new Callable<String>() {
@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<String>() {
@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) {

Expand Down Expand Up @@ -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() {
Expand Down
77 changes: 46 additions & 31 deletions ios/EscPosPrinter.m
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {}];
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
IPrinterInitParams,
PrinterSeriesName,
IMonitorStatus,
IPrinterStatus,
} from './types';
import {
PRINTER_SERIES,
Expand Down Expand Up @@ -118,6 +119,10 @@ const _default = {
EscPosPrinter.disconnect();
},

getPrinterStatus(): Promise<IPrinterStatus> {
return EscPosPrinter.getPrinterStatus();
},

startMonitorPrinter(interval: number = 5) {
return EscPosPrinter.startMonitorPrinter(Math.max(5, Math.floor(interval)));
},
Expand Down Expand Up @@ -145,6 +150,7 @@ export type {
EventListenerCallback,
IPrinter,
PrinterSeriesName,
IPrinterStatus,
};

export default _default;
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,5 @@ export interface ISpaceBetweenParams {
gapSymbol?: string;
noTrim?: boolean;
}

export type IPrinterStatus = IMonitorStatus;

0 comments on commit 87866da

Please sign in to comment.