UNPKG

@supernick135/face-scanner-client

Version:

Node.js client library for ZKTeco face scanning devices integration with comprehensive API support

239 lines (198 loc) 6.68 kB
/** * Face Scanner API Client * Main client class for interacting with Face Scanner API */ const axios = require('axios'); const EventEmitter = require('events'); class FaceScannerClient extends EventEmitter { constructor(options = {}) { super(); // Configuration this.config = { baseURL: options.baseURL || options.apiUrl || 'http://localhost:9999', apiKey: options.apiKey || options.auth?.apiKey, timeout: options.timeout || 30000, retries: options.retries || 3, ...options }; // HTTP client this.http = axios.create({ baseURL: `${this.config.baseURL}/api`, timeout: this.config.timeout, headers: { 'Content-Type': 'application/json', ...(this.config.apiKey && { 'X-API-Key': this.config.apiKey }) } }); // Setup interceptors this.setupInterceptors(); } setupInterceptors() { // Request interceptor this.http.interceptors.request.use( (config) => { this.emit('request', { url: config.url, method: config.method }); return config; }, (error) => { this.emit('error', error); return Promise.reject(error); } ); // Response interceptor this.http.interceptors.response.use( (response) => { this.emit('response', { status: response.status, data: response.data }); return response.data; }, (error) => { this.emit('error', error); return Promise.reject(error); } ); } // System Methods async getSystemHealth() { return this.http.get('/system/health'); } async getSystemStats() { return this.http.get('/system/stats'); } // Device Methods async getDevices() { return this.http.get('/devices'); } async getDevice(deviceId) { return this.http.get(`/devices/${deviceId}`); } async getDeviceStatus(deviceId) { return this.http.get(`/devices/${deviceId}/status`); } // Scanner Methods async getScannerStatus() { return this.http.get('/scanner/status'); } async getScans(options = {}) { const params = new URLSearchParams(); if (options.limit) params.append('limit', options.limit); if (options.offset) params.append('offset', options.offset); if (options.startDate) params.append('startDate', options.startDate); if (options.endDate) params.append('endDate', options.endDate); if (options.deviceId) params.append('deviceId', options.deviceId); return this.http.get(`/scanner/scans?${params}`); } async getScan(scanId) { return this.http.get(`/scanner/scans/${scanId}`); } // Command Methods async sendCommand(deviceId, command) { return this.http.post(`/devices/${deviceId}/commands`, command); } async getCommandStatus(deviceId, commandId) { return this.http.get(`/devices/${deviceId}/commands/${commandId}`); } async getCommandQueue(deviceId) { return this.http.get(`/commands/status/${deviceId}`); } // Image Methods async getImages(options = {}) { const params = new URLSearchParams(); if (options.limit) params.append('limit', options.limit); if (options.offset) params.append('offset', options.offset); if (options.sortBy) params.append('sortBy', options.sortBy); return this.http.get(`/images?${params}`); } async getImageInfo(imageName) { return this.http.get(`/images/${imageName}/info`); } async downloadImage(deviceId, imageName) { const response = await axios.get( `${this.config.baseURL}/images/${deviceId}/${imageName}`, { responseType: 'arraybuffer', headers: this.config.apiKey ? { 'X-API-Key': this.config.apiKey } : {} } ); return response.data; } // Recovery Methods async startRecovery(options = {}) { return this.http.post('/recovery/start', { recoveryType: options.type || 'full', clearCorrupted: options.clearCorrupted || false, ...options }); } async getRecoveryStatus() { return this.http.get('/recovery/status'); } async stopRecovery() { return this.http.post('/recovery/stop'); } async clearCorrupted() { return this.http.post('/recovery/clear-corrupted'); } // Queue Methods async getQueueStats() { return this.http.get('/queue/stats'); } async clearAllQueues() { return this.http.post('/queue/clear-all'); } // Biometric Methods async getBiometricUsers(options = {}) { const params = new URLSearchParams(); if (options.limit) params.append('limit', options.limit); if (options.offset) params.append('offset', options.offset); return this.http.get(`/biometric/users?${params}`); } async getBiometricUser(userId) { return this.http.get(`/biometric/users/${userId}`); } async syncBiometricData(deviceId) { return this.http.post(`/biometric/sync/${deviceId}`); } // Historical Data Methods async getHistoricalScans(options = {}) { const params = new URLSearchParams(); if (options.startDate) params.append('startDate', options.startDate); if (options.endDate) params.append('endDate', options.endDate); if (options.limit) params.append('limit', options.limit); if (options.offset) params.append('offset', options.offset); return this.http.get(`/historical/scans?${params}`); } async getHistoricalPhotos(options = {}) { const params = new URLSearchParams(); if (options.startDate) params.append('startDate', options.startDate); if (options.endDate) params.append('endDate', options.endDate); if (options.limit) params.append('limit', options.limit); if (options.offset) params.append('offset', options.offset); return this.http.get(`/historical/photos?${params}`); } // Utility Methods setApiKey(apiKey) { this.config.apiKey = apiKey; this.http.defaults.headers['X-API-Key'] = apiKey; } setTimeout(timeout) { this.config.timeout = timeout; this.http.defaults.timeout = timeout; } // Test connection async testConnection() { try { await this.getSystemHealth(); return { success: true, message: 'Connection successful' }; } catch (error) { return { success: false, message: error.message, error: error }; } } } module.exports = FaceScannerClient;