-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.js
More file actions
60 lines (51 loc) · 2.41 KB
/
Copy pathreader.js
File metadata and controls
60 lines (51 loc) · 2.41 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
import { knx, options, crossEvent } from './index.js';
import { parseJSON, isEmptyObject } from './helpers/misc.js';
// import { Buffer } from 'node:Buffer';
/**
* Websocket kommunikáció kezelése.
* @param {import('ws').WebSocketServer} ws A websocket szerver, amit callback paraméterként kap.
*/
function handleWebsocket(ws) {
let authenticated = false;
let ID = -1;
// TODO: interval(options.json-ben meghatározott időköz) újra authentikálással, amúgy ledobni?
let timeout = setTimeout(() => ws.close(1008), options.readerConnection.websocket.authenticationTimeout);
ws.on('message', async data => {
const recieved = parseJSON(data.toString());
if (isEmptyObject(recieved)) console.error('Invalid JSON recieved on websocket connection');
if (!authenticated && recieved.cmd != 0) {
ws.close(1008);
return;
} else if (!authenticated && recieved.cmd == 0) {
if (recieved.secret == options.readerConnection.websocket.secret && (recieved.ID != undefined)) {
clearTimeout(timeout);
authenticated = true;
ID = recieved.ID;
console.log(`Server connected to reader (${ID})!`);
} else ws.close(1008);
return;
}
// {"cmd": 1, "tag": [182, 159, 102, 105, 215, 44, 92, 224, 240, 196, 186, 192, 39, 205, 150, 28, 156, 154, 208, 111, 218, 245, 233, 50, 68, 41, 122, 100, 252, 85, 90, 122]}
// {"cmd": 0, "secret": "pokjihgvfcdrxdctfzgv", "ID":0}
if (recieved.cmd == 1) {
const fetched = await knx('mifare_tags').first('*').where('Bytes', Buffer.from(recieved.tag));
if (fetched == undefined)
ws.send(JSON.stringify({ 'cmd': 1, 'correct': false, 'tag': recieved.tag }));
else {
ws.send(JSON.stringify({ 'cmd': 1, 'correct': true, 'tag': recieved.tag }));
const lastCrossing = await knx('crossings').first('Direction', 'Time').where('UID', fetched.UID).orderBy('Time', 'desc');
const isInside = lastCrossing?.Direction[0] || '' ? 0 : 1; // bent van-e? alapból bent van -- bement/bent van 0 - kiment/kint van 1
crossEvent.emit('cross', { 'UID': fetched.UID, 'direction': isInside });
await knx('crossings').insert({ 'UID': fetched.UID, 'direction': isInside });
}
}
});
ws.on('close', () => {
if (ID != -1)
console.log(`Connection to reader (${ID.toString()}) was closed!`);
});
ws.on('error', err => {
console.error(err);
});
}
export { handleWebsocket };