125 lines
4.5 KiB
JavaScript
125 lines
4.5 KiB
JavaScript
|
const config = require('../config');
|
||
|
const got = require('got');
|
||
|
const con = require('../db/connect');
|
||
|
|
||
|
module.exports = async() => {
|
||
|
const ip = config.ip;
|
||
|
let bufferTime = [];
|
||
|
let bufferEpcHex = [];
|
||
|
let interval;
|
||
|
let jsonMessage;
|
||
|
|
||
|
console.log('--START BACKEND--');
|
||
|
|
||
|
const stream = await got.stream(`http://${ip}/api/v1/data/stream`);
|
||
|
stream
|
||
|
.on('data', value => {
|
||
|
// value = '{"timestamp":"2022-07-27T11:57:39.226680176Z","tagInventoryEvent":{"epc":"qrsAAAAAAAAFdgQg","epcHex":"AABB00000000000005760420","pc":"MAA=","antennaPort":4,"antennaName":"4","peakRssiCdbm":-7600,"lastSeenTime":"2022-07-27T11:57:39.041444Z","phaseAngle":46.05}}'
|
||
|
let buffer = value.toString();
|
||
|
buffer = buffer.split('\n');
|
||
|
for (let result of buffer) {
|
||
|
if (result && /{.*:{.*:.*}}/.test(result)) {
|
||
|
const jsonResult = JSON.parse(result);
|
||
|
let epcHex = jsonResult?.tagInventoryEvent?.epcHex;
|
||
|
|
||
|
if (!epcHex) return;
|
||
|
if (epcHex.search('AABB') == -1) return;
|
||
|
|
||
|
// console.log(epcHex)
|
||
|
epcHex = epcHex.replace('AABB', '');
|
||
|
const existEpcHex = element => element == epcHex;
|
||
|
const bufferIndex = bufferEpcHex.findIndex(existEpcHex);
|
||
|
|
||
|
if (bufferIndex >= 0)
|
||
|
return bufferTime[bufferIndex].count = bufferTime[bufferIndex].count + 1;
|
||
|
|
||
|
const newObject = {
|
||
|
code: epcHex,
|
||
|
created: jsonResult.timestamp,
|
||
|
peakRssi: jsonResult.tagInventoryEvent.peakRssiCdbm,
|
||
|
count: 1
|
||
|
};
|
||
|
|
||
|
bufferEpcHex.push(epcHex);
|
||
|
bufferTime.push(newObject);
|
||
|
insertLog(bufferTime);
|
||
|
|
||
|
if (interval) {
|
||
|
clearInterval(interval);
|
||
|
interval = null;
|
||
|
}
|
||
|
interval = setInterval(insertDB, 10000);
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
/* .on("close", () => {
|
||
|
console.log('CLOSE_STREAM')
|
||
|
})*/
|
||
|
|
||
|
async function insertDB() {
|
||
|
clearInterval(interval);
|
||
|
|
||
|
interval = null;
|
||
|
// console.log('--INSERT IN DB--');
|
||
|
// console.log(bufferEpcHex, bufferTime);
|
||
|
|
||
|
let rfids = [];
|
||
|
let response;
|
||
|
for (let code of bufferEpcHex) {
|
||
|
while (code.substring(0, 1) == 0)
|
||
|
code = code.substring(1);
|
||
|
|
||
|
rfids.push(parseInt(code));
|
||
|
}
|
||
|
|
||
|
// console.log('rfids', rfids)
|
||
|
rfids = await cleanDev(rfids);
|
||
|
if (!rfids.length) return console.log({error: 'NOT_PARSED_RFIDS', rfids});
|
||
|
|
||
|
// response = await con.query(`CALL vn.pallet_build(JSON_ARRAY(?),?,@pallet);SELECT @pallet;`, params);
|
||
|
response = await con.query(`CALL vn.pallet_build(JSON_ARRAY(?), ?, @pallet);SELECT @pallet;`, [rfids, 19294]);
|
||
|
console.log(response);
|
||
|
console.log(response[0][1][0]['@pallet']);
|
||
|
|
||
|
// console.log(response)
|
||
|
// console.log(response[1]);
|
||
|
bufferTime = [];
|
||
|
bufferEpcHex = [];
|
||
|
const pallet = response[0][1][0]['@pallet'];
|
||
|
if (pallet) {
|
||
|
console.log('CALL TO PRINT');
|
||
|
const print = await con.query(`CALL vn.expeditionPallet_printLabel(?);`, [pallet]);
|
||
|
// console.log('print', print)
|
||
|
jsonMessage = {pallet: pallet, print: print};
|
||
|
// stream.destroy();
|
||
|
console.log(jsonMessage);
|
||
|
} else
|
||
|
console.log({error: 'ERROR_CREATING_PALLET', expeditions: rfids});
|
||
|
}
|
||
|
|
||
|
async function insertLog() {
|
||
|
const sql = `INSERT INTO lastRFID (code, antennaFk, isConsulted, isChoosed, antennaPort, peakRssi, seenCount)
|
||
|
VALUES(?, 5, 1, 0, 1, ?, ?);`;
|
||
|
|
||
|
for (let rfid of bufferTime) {
|
||
|
const response = await con.query(sql, [rfid.code, rfid.peakRssi, rfid.count]);
|
||
|
// console.log('insertLog', response, [rfid.code, rfid.peakRssi, rfid.count])
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async function cleanDev(ids) {
|
||
|
if (config.env != 'dev') return ids;
|
||
|
const sql = `SELECT id FROM vn.expedition WHERE id = ?`;
|
||
|
|
||
|
const idsCleaned = [];
|
||
|
for (let id of ids) {
|
||
|
const [response] = await con.query(sql, [id]);
|
||
|
console.log('cleanDev', response);
|
||
|
if (response.length > 0)
|
||
|
idsCleaned.push(id);
|
||
|
}
|
||
|
|
||
|
return idsCleaned;
|
||
|
}
|
||
|
};
|