UNPKG

@supernick135/face-scanner-client

Version:

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

176 lines (146 loc) • 5.92 kB
/** * Device Management Example - Face Scanner API Client * This example shows how to manage devices and send commands */ const { DeviceClient } = require('../lib'); async function deviceManagementExample() { console.log('šŸ”§ Face Scanner API - Device Management Example'); // Create device client const deviceClient = new DeviceClient({ baseURL: 'http://localhost:9999', apiKey: 'your-api-key-here', // Replace with your actual API key deviceId: 'PYA8244800545' // Replace with your device ID }); try { // 1. Get device status console.log('\nšŸ“± Getting device status...'); const status = await deviceClient.getStatus(); console.log('Device status:', status); // 2. Send device info command console.log('\nšŸ“‹ Requesting device information...'); const infoCommand = await deviceClient.sendDeviceInfo(); console.log('Info command sent:', infoCommand.id); // Wait for command completion console.log('ā³ Waiting for command completion...'); const completedInfo = await deviceClient.waitForCommand(infoCommand.id, 30000); console.log('Device info result:', completedInfo); // 3. Query attendance logs console.log('\nšŸ“Š Querying attendance logs...'); const attLogCommand = await deviceClient.sendAttLogQuery(); console.log('AttLog command sent:', attLogCommand.id); // 4. Query photos console.log('\nšŸ“· Querying photos...'); const photoCommand = await deviceClient.sendAttPhotoQuery(); console.log('Photo command sent:', photoCommand.id); // 5. Get command queue status console.log('\nšŸ“‹ Getting command queue...'); const queue = await deviceClient.getQueue(); console.log(`Queue has ${queue.length} pending commands:`); queue.forEach(cmd => { console.log(` - ${cmd.type} (${cmd.status}) - ${cmd.id}`); }); // 6. Bulk data query console.log('\nšŸ“¦ Performing bulk data query...'); const bulkResults = await deviceClient.bulkDataQuery(['ATTLOG', 'ATTPHOTO', 'USER']); bulkResults.forEach(result => { console.log(`${result.table}: ${result.success ? 'āœ…' : 'āŒ'} ${result.error || 'Success'}`); }); // 7. Get device statistics console.log('\nšŸ“ˆ Getting device statistics...'); const deviceStats = await deviceClient.getStats(); console.log('Device stats:', deviceStats); // 8. Get device logs console.log('\nšŸ“ Getting device logs...'); const logs = await deviceClient.getLogs({ limit: 5 }); console.log(`Recent device logs (${logs.length}):`); logs.forEach(log => { console.log(` ${log.timestamp}: [${log.level}] ${log.message}`); }); } catch (error) { console.error('āŒ Error:', error.message); console.error('Details:', error.response?.data || error); } } // Example: Managing multiple devices async function multipleDevicesExample() { console.log('\nšŸ”„ Multiple Devices Management Example'); const deviceIds = ['DEVICE_001', 'DEVICE_002', 'DEVICE_003']; for (const deviceId of deviceIds) { console.log(`\nšŸ“± Managing device: ${deviceId}`); const client = new DeviceClient({ baseURL: 'http://localhost:9999', apiKey: 'your-api-key-here', deviceId: deviceId }); try { const status = await client.getStatus(); console.log(` Status: ${status.connected ? '🟢 Online' : 'šŸ”“ Offline'}`); if (status.connected) { // Send data query for online devices const command = await client.sendAttLogQuery(); console.log(` Command sent: ${command.id}`); } } catch (error) { console.log(` āŒ Error: ${error.message}`); } } } // Example: Command monitoring async function commandMonitoringExample() { console.log('\nšŸ” Command Monitoring Example'); const client = new DeviceClient({ baseURL: 'http://localhost:9999', apiKey: 'your-api-key-here', deviceId: 'PYA8244800545' }); try { // Send a command console.log('šŸ“¤ Sending command...'); const command = await client.sendDeviceInfo(); console.log(`Command sent: ${command.id}`); // Monitor command progress console.log('šŸ‘€ Monitoring command progress...'); const checkInterval = setInterval(async () => { try { const status = await client.getCommandStatus(client.deviceId, command.id); console.log(` Status: ${status.status} (${new Date().toLocaleTimeString()})`); if (status.status === 'completed') { console.log('āœ… Command completed successfully!'); console.log('Result:', status.result); clearInterval(checkInterval); } else if (status.status === 'failed') { console.log('āŒ Command failed:', status.error); clearInterval(checkInterval); } } catch (error) { console.error('āŒ Monitoring error:', error.message); clearInterval(checkInterval); } }, 2000); // Stop monitoring after 30 seconds setTimeout(() => { clearInterval(checkInterval); console.log('ā° Monitoring timeout'); }, 30000); } catch (error) { console.error('āŒ Error:', error.message); } } // Run examples if (require.main === module) { (async () => { try { await deviceManagementExample(); await multipleDevicesExample(); await commandMonitoringExample(); console.log('\nšŸŽ‰ Device management examples completed!'); } catch (error) { console.error('\nšŸ’„ Examples failed:', error.message); } })(); } module.exports = { deviceManagementExample, multipleDevicesExample, commandMonitoringExample };