Comments (1)
I fixed the large attendance data issue by adding a check when packet is fully sent.
Just change the whole function to this
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)
//console.log(reply.toString('hex'));
} 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([])
let isDataOnGoing = false
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
isDataOnGoing = false;
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)
}
isDataOnGoing = true;
while (isDataOnGoing) {
await new Promise(r => setTimeout(r, 100))
}
}
break;
}
default: {
reject(new Error('ERROR_IN_UNHANDLE_CMD ' + exportErrorMessage(header.commandId)))
}
}
})
}
from node-zklib.
Related Issues (20)
- How to add a user with fingerprint HOT 6
- getting all device data
- getRealTimeLogs retrieved data HOT 4
- get attendance timeout with large amounts of data HOT 2
- How to add a new user?
- How to configure node-zklib for multiple terminal HOT 2
- How to get realtime log ? HOT 4
- NodeRed HOT 1
- CMD_OPTIONS_RRQ returns = instead 2 in some instances. HOT 1
- timeout error HOT 2
- How upload / Download user finger print template node-zklib ? HOT 4
- How to set the Comm Password? HOT 1
- RangeError [ERR_OUT_OF_RANGE]: The value of "offset" is out of range. It must be >= 0 and <= 4. Received 24 HOT 3
- Getting error on getTime method, although it's getting user and attendance records.
- This log showed that you ran into TIMEOUT error ,and just that ...
- how to get the total number of biophoto/userpic, or if a user has a biophoto/userpic?
- can we get attendence Recode between Date range?
- Does this package support ZKTeco F18? HOT 1
- It shows me error in decodetcpheader method
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-zklib.