@supernick135/face-scanner-client
Version:
Node.js client library for ZKTeco face scanning devices integration with comprehensive API support
199 lines (159 loc) • 5.02 kB
JavaScript
/**
* Device Client for Face Scanner API
* Specialized client for device management and commands
*/
const FaceScannerClient = require('./FaceScannerClient');
class DeviceClient extends FaceScannerClient {
constructor(options = {}) {
super(options);
this.deviceId = options.deviceId;
}
// Device-specific methods
async getStatus() {
if (!this.deviceId) {
throw new Error('Device ID not specified');
}
return this.getDeviceStatus(this.deviceId);
}
async sendDataQuery(table, stamp = '9999') {
if (!this.deviceId) {
throw new Error('Device ID not specified');
}
return this.sendCommand(this.deviceId, {
type: 'DATA_QUERY',
parameters: { table, stamp }
});
}
async sendAttLogQuery(stamp = '9999') {
return this.sendDataQuery('ATTLOG', stamp);
}
async sendAttPhotoQuery(stamp = '9999') {
return this.sendDataQuery('ATTPHOTO', stamp);
}
async sendUserQuery(stamp = '9999') {
return this.sendDataQuery('USER', stamp);
}
async sendBioPhotoQuery(stamp = '9999') {
return this.sendDataQuery('BIOPHOTO', stamp);
}
async sendDeviceInfo() {
if (!this.deviceId) {
throw new Error('Device ID not specified');
}
return this.sendCommand(this.deviceId, {
type: 'INFO'
});
}
async sendRestart() {
if (!this.deviceId) {
throw new Error('Device ID not specified');
}
return this.sendCommand(this.deviceId, {
type: 'RESTART'
});
}
async sendClearData() {
if (!this.deviceId) {
throw new Error('Device ID not specified');
}
return this.sendCommand(this.deviceId, {
type: 'CLEAR_DATA'
});
}
async sendOptions() {
if (!this.deviceId) {
throw new Error('Device ID not specified');
}
return this.sendCommand(this.deviceId, {
type: 'OPTIONS'
});
}
// Get command queue for this device
async getQueue() {
if (!this.deviceId) {
throw new Error('Device ID not specified');
}
return this.getCommandQueue(this.deviceId);
}
// Clear command queue for this device
async clearQueue() {
if (!this.deviceId) {
throw new Error('Device ID not specified');
}
return this.http.delete(`/devices/${this.deviceId}/commands`);
}
// Get device statistics
async getStats() {
if (!this.deviceId) {
throw new Error('Device ID not specified');
}
return this.http.get(`/devices/${this.deviceId}/stats`);
}
// Get device logs
async getLogs(options = {}) {
if (!this.deviceId) {
throw new Error('Device ID not specified');
}
const params = new URLSearchParams();
if (options.limit) params.append('limit', options.limit);
if (options.offset) params.append('offset', options.offset);
if (options.level) params.append('level', options.level);
return this.http.get(`/devices/${this.deviceId}/logs?${params}`);
}
// Bulk operations
async bulkDataQuery(tables = ['ATTLOG', 'ATTPHOTO'], stamp = '9999') {
const commands = tables.map(table => ({
type: 'DATA_QUERY',
parameters: { table, stamp }
}));
const results = await Promise.allSettled(
commands.map(command => this.sendCommand(this.deviceId, command))
);
return results.map((result, index) => ({
table: tables[index],
success: result.status === 'fulfilled',
data: result.status === 'fulfilled' ? result.value : null,
error: result.status === 'rejected' ? result.reason.message : null
}));
}
// Wait for command completion
async waitForCommand(commandId, timeout = 30000) {
if (!this.deviceId) {
throw new Error('Device ID not specified');
}
const startTime = Date.now();
return new Promise((resolve, reject) => {
const checkStatus = async () => {
try {
const status = await this.getCommandStatus(this.deviceId, commandId);
if (status.status === 'completed') {
resolve(status);
return;
}
if (status.status === 'failed') {
reject(new Error(`Command failed: ${status.error}`));
return;
}
if (Date.now() - startTime > timeout) {
reject(new Error('Command timeout'));
return;
}
// Check again in 1 second
setTimeout(checkStatus, 1000);
} catch (error) {
reject(error);
}
};
checkStatus();
});
}
// Set device ID
setDeviceId(deviceId) {
this.deviceId = deviceId;
}
// Get current device ID
getDeviceId() {
return this.deviceId;
}
}
module.exports = DeviceClient;