Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Fix metric names #17

Merged
merged 2 commits into from
Aug 2, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions lib/mqttclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,19 @@ export default class MQTTClient {

if (value === null) return;

// Get the value after the last /
let metricName = birth.name.split('/').pop();

// Get the path as everything behind the last /
let path = birth.name.substring(0, birth.name.lastIndexOf("/"));

writeApi.useDefaultTags({
instance: birth.instance,
schema: birth.schema,
group: topic.address.group,
node: topic.address.node,
device: topic.address.device
device: topic.address.device,
path: path,
});


Expand All @@ -309,10 +316,10 @@ export default class MQTTClient {
// Validate
numVal = Number(value);
if (!Number.isInteger(numVal)) {
logger.warn(`${topic.address}/${birth.name} should be a ${birth.type} but received ${numVal}. Not recording.`);
logger.warn(`${topic.address}/${path}/${metricName} should be a ${birth.type} but received ${numVal}. Not recording.`);
return;
}
writeApi.writePoint(new Point(birth.name).intField('value', numVal));
writeApi.writePoint(new Point(`${metricName}_i`).intField('value', numVal));
break;
case "UInt8":
case "UInt16":
Expand All @@ -321,37 +328,37 @@ export default class MQTTClient {
// Validate
numVal = Number(value);
if (!Number.isInteger(numVal)) {
logger.warn(`${topic.address}/${birth.name} should be a ${birth.type} but received ${numVal}. Not recording.`);
logger.warn(`${topic.address}/${path}/${metricName} should be a ${birth.type} but received ${numVal}. Not recording.`);
return;
}
writeApi.writePoint(new Point(birth.name).uintField('value', numVal));
writeApi.writePoint(new Point(`${metricName}_u`).uintField('value', numVal));
break;
case "Float":
case "Double":
// Validate
numVal = Number(value);
if (isNaN(parseFloat(numVal))) {
logger.warn(`${topic.address}/${birth.name} should be a ${birth.type} but received ${numVal}. Not recording.`);
logger.warn(`${topic.address}/${path}/${metricName} should be a ${birth.type} but received ${numVal}. Not recording.`);
return;
}
writeApi.writePoint(new Point(birth.name).floatField('value', numVal));
writeApi.writePoint(new Point(`${metricName}_d`).floatField('value', numVal));
break;
case "Boolean":
if (typeof value != "boolean") {
logger.warn(`${topic.address}/${birth.name} should be a ${birth.type} but received ${value}. Not recording.`);
logger.warn(`${topic.address}/${path}/${metricName} should be a ${birth.type} but received ${value}. Not recording.`);
return;
}
writeApi.writePoint(new Point(birth.name).booleanField('value', value));
writeApi.writePoint(new Point(`${metricName}_b`).booleanField('value', value));
break;
default:
writeApi.writePoint(new Point(birth.name).stringField('value', value));
writeApi.writePoint(new Point(`${metricName}_s`).stringField('value', value));
break;

}

i++;

logger.debug(`Added to write buffer (${i}/${batchSize}): [${birth.type}] ${topic.address}/${birth.name} = ${value}`);
logger.debug(`Added to write buffer (${i}/${batchSize}): [${birth.type}] ${topic.address}/${path}/${metricName} = ${value}`);

if (i >= batchSize) {
this.flushBuffer(`${batchSize} point BATCH`);
Expand Down
Loading