sunmi-device-sdk
Version:
JavaScript SDK for Sunmi card readers and printers
217 lines (216 loc) • 7.25 kB
JavaScript
;
/**
* Sunmi Printer API
* Provides methods to interact with Sunmi thermal printers
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.SunmiPrinter = void 0;
const bridge_1 = require("./bridge");
class SunmiPrinter {
/**
* Initialize the printer
* @returns Promise resolving when printer is initialized
*/
static async init() {
const bridge = (0, bridge_1.getNativeBridge)();
return bridge.execute('Printer', 'printerInit', []);
}
/**
* Perform printer self-check
* @returns Promise resolving with self-check result
*/
static async selfCheck() {
const bridge = (0, bridge_1.getNativeBridge)();
return bridge.execute('Printer', 'printerSelfChecking', []);
}
/**
* Get printer serial number
* @returns Promise resolving to serial number
*/
static async getSerialNumber() {
const bridge = (0, bridge_1.getNativeBridge)();
return bridge.execute('Printer', 'getPrinterSerialNo', []);
}
/**
* Get printer firmware version
* @returns Promise resolving to firmware version
*/
static async getVersion() {
const bridge = (0, bridge_1.getNativeBridge)();
return bridge.execute('Printer', 'getPrinterVersion', []);
}
/**
* Check if printer is available
* @returns Promise resolving to true if printer exists
*/
static async hasPrinter() {
const bridge = (0, bridge_1.getNativeBridge)();
return bridge.execute('Printer', 'hasPrinter', []);
}
/**
* Get printed paper length
* @returns Promise resolving to length in pixels
*/
static async getPrintedLength() {
const bridge = (0, bridge_1.getNativeBridge)();
return bridge.execute('Printer', 'getPrintedLength', []);
}
/**
* Feed paper (line wrap)
* @param count Number of lines to feed
* @returns Promise resolving when operation completes
*/
static async feedPaper(count) {
const bridge = (0, bridge_1.getNativeBridge)();
return bridge.execute('Printer', 'lineWrap', [count]);
}
/**
* Send raw ESC/POS commands
* @param base64Data Base64-encoded raw data
* @returns Promise resolving when data is sent
*/
static async sendRawData(base64Data) {
const bridge = (0, bridge_1.getNativeBridge)();
return bridge.execute('Printer', 'sendRAWData', [base64Data]);
}
/**
* Set text alignment
* @param alignment Alignment option (LEFT, CENTER, RIGHT)
* @returns Promise resolving when alignment is set
*/
static async setAlignment(alignment) {
const bridge = (0, bridge_1.getNativeBridge)();
return bridge.execute('Printer', 'setAlignment', [alignment]);
}
/**
* Set font typeface
* @param typeface Font name
* @returns Promise resolving when font is set
*/
static async setFontName(typeface) {
const bridge = (0, bridge_1.getNativeBridge)();
return bridge.execute('Printer', 'setFontName', [typeface]);
}
/**
* Set font size
* @param fontSize Font size in points
* @returns Promise resolving when font size is set
*/
static async setFontSize(fontSize) {
const bridge = (0, bridge_1.getNativeBridge)();
return bridge.execute('Printer', 'setFontSize', [fontSize]);
}
/**
* Print text with specific font
* @param text Text to print
* @param typeface Font name
* @param fontSize Font size
* @returns Promise resolving when text is printed
*/
static async printTextWithFont(text, typeface, fontSize) {
const bridge = (0, bridge_1.getNativeBridge)();
return bridge.execute('Printer', 'printTextWithFont', [
text,
typeface,
fontSize
]);
}
/**
* Print text in columns
* @param columns Array of column text
* @param widths Array of column widths
* @param alignments Array of column alignments
* @returns Promise resolving when columns are printed
*/
static async printColumns(columns, widths, alignments) {
const bridge = (0, bridge_1.getNativeBridge)();
return bridge.execute('Printer', 'printColumnsText', [
columns,
widths,
alignments
]);
}
/**
* Print a bitmap image
* @param base64Data Base64-encoded image data
* @param width Image width in pixels
* @param height Image height in pixels
* @returns Promise resolving when image is printed
*/
static async printBitmap(base64Data, width, height) {
const bridge = (0, bridge_1.getNativeBridge)();
return bridge.execute('Printer', 'printBitmap', [
base64Data,
width,
height
]);
}
/**
* Print a barcode
* @param data Barcode data
* @param symbology Barcode type (e.g., 'CODE128', 'QR', 'EAN13')
* @param width Width in pixels
* @param height Height in pixels
* @param textPosition Text position relative to barcode
* @returns Promise resolving when barcode is printed
*/
static async printBarcode(data, symbology, width, height, textPosition) {
const bridge = (0, bridge_1.getNativeBridge)();
return bridge.execute('Printer', 'printBarCode', [
data,
symbology,
width,
height,
textPosition
]);
}
/**
* Print a QR code
* @param data QR code data
* @param moduleSize Size of each QR module (1-16)
* @param errorLevel Error correction level (0-3)
* @returns Promise resolving when QR code is printed
*/
static async printQRCode(data, moduleSize, errorLevel) {
const bridge = (0, bridge_1.getNativeBridge)();
return bridge.execute('Printer', 'printQRCode', [
data,
moduleSize,
errorLevel
]);
}
/**
* Print text without encoding
* @param text Text to print
* @returns Promise resolving when text is printed
*/
static async printOriginalText(text) {
const bridge = (0, bridge_1.getNativeBridge)();
return bridge.execute('Printer', 'printOriginalText', [text]);
}
/**
* Print text with current settings
* @param text Text to print
* @returns Promise resolving when text is printed
*/
static async printText(text) {
const bridge = (0, bridge_1.getNativeBridge)();
return bridge.execute('Printer', 'printString', [text]);
}
/**
* Start listening to printer status changes
* @param callback Callback function receiving status updates
*/
static startStatusListener(callback) {
const bridge = (0, bridge_1.getNativeBridge)();
bridge.executeWithCallback('Printer', 'printerStatusStartListener', [], callback);
}
/**
* Stop listening to printer status changes
*/
static stopStatusListener() {
const bridge = (0, bridge_1.getNativeBridge)();
bridge.execute('Printer', 'printerStatusStopListener', []);
}
}
exports.SunmiPrinter = SunmiPrinter;