@supernick135/face-scanner-client
Version:
Node.js client library for ZKTeco face scanning devices integration with comprehensive API support
109 lines (90 loc) ⢠3.07 kB
JavaScript
/**
* Face Scanner Device Client Example
*
* This example demonstrates how to use the Face Scanner WebSocket client
* as a scanning device with API key authentication.
*/
const WebSocketClient = require('../lib/client/WebSocketClient');
async function deviceExample() {
console.log('š§ Starting Face Scanner Device Client Example');
// Create device client using API key authentication
const deviceClient = WebSocketClient.createDeviceClient({
wsUrl: 'ws://localhost:8080/ws',
apiKey: 'your-device-api-key-here',
schoolId: 'school123',
deviceId: 'device456',
reconnect: true,
heartbeatInterval: 30000
});
// Set up event listeners
deviceClient.on('connected', () => {
console.log('ā
Device connected to Face Scanner server');
});
deviceClient.on('authenticated', () => {
console.log('š Device authenticated successfully');
});
deviceClient.on('authError', (error) => {
console.error('ā Authentication failed:', error.message);
});
deviceClient.on('disconnected', ({ code, reason }) => {
console.log(`š Device disconnected: ${code} - ${reason}`);
});
deviceClient.on('reconnecting', (attempt) => {
console.log(`š Attempting to reconnect... (${attempt})`);
});
deviceClient.on('error', (error) => {
console.error('ā WebSocket error:', error);
});
// Listen for server responses
deviceClient.on('roomUpdate', (data) => {
console.log('š Room update:', data);
});
try {
// Connect to server
await deviceClient.connect();
// Simulate face scanning events
console.log('šø Starting face scan simulation...');
// Simulate face detected event
setTimeout(() => {
console.log('š¤ Sending face detected event');
deviceClient.sendFaceScanEvent('face_detected', {
studentId: 'student789',
confidence: 0.95,
imageUrl: 'https://example.com/face-captures/student789.jpg'
});
}, 2000);
// Simulate scan complete event
setTimeout(() => {
console.log('ā
Sending scan complete event');
deviceClient.sendFaceScanEvent('scan_complete', {
studentId: 'student789',
success: true,
processingTime: 1200
});
}, 4000);
// Simulate no face detected
setTimeout(() => {
console.log('š» Sending no face event');
deviceClient.sendFaceScanEvent('no_face', {
reason: 'No face detected in frame'
});
}, 6000);
// Keep running for demonstration
console.log('ā±ļø Device will run for 30 seconds for demonstration...');
setTimeout(() => {
console.log('š Shutting down device client');
deviceClient.disconnect();
process.exit(0);
}, 30000);
} catch (error) {
console.error('ā Failed to start device client:', error);
process.exit(1);
}
}
// Handle process termination
process.on('SIGINT', () => {
console.log('\nš Received SIGINT, shutting down gracefully...');
process.exit(0);
});
// Run the example
deviceExample().catch(console.error);