5130-arc_counter #2

Merged
alexm merged 5 commits from 5130-arc_counter into dev 2023-02-06 08:59:03 +00:00
9 changed files with 86 additions and 49 deletions

View File

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

View File

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

10
src/counter.js Normal file
View File

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

View File

@ -1,13 +1,12 @@
import con from '../db/connect.js';
import counter from './counter.js';
import t from '../util/translator.js';
export default async(rfids, arcId) => {
const codes = new Set();
for (let rfid of rfids)
codes.add(rfid.code);
console.logger.info('PRINTING...');
const palletId = await con.query(`CALL vn.expeditionPallet_build(JSON_ARRAY(?), ?, ?, @palletId);`, [Array.from(codes), arcId, null]);
alexm marked this conversation as resolved Outdated
Outdated
Review

Que traduzca el error tambien

Que traduzca el error tambien
if (!palletId)
console.logger.info({error: 'ERROR_CREATING_PALLET', expeditions: rfids});
try {
await con.query(`CALL vn.expeditionPallet_build(JSON_ARRAY(?), ?, ?, @palletId);`, [Array.from(rfids), arcId, null]);
await counter(null, arcId);
alexm marked this conversation as resolved Outdated
Outdated
Review

No fa falta seleccionar el id pallet per saber si ha donat error, deuría de ficarse un try { } catch ... en la crida a vn.expeditionPallet_build

No fa falta seleccionar el id pallet per saber si ha donat error, deuría de ficarse un `try { } catch ...` en la crida a `vn.expeditionPallet_build`
} catch (error) {
await con.query(`UPDATE vn.arcRead SET error = ?, counter = NULL WHERE id = ?;`, [t(error.sqlMessage), arcId]);
}
};
alexm marked this conversation as resolved Outdated
Outdated
Review

Ficar dins del catch anterior

Ficar dins del catch anterior

View File

@ -2,13 +2,15 @@ export default async data => {
data = data.toString();
const crudeRfids = data.split('\n');
const rfidsParsed = [];
const rfidsParsed = new Set();
const rfidsParsedExtended = [];
for (let crudeRfid of crudeRfids) {
if (crudeRfid && /{.*:{.*:.*}}/.test(crudeRfid)) {
const jsonResult = JSON.parse(crudeRfid);
let epcHex = jsonResult?.tagInventoryEvent?.epcHex;
if (!epcHex) return;
if (!epcHex) continue;
if (epcHex.search('AABB') == -1) continue;
epcHex = epcHex.replace('AABB', '');
@ -21,9 +23,11 @@ export default async data => {
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 rfidParser from './rfidParser.js';
import newPallet from './newPallet.js';
import debug from '../util/debugStream.js';
import counter from './counter.js';
let interval;
let counterInterval;
export default async(conf, cb) => {
let rfidbuffer = [];
let rfidbufferSet = [new Set(), new Set(), new Set(), new Set()];
let rfidbuffer = new Set();
let rfidbufferExtend = [];
const stream = got.stream(`http://${conf.ip}/api/v1/data/stream`);
@ -14,15 +17,19 @@ export default async(conf, cb) => {
.on('data', async value => {
const parsed = await rfidParser(value);
alexm marked this conversation as resolved Outdated
Outdated
Review

Has oblidat llevar este console.log? Plenara el log del servidor de ruido

Has oblidat llevar este console.log? Plenara el log del servidor de ruido
if (parsed)
rfidbuffer = rfidbuffer.concat(parsed);
if (!parsed.codes.size) return;
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) {
clearInterval(interval);
if (rfidbuffer.size) {
clearTimeout(interval);
interval = null;
interval = setInterval(createPallet, conf.interval);
interval = setTimeout(createPallet, conf.interval);
if (!counterInterval)
counterInterval = setTimeout(counterIntervalManager, conf.counterInterval);
}
})
.on('error', e => {
@ -30,34 +37,16 @@ export default async(conf, cb) => {
});
function createPallet() {
clearInterval(interval); // try remove
if (!rfidbuffer.length) return;
clearTimeout(interval);
newPallet(rfidbuffer, conf.arcId);
rfidbuffer = [];
rfidbufferSet = [new Set(), new Set(), new Set(), new Set()];
rfidbuffer = new Set();
rfidbufferExtend = [];
}
function debug(parsed) {
if (conf.env != 'dev') return;
let totalBuffer = rfidbuffer.map(rfid => rfid.code);
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('----------------------------------------------------------------');
function counterIntervalManager() {
counterInterval = null;
counter(rfidbuffer.size, conf.arcId);
}
};
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;
}