Skip to content

Commit

Permalink
feat(add): add print helpers and pairing printer method
Browse files Browse the repository at this point in the history
  • Loading branch information
tr3v3r committed Mar 12, 2024
1 parent a183577 commit 4f32215
Show file tree
Hide file tree
Showing 31 changed files with 746 additions and 207 deletions.
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ PODS:
- React-jsinspector (0.72.6)
- React-logger (0.72.6):
- glog
- react-native-esc-pos-printer (3.0.1):
- react-native-esc-pos-printer (3.1.0):
- RCT-Folly (= 2021.07.22.00)
- React-Core
- react-native-image-picker (7.1.0):
Expand Down Expand Up @@ -605,7 +605,7 @@ SPEC CHECKSUMS:
React-jsiexecutor: 3bf18ff7cb03cd8dfdce08fbbc0d15058c1d71ae
React-jsinspector: 194e32c6aab382d88713ad3dd0025c5f5c4ee072
React-logger: cebf22b6cf43434e471dc561e5911b40ac01d289
react-native-esc-pos-printer: 6e971d88f296caa084c7b52cee955db235bc5ed6
react-native-esc-pos-printer: a01888ff0789f75d80370b8cbf291e9801eea4c8
react-native-image-picker: 5e076db26cd81660cfb6db5bcf517cfa12054d45
react-native-safe-area-context: 2cd91d532de12acdb0a9cbc8d43ac72a8e4c897c
React-NativeModulesApple: 02e35e9a51e10c6422f04f5e4076a7c02243fff2
Expand Down
47 changes: 47 additions & 0 deletions example/src/components/PrinterStatus/PrinterStatus.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React, { memo } from 'react';
import { Text, View } from 'react-native';
import {
type PrinterStatusResponse,
PrinterConstants,
} from 'react-native-esc-pos-printer';
import { styles } from './styles';

interface PrinterStatusProps {
status: PrinterStatusResponse;
}

export const PrinterStatus = memo(({ status }: PrinterStatusProps) => {
const { connection, online, coverOpen, paper } = status;

const renderRow = (
title: string,
isGreenIndicator: boolean,
isYellowIndicator?: boolean
) => {
return (
<View style={styles.container}>
<Text style={styles.text}>{title}</Text>
<View
style={[
styles.indicator,
isGreenIndicator ? styles.indicatorGreen : styles.indicatorRed,
isYellowIndicator && styles.indicatorYellow,
]}
/>
</View>
);
};

return (
<View style={[styles.container, styles.contentContainer]}>
{renderRow('Connected', connection.statusCode === PrinterConstants.TRUE)}
{renderRow('Online', online.statusCode === PrinterConstants.TRUE)}
{renderRow('Cover open', coverOpen.statusCode === PrinterConstants.FALSE)}
{renderRow(
'Paper',
paper.statusCode === PrinterConstants.PAPER_OK,
paper.statusCode === PrinterConstants.PAPER_NEAR_END
)}
</View>
);
});
1 change: 1 addition & 0 deletions example/src/components/PrinterStatus/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { PrinterStatus } from './PrinterStatus';
31 changes: 31 additions & 0 deletions example/src/components/PrinterStatus/styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { StyleSheet } from 'react-native';

export const styles = StyleSheet.create({
contentContainer: {
justifyContent: 'space-around',
},
indicator: {
width: 6,
height: 6,
borderRadius: 5,
marginRight: 15,
marginLeft: 3,
},
indicatorGreen: {
backgroundColor: 'green',
},
indicatorRed: {
backgroundColor: 'red',
},
indicatorYellow: {
backgroundColor: 'yellow',
},
container: {
flexDirection: 'row',
alignItems: 'center',
},
text: {
fontSize: 14,
color: '#000',
},
});
1 change: 1 addition & 0 deletions example/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './PrintersList';
export * from './Button';
export * from './ScreenTitle';
export * from './PrinterInfo';
export * from './PrinterStatus';
66 changes: 39 additions & 27 deletions example/src/screens/SimplePrint.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import React, { memo, useState } from 'react';
import React, { memo, useEffect, useMemo, useState } from 'react';
import { View, StyleSheet, Text } from 'react-native';
import { PrinterInfo, Button, ScreenTitle } from '../components';
import { PrinterInfo, Button, ScreenTitle, PrinterStatus } from '../components';
import type { RootStackParamList } from '../navigation/RootNavigator';
import { Printer, PrinterConstants } from 'react-native-esc-pos-printer';
import {
Printer,
PrinterConstants,
addTextLine,
monitorPrinter,
type PrinterStatusResponse,
} from 'react-native-esc-pos-printer';
import { useRoute, type RouteProp } from '@react-navigation/native';
import { base64Image } from '../base64Image';

Check failure on line 13 in example/src/screens/SimplePrint.tsx

View workflow job for this annotation

GitHub Actions / lint

'base64Image' is defined but never used

Expand All @@ -17,48 +23,53 @@ export const SimplePrint = memo(() => {
} = useRoute<SimplePrintRouteProp>();

const [printing, setPrinting] = useState(false);
const [status, setStatus] = useState<PrinterStatusResponse | null>(null);

const printerInstance = useMemo(
() =>
new Printer({
target: printer.target,
deviceName: printer.deviceName,
}),
[printer]
);

const printSimpleReceipt = async () => {
const printerInstance = new Printer({
target: printer.target,
deviceName: printer.deviceName,
useEffect(() => {
const stop = monitorPrinter(printerInstance, (nextStatus) => {
setStatus(nextStatus);
});

return stop;
}, [printerInstance]);

const printSimpleReceipt = async () => {
// const result = await launchImageLibrary({ mediaType: 'photo' });
// console.log(result);
try {
setPrinting(true);

const res = await printerInstance.queue.add(async () => {
await printerInstance.connect();

await printerInstance.addImage({
source: { uri: base64Image },
width: 100,
});
// await printerInstance.addImage({
// source: { uri: base64Image },
// width: 100,
// });

// await printerInstance.addImage({
// source: {
// uri: 'file:///data/user/0/com.escposprinterexample/cache/rn_image_picker_lib_temp_bed8229d-f605-4417-94b9-41062c6a80e5.jpg',
// },
// width: 200,
// });

await printerInstance.addBarcode({
data: 'Test123',
type: PrinterConstants.BARCODE_CODE93,
width: 2,
height: 50,
hri: PrinterConstants.HRI_BELOW,
await printerInstance.addTextSize({ width: 2, height: 2 });
await printerInstance.addTextSmooth(PrinterConstants.FALSE);
await addTextLine(printerInstance, {
left: 'Chickenburger',
right: '1.5 EUR',
gapSymbol: '.',
});

// await printerInstance.addSymbol({
// type: PrinterConstants.SYMBOL_QRCODE_MODEL_2,
// level: PrinterConstants.LEVEL_M,
// width: 5,
// height: 5,
// size: 5,
// data: 'Test123',
// });
await printerInstance.addFeedLine();

await printerInstance.addFeedLine();
await printerInstance.addCut();
Expand Down Expand Up @@ -94,6 +105,7 @@ export const SimplePrint = memo(() => {
<ScreenTitle title={'Simple Print'} />
</View>
<View style={styles.contentCotainer}>
{status ? <PrinterStatus status={status} /> : null}
<PrinterInfo printer={printer} />
</View>
<View style={styles.contentCotainer}>
Expand Down
52 changes: 27 additions & 25 deletions ios/EscPosPrinter.m
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,13 @@ - (NSDictionary *)constantsToExport
@"ALIGN_LEFT": @(EPOS2_ALIGN_LEFT),
@"ALIGN_CENTER": @(EPOS2_ALIGN_CENTER),
@"ALIGN_RIGHT": @(EPOS2_ALIGN_RIGHT),

// pair bluetooth device error

@"BT_ERR_PARAM": @(EPOS2_BT_ERR_PARAM),
@"BT_ERR_UNSUPPORTED": @(EPOS2_BT_ERR_UNSUPPORTED),
@"BT_ERR_CANCEL": @(EPOS2_BT_ERR_CANCEL),
@"BT_ERR_ILLEGAL_DEVICE": @(EPOS2_BT_ERR_ILLEGAL_DEVICE),
};
}

Expand Down Expand Up @@ -475,18 +482,6 @@ + (BOOL)requiresMainQueueSetup
reject(@"event_failure", [@(result) stringValue], nil);
}
}

int timeout = (int)[params[@"timeout"] integerValue] ?: EPOS2_PARAM_DEFAULT;

result = [printer sendData:timeout];
if (result != EPOS2_SUCCESS) {
[printer clearCommandBuffer];
[printer disconnect];
return result;
}

return result;

}


Expand Down Expand Up @@ -609,8 +604,6 @@ + (BOOL)requiresMainQueueSetup
reject(@"event_failure", [@(result) stringValue], nil);
}
}

return result;
}


Expand Down Expand Up @@ -639,17 +632,6 @@ + (BOOL)requiresMainQueueSetup
reject(@"event_failure", [@(result) stringValue], nil);
}
}

result = [self printData:params];
if (result != EPOS2_SUCCESS) {
NSString *errorString = [ErrorManager getEposErrorText: result];
onError(errorString);
return;
}

[self->printer clearCommandBuffer];
NSString *successString = [ErrorManager getEposErrorText: EPOS2_SUCCESS];
onSuccess(successString);
}

RCT_EXPORT_METHOD(addSymbol: (nonnull NSString*) target
Expand Down Expand Up @@ -726,4 +708,24 @@ + (BOOL)requiresMainQueueSetup
}
}



RCT_EXPORT_METHOD(pairBluetoothDevice: (NSString *) macAddress
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
{
@synchronized (self) {
Epos2BluetoothConnection *pairingPrinter = [[Epos2BluetoothConnection alloc] init];
NSMutableString *address = [NSMutableString stringWithString: macAddress];
int result = [pairingPrinter connectDevice: address];

if(result == EPOS2_BT_SUCCESS || result == EPOS2_BT_ERR_ALREADY_CONNECT) {
resolve(nil);
} else {
reject(@"event_failure", [@(result) stringValue], nil);
}
}
}


@end
4 changes: 3 additions & 1 deletion ios/ThePrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ typedef NS_ENUM(NSUInteger, ThePrinterState) {

-(nonnull NSDictionary*) getStatus;

-(int) pairBluetoothDevice;

/**
Returns BOOL
Function isConnected uses getStatus to understand if it is connected or not.
Expand Down Expand Up @@ -174,7 +176,7 @@ typedef NS_ENUM(NSUInteger, ThePrinterState) {
@return ePOS int result
*/
-(void) getPrinterSetting:(long)timeout type:(int)type successHandler: (void(^_Nonnull)(NSDictionary*_Nonnull data)) successHandler
errorHandler: (void(^_Nonnull)(NSString* data)) errorHandler;
errorHandler: (void(^_Nonnull)(NSString*_Nonnull data)) errorHandler;
/**
Returns ePOS int result
Function sendData see ePOS SDK
Expand Down
45 changes: 22 additions & 23 deletions ios/ThePrinter.m
Original file line number Diff line number Diff line change
Expand Up @@ -332,23 +332,12 @@ - (void) sendData:(long)timeout
successHandler: (void(^)(NSDictionary* data)) successHandler
errorHandler: (void(^)(NSString* data)) errorHandler
{
@synchronized (shutdownLock_) {
if (shutdown_) {

errorHandler(@{
@"data": @(EPOS2_ERR_ILLEGAL),
@"type": @"result"
});
return;
};
}

@synchronized (self) {
if (epos2Printer_ == nil) {
errorHandler(@{
@"data": @(EPOS2_ERR_MEMORY),
@"type": @"result"
});
errorHandler([ErrorManager convertDictionatyToJsonString:@{
@"data": @(EPOS2_ERR_MEMORY),
@"type": @"result"
}]);

return;
};
Expand All @@ -359,10 +348,10 @@ - (void) sendData:(long)timeout
onPtrRecieveSuccessHandler_ = [successHandler copy];
onPtrRecieveErrorHandler_ = [errorHandler copy];
} else {
errorHandler(@{
errorHandler([ErrorManager convertDictionatyToJsonString:@{
@"data": @(result),
@"type": @"result"
});
}]);
}

}
Expand Down Expand Up @@ -625,17 +614,26 @@ -(NSDictionary*) getStatus
}
}

-(int) pairBluetoothDevice
{
Epos2BluetoothConnection *pairingPrinter = [[Epos2BluetoothConnection alloc] init];
NSMutableString *address = [[NSMutableString alloc] init];
int result = [pairingPrinter connectDevice: address];

return result;
}

-(void) getPrinterSetting: (long)timeout
type:(int)type
successHandler: (void(^)(NSDictionary* data)) successHandler
errorHandler: (void(^)(NSString* data)) errorHandler
{
@synchronized (self) {
if (epos2Printer_ == nil) {
errorHandler(@{
@"data": @(EPOS2_ERR_MEMORY),
@"type": @"result"
});
errorHandler([ErrorManager convertDictionatyToJsonString:@{
@"data": @(EPOS2_ERR_MEMORY),
@"type": @"result"
}]);
return;
}

Expand All @@ -646,10 +644,11 @@ -(void) getPrinterSetting: (long)timeout
onGetPrinterSettingSuccessHandler_ = [successHandler copy];
onGetPrinterSettingErrorHandler_ = [errorHandler copy];
} else {
errorHandler(@{
errorHandler(
[ErrorManager convertDictionatyToJsonString:@{
@"data": @(result),
@"type": @"result"
});
}]);
}
}
}
Expand Down
Loading

0 comments on commit 4f32215

Please sign in to comment.