UNPKG

@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.

739 lines (516 loc) 10.4 kB
# Introduction 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. Devices and drivers communicate for data transmission, command reception, and status updates. Heartbeat monitoring is implemented for drivers to ensure continuous communication. # Table of Contents 1. [Printer](#printer-api) 1. [IDCardReader](#idcardreader-api) 1. [Fingerprint](#fingerprint-api) 1. [NFC](#nfc-api) # Printer API ### Installation ` npm install @cutos/core ` ` npm install @cutos/devices ` ### Import dependencies ```js import {CoreAPI} from '@cutos/core'; import {DevicePrinter, PrinterCMD} from '@cutos/devices'; ``` ### DevicePrinter Constructor, create a printer instance ```js let devPrinter = new DevicePrinter(); ``` ### DevicePrinter.init Printer initialization ```js devPrinter.init(callback) ``` callback: callback function ##### Example: ```js devPrinter.init((result, error) => { if (error) { console.log(error) return; } console.log(result) }) ``` - Return result example: ```json "Driver device-printer loaded" ``` ### DevicePrinter.readDeviceInfo Read printer information ```js DevicePrinter.readDeviceInfo(callback) ``` callback: callback function ##### Example: ```js DevicePrinter.readDeviceInfo(data => { console.log(data) }) ``` - Return result example: ```json { "name": "w80", "shareName": "w80", "portName": "USB001", "driverName": "MSW Printer Driver", "printProcessor": "winprint", "datatype": "RAW", "status": [], "statusNumber": 0, "attributes": [ "DO-COMPLETE-FIRST", "LOCAL" ], "priority": 1, "defaultPriority": 0, "averagePPM": 0 } ``` ### DevicePrinter.onData Get printer data ```js devPrinter.onData(callback) ``` * callback: callback function ##### Example: ```js DevicePrinter.onData(function (data) { let resp = data.response; if (!resp.status) { console.warn('err:', resp.msg); return; } switch (data.cmd) { case PrinterCMD.PRINT_TEST: //DevicePrinter.printTestPage(printer); break case PrinterCMD.PRINT_PDF_URL: //DevicePrinter.printPdfUrl(pdfUrl, printer); break } }); ``` ### DevicePrinter.printTestPage Print test page ```js devPrinter.printTestPage(printer); ``` * printer: printer name; when this parameter is not passed, the default printer is used for printing. ### DevicePrinter.printPdfUrl Print specified pdf file ```js devPrinter.printPdfUrl(pdfUrl, printer); ``` * pdfUrl: URL address. For example: 'https://oss.cut-os.com/resources/developer/examples/printer/print-sample.pdf' * printer: printer name; when this parameter is not passed, the default printer is used for printing. ### DevicePrinter.printDataUrl Print the specified base64 format data as a pdf file ```js devPrinter.printDataUrl(dataUrl, printer); ``` * dataUrl:The following format —— data:application/pdf;base64,<base64 encoded data>。For example:data:application/pdf;base64,JVBERi0xLjcK...VFT0YK * printer:Printer name; if this parameter is not passed, the default printer is used for printing. # IDCardReader API ### Installation ` npm install @cutos/core ` ` npm install @cutos/devices ` ### Import dependencies ```js import {CoreAPI} from '@cutos/core'; import {DeviceIDCardReader} from '@cutos/devices'; ``` ### DeviceIDCardReader Constructor, create ID card instance ```js var devIDCardReader = new DeviceIDCardReader(); ``` ### DeviceIDCardReader.init ID card reader initialization ```js devIDCardReader.init(callback); ``` * callback: callback function ##### Example: ```js devIDCardReader.init((result, error) => { if (!error) { console.log('onDeviceCreate', result) } else { console.log(error) } }); ``` - Return result example: ``` Driver device-id-card-reader loaded ``` ### DeviceIDCardReader.connect Connect ID card reader ```js devIDCardReader.connect(callback); ``` * callback: callback function ##### Example: ```js devIDCardReader.connect((result) => { if (result.status) { console.log('connect success:', result) } else { console.log('connect failed:', result.msg) } }); ``` ### DeviceIDCardReader.disconnect Disconnect ID card reader ```js devIDCardReader.disconnect(); ``` ### DeviceIDCardReader.startRead The card reader starts searching for the card ```js devIDCardReader.startRead([image], callback); ``` * image: optional parameter, whether to read the ID card photo, the default is false. true means read, false means not read. * callback: callback function ###### Example: ```js devIDCardReader.startRead(result => console.log(result)) ``` - Return result example: ```json { "status": true, "msg": "reading" } ``` ### DeviceIDCardReader.readDeviceInfo Read ID card reader device information ```js devIDCardReader.readDeviceInfo(callback); ``` * callback: callback function ###### Example: ```js devIDCardReader.readDeviceInfo(result => { console.log('device info:', result) }) ``` - Return result example: ```json { "status": true, "msg": { "SAMID": "5-3-20220810-11478877-3979136230" } } ``` ### DeviceIDCardReader.onData Receive ID card information ```js devIDCardReader.onData(callback) ``` * callback: callback function ##### Example: ```js devIDCardReader.onData((data) => { console.log('data', data) }) ``` - Return result example: ```json { "code": 110111201607101234, "name": "Si Pu", "sex": "Male", "birthday": 20160710, "address": "No. 1705, Beihuan Center, No. 18 Yumin Road, Xicheng District, Beijing", "nation": "China", "department": "Xicheng District, Beijing", "startDate": 20160710, "endDate": 20260710, "certType": "ID card" } ``` # Fingerprint API ### Installation ` npm install @cutos/core ` ` npm install @cutos/devices ` ### Import dependencies ```js import {CoreAPI} from '@cutos/core'; import {DeviceFingerprint} from '@cutos/devices'; ``` ### DeviceFingerprint Constructor, create fingerprint device instance ```js let devFingerprint = new DeviceFingerprint(name); ``` * name: fingerprint device name ##### Example: ```js devFingerprint = new DeviceFingerprint(); ``` ### DeviceFingerprint.init Fingerprint initialization ```js devFingerprint.init(callback); ``` * callback: callback function ##### Example: ```js devFingerprint.init((result, error) => { if (!error) { console.log('onDeviceCreate', result) } else { console.log(error) } }); ``` - Return result example: ``` Driver device-fingerprint loaded ``` ### DeviceFingerprint.connect Connect fingerprint ```js DevFingerprint.connect(path, callback); ``` * path: device port * callback: callback function ##### Example: ```js DevFingerprint.connect('/dev/ttyS1', (result) => { console.log(result) }); ``` - Return result example: ```json { "status": true, "msg": "open success" } ``` ### DeviceFingerprint.auth Fingerprint recognition mode ```js DevFingerprint.auth(callback) ``` * callback: callback function ##### Example: ```js DevFingerprint.auth((data) => { console.log('data', data) }) ``` - Return result example: ```json { "status": true, "msg": "auth mode" } ``` ### DeviceFingerprint.admin Fingerprint management mode ```js DevFingerprint.admin(callback) ``` * callback: callback function ##### Example: ```js DevFingerprint.admin((data) => { console.log(data) }) ``` - Return result example: ```json { "status": true, "msg": "admin mode" } ``` ### DeviceFingerprint.createUser1 Enter fingerprint step 1 ```js DevFingerprint.createUser1(userID, callback) ``` * userID: user ID * callback: callback function ##### Example: ```js DevFingerprint.createUser1(1, (data) => { console.log('data', data) }) ``` - Return result example: ```json { "status": true } ``` ### DeviceFingerprint.createUser2 Enter fingerprint step 2 ```js DevFingerprint.createUser2(userID, callback) ``` * userID: user ID * callback: Callback function ##### Example: ```js DevFingerprint.createUser2(1, (data) => { console.log('data', data) }) ``` - Return result example: ```json { "status": true } ``` ### DeviceFingerprint.createUser3 Enter fingerprint step 3 ```js DevFingerprint.createUser3(userID, callback) ``` * userID: user ID * callback: callback function ##### Example: ```js DevFingerprint.createUser3(1, (data) => { console.log('data', data) }) ``` - Return result example: ```json { "status": true } ``` ### DeviceFingerprint.deleteUser Delete user ```js DevFingerprint.deleteUser(userID, callback) ``` * userID: user ID * callback: callback function ##### Example: ```js DevFingerprint.deleteUser(1, (data) => { console.log('data', data) }) ``` - Return result example: ```json { "status": true } ``` ### DeviceFingerprint.onData Get fingerprint data information ```js DevFingerprint.onData(callback) ``` * callback: callback function ##### Example: ```js device.onData(data => { console.log(data) }) ``` - Return result example: ```json { "authorized": 0 } ``` # NFC API ### Installation ` npm install @cutos/core ` ` npm install @cutos/devices ` ### Import dependencies ```js import {CoreAPI} from '@cutos/core'; import {DeviceNFC} from '@cutos/devices'; ``` ### DeviceNFC Constructor, create NFC device instance ```js let devNFC = new DeviceNFC(name); ``` * name: NFC device name ##### Example: ```js devNFC = new DeviceNFC('demo-nfc'); ``` ### DeviceNFC.init NFC device initialization ```js devNFC.init(callback); ``` * callback: callback function ##### Example: ```js devNFC.init((result, error) => { if (!error) { console.log('onDeviceCreate', result) } else { console.log(error) } }); ``` ### DeviceNFC.connect Connect NFC ```js devNFC.connect(callback); ``` * callback: callback function ##### Example: ```js devNFC.connect((result) => { if (result.status) { console.log('connect success:', result) } else { console.log('connect failed:', result.msg) } }); ``` ### DeviceNFC.onData Receive NFC information ```js DeviceNFC.onData(callback) ``` * callback: callback function ##### Example: ```js DeviceNFC.onData((data) => { console.log('data', data) }) ``` - Return result example: ```json { "id": "110111201" } ```