From 6d81475054ee51fa00049af6376c6f129926dd22 Mon Sep 17 00:00:00 2001 From: Daniel Wippermann Date: Sat, 15 Jul 2023 07:24:16 +0200 Subject: [PATCH] Copy text logging between examples. --- .../json-live-data-server/config.js.example | 24 +++++++++ examples/json-live-data-server/index.js | 53 +++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/examples/json-live-data-server/config.js.example b/examples/json-live-data-server/config.js.example index 2da37375..4b7d74c5 100644 --- a/examples/json-live-data-server/config.js.example +++ b/examples/json-live-data-server/config.js.example @@ -30,6 +30,30 @@ const config = { */ loggingFilename: path.resolve(__dirname, 'live-data.json'), + /** + * Text file logging interval in milliseconds. A value of zero disables this functionality. + */ + textLoggingInterval: 0, + + /** + * Text file logging time to live in milliseconds. + */ + textLoggingTimeToLive: 60000, + + /** + * Text file logging directory. + */ + textLoggingPath: path.resolve(__dirname, 'log'), + + /** + * Text file logging options, passed to the `TextConverter` constructor. + */ + textLoggingOptions: { + columnSeparator: '\t', + lineSeparator: '\r\n', + separateDateAndTime: false, + }, + /** * Either 'TcpConnection' or 'SerialConnection' to connect to the VBus. * @type {String} diff --git a/examples/json-live-data-server/index.js b/examples/json-live-data-server/index.js index bea2a1fe..f5c72a89 100644 --- a/examples/json-live-data-server/index.js +++ b/examples/json-live-data-server/index.js @@ -17,6 +17,7 @@ const { Specification, SerialConnection, TcpConnection, + TextConverter, VBusRecordingConverter, utils: { promisify, @@ -653,6 +654,10 @@ async function main(options) { interval: config.loggingInterval, }); + const textHeaderSetConsolidator = new HeaderSetConsolidator({ + timeToLive: config.textLoggingTimeToLive, + }); + const ConnectionClass = connectionClassByName [config.connectionClassName]; const connection = new ConnectionClass(config.connectionOptions); @@ -660,6 +665,7 @@ async function main(options) { connection.on('packet', (packet) => { headerSet.addHeader(packet); hsc.addHeader(packet); + textHeaderSetConsolidator.addHeader(packet); processEmSimulatorPacket(connection, packet); }); @@ -672,6 +678,53 @@ async function main(options) { } }); + if (config.textLoggingInterval) { + let currentDatecode = null; + + let currentConverter = null; + + const onHeaderSet = async (headerSet) => { + const datecode = spec.i18n.moment(headerSet.timestamp).format('YYYYMMDD'); + if (currentDatecode !== datecode) { + currentDatecode = datecode; + + if (currentConverter) { + currentConverter.finish(); + currentConverter = null; + } + + const filename = path.resolve(config.textLoggingPath, datecode + '.csv'); + + const file = fs.createWriteStream(filename, { flags: 'a' }); + + const options = Object.assign({}, config.textLoggingOptions, { + specification: spec, + }); + + const converter = new TextConverter(options); + converter.pipe(file); + + currentConverter = converter; + } + + if (currentConverter) { + currentConverter.convertHeaderSet(headerSet); + } + }; + + logger.debug('Starting text logging'); + + const hsc = new HeaderSetConsolidator({ + interval: config.textLoggingInterval, + }); + + hsc.on('headerSet', () => { + onHeaderSet(textHeaderSetConsolidator); + }); + + hsc.startTimer(); + } + await connection.connect(); logger.info('Ready to serve from the following URLs:');