Skip to content

Commit

Permalink
Copy text logging between examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielwippermann committed Jul 15, 2023
1 parent 83024c8 commit 6d81475
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
24 changes: 24 additions & 0 deletions examples/json-live-data-server/config.js.example
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
53 changes: 53 additions & 0 deletions examples/json-live-data-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const {
Specification,
SerialConnection,
TcpConnection,
TextConverter,
VBusRecordingConverter,
utils: {
promisify,
Expand Down Expand Up @@ -653,13 +654,18 @@ 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);

connection.on('packet', (packet) => {
headerSet.addHeader(packet);
hsc.addHeader(packet);
textHeaderSetConsolidator.addHeader(packet);
processEmSimulatorPacket(connection, packet);
});

Expand All @@ -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:');
Expand Down

0 comments on commit 6d81475

Please sign in to comment.