-
Notifications
You must be signed in to change notification settings - Fork 2
/
zklibtcp.js
503 lines (390 loc) · 12.5 KB
/
zklibtcp.js
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
const net = require('net')
const timeParser = require('./timeParser');
const { MAX_CHUNK, COMMANDS, REQUEST_DATA } = require('./constants')
const { createTCPHeader,
exportErrorMessage,
removeTcpHeader,
decodeUserData72,
decodeRecordData40,
decodeRecordRealTimeLog52,
checkNotEventTCP,
decodeTCPHeader } = require('./utils')
const { log } = require('./helpers/errorLog')
class ZKLibTCP {
constructor(ip, port, timeout) {
this.ip = ip
this.port = port
this.timeout = timeout
this.sessionId = null
this.replyId = 0
this.socket = null
}
createSocket(cbError, cbClose) {
return new Promise((resolve, reject) => {
this.socket = new net.Socket()
this.socket.once('error', err => {
reject(err)
cbError && cbError(err)
})
this.socket.once('connect', () => {
resolve(this.socket)
})
this.socket.once('close', (err) => {
this.socket = null;
cbClose && cbClose('tcp')
})
if (this.timeout) {
this.socket.setTimeout(this.timeout)
}
this.socket.connect(this.port, this.ip)
})
}
connect() {
return new Promise(async (resolve, reject) => {
try {
const reply = await this.executeCmd(COMMANDS.CMD_CONNECT, '')
if (reply) {
resolve(true)
} else {
reject(new Error('NO_REPLY_ON_CMD_CONNECT'))
}
} catch (err) {
reject(err)
}
})
}
closeSocket() {
return new Promise((resolve, reject) => {
this.socket.removeAllListeners('data')
this.socket.end(() => {
clearTimeout(timer)
resolve(true)
})
/**
* When socket isn't connected so this.socket.end will never resolve
* we use settimeout for handling this case
*/
const timer = setTimeout(() => {
resolve(true)
}, 2000)
})
}
writeMessage(msg, connect) {
return new Promise((resolve, reject) => {
let timer = null
this.socket.once('data', (data) => {
timer && clearTimeout(timer)
resolve(data)
})
this.socket.write(msg, null, async (err) => {
if (err) {
reject(err)
} else if (this.timeout) {
timer = await setTimeout(() => {
clearTimeout(timer)
reject(new Error('TIMEOUT_ON_WRITING_MESSAGE'))
}, connect ? 2000 : this.timeout)
}
})
})
}
requestData(msg) {
return new Promise((resolve, reject) => {
let timer = null
let replyBuffer = Buffer.from([])
const internalCallback = (data) => {
this.socket.removeListener('data', handleOnData)
timer && clearTimeout(timer)
resolve(data)
}
const handleOnData = (data) => {
replyBuffer = Buffer.concat([replyBuffer, data])
if (checkNotEventTCP(data)) return;
clearTimeout(timer)
const header = decodeTCPHeader(replyBuffer.subarray(0,16));
if(header.commandId === COMMANDS.CMD_DATA){
timer = setTimeout(()=>{
internalCallback(replyBuffer)
}, 1000)
}else{
timer = setTimeout(() => {
reject(new Error('TIMEOUT_ON_RECEIVING_REQUEST_DATA'))
}, this.timeout)
const packetLength = data.readUIntLE(4, 2)
if (packetLength > 8) {
internalCallback(data)
}
}
}
this.socket.on('data', handleOnData)
this.socket.write(msg, null, err => {
if (err) {
reject(err)
}
timer = setTimeout(() => {
reject(Error('TIMEOUT_IN_RECEIVING_RESPONSE_AFTER_REQUESTING_DATA'))
}, this.timeout)
})
})
}
/**
*
* @param {*} command
* @param {*} data
*
*
* reject error when command fail and resolve data when success
*/
executeCmd(command, data) {
return new Promise(async (resolve, reject) => {
if (command === COMMANDS.CMD_CONNECT) {
this.sessionId = 0
this.replyId = 0
} else {
this.replyId++
}
const buf = createTCPHeader(command, this.sessionId, this.replyId, data)
let reply = null
try{
reply = await this.writeMessage(buf, command === COMMANDS.CMD_CONNECT || command === COMMANDS.CMD_EXIT)
const rReply = removeTcpHeader(reply);
if (rReply && rReply.length && rReply.length >= 0) {
if (command === COMMANDS.CMD_CONNECT) {
this.sessionId = rReply.readUInt16LE(4);
}
}
resolve(rReply)
}catch(err){
reject(err)
}
})
}
sendChunkRequest(start, size) {
this.replyId++;
const reqData = Buffer.alloc(8)
reqData.writeUInt32LE(start, 0)
reqData.writeUInt32LE(size, 4)
const buf = createTCPHeader(COMMANDS.CMD_DATA_RDY, this.sessionId, this.replyId, reqData)
this.socket.write(buf, null, err => {
if (err) {
log(`[TCP][SEND_CHUNK_REQUEST]` + err.toString())
}
})
}
/**
*
* @param {*} reqData - indicate the type of data that need to receive ( user or attLog)
* @param {*} cb - callback is triggered when receiving packets
*
* readWithBuffer will reject error if it'wrong when starting request data
* readWithBuffer will return { data: replyData , err: Error } when receiving requested data
*/
readWithBuffer(reqData, cb = null) {
return new Promise(async (resolve, reject) => {
this.replyId++;
const buf = createTCPHeader(COMMANDS.CMD_DATA_WRRQ, this.sessionId, this.replyId, reqData)
let reply = null
try {
reply = await this.requestData(buf)
} catch (err) {
reject(err)
}
const header = decodeTCPHeader(reply.subarray(0, 16))
switch (header.commandId) {
case COMMANDS.CMD_DATA: {
resolve({ data: reply.subarray(16), mode: 8 })
break;
}
case COMMANDS.CMD_ACK_OK:
case COMMANDS.CMD_PREPARE_DATA: {
// this case show that data is prepared => send command to get these data
// reply variable includes information about the size of following data
const recvData = reply.subarray(16)
const size = recvData.readUIntLE(1, 4)
// We need to split the data to many chunks to receive , because it's to large
// After receiving all chunk data , we concat it to TotalBuffer variable , that 's the data we want
let remain = size % MAX_CHUNK
let numberChunks = Math.round(size - remain) / MAX_CHUNK
let totalPackets = numberChunks + (remain > 0 ? 1 : 0)
let replyData = Buffer.from([])
let totalBuffer = Buffer.from([])
let realTotalBuffer = Buffer.from([])
const timeout = 10000
let timer = setTimeout(() => {
internalCallback(replyData, new Error('TIMEOUT WHEN RECEIVING PACKET'))
}, timeout)
const internalCallback = (replyData, err = null) => {
// this.socket && this.socket.removeListener('data', handleOnData)
timer && clearTimeout(timer)
resolve({ data: replyData, err })
}
const handleOnData = (reply) => {
if (checkNotEventTCP(reply)) return;
clearTimeout(timer)
timer = setTimeout(() => {
internalCallback(replyData,
new Error(`TIME OUT !! ${totalPackets} PACKETS REMAIN !`))
}, timeout)
totalBuffer = Buffer.concat([totalBuffer, reply])
const packetLength = totalBuffer.readUIntLE(4, 2)
if (totalBuffer.length >= 8 + packetLength) {
realTotalBuffer = Buffer.concat([realTotalBuffer, totalBuffer.subarray(16, 8 + packetLength)])
totalBuffer = totalBuffer.subarray(8 + packetLength)
if ((totalPackets > 1 && realTotalBuffer.length === MAX_CHUNK + 8)
|| (totalPackets === 1 && realTotalBuffer.length === remain + 8)) {
replyData = Buffer.concat([replyData, realTotalBuffer.subarray(8)])
totalBuffer = Buffer.from([])
realTotalBuffer = Buffer.from([])
totalPackets -= 1
cb && cb(replyData.length, size)
if (totalPackets <= 0) {
internalCallback(replyData)
}
}
}
}
this.socket.once('close', () => {
internalCallback(replyData, new Error('Socket is disconnected unexpectedly'))
})
this.socket.on('data', handleOnData);
for (let i = 0; i <= numberChunks; i++) {
if (i === numberChunks) {
this.sendChunkRequest(numberChunks * MAX_CHUNK, remain)
} else {
this.sendChunkRequest(i * MAX_CHUNK, MAX_CHUNK)
}
}
break;
}
default: {
reject(new Error('ERROR_IN_UNHANDLE_CMD ' + exportErrorMessage(header.commandId)))
}
}
})
}
async getSmallAttendanceLogs(){
}
/**
* reject error when starting request data
* return { data: users, err: Error } when receiving requested data
*/
async getUsers() {
// Free Buffer Data to request Data
if (this.socket) {
try {
await this.freeData()
} catch (err) {
return Promise.reject(err)
}
}
let data = null
try {
data = await this.readWithBuffer(REQUEST_DATA.GET_USERS)
} catch (err) {
return Promise.reject(err)
}
// Free Buffer Data after requesting data
if (this.socket) {
try {
await this.freeData()
} catch (err) {
return Promise.reject(err)
}
}
const USER_PACKET_SIZE = 72
let userData = data.data.subarray(4)
let users = []
while (userData.length >= USER_PACKET_SIZE) {
const user = decodeUserData72(userData.subarray(0, USER_PACKET_SIZE))
users.push(user)
userData = userData.subarray(USER_PACKET_SIZE)
}
return { data: users, err: data.err }
}
/**
*
* @param {*} ip
* @param {*} callbackInProcess
* reject error when starting request data
* return { data: records, err: Error } when receiving requested data
*/
async getAttendances(callbackInProcess = () => { }) {
if (this.socket) {
try {
await this.freeData()
} catch (err) {
return Promise.reject(err)
}
}
let data = null
try {
data = await this.readWithBuffer(REQUEST_DATA.GET_ATTENDANCE_LOGS, callbackInProcess)
} catch (err) {
return Promise.reject(err)
}
if (this.socket) {
try {
await this.freeData()
} catch (err) {
return Promise.reject(err)
}
}
const RECORD_PACKET_SIZE = 40
let recordData = data.data.subarray(4)
let records = []
while (recordData.length >= RECORD_PACKET_SIZE) {
const record = decodeRecordData40(recordData.subarray(0, RECORD_PACKET_SIZE))
records.push({ ...record, ip: this.ip })
recordData = recordData.subarray(RECORD_PACKET_SIZE)
}
return { data: records, err: data.err }
}
async getTime() {
const time = await this.executeCmd(COMMANDS.CMD_GET_TIME, '');
return timeParser.decode(time.readUInt32LE(8));
}
async freeData() {
return await this.executeCmd(COMMANDS.CMD_FREE_DATA, '')
}
async disableDevice() {
return await this.executeCmd(COMMANDS.CMD_DISABLEDEVICE, REQUEST_DATA.DISABLE_DEVICE)
}
async enableDevice() {
return await this.executeCmd(COMMANDS.CMD_ENABLEDEVICE, '')
}
async disconnect() {
try {
await this.executeCmd(COMMANDS.CMD_EXIT, '')
} catch (err) {
}
return await this.closeSocket()
}
async getInfo() {
try {
const data = await this.executeCmd(COMMANDS.CMD_GET_FREE_SIZES, '')
return {
userCounts: data.readUIntLE(24, 4),
logCounts: data.readUIntLE(40, 4),
logCapacity: data.readUIntLE(72, 4)
}
} catch (err) {
return Promise.reject(err)
}
}
async clearAttendanceLog (){
return await this.executeCmd(COMMANDS.CMD_CLEAR_ATTLOG, '')
}
async getRealTimeLogs(cb = () => { }) {
this.replyId++;
const buf = createTCPHeader(COMMANDS.CMD_REG_EVENT, this.sessionId, this.replyId, Buffer.from([0x01, 0x00, 0x00, 0x00]))
this.socket.write(buf, null, err => {
})
this.socket.listenerCount('data') === 0 && this.socket.on('data', (data) => {
if (!checkNotEventTCP(data)) return;
if (data.length > 16) {
cb(decodeRecordRealTimeLog52(data))
}
})
}
}
module.exports = ZKLibTCP