diff --git a/config.yml b/config.yml index 007d574..c0c66d5 100644 --- a/config.yml +++ b/config.yml @@ -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 diff --git a/package.json b/package.json index 7e0a3ea..f7c88a8 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/counter.js b/src/counter.js new file mode 100644 index 0000000..05c5aaa --- /dev/null +++ b/src/counter.js @@ -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]); +}; diff --git a/src/newPallet.js b/src/newPallet.js index 40921b0..d22b624 100644 --- a/src/newPallet.js +++ b/src/newPallet.js @@ -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]); - 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); + } catch (error) { + await con.query(`UPDATE vn.arcRead SET error = ?, counter = NULL WHERE id = ?;`, [t(error.sqlMessage), arcId]); + } }; diff --git a/src/rfidParser.js b/src/rfidParser.js index d58518e..9df302e 100644 --- a/src/rfidParser.js +++ b/src/rfidParser.js @@ -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}; }; diff --git a/src/stream.js b/src/stream.js index 10a3256..f8dd802 100644 --- a/src/stream.js +++ b/src/stream.js @@ -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); - 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; diff --git a/util/debugStream.js b/util/debugStream.js new file mode 100644 index 0000000..2a73ec4 --- /dev/null +++ b/util/debugStream.js @@ -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('----------------------------------------------------------------'); +}; diff --git a/util/locale/es.yml b/util/locale/es.yml new file mode 100644 index 0000000..ea5b1c1 --- /dev/null +++ b/util/locale/es.yml @@ -0,0 +1 @@ +TRUCK_NOT_AVAILABLE: No hay un camiĆ³n disponible \ No newline at end of file diff --git a/util/translator.js b/util/translator.js new file mode 100644 index 0000000..ee55218 --- /dev/null +++ b/util/translator.js @@ -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; +}