react-native-thermal-pos-printer
Version:
React Native thermal printer package for POS systems supporting Xprinter and other popular brands
205 lines (204 loc) • 7.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ReactNativePosPrinter = void 0;
var _native = require("../utils/native.js");
var _errors = require("../types/errors.js");
var _commands = require("../constants/commands.js");
var _ThermalPrinterDevice = require("./ThermalPrinterDevice.js");
class ReactNativePosPrinter {
static currentDevice = null;
static async ensureConnected() {
const connected = await this.isConnected();
if (!connected) {
throw new _errors.PrinterError(_errors.PrinterErrorCode.NOT_CONNECTED, 'No printer connected. Please connect a printer first.');
}
}
static async init(options) {
try {
if (!_native.PosPrinter) {
throw new Error('PosPrinter native module is not available');
}
await _native.PosPrinter.init(options);
} catch (error) {
throw new _errors.PrinterError(_errors.PrinterErrorCode.INIT_FAILED, `Failed to initialize printer: ${error instanceof Error ? error.message : 'Unknown error'}`, error);
}
}
static async getDeviceList() {
try {
if (!_native.PosPrinter) {
throw new Error('PosPrinter native module is not available');
}
const devices = await _native.PosPrinter.getDeviceList();
return devices.map(device => new _ThermalPrinterDevice.ThermalPrinterDevice({
name: device.name || 'Unknown Device',
address: device.address,
id: device.address,
type: device.type?.toUpperCase() || 'BLUETOOTH',
connected: device.connected || false,
rssi: device.rssi,
batteryLevel: device.batteryLevel,
bondState: device.bondState,
deviceClass: device.deviceClass,
extra: device.extra
}));
} catch (error) {
throw new _errors.PrinterError(_errors.PrinterErrorCode.DEVICE_LIST_FAILED, `Failed to get device list: ${error instanceof Error ? error.message : 'Unknown error'}`, error);
}
}
static async connectPrinter(address, options) {
try {
const deviceType = options?.type || 'BLUETOOTH';
await _native.PosPrinter.connectPrinter(address, deviceType);
const devices = await _native.PosPrinter.getDeviceList();
const device = devices.find(d => d.address === address);
if (!device) {
throw new _errors.PrinterError(_errors.PrinterErrorCode.DEVICE_NOT_FOUND, `Device with address ${address} not found`);
}
this.currentDevice = new _ThermalPrinterDevice.ThermalPrinterDevice(device);
return this.currentDevice;
} catch (error) {
if (error instanceof _errors.PrinterError) {
throw error;
}
throw new _errors.PrinterError(_errors.PrinterErrorCode.CONNECTION_FAILED, `Failed to connect to printer ${address}`, error);
}
}
static async disconnectPrinter() {
try {
await _native.PosPrinter.disconnectPrinter();
this.currentDevice = null;
} catch (error) {
throw new _errors.PrinterError(_errors.PrinterErrorCode.DISCONNECTION_FAILED, 'Failed to disconnect printer', error);
}
}
static async isConnected() {
try {
return await _native.PosPrinter.isConnected();
} catch (error) {
return false;
}
}
static async sendRawCommand(command) {
try {
await _native.PosPrinter.sendRawCommand(command);
} catch (error) {
throw new _errors.PrinterError(_errors.PrinterErrorCode.PRINT_FAILED, 'Failed to send raw command', error);
}
}
static async initializePrinter() {
await this.sendRawCommand([..._commands.ESCPOSCommands.INIT]);
}
static async setAlignment(alignment) {
const commands = {
LEFT: _commands.ESCPOSCommands.ALIGN_LEFT,
CENTER: _commands.ESCPOSCommands.ALIGN_CENTER,
RIGHT: _commands.ESCPOSCommands.ALIGN_RIGHT
};
await this.sendRawCommand([...commands[alignment]]);
}
static async setBold(enabled) {
const command = enabled ? _commands.ESCPOSCommands.BOLD_ON : _commands.ESCPOSCommands.BOLD_OFF;
await this.sendRawCommand([...command]);
}
static async setUnderline(enabled) {
const command = enabled ? _commands.ESCPOSCommands.UNDERLINE_ON : _commands.ESCPOSCommands.UNDERLINE_OFF;
await this.sendRawCommand([...command]);
}
static async feedLine() {
await this.sendRawCommand([..._commands.ESCPOSCommands.LINE_FEED]);
}
static async formFeed() {
await this.sendRawCommand([..._commands.ESCPOSCommands.FORM_FEED]);
}
static async newLine(lines = 1) {
await this.ensureConnected();
if (lines < 1) {
throw new _errors.PrinterError(_errors.PrinterErrorCode.INVALID_PARAMETER, 'Number of lines must be at least 1');
}
if (lines > 10) {
throw new _errors.PrinterError(_errors.PrinterErrorCode.INVALID_PARAMETER, 'Number of lines cannot exceed 10 for performance reasons');
}
try {
for (let i = 0; i < lines; i++) {
await this.feedLine();
}
} catch (error) {
throw new _errors.PrinterError(_errors.PrinterErrorCode.PRINT_FAILED, `Failed to add new lines (${lines})`, error);
}
}
static async printText(text, options) {
await this.ensureConnected();
if (!text || text.trim().length === 0) {
throw new _errors.PrinterError(_errors.PrinterErrorCode.INVALID_TEXT, 'Text cannot be empty');
}
try {
await _native.PosPrinter.printText(text, options);
} catch (error) {
throw new _errors.PrinterError(_errors.PrinterErrorCode.PRINT_FAILED, 'Failed to print text', error);
}
}
static async printImage(imageUri, options) {
await this.ensureConnected();
if (!imageUri) {
throw new _errors.PrinterError(_errors.PrinterErrorCode.INVALID_PARAMETER, 'Image URI cannot be empty');
}
try {
const imageData = imageUri.startsWith('data:image') ? imageUri.split(',')[1] : imageUri;
await _native.PosPrinter.printImage(imageData, options);
} catch (error) {
throw new _errors.PrinterError(_errors.PrinterErrorCode.PRINT_FAILED, 'Failed to print image', error);
}
}
static async cutPaper() {
await this.ensureConnected();
try {
await this.sendRawCommand([..._commands.ESCPOSCommands.CUT_PAPER]);
} catch (error) {
throw new _errors.PrinterError(_errors.PrinterErrorCode.PRINT_FAILED, 'Failed to cut paper', error);
}
}
static async printBarcode(data, type, options) {
if (!data) {
throw new _errors.PrinterError(_errors.PrinterErrorCode.INVALID_PARAMETER, 'Barcode data cannot be empty');
}
try {
await _native.PosPrinter.printBarcode(data, type, options);
} catch (error) {
throw new _errors.PrinterError(_errors.PrinterErrorCode.PRINT_FAILED, 'Failed to print barcode', error);
}
}
static async printQRCode(data, options) {
if (!data) {
throw new _errors.PrinterError(_errors.PrinterErrorCode.INVALID_PARAMETER, 'QR code data cannot be empty');
}
try {
await _native.PosPrinter.printQRCode(data, options);
} catch (error) {
throw new _errors.PrinterError(_errors.PrinterErrorCode.PRINT_FAILED, 'Failed to print QR code', error);
}
}
static async openCashDrawer() {
try {
await _native.PosPrinter.openCashDrawer();
} catch (error) {
throw new _errors.PrinterError(_errors.PrinterErrorCode.HARDWARE_ERROR, 'Failed to open cash drawer', error);
}
}
static async printRaw(data) {
if (!data || data.length === 0) {
throw new _errors.PrinterError(_errors.PrinterErrorCode.INVALID_PARAMETER, 'Raw data cannot be empty');
}
try {
await this.sendRawCommand(data);
} catch (error) {
throw new _errors.PrinterError(_errors.PrinterErrorCode.PRINT_FAILED, 'Failed to print raw data', error);
}
}
static getCurrentDevice() {
return this.currentDevice;
}
}
exports.ReactNativePosPrinter = ReactNativePosPrinter;
//# sourceMappingURL=ReactNativePosPrinter.js.map