From 94296c23b94095a5c64e1715bdbf142c5fb57cc0 Mon Sep 17 00:00:00 2001 From: alexm Date: Thu, 2 Feb 2023 14:51:25 +0100 Subject: [PATCH] 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('----------------------------------------------------------------');