100 lines
3.6 KiB
JavaScript
100 lines
3.6 KiB
JavaScript
|
const config = require('../config');
|
||
|
const express = require('express');
|
||
|
const router = express.Router(); // eslint-disable-line
|
||
|
const got = require('got');
|
||
|
const con = require('../db/connect');
|
||
|
|
||
|
// Obtenemos los parametros
|
||
|
router.get('/:truckFk', async(req, res) => {
|
||
|
const ip = config.ip;
|
||
|
const truckFk = req.params.truckFk;
|
||
|
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);
|
||
|
if (interval) {
|
||
|
clearInterval(interval);
|
||
|
interval = null;
|
||
|
}
|
||
|
interval = setInterval(insertDB, 5000, truckFk);
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
.on('close', () => {
|
||
|
console.log('CLOSE_STREAM');
|
||
|
});
|
||
|
|
||
|
async function insertDB(truckFk) {
|
||
|
clearInterval(interval);
|
||
|
stream.destroy();
|
||
|
|
||
|
interval = null;
|
||
|
console.log('--INSERT IN DB--');
|
||
|
console.log(bufferEpcHex, bufferTime, truckFk);
|
||
|
|
||
|
const rfids = [];
|
||
|
let response;
|
||
|
for (let code of bufferEpcHex) {
|
||
|
while (code.substring(0, 1) == 0)
|
||
|
code = code.substring(1);
|
||
|
|
||
|
rfids.push(code);
|
||
|
}
|
||
|
|
||
|
console.log('rfids', rfids.join(','));
|
||
|
if (rfids.length)
|
||
|
[response] = await con.query(`CALL vn.palletBuilding(?,?);`, [rfids.join(','), truckFk]);
|
||
|
|
||
|
console.log(response);
|
||
|
console.log(response[0]);
|
||
|
console.log(response[0][0]);
|
||
|
console.log(response[0][0]?.palletFk);
|
||
|
bufferTime = [];
|
||
|
bufferEpcHex = [];
|
||
|
const pallet = response[0][0]?.palletFk;
|
||
|
if (pallet) {
|
||
|
const print = await con.query(`CALL vn.expeditionPallet_printLabel(?);`, [pallet]);
|
||
|
console.log('print', print);
|
||
|
jsonMessage = {pallet: pallet, print: print};
|
||
|
stream.destroy();
|
||
|
return res.json(jsonMessage);
|
||
|
}
|
||
|
return res.json({error: 'ERROR_CREATING_PALLET'});
|
||
|
}
|
||
|
});
|
||
|
|
||
|
module.exports = router;
|