From 8142dfb43003b9a0cee1dc018201d1ff13cd48b1 Mon Sep 17 00:00:00 2001 From: alexm Date: Fri, 27 Jan 2023 12:40:09 +0100 Subject: [PATCH 1/7] feat: add counter refs #5130 --- config.yml | 1 + src/counter.js | 9 ++++++++ src/newPallet.js | 4 ---- src/rfidParser.js | 7 ++++-- src/stream.js | 52 ++++++++++++++------------------------------- util/debugStream.js | 26 +++++++++++++++++++++++ 6 files changed, 57 insertions(+), 42 deletions(-) create mode 100644 src/counter.js create mode 100644 util/debugStream.js diff --git a/config.yml b/config.yml index 007d574..c7d9c04 100644 --- a/config.yml +++ b/config.yml @@ -4,6 +4,7 @@ ip: 1.2.3.4 env: dev interval: 1000 reconnectInterval: 5000 +counterInterval: 1000 db: host: host port: 3307 diff --git a/src/counter.js b/src/counter.js new file mode 100644 index 0000000..679264d --- /dev/null +++ b/src/counter.js @@ -0,0 +1,9 @@ +import con from '../db/connect.js'; + +export default async(size, 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..537a6c3 100644 --- a/src/newPallet.js +++ b/src/newPallet.js @@ -1,10 +1,6 @@ import con from '../db/connect.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]); diff --git a/src/rfidParser.js b/src/rfidParser.js index d58518e..1b960c5 100644 --- a/src/rfidParser.js +++ b/src/rfidParser.js @@ -2,7 +2,9 @@ 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); @@ -21,9 +23,10 @@ export default async data => { antenna: jsonResult.tagInventoryEvent.antennaPort }; + rfidsParsed.add(rfidParsed.code); rfidsParsed.push(rfidParsed); } } - return rfidsParsed; + return {codes: rfidsParsed, extended: rfidsParsedExtended}; }; diff --git a/src/stream.js b/src/stream.js index 10a3256..131459a 100644 --- a/src/stream.js +++ b/src/stream.js @@ -1,28 +1,30 @@ import got from 'got'; import rfidParser from './rfidParser.js'; import newPallet from './newPallet.js'; +import debug from '../util/streamDebug.js'; +import counter from './counter.js'; let interval; export default async(conf, cb) => { - let rfidbuffer = []; - let rfidbufferSet = [new Set(), new Set(), new Set(), new Set()]; + let rfidbuffer = new Set(); + let rfidbufferExtend = []; + const counterInterval = setInterval(counter, conf.counterInterval, rfidbuffer.size, conf.arcId); const stream = got.stream(`http://${conf.ip}/api/v1/data/stream`); stream .on('data', async value => { const parsed = await rfidParser(value); + rfidbuffer = new Set([...rfidbuffer, ...parsed.codes]); + rfidbufferExtend = rfidbufferExtend.concat(parsed.extended); - if (parsed) - rfidbuffer = rfidbuffer.concat(parsed); + debug(rfidbuffer, rfidbufferExtend); - debug(parsed); - - 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); } }) .on('error', e => { @@ -30,34 +32,12 @@ export default async(conf, cb) => { }); function createPallet() { - clearInterval(interval); // try remove - if (!rfidbuffer.length) return; + clearTimeout(interval); // try remove + clearInterval(counterInterval); // try remove + newPallet(rfidbuffer, conf.arcId); - rfidbuffer = []; - rfidbufferSet = [new Set(), new Set(), new Set(), new Set()]; - } - 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('----------------------------------------------------------------'); + rfidbuffer = new Set(); + rfidbufferExtend = []; } }; - -50; diff --git a/util/debugStream.js b/util/debugStream.js new file mode 100644 index 0000000..112a22f --- /dev/null +++ b/util/debugStream.js @@ -0,0 +1,26 @@ +export default parsed => { + 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.log('[WARNING_ANTENNA]: ', index + 1, ' ONLY ', set.size); + } + + console.log('----------------------------------------------------------------'); +}; -- 2.40.1 From ebbc27b56d162faac96205fdc5fa9b2bd1fe9bed Mon Sep 17 00:00:00 2001 From: alexm Date: Fri, 27 Jan 2023 12:45:25 +0100 Subject: [PATCH 2/7] feat: set null when stop --- src/newPallet.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/newPallet.js b/src/newPallet.js index 537a6c3..4659056 100644 --- a/src/newPallet.js +++ b/src/newPallet.js @@ -1,9 +1,11 @@ import con from '../db/connect.js'; +import counter from './counter.js'; export default async(rfids, arcId) => { console.logger.info('PRINTING...'); const palletId = await con.query(`CALL vn.expeditionPallet_build(JSON_ARRAY(?), ?, ?, @palletId);`, [Array.from(codes), arcId, null]); + await counter(null, arcId); if (!palletId) console.logger.info({error: 'ERROR_CREATING_PALLET', expeditions: rfids}); }; -- 2.40.1 From 99d6f8167147e903d03ec3d8e274bbbf68deb8b3 Mon Sep 17 00:00:00 2001 From: alexm Date: Tue, 31 Jan 2023 14:59:05 +0100 Subject: [PATCH 3/7] fix bugs --- src/newPallet.js | 8 +++++--- src/rfidParser.js | 5 +++-- src/stream.js | 5 +++-- util/debugStream.js | 3 +-- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/newPallet.js b/src/newPallet.js index 4659056..f15d537 100644 --- a/src/newPallet.js +++ b/src/newPallet.js @@ -3,9 +3,11 @@ import counter from './counter.js'; export default async(rfids, arcId) => { console.logger.info('PRINTING...'); - - const palletId = await con.query(`CALL vn.expeditionPallet_build(JSON_ARRAY(?), ?, ?, @palletId);`, [Array.from(codes), arcId, null]); + console.log([Array.from(rfids), arcId, null]); + await con.query(`CALL vn.expeditionPallet_build(JSON_ARRAY(?), ?, ?, @palletId);`, [Array.from(rfids), arcId, null]); + const palletId = await con.query(`SELECT @palletId;`); + console.log(palletId); await counter(null, arcId); if (!palletId) - console.logger.info({error: 'ERROR_CREATING_PALLET', expeditions: rfids}); + console.logger.error({error: 'ERROR_CREATING_PALLET', expeditions: rfids}); }; diff --git a/src/rfidParser.js b/src/rfidParser.js index 1b960c5..9df302e 100644 --- a/src/rfidParser.js +++ b/src/rfidParser.js @@ -10,7 +10,7 @@ export default async data => { 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', ''); @@ -23,8 +23,9 @@ export default async data => { antenna: jsonResult.tagInventoryEvent.antennaPort }; + const rfidsParsedExtended = []; + rfidsParsedExtended.push(rfidParsed); rfidsParsed.add(rfidParsed.code); - rfidsParsed.push(rfidParsed); } } diff --git a/src/stream.js b/src/stream.js index 131459a..6853ba9 100644 --- a/src/stream.js +++ b/src/stream.js @@ -1,7 +1,7 @@ import got from 'got'; import rfidParser from './rfidParser.js'; import newPallet from './newPallet.js'; -import debug from '../util/streamDebug.js'; +import debug from '../util/debugStream.js'; import counter from './counter.js'; let interval; @@ -16,10 +16,11 @@ export default async(conf, cb) => { stream .on('data', async value => { const parsed = await rfidParser(value); + console.log(parsed.codes); rfidbuffer = new Set([...rfidbuffer, ...parsed.codes]); rfidbufferExtend = rfidbufferExtend.concat(parsed.extended); - debug(rfidbuffer, rfidbufferExtend); + debug({codes: rfidbuffer, extended: rfidbufferExtend}, conf); if (rfidbuffer.size) { clearTimeout(interval); diff --git a/util/debugStream.js b/util/debugStream.js index 112a22f..d08ce80 100644 --- a/util/debugStream.js +++ b/util/debugStream.js @@ -1,6 +1,5 @@ -export default parsed => { +export default (parsed, conf) => { if (conf.env != 'dev') return; - // TOTAL console.log('TOTAL BUFFER: ', parsed.codes.size); -- 2.40.1 From 94296c23b94095a5c64e1715bdbf142c5fb57cc0 Mon Sep 17 00:00:00 2001 From: alexm Date: Thu, 2 Feb 2023 14:51:25 +0100 Subject: [PATCH 4/7] fix: update counter --- config.yml | 2 +- package.json | 2 +- src/counter.js | 1 + src/newPallet.js | 4 ++-- src/stream.js | 12 ++++++++++-- util/debugStream.js | 2 +- 6 files changed, 16 insertions(+), 7 deletions(-) diff --git a/config.yml b/config.yml index c7d9c04..c0c66d5 100644 --- a/config.yml +++ b/config.yml @@ -2,7 +2,7 @@ arcId: 1 port: 1234 ip: 1.2.3.4 env: dev -interval: 1000 +interval: 3000 reconnectInterval: 5000 counterInterval: 1000 db: 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 index 679264d..05c5aaa 100644 --- a/src/counter.js +++ b/src/counter.js @@ -1,6 +1,7 @@ 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 = ? diff --git a/src/newPallet.js b/src/newPallet.js index f15d537..10a9c9f 100644 --- a/src/newPallet.js +++ b/src/newPallet.js @@ -5,8 +5,8 @@ export default async(rfids, arcId) => { console.logger.info('PRINTING...'); console.log([Array.from(rfids), arcId, null]); await con.query(`CALL vn.expeditionPallet_build(JSON_ARRAY(?), ?, ?, @palletId);`, [Array.from(rfids), arcId, null]); - const palletId = await con.query(`SELECT @palletId;`); - console.log(palletId); + const [[{palletId}]] = await con.query(`SELECT @palletId as palletId;`); + await counter(null, arcId); if (!palletId) console.logger.error({error: 'ERROR_CREATING_PALLET', expeditions: rfids}); diff --git a/src/stream.js b/src/stream.js index 6853ba9..bb09003 100644 --- a/src/stream.js +++ b/src/stream.js @@ -5,18 +5,19 @@ import debug from '../util/debugStream.js'; import counter from './counter.js'; let interval; +let counterInterval; export default async(conf, cb) => { let rfidbuffer = new Set(); let rfidbufferExtend = []; - const counterInterval = setInterval(counter, conf.counterInterval, rfidbuffer.size, conf.arcId); const stream = got.stream(`http://${conf.ip}/api/v1/data/stream`); stream .on('data', async value => { const parsed = await rfidParser(value); console.log(parsed.codes); + if (!parsed.codes.size) return; rfidbuffer = new Set([...rfidbuffer, ...parsed.codes]); rfidbufferExtend = rfidbufferExtend.concat(parsed.extended); @@ -26,6 +27,9 @@ export default async(conf, cb) => { clearTimeout(interval); interval = null; interval = setTimeout(createPallet, conf.interval); + + if (!counterInterval) + counterInterval = setTimeout(counterIntervalManager, conf.counterInterval); } }) .on('error', e => { @@ -34,11 +38,15 @@ export default async(conf, cb) => { function createPallet() { clearTimeout(interval); // try remove - clearInterval(counterInterval); // try remove newPallet(rfidbuffer, conf.arcId); rfidbuffer = new Set(); rfidbufferExtend = []; } + + function counterIntervalManager() { + counterInterval = null; + counter(rfidbuffer.size, conf.arcId); + } }; diff --git a/util/debugStream.js b/util/debugStream.js index d08ce80..2a73ec4 100644 --- a/util/debugStream.js +++ b/util/debugStream.js @@ -18,7 +18,7 @@ export default (parsed, conf) => { // 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.log('[WARNING_ANTENNA]: ', index + 1, ' ONLY ', set.size); + console.logger.warn(`[ANTENNA]: ${index + 1} ONLY ${set.size}`); } console.log('----------------------------------------------------------------'); -- 2.40.1 From 00262e5dd0ad1bf2608a435aa4c8fc83564c5985 Mon Sep 17 00:00:00 2001 From: alexm Date: Fri, 3 Feb 2023 13:20:00 +0100 Subject: [PATCH 5/7] refactor newPallet and use translator --- src/newPallet.js | 15 +++++++-------- src/stream.js | 4 ++-- util/locale/es.yml | 1 + util/translator.js | 8 ++++++++ 4 files changed, 18 insertions(+), 10 deletions(-) create mode 100644 util/locale/es.yml create mode 100644 util/translator.js diff --git a/src/newPallet.js b/src/newPallet.js index 10a9c9f..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) => { - console.logger.info('PRINTING...'); - console.log([Array.from(rfids), arcId, null]); - await con.query(`CALL vn.expeditionPallet_build(JSON_ARRAY(?), ?, ?, @palletId);`, [Array.from(rfids), arcId, null]); - const [[{palletId}]] = await con.query(`SELECT @palletId as palletId;`); - - await counter(null, arcId); - if (!palletId) - console.logger.error({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/stream.js b/src/stream.js index bb09003..f8dd802 100644 --- a/src/stream.js +++ b/src/stream.js @@ -16,7 +16,7 @@ export default async(conf, cb) => { stream .on('data', async value => { const parsed = await rfidParser(value); - console.log(parsed.codes); + if (!parsed.codes.size) return; rfidbuffer = new Set([...rfidbuffer, ...parsed.codes]); rfidbufferExtend = rfidbufferExtend.concat(parsed.extended); @@ -37,7 +37,7 @@ export default async(conf, cb) => { }); function createPallet() { - clearTimeout(interval); // try remove + clearTimeout(interval); newPallet(rfidbuffer, conf.arcId); 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; +} -- 2.40.1 From 345bc47df1039937cc753d055015f4844ca16e62 Mon Sep 17 00:00:00 2001 From: alexm Date: Tue, 7 Feb 2023 15:23:25 +0100 Subject: [PATCH 6/7] feat: lastCounter --- src/counter.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/counter.js b/src/counter.js index 05c5aaa..aacc15c 100644 --- a/src/counter.js +++ b/src/counter.js @@ -1,10 +1,14 @@ 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 = ? WHERE id = ?; `, [size, arcId]); + + lastCounter = size; }; -- 2.40.1 From 87a3bc3e8fb22b724da75a0ad2bd513c5029070c Mon Sep 17 00:00:00 2001 From: alexm Date: Thu, 9 Feb 2023 15:19:42 +0100 Subject: [PATCH 7/7] set error to null when read --- src/counter.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/counter.js b/src/counter.js index aacc15c..44ab4c0 100644 --- a/src/counter.js +++ b/src/counter.js @@ -6,7 +6,8 @@ export default async(size, arcId) => { console.logger.info(`COUNTER: SIZE:${size} ARC_ID:${arcId}`); await con.query(` UPDATE vn.arcRead - SET counter = ? + SET counter = ?, + error = NULL WHERE id = ?; `, [size, arcId]); -- 2.40.1