react-native-thermal-pos-printer
Version:
React Native thermal printer package for POS systems supporting Xprinter and other popular brands
215 lines (214 loc) • 7.83 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ThermalPrinterDevice = void 0;
var _native = require("../utils/native.js");
var _errors = require("../types/errors.js");
var _events = require("../types/events.js");
var _commands = require("../constants/commands.js");
var _PrinterEventManager = require("./PrinterEventManager.js");
class ThermalPrinterDevice {
connected = false;
constructor(device) {
this.device = device;
this.connected = device.connected || false;
}
async connect(options) {
try {
const deviceType = options?.type || this.device.type;
await _native.PosPrinter.connectPrinter(this.device.address, deviceType);
this.connected = true;
this.device.connected = true;
_PrinterEventManager.PrinterEventManager.emit(_events.PrinterEvent.DEVICE_CONNECTED, {
device: this.device
});
} catch (error) {
const printerError = new _errors.PrinterError(_errors.PrinterErrorCode.CONNECTION_FAILED, `Failed to connect to device ${this.device.name}`, error);
_PrinterEventManager.PrinterEventManager.emit(_events.PrinterEvent.PRINT_FAILED, {
error: printerError
});
throw printerError;
}
}
async disconnect() {
try {
await _native.PosPrinter.disconnectPrinter();
this.connected = false;
this.device.connected = false;
_PrinterEventManager.PrinterEventManager.emit(_events.PrinterEvent.DEVICE_DISCONNECTED, {
device: this.device
});
} catch (error) {
const printerError = new _errors.PrinterError(_errors.PrinterErrorCode.DISCONNECTION_FAILED, `Failed to disconnect from device ${this.device.name}`, error);
_PrinterEventManager.PrinterEventManager.emit(_events.PrinterEvent.PRINT_FAILED, {
error: printerError
});
throw printerError;
}
}
async checkConnectionStatus() {
try {
if (!this.device?.address) {
return false;
}
const isConnected = await _native.PosPrinter.isConnected();
this.connected = isConnected;
this.device.connected = isConnected;
return isConnected;
} catch (error) {
console.warn('Failed to check connection status:', error);
this.connected = false;
this.device.connected = false;
return false;
}
}
async getStatus() {
try {
const rawStatus = await _native.PosPrinter.getStatus();
const status = {
online: rawStatus.online ?? true,
paperOut: rawStatus.paperOut ?? false,
coverOpen: rawStatus.coverOpen ?? false,
cutterError: rawStatus.cutterError ?? false,
temperature: rawStatus.temperature ?? 'NORMAL',
voltage: rawStatus.voltage ?? 'NORMAL'
};
return status;
} catch (error) {
throw new _errors.PrinterError(_errors.PrinterErrorCode.STATUS_CHECK_FAILED, 'Failed to get printer status', error);
}
}
async sendRawCommand(command) {
const connected = await this.checkConnectionStatus();
if (!connected) {
throw new _errors.PrinterError(_errors.PrinterErrorCode.NOT_CONNECTED, 'Device not connected');
}
try {
await _native.PosPrinter.sendRawCommand(command);
} catch (error) {
throw new _errors.PrinterError(_errors.PrinterErrorCode.PRINT_FAILED, 'Failed to send raw command', error);
}
}
async printText(text, options) {
if (!this.connected) {
throw new _errors.PrinterError(_errors.PrinterErrorCode.NOT_CONNECTED, 'Device not connected');
}
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);
_PrinterEventManager.PrinterEventManager.emit(_events.PrinterEvent.PRINT_COMPLETED, {
device: this.device,
data: text
});
} catch (error) {
const printerError = new _errors.PrinterError(_errors.PrinterErrorCode.PRINT_FAILED, 'Failed to print text', error);
_PrinterEventManager.PrinterEventManager.emit(_events.PrinterEvent.PRINT_FAILED, {
error: printerError
});
throw printerError;
}
}
async printImage(imageUri, options) {
if (!this.connected) {
throw new _errors.PrinterError(_errors.PrinterErrorCode.NOT_CONNECTED, 'Device not connected');
}
if (!imageUri) {
throw new _errors.PrinterError(_errors.PrinterErrorCode.INVALID_PARAMETER, 'Image URI cannot be empty');
}
try {
await _native.PosPrinter.printImage(imageUri, options);
_PrinterEventManager.PrinterEventManager.emit(_events.PrinterEvent.PRINT_COMPLETED, {
device: this.device,
data: imageUri
});
} catch (error) {
const printerError = new _errors.PrinterError(_errors.PrinterErrorCode.PRINT_FAILED, 'Failed to print image', error);
_PrinterEventManager.PrinterEventManager.emit(_events.PrinterEvent.PRINT_FAILED, {
error: printerError
});
throw printerError;
}
}
async printBarcode(data, type, options) {
if (!this.connected) {
throw new _errors.PrinterError(_errors.PrinterErrorCode.NOT_CONNECTED, 'Device not connected');
}
if (!data) {
throw new _errors.PrinterError(_errors.PrinterErrorCode.INVALID_PARAMETER, 'Barcode data cannot be empty');
}
try {
await _native.PosPrinter.printBarcode(data, type, options);
_PrinterEventManager.PrinterEventManager.emit(_events.PrinterEvent.PRINT_COMPLETED, {
device: this.device,
data
});
} catch (error) {
const printerError = new _errors.PrinterError(_errors.PrinterErrorCode.PRINT_FAILED, 'Failed to print barcode', error);
_PrinterEventManager.PrinterEventManager.emit(_events.PrinterEvent.PRINT_FAILED, {
error: printerError
});
throw printerError;
}
}
async printQRCode(data, options) {
if (!this.connected) {
throw new _errors.PrinterError(_errors.PrinterErrorCode.NOT_CONNECTED, 'Device not connected');
}
if (!data) {
throw new _errors.PrinterError(_errors.PrinterErrorCode.INVALID_PARAMETER, 'QR code data cannot be empty');
}
try {
await _native.PosPrinter.printQRCode(data, options);
_PrinterEventManager.PrinterEventManager.emit(_events.PrinterEvent.PRINT_COMPLETED, {
device: this.device,
data
});
} catch (error) {
const printerError = new _errors.PrinterError(_errors.PrinterErrorCode.PRINT_FAILED, 'Failed to print QR code', error);
_PrinterEventManager.PrinterEventManager.emit(_events.PrinterEvent.PRINT_FAILED, {
error: printerError
});
throw printerError;
}
}
async cutPaper() {
if (!this.connected) {
throw new _errors.PrinterError(_errors.PrinterErrorCode.NOT_CONNECTED, 'Device not connected');
}
try {
await this.sendRawCommand([..._commands.ESCPOSCommands.CUT_PAPER]);
} catch (error) {
throw new _errors.PrinterError(_errors.PrinterErrorCode.PRINT_FAILED, 'Failed to cut paper', error);
}
}
async openCashDrawer() {
if (!this.connected) {
throw new _errors.PrinterError(_errors.PrinterErrorCode.NOT_CONNECTED, 'Device not connected');
}
try {
await _native.PosPrinter.openCashDrawer();
} catch (error) {
throw new _errors.PrinterError(_errors.PrinterErrorCode.HARDWARE_ERROR, 'Failed to open cash drawer', error);
}
}
isConnected() {
return this.connected;
}
getDevice() {
return this.device;
}
getName() {
return this.device.name;
}
getAddress() {
return this.device.address;
}
getType() {
return this.device.type;
}
}
exports.ThermalPrinterDevice = ThermalPrinterDevice;
//# sourceMappingURL=ThermalPrinterDevice.js.map