dev to master #3

Merged
alexm merged 8 commits from dev into master 2023-02-23 13:20:37 +00:00
9 changed files with 91 additions and 49 deletions

View File

@ -2,8 +2,9 @@ arcId: 1
port: 1234 port: 1234
ip: 1.2.3.4 ip: 1.2.3.4
env: dev env: dev
interval: 1000 interval: 3000
reconnectInterval: 5000 reconnectInterval: 5000
counterInterval: 1000
db: db:
host: host host: host
port: 3307 port: 3307

View File

@ -3,7 +3,7 @@
"version": "1.0.0", "version": "1.0.0",
"author": "Verdnatura Levante SL", "author": "Verdnatura Levante SL",
"description": "rfid backend", "description": "rfid backend",
"main": "index.js", "main": "server.js",
"type": "module", "type": "module",
"scripts": { "scripts": {
"start": "nodemon ./server.js" "start": "nodemon ./server.js"

15
src/counter.js Normal file
View File

@ -0,0 +1,15 @@
import con from '../db/connect.js';
let lastCounter;
export default async(size, arcId) => {
if (lastCounter == size) return;
console.logger.info(`COUNTER: SIZE:${size} ARC_ID:${arcId}`);
await con.query(`
UPDATE vn.arcRead
SET counter = ?,
error = NULL
WHERE id = ?;
`, [size, arcId]);
lastCounter = size;
};

View File

@ -1,13 +1,12 @@
import con from '../db/connect.js'; import con from '../db/connect.js';
import counter from './counter.js';
import t from '../util/translator.js';
export default async(rfids, arcId) => { export default async(rfids, arcId) => {
const codes = new Set(); try {
for (let rfid of rfids) await con.query(`CALL vn.expeditionPallet_build(JSON_ARRAY(?), ?, ?, @palletId);`, [Array.from(rfids), arcId, null]);
codes.add(rfid.code); await counter(null, arcId);
} catch (error) {
console.logger.info('PRINTING...'); await con.query(`UPDATE vn.arcRead SET error = ?, counter = NULL WHERE id = ?;`, [t(error.sqlMessage), arcId]);
}
const palletId = await con.query(`CALL vn.expeditionPallet_build(JSON_ARRAY(?), ?, ?, @palletId);`, [Array.from(codes), arcId, null]);
if (!palletId)
console.logger.info({error: 'ERROR_CREATING_PALLET', expeditions: rfids});
}; };

View File

@ -2,13 +2,15 @@ export default async data => {
data = data.toString(); data = data.toString();
const crudeRfids = data.split('\n'); const crudeRfids = data.split('\n');
const rfidsParsed = []; const rfidsParsed = new Set();
const rfidsParsedExtended = [];
for (let crudeRfid of crudeRfids) { for (let crudeRfid of crudeRfids) {
if (crudeRfid && /{.*:{.*:.*}}/.test(crudeRfid)) { if (crudeRfid && /{.*:{.*:.*}}/.test(crudeRfid)) {
const jsonResult = JSON.parse(crudeRfid); const jsonResult = JSON.parse(crudeRfid);
let epcHex = jsonResult?.tagInventoryEvent?.epcHex; let epcHex = jsonResult?.tagInventoryEvent?.epcHex;
if (!epcHex) return; if (!epcHex) continue;
if (epcHex.search('AABB') == -1) continue; if (epcHex.search('AABB') == -1) continue;
epcHex = epcHex.replace('AABB', ''); epcHex = epcHex.replace('AABB', '');
@ -21,9 +23,11 @@ export default async data => {
antenna: jsonResult.tagInventoryEvent.antennaPort antenna: jsonResult.tagInventoryEvent.antennaPort
}; };
rfidsParsed.push(rfidParsed); const rfidsParsedExtended = [];
rfidsParsedExtended.push(rfidParsed);
rfidsParsed.add(rfidParsed.code);
} }
} }
return rfidsParsed; return {codes: rfidsParsed, extended: rfidsParsedExtended};
}; };

View File

@ -1,12 +1,15 @@
import got from 'got'; import got from 'got';
import rfidParser from './rfidParser.js'; import rfidParser from './rfidParser.js';
import newPallet from './newPallet.js'; import newPallet from './newPallet.js';
import debug from '../util/debugStream.js';
import counter from './counter.js';
let interval; let interval;
let counterInterval;
export default async(conf, cb) => { export default async(conf, cb) => {
let rfidbuffer = []; let rfidbuffer = new Set();
let rfidbufferSet = [new Set(), new Set(), new Set(), new Set()]; let rfidbufferExtend = [];
const stream = got.stream(`http://${conf.ip}/api/v1/data/stream`); const stream = got.stream(`http://${conf.ip}/api/v1/data/stream`);
@ -14,15 +17,19 @@ export default async(conf, cb) => {
.on('data', async value => { .on('data', async value => {
const parsed = await rfidParser(value); const parsed = await rfidParser(value);
if (parsed) if (!parsed.codes.size) return;
rfidbuffer = rfidbuffer.concat(parsed); rfidbuffer = new Set([...rfidbuffer, ...parsed.codes]);
rfidbufferExtend = rfidbufferExtend.concat(parsed.extended);
debug(parsed); debug({codes: rfidbuffer, extended: rfidbufferExtend}, conf);
if (rfidbuffer && rfidbuffer.length && parsed && parsed.length) { if (rfidbuffer.size) {
clearInterval(interval); clearTimeout(interval);
interval = null; interval = null;
interval = setInterval(createPallet, conf.interval); interval = setTimeout(createPallet, conf.interval);
if (!counterInterval)
counterInterval = setTimeout(counterIntervalManager, conf.counterInterval);
} }
}) })
.on('error', e => { .on('error', e => {
@ -30,34 +37,16 @@ export default async(conf, cb) => {
}); });
function createPallet() { function createPallet() {
clearInterval(interval); // try remove clearTimeout(interval);
if (!rfidbuffer.length) return;
newPallet(rfidbuffer, conf.arcId); newPallet(rfidbuffer, conf.arcId);
rfidbuffer = [];
rfidbufferSet = [new Set(), new Set(), new Set(), new Set()]; rfidbuffer = new Set();
rfidbufferExtend = [];
} }
function debug(parsed) { function counterIntervalManager() {
if (conf.env != 'dev') return; counterInterval = null;
let totalBuffer = rfidbuffer.map(rfid => rfid.code); counter(rfidbuffer.size, conf.arcId);
let totalBufferSet = new Set(totalBuffer);
console.log('TOTAL BUFFER: ', totalBufferSet.size);
const totalRead = [0, 0, 0, 0];
for (let buffer of rfidbuffer)
totalRead[buffer.antenna - 1]++;
console.log('TOTAL READ ANTENNA:', totalRead);
for (let buffer of parsed)
rfidbufferSet[buffer.antenna - 1].add(buffer.code);
console.log('UNIQUE READ ANTENNA:', rfidbufferSet[0].size, rfidbufferSet[1].size, rfidbufferSet[2].size, rfidbufferSet[3].size);
for (const [index, set] of rfidbufferSet.entries()) {
if (((set.size * 100) / totalBufferSet.size) < 25)
console.log('[WARNING_ANTENNA]: ', index + 1, ' ONLY ', set.size);
}
console.log('----------------------------------------------------------------');
} }
}; };
50;

25
util/debugStream.js Normal file
View File

@ -0,0 +1,25 @@
export default (parsed, conf) => {
if (conf.env != 'dev') return;
// TOTAL
console.log('TOTAL BUFFER: ', parsed.codes.size);
// TOTAL READS BY ANTENNA
const totalRead = [0, 0, 0, 0];
for (let read of parsed.extended)
totalRead[read.antenna - 1]++;
console.log('TOTAL READ ANTENNA:', totalRead);
// UNIQUE READS BY ANTENNA
const uniqueRead = [new Set(), new Set(), new Set(), new Set()];
for (let read of parsed.extended)
uniqueRead[read.antenna - 1].add(read.code);
console.log('UNIQUE READ ANTENNA:', uniqueRead[0].size, uniqueRead[1].size, uniqueRead[2].size, uniqueRead[3].size);
// WARNING IF AN ANTENNA READS LESS THAN IT SHOULD
for (const [index, set] of uniqueRead.entries()) {
if (((set.size * 100) / parsed.codes.size) < 25)
console.logger.warn(`[ANTENNA]: ${index + 1} ONLY ${set.size}`);
}
console.log('----------------------------------------------------------------');
};

1
util/locale/es.yml Normal file
View File

@ -0,0 +1 @@
TRUCK_NOT_AVAILABLE: No hay un camión disponible

8
util/translator.js Normal file
View File

@ -0,0 +1,8 @@
import yml from 'require-yml';
import path from 'path';
export default function t(expression) {
const {pathname: root} = new URL('./locale', import.meta.url);
let es = yml(path.join(root, 'es.yml')) || {};
return es[expression] || expression;
}