@cutos/devices
Version:
CUTOS Devices API is a JavaScript library that provides a unified interface for accessing and controlling hardware devices. Developers can use this interface to send and receive data, configure device parameters, and handle device events.
204 lines (180 loc) • 5.99 kB
JavaScript
import { CoreClass, CoreDefine } from '@cutos/core';
const CMD$1 = {
PRINT_PDF_URL: 'print-pdf-url',
PRINT_DATA_URL: 'print-data-url',
PRINT_TEST: 'print-test',
READ_DEVICE_INFO: 'read-device-info',
};
class DevicePrinter extends CoreClass.Device {
constructor(name = "PRINTER") {
super(name, CoreDefine.DEVICE.PRINTER);
}
/**
* Print PDF files stored on the network
* @param pdfUrl https://xxx.xx/xxx/xxx.pdf
* @param printer printer name
* @param context Send with command and return with onData
*/
printPdfUrl(pdfUrl, printer, context) {
super.sendCommand({
context: context,
cmd: CMD$1.PRINT_PDF_URL,
args: {
dataUrl: pdfUrl,
printer: printer
}
});
}
/**
* Print PDF files encoded in data url format
* @param dataUrl data:application/pdf;base64,<base64 encoded data>
* @param printer printer name
* @param context Send with command and return with onData
*/
printDataUrl(dataUrl, printer, context) {
super.sendCommand({
context: context,
cmd: CMD$1.PRINT_DATA_URL,
args: {
dataUrl: dataUrl,
printer: printer
}
});
}
/**
* Print test page
* @param printer printer name
* @param context Send with command and return with onData
*/
printTestPage(printer, context) {
super.sendCommand({
context: context,
cmd: CMD$1.PRINT_TEST,
args: {
printer: printer
}
});
}
}
const CMD = {
ADMIN: 'admin',
AUTH: 'auth',
CONNECT: 'connect',
CREATE_USER_1: 'create_user_1',
CREATE_USER_2: 'create_user_2',
CREATE_USER_3: 'create_user_3',
DELETE_USER: 'delete_user',
DELETE_ALL: 'delete_all'
};
class DeviceFingerprint extends CoreClass.Device {
constructor(name) {
super(name, CoreDefine.DEVICE.FINGERPRINT);
}
/**
* Connect fingerprint
* @param {String} path
* @param {Function} callback - callback(result) , the result is normal make up of {status: true|false, msg: (''| {})}
*/
connect(path, callback) {
this.sendCommand({
cmd: CMD.CONNECT, args: {path}
}, callback);
}
/**
* Fingerprint management mode
* @param {Function} callback - callback(data) , the data is normal make up of {status: true|false, msg: 'admin mode'}
*/
admin(callback) {
this.sendCommand({cmd: CMD.ADMIN}, callback);
}
/**
* Fingerprint recognition mode
* @param {Function} callback - callback(data) , the data is normal make up of {status: true|false, msg: 'auth mode'}
*/
auth(callback) {
this.sendCommand({cmd: CMD.AUTH}, callback);
}
/**
* Enter fingerprint step 1~3
* @param {Number} userID
* @param {Function} callback - callback(data) , the data is normal make up of {status: true|false}
*/
createUser1(userID, callback) {
this.sendCommand({cmd: CMD.CREATE_USER_1, args: {userID}}, callback);
}
createUser2(userID, callback) {
this.sendCommand({cmd: CMD.CREATE_USER_2, args: {userID}}, callback);
}
createUser3(userID, callback) {
this.sendCommand({cmd: CMD.CREATE_USER_3, args: {userID}}, callback);
}
/**
* Delete user
* @param {Number} userID
* @param {Function} callback - callback(data) , the data is normal make up of {status: true|false}
*/
deleteUser(userID, callback) {
this.sendCommand({cmd: CMD.DELETE_USER, args: {userID}}, callback);
}
deleteAll(callback) {
this.sendCommand({cmd: CMD.DELETE_ALL}, callback);
}
}
class DeviceNFC extends CoreClass.Device {
/**
* The constructor of NFC Device
* @param {string} name - device name
*/
constructor(name = "NFC") {
super(name, CoreDefine.DEVICE.NFC);
}
/**
* Connect NFC
* @param {String} path
* @param {Function} callback - callback(result) , the result is normal make up of {status: true|false, msg: (''| {})}
*/
connect(path, callback) {
this.sendCommand({cmd: "connect", args: {path: path}}, callback);
}
}
class DeviceIDCardReader extends CoreClass.Device {
/**
* The constructor of ID-Card-Reader Device
* @param {String} name - device name
*/
constructor(name = "ID-CARD-READER") {
super(name, CoreDefine.DEVICE.ID_CARD_READER);
}
/**
* Connect ID-Card-Reader Device
* @param {Function} callback - callback(result) , the result is normal make up of {status: true|false, msg: (''| {})}
*/
connect(callback) {
super.sendCommand({cmd: "connect", args: ""}, callback);
}
/**
* disconnect ID-Card-Reader Device
* @param {Function} callback - callback(result) , the result is normal make up of {status: true|false, msg: (''| {})}
*/
disconnect(callback) {
super.sendCommand({cmd: "disconnect", args: ""}, callback);
}
/**
* Start waiting for the user to swipe their ID card
* @param {Boolean?} image
* @param callback
*/
startRead(image, callback) {
if (typeof image === "function") {
callback = image;
image = false;
}
super.sendCommand({cmd: "start-read", args: {image}}, callback);
}
}
class DeviceQrCodeScanner extends CoreClass.Device {
constructor(name = "QR-CODE-SCANNER") {
super(name, CoreDefine.DEVICE.QR_CODE_SCANNER);
}
}
export { DeviceFingerprint, DeviceIDCardReader, DeviceNFC, DevicePrinter, DeviceQrCodeScanner, CMD as FingerprintCMD, CMD$1 as PrinterCMD };