-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-simulator.js
More file actions
179 lines (147 loc) · 5.12 KB
/
test-simulator.js
File metadata and controls
179 lines (147 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* RFID Reader Simulator for Testing
* This simulates an RFID reader for development and testing purposes
*/
const net = require('net');
class RFIDSimulator {
constructor(port = 6677) {
this.port = port;
this.server = null;
this.clients = new Set();
this.isRunning = false;
this.autoReadInterval = null;
}
start() {
this.server = net.createServer((socket) => {
console.log('Client connected to RFID simulator');
this.clients.add(socket);
// Send welcome message
socket.write('Equicus RFID Reader Simulator v1.0\r\n');
socket.on('data', (data) => {
const command = data.toString().trim();
console.log(`Received command: ${command}`);
this.handleCommand(socket, command);
});
socket.on('close', () => {
console.log('Client disconnected from RFID simulator');
this.clients.delete(socket);
});
socket.on('error', (error) => {
console.log('Client connection error:', error.message);
this.clients.delete(socket);
});
});
this.server.listen(this.port, () => {
console.log(`RFID Simulator running on port ${this.port}`);
this.isRunning = true;
});
this.server.on('error', (error) => {
console.error('Server error:', error.message);
});
}
handleCommand(socket, command) {
// Remove @ prefix if present (machine mode)
const cleanCommand = command.replace(/^@/, '');
switch (cleanCommand.toLowerCase()) {
case 'sv':
// Software version
socket.write('D24D0C70\r\n');
break;
case 'param':
// Parameters
socket.write('freq=125\r\n');
socket.write('power=30\r\n');
socket.write('mode=auto\r\n');
break;
case 'ata':
// Antenna tuning A
socket.write('+ CA0000AA\r\n');
break;
case 'atb':
// Antenna tuning B
socket.write('+ CB0000BB\r\n');
break;
case 'se':
// Status
socket.write('+ OK\r\n');
break;
case 'fmt 15':
// Set format to show all fields
socket.write('+ OK\r\n');
break;
case 'fmt 2':
// Set format to ID only
socket.write('+ OK\r\n');
break;
case 'sa 1':
// Start autoread
socket.write('+ OK\r\n');
this.startAutoRead(socket);
break;
case 'sa 0':
// Stop autoread
socket.write('+ OK\r\n');
this.stopAutoRead();
break;
default:
// Unknown command
socket.write('+ ERROR: Unknown command\r\n');
break;
}
}
startAutoRead(socket) {
console.log('Starting simulated autoread...');
// Generate random tag IDs
const tagIds = [
'A1B2C3D4E5',
'F6G7H8I9J0',
'K1L2M3N4O5',
'P6Q7R8S9T0',
'U1V2W3X4Y5'
];
this.autoReadInterval = setInterval(() => {
if (this.clients.has(socket)) {
const randomTag = tagIds[Math.floor(Math.random() * tagIds.length)];
const channel = Math.random() > 0.5 ? 'a' : 'b';
const speed = Math.random() > 0.5 ? '2' : '4';
const polarity = Math.random() > 0.5 ? '+' : '-';
const signalStrength = Math.floor(Math.random() * 50) + 10;
const timestamp = Date.now() % 1000000;
const tagData = `${channel}${speed}${polarity}: ${randomTag} ${signalStrength} :${timestamp}\r\n`;
socket.write(tagData);
}
}, 2000); // Send tag every 2 seconds
}
stopAutoRead() {
console.log('Stopping simulated autoread...');
if (this.autoReadInterval) {
clearInterval(this.autoReadInterval);
this.autoReadInterval = null;
}
}
stop() {
if (this.autoReadInterval) {
this.stopAutoRead();
}
if (this.server) {
this.server.close(() => {
console.log('RFID Simulator stopped');
this.isRunning = false;
});
}
}
}
// Start simulator if this file is run directly
if (require.main === module) {
const simulator = new RFIDSimulator(6677);
console.log('Starting Equicus RFID Reader Simulator...');
console.log('Connect your application to localhost:6677');
console.log('Press Ctrl+C to stop');
simulator.start();
process.on('SIGINT', () => {
console.log('\nStopping simulator...');
simulator.stop();
process.exit(0);
});
}
module.exports = RFIDSimulator;