From 8142dfb43003b9a0cee1dc018201d1ff13cd48b1 Mon Sep 17 00:00:00 2001 From: alexm Date: Fri, 27 Jan 2023 12:40:09 +0100 Subject: [PATCH] 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('----------------------------------------------------------------'); +};