Skip to content

Commit

Permalink
finised adding Ciontek printer support
Browse files Browse the repository at this point in the history
  • Loading branch information
robertclarkson committed Nov 27, 2023
1 parent aa74fc2 commit 85f20cb
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 84 deletions.
4 changes: 2 additions & 2 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ android {
/*
* https://www.epochconverter.com/
*/
versionCode 1701031135
versionName "1.3.3"
versionCode 1701051529
versionName "1.4.0"

missingDimensionStrategy 'react-native-camera', 'general'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,14 @@ public void printQRCode(String text, int width, int height) {
public void printQRCodeCiontek(String text, int width, int height) {
try {
int result = posApiHelper.PrintInit();
result = posApiHelper.PrintQrCode_Cut(text, width, height, "QR_CODE");
result = posApiHelper.PrintBarcode(text, width, height, "QR_CODE");
result = posApiHelper.PrintStart();
} catch (Exception e) {
e.printStackTrace();
// callBack.invoke("Error: "+e.getMessage());
}
}


public void testPrint(Callback callBack) {
Log.d(TAG, "Test Print!");
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void paperOut(int pixels) {
MainActivity activity = (MainActivity) getCurrentActivity();
if(activity != null) activity.paperOut(pixels);
}

@ReactMethod
public void testPrint(
Callback callBack
Expand All @@ -52,6 +52,24 @@ public void testPrint(
if(activity != null) activity.testPrint(callBack);
}

@ReactMethod
public void printTextCiontek(String text, int size) {
MainActivity activity = (MainActivity) getCurrentActivity();
if(activity != null) activity.printTextCiontek(text, size);
}

@ReactMethod
public void printQRCodeCiontek(String text, int width, int height) {
MainActivity activity = (MainActivity) getCurrentActivity();
if(activity != null) activity.printQRCodeCiontek(text, width, height);
}

@ReactMethod
public void paperOutCiontek() {
MainActivity activity = (MainActivity) getCurrentActivity();
if(activity != null) activity.paperOutCiontek();
}

@ReactMethod
public void testPrintCiontek() {
MainActivity activity = (MainActivity) getCurrentActivity();
Expand Down
2 changes: 1 addition & 1 deletion contexts/ShopSettingsContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const ShopSettingsProvider = ({children}) => {
}
}
saveShopSettings();
}, [shopName, lndhub, lndhubUser]);
}, [shopName, lndhub, lndhubUser, printer]);

useEffect(() => {
async function initWallet() {
Expand Down
91 changes: 91 additions & 0 deletions helper/printing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import {NativeModules, Alert} from 'react-native';
import moment from 'moment';

export const printBitcoinize = async (
description,
timestamp,
ispaid,
payment_hash,
amt,
) => {
try {
await NativeModules.PrintModule.printText(description, 32);
await NativeModules.PrintModule.paperOut(24);

await NativeModules.PrintModule.printText('Payment made in Bitcoin', 24);
await NativeModules.PrintModule.paperOut(24);

await NativeModules.PrintModule.printText(
moment(timestamp * 1000).format('DD/MM/YY HH:mm:ss'),
24,
);
await NativeModules.PrintModule.paperOut(24);

await NativeModules.PrintModule.printText(
amt + ' sats ' + (ispaid ? '(PAID)' : '(PENDING)'),
32,
);
await NativeModules.PrintModule.paperOut(24);

await NativeModules.PrintModule.printText(payment_hash, 24);
await NativeModules.PrintModule.paperOut(24);
await NativeModules.PrintModule.printQRCode(
JSON.stringify({payment_hash: payment_hash}),
400,
400,
);

await NativeModules.PrintModule.paperOut(100);
} catch (e) {
Alert.alert('Error', 'There was an error when printing ' + e.message, [
{text: 'OK', onPress: () => console.log('OK Pressed')},
]);
}
};

export const printCiontek = async (
description,
timestamp,
ispaid,
payment_hash,
amt,
) => {
try {
await NativeModules.PrintModule.printTextCiontek(description, 32);
await NativeModules.PrintModule.paperOutCiontek();

await NativeModules.PrintModule.printTextCiontek(
'Payment made in Bitcoin',
24,
);
await NativeModules.PrintModule.paperOutCiontek();

await NativeModules.PrintModule.printTextCiontek(
moment(timestamp * 1000).format('DD/MM/YY HH:mm:ss'),
24,
);
await NativeModules.PrintModule.paperOutCiontek();

await NativeModules.PrintModule.printTextCiontek(
amt + ' sats ' + (ispaid ? '(PAID)' : '(PENDING)'),
32,
);
await NativeModules.PrintModule.paperOutCiontek();

await NativeModules.PrintModule.printTextCiontek(payment_hash, 24);
await NativeModules.PrintModule.paperOutCiontek();
await NativeModules.PrintModule.printQRCodeCiontek(
JSON.stringify({payment_hash: payment_hash}),
360,
360,
);

await NativeModules.PrintModule.paperOutCiontek();
await NativeModules.PrintModule.paperOutCiontek();
await NativeModules.PrintModule.paperOutCiontek();
} catch (e) {
Alert.alert('Error', 'There was an error when printing ' + e.message, [
{text: 'OK', onPress: () => console.log('OK Pressed')},
]);
}
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "boltcardpos",
"version": "1.2.2",
"version": "1.4.0",
"private": true,
"scripts": {
"android": "react-native run-android",
Expand Down
50 changes: 19 additions & 31 deletions screens/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import LottieView from 'lottie-react-native';
import RNHTMLtoPDF from 'react-native-html-to-pdf';
import FileViewer from 'react-native-file-viewer';
import moment from 'moment';
import {printCiontek, printBitcoinize} from '../helper/printing';

const currency = require('../helper/currency');

Expand Down Expand Up @@ -87,7 +88,8 @@ function Home({navigation}): React.FC<Props> {
const [lndWallet, setLndWallet] = useState<LightningCustodianWallet>();

//shop settings
const {shopName, lndhub, lndhubUser} = useContext(ShopSettingsContext);
const {shopName, lndhub, lndhubUser, printer} =
useContext(ShopSettingsContext);

//PIN
const [showPinModal, setShowPinModal] = useState(false);
Expand Down Expand Up @@ -675,38 +677,24 @@ function Home({navigation}): React.FC<Props> {
};

const onPrint = async invoice => {
try {
await NativeModules.PrintModule.printText(invoice.description, 32);
await NativeModules.PrintModule.paperOut(24);

await NativeModules.PrintModule.printText('Payment made in Bitcoin', 24);
await NativeModules.PrintModule.paperOut(24);

await NativeModules.PrintModule.printText(
moment(invoice.timestamp * 1000).format('DD/MM/YY HH:mm:ss'),
24,
);
await NativeModules.PrintModule.paperOut(24);

await NativeModules.PrintModule.printText(
invoice.amt + ' sats ' + (invoice.ispaid ? '(PAID)' : '(PENDING)'),
32,
if (printer == 'ciontek') {
console.log('printCiontek');
printCiontek(
invoice.description,
invoice.timestamp,
invoice.ispaid,
invoice.payment_hash,
invoice.amt,
);
await NativeModules.PrintModule.paperOut(24);

await NativeModules.PrintModule.printText(invoice.payment_hash, 24);
await NativeModules.PrintModule.paperOut(24);
await NativeModules.PrintModule.printQRCode(
JSON.stringify({payment_hash: invoice.payment_hash}),
400,
400,
} else {
console.log('printBitcoinize');
printBitcoinize(
invoice.description,
invoice.timestamp,
invoice.ispaid,
invoice.payment_hash,
invoice.amt,
);

await NativeModules.PrintModule.paperOut(100);
} catch (e) {
Alert.alert('Error', 'There was an error when printing ' + e.message, [
{text: 'OK', onPress: () => console.log('OK Pressed')},
]);
}
};

Expand Down
69 changes: 23 additions & 46 deletions screens/recentInvoices/InvoiceDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ import RNHTMLtoPDF from 'react-native-html-to-pdf';
import FileViewer from 'react-native-file-viewer';
import QRCode from 'react-native-qrcode-svg';
import Toast from 'react-native-toast-message';

import {printCiontek, printBitcoinize} from '../../helper/printing';
import {ShopSettingsContext} from '../../contexts/ShopSettingsContext';
import moment from 'moment';

const InvoiceDetail = ({route}) => {
const {navigate} = useNavigation();
const {shopName, lndhub, lndhubUser, printer} =
useContext(ShopSettingsContext);

const isDarkMode = useColorScheme() === 'dark';
const backgroundStyle = {
Expand Down Expand Up @@ -77,51 +80,25 @@ const InvoiceDetail = ({route}) => {
};

const print = async () => {
Alert.alert('Print Receipt', 'Are you sure?', [
{
text: 'Cancel',
onPress: () => console.log('Cancel Pressed'),
style: 'cancel',
},
{
text: 'OK',
onPress: async () => {
await NativeModules.PrintModule.printText(invoice.description, 32);
await NativeModules.PrintModule.paperOut(24);

await NativeModules.PrintModule.printText(
'Payment made in Bitcoin',
24,
);
await NativeModules.PrintModule.paperOut(24);

await NativeModules.PrintModule.printText(
formatDate(invoice.timestamp),
24,
);
await NativeModules.PrintModule.paperOut(24);

await NativeModules.PrintModule.printText(
invoice.amt + ' sats ' + (invoice.ispaid ? '(PAID)' : '(PENDING)'),
32,
);
await NativeModules.PrintModule.paperOut(24);

await NativeModules.PrintModule.printText(invoice.payment_hash, 24);
await NativeModules.PrintModule.paperOut(24);
await NativeModules.PrintModule.printQRCode(
JSON.stringify({payment_hash: invoice.payment_hash}),
400,
400,
result => {
console.log(result);
},
);

await NativeModules.PrintModule.paperOut(100);
},
},
]);
if (printer == 'ciontek') {
console.log('printCiontek');
printCiontek(
invoice.description,
invoice.timestamp,
invoice.ispaid,
invoice.payment_hash,
invoice.amt,
);
} else {
console.log('printBitcoinize');
printBitcoinize(
invoice.description,
invoice.timestamp,
invoice.ispaid,
invoice.payment_hash,
invoice.amt,
);
}
};

return (
Expand Down

0 comments on commit 85f20cb

Please sign in to comment.