63 lines
1.8 KiB
TypeScript
63 lines
1.8 KiB
TypeScript
import * as SecureStore from 'expo-secure-store';
|
|
import {myTcpSocket} from "../connectors/myTcpSocket";
|
|
|
|
class PrintingService {
|
|
private static instance: PrintingService | null = null;
|
|
private readonly apiUrl: string;
|
|
private readonly port: number;
|
|
|
|
public static getInstance(port?: number): PrintingService {
|
|
if (!this.instance) {
|
|
this.instance = new PrintingService(port);
|
|
}
|
|
return this.instance;
|
|
}
|
|
|
|
private constructor(port = 2282) {
|
|
this.apiUrl = "127.0.0.1";
|
|
this.port = port;
|
|
}
|
|
|
|
private async printLabelAsync(pdf: { labels: string[] }): Promise<void> {
|
|
const printerIp = await SecureStore.getItemAsync("printerIp");
|
|
if (!printerIp) {
|
|
throw new Error("Printer IP not found in secure store");
|
|
}
|
|
// Fetch the PDF, convert to bitmaps, generate ZPL codes
|
|
// const images = await convertPdfToBitmaps(pdf);
|
|
// const codes = await this.generateZplCodes(images);
|
|
const codes = pdf.labels;
|
|
try {
|
|
const result = await myTcpSocket.writeToSocket(printerIp, 9100, codes);
|
|
} catch (error) {
|
|
console.error("Error printing label:", error);
|
|
await Promise.reject(error)
|
|
}
|
|
await Promise.resolve()
|
|
|
|
}
|
|
|
|
|
|
private async print(printer: string, type: string, bytes: { labels: string[] }): Promise<boolean> {
|
|
try {
|
|
|
|
await this.printLabelAsync(bytes);
|
|
return true;
|
|
} catch (error) {
|
|
console.log(error);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public async printPdf(printer: string, pdfBytes: { labels: string[] }): Promise<boolean> {
|
|
|
|
return this.print(printer, "pdf", pdfBytes);
|
|
}
|
|
|
|
|
|
public getPrinter(baseMarketplace: number): string {
|
|
return "wildberries";
|
|
}
|
|
}
|
|
|
|
export default PrintingService; |