Skip to content

Commit

Permalink
feat: support multiple instances
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The open method now returns a printer instance, containing most of the previous methods.
  • Loading branch information
vicary committed Dec 31, 2024
1 parent 9df5c6a commit fd80605
Show file tree
Hide file tree
Showing 11 changed files with 2,316 additions and 1,715 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup
uses: ./.github/actions/setup
Expand All @@ -30,7 +30,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup
uses: ./.github/actions/setup
Expand All @@ -44,7 +44,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup
uses: ./.github/actions/setup
Expand Down
15 changes: 6 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,20 @@ of methods and their details, you may refer to the PDF manual inside the
```tsx
import {
connect,
cutPaper,
disconnect,
ESCPOSConst,
printText,
searchCitizenPrinters,
} from "react-native-citizen-escposprinter";

const main = async () => {
const printers = await searchCitizenPrinters(
const results = await searchCitizenPrinters(
ESCPOSConst.CMP_PORT_WiFi,
);
console.info("Found printers:", printers);
console.info("Found printers:", results);

await connect(printers[0].ipAddress);
await printText("Hello World!\n");
await cutPaper(ESCPOSConst.CMP_CUT_FULL_PREFEED);
await disconnect();
const printer = await connect(results[0].ipAddress);
await printer.printText("Hello World!\n");
await printer.cutPaper(ESCPOSConst.CMP_CUT_FULL_PREFEED);
await printer.disconnect();
};
```

Expand Down
37 changes: 15 additions & 22 deletions examples/testprint-app/app/(tabs)/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,79 +11,72 @@ import {
} from "react-native";
import {
connect,
cutPaper,
disconnect,
ESCPOSConst,
printerCheck,
printQRCode,
printText,
searchCitizenPrinter,
setEncoding,
status,
type CitizenPrinerInfo,
type CitizenPrinerWiFiInfo,
} from "react-native-citizen-escposprinter";

const testPrint = async (connectType: number, address?: string) => {
await connect(connectType, address);
const printer = await connect(connectType, address);
console.log("✅ connected:", address);
await printerCheck();
await printer.printerCheck();
console.log("✅ printerCheck");
const printerStatus = await status();
await setEncoding("UTF-8");
const printerStatus = await printer.status();
await printer.setEncoding("UTF-8");
console.log("✅ status", printerStatus);
await printText("Hello World!你好世界!\n");
await printText(
await printer.printText("Hello World!你好世界!\n");
await printer.printText(
"Hello World!你好世界!\n",
ESCPOSConst.CMP_ALIGNMENT_LEFT,
ESCPOSConst.CMP_FNT_DEFAULT,
ESCPOSConst.CMP_TXT_2WIDTH | ESCPOSConst.CMP_TXT_2HEIGHT,
);
await printText(
await printer.printText(
"Hello World!你好世界!\n",
ESCPOSConst.CMP_ALIGNMENT_LEFT,
ESCPOSConst.CMP_FNT_DEFAULT,
ESCPOSConst.CMP_TXT_3WIDTH | ESCPOSConst.CMP_TXT_3HEIGHT,
);
await printText(
await printer.printText(
"Hello World!你好世界!\n",
ESCPOSConst.CMP_ALIGNMENT_LEFT,
ESCPOSConst.CMP_FNT_DEFAULT,
ESCPOSConst.CMP_TXT_4WIDTH | ESCPOSConst.CMP_TXT_4HEIGHT,
);
await printText(
await printer.printText(
"Hello World!你好世界!\n",
ESCPOSConst.CMP_ALIGNMENT_LEFT,
ESCPOSConst.CMP_FNT_DEFAULT,
ESCPOSConst.CMP_TXT_5WIDTH | ESCPOSConst.CMP_TXT_5HEIGHT,
);
await printText(
await printer.printText(
"Hello World!你好世界!\n",
ESCPOSConst.CMP_ALIGNMENT_LEFT,
ESCPOSConst.CMP_FNT_DEFAULT,
ESCPOSConst.CMP_TXT_6WIDTH | ESCPOSConst.CMP_TXT_6HEIGHT,
);
await printText(
await printer.printText(
"Hello World!你好世界!\n",
ESCPOSConst.CMP_ALIGNMENT_LEFT,
ESCPOSConst.CMP_FNT_DEFAULT,
ESCPOSConst.CMP_TXT_7WIDTH | ESCPOSConst.CMP_TXT_7HEIGHT,
);
await printText(
await printer.printText(
"Hello World!你好世界!\n",
ESCPOSConst.CMP_ALIGNMENT_LEFT,
ESCPOSConst.CMP_FNT_DEFAULT,
ESCPOSConst.CMP_TXT_8WIDTH | ESCPOSConst.CMP_TXT_8HEIGHT,
);
await printQRCode(
await printer.printQRCode(
"https://www.google.com",
8,
ESCPOSConst.CMP_QRCODE_EC_LEVEL_H,
);
console.log("✅ printText");
await cutPaper(ESCPOSConst.CMP_CUT_FULL_PREFEED);
await printer.cutPaper(ESCPOSConst.CMP_CUT_FULL_PREFEED);
console.log("✅ cutPaper");
await disconnect();
await printer.disconnect();
console.log("✅ disconnect");
};

Expand Down
Loading

0 comments on commit fd80605

Please sign in to comment.