Support config-driven output formatting

This commit is contained in:
Rene De Ren
2026-03-12 16:13:39 +01:00
parent 31928fd124
commit 814ee3d763
6 changed files with 135 additions and 51 deletions

View File

@@ -1,12 +1,13 @@
const { getFormatter } = require('./formatters');
//this class will handle the output events for the node red node
class OutputUtils {
constructor() {
this.output ={};
this.output['influxdb'] = {};
this.output['process'] = {};
this.output = {};
}
checkForChanges(output, format) {
this.output[format] = this.output[format] || {};
const changedFields = {};
for (const key in output) {
if (Object.prototype.hasOwnProperty.call(output, key) && output[key] !== this.output[format][key]) {
@@ -27,55 +28,43 @@ class OutputUtils {
}
formatMsg(output, config, format) {
//define emtpy message
let msg = {};
// Compare output with last output and only include changed values
const changedFields = this.checkForChanges(output,format);
if (Object.keys(changedFields).length > 0) {
switch (format) {
case 'influxdb': {
// Extract the relevant config properties.
const relevantConfig = this.extractRelevantConfig(config);
// Flatten the tags so that no nested objects are passed on.
const flatTags = this.flattenTags(relevantConfig);
msg = this.influxDBFormat(changedFields, config, flatTags);
break;
}
case 'process':
// Compare output with last output and only include changed values
msg = this.processFormat(changedFields,config);
//console.log(msg);
break;
default:
console.log('Unknown format in output utils');
break;
}
const measurement = config.general.name;
const flatTags = this.flattenTags(this.extractRelevantConfig(config));
const formatterName = this.resolveFormatterName(config, format);
const formatter = getFormatter(formatterName);
const payload = formatter.format(measurement, {
fields: changedFields,
tags: flatTags,
config,
channel: format,
});
msg = this.wrapMessage(measurement, payload);
return msg;
}
}
influxDBFormat(changedFields, config , flatTags) {
// Create the measurement and topic using softwareType and name config.functionality.softwareType + .
const measurement = config.general.name;
const payload = {
measurement: measurement,
fields: changedFields,
tags: flatTags,
timestamp: new Date(),
resolveFormatterName(config, channel) {
const outputConfig = config.output || {};
if (channel === 'process') {
return outputConfig.process || 'process';
}
if (channel === 'influxdb') {
return outputConfig.dbase || 'influxdb';
}
return outputConfig[channel] || channel;
}
wrapMessage(measurement, payload) {
return {
topic: measurement,
payload,
};
const topic = measurement;
const msg = { topic: topic, payload: payload };
return msg;
}
flattenTags(obj) {
@@ -119,15 +108,6 @@ class OutputUtils {
model: config.asset?.model,
};
}
processFormat(changedFields,config) {
// Create the measurement and topic using softwareType and name config.functionality.softwareType + .
const measurement = config.general.name;
const payload = changedFields;
const topic = measurement;
const msg = { topic: topic, payload: payload };
return msg;
}
}
module.exports = OutputUtils;