collect environment metrics
This commit is contained in:
@ -0,0 +1,21 @@
|
||||
-- CreateTable
|
||||
CREATE TABLE `environment_metrics` (
|
||||
`id` BIGINT NOT NULL AUTO_INCREMENT,
|
||||
`node_id` BIGINT NOT NULL,
|
||||
`packet_id` BIGINT NULL,
|
||||
`temperature` DECIMAL(65, 30) NULL,
|
||||
`relative_humidity` DECIMAL(65, 30) NULL,
|
||||
`barometric_pressure` DECIMAL(65, 30) NULL,
|
||||
`gas_resistance` DECIMAL(65, 30) NULL,
|
||||
`voltage` DECIMAL(65, 30) NULL,
|
||||
`current` DECIMAL(65, 30) NULL,
|
||||
`iaq` INTEGER NULL,
|
||||
`created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updated_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
|
||||
INDEX `environment_metrics_created_at_idx`(`created_at`),
|
||||
INDEX `environment_metrics_updated_at_idx`(`updated_at`),
|
||||
INDEX `environment_metrics_node_id_idx`(`node_id`),
|
||||
INDEX `environment_metrics_packet_id_idx`(`packet_id`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
@ -116,6 +116,29 @@ model DeviceMetric {
|
||||
@@map("device_metrics")
|
||||
}
|
||||
|
||||
model EnvironmentMetric {
|
||||
id BigInt @id @default(autoincrement())
|
||||
node_id BigInt
|
||||
packet_id BigInt?
|
||||
|
||||
temperature Decimal?
|
||||
relative_humidity Decimal?
|
||||
barometric_pressure Decimal?
|
||||
gas_resistance Decimal?
|
||||
voltage Decimal?
|
||||
current Decimal?
|
||||
iaq Int?
|
||||
|
||||
created_at DateTime @default(now())
|
||||
updated_at DateTime @default(now()) @updatedAt
|
||||
|
||||
@@index(created_at)
|
||||
@@index(updated_at)
|
||||
@@index(node_id)
|
||||
@@index(packet_id)
|
||||
@@map("environment_metrics")
|
||||
}
|
||||
|
||||
model Position {
|
||||
id BigInt @id @default(autoincrement())
|
||||
node_id BigInt
|
||||
|
49
src/mqtt.js
49
src/mqtt.js
@ -654,6 +654,55 @@ client.on("message", async (topic, message) => {
|
||||
|
||||
}
|
||||
|
||||
// handle environment metrics
|
||||
if(telemetry.environmentMetrics){
|
||||
|
||||
// get metric values
|
||||
const temperature = telemetry.environmentMetrics.temperature !== 0 ? telemetry.environmentMetrics.temperature : null;
|
||||
const relativeHumidity = telemetry.environmentMetrics.relativeHumidity !== 0 ? telemetry.environmentMetrics.relativeHumidity : null;
|
||||
const barometricPressure = telemetry.environmentMetrics.barometricPressure !== 0 ? telemetry.environmentMetrics.barometricPressure : null;
|
||||
const gasResistance = telemetry.environmentMetrics.gasResistance !== 0 ? telemetry.environmentMetrics.gasResistance : null;
|
||||
const voltage = telemetry.environmentMetrics.voltage !== 0 ? telemetry.environmentMetrics.voltage : null;
|
||||
const current = telemetry.environmentMetrics.current !== 0 ? telemetry.environmentMetrics.current : null;
|
||||
const iaq = telemetry.environmentMetrics.iaq !== 0 ? telemetry.environmentMetrics.iaq : null;
|
||||
|
||||
// create environment metric
|
||||
try {
|
||||
|
||||
// find an existing metric with duplicate information created in the last 15 seconds
|
||||
const existingDuplicateEnvironmentMetric = await prisma.environmentMetric.findFirst({
|
||||
where: {
|
||||
node_id: envelope.packet.from,
|
||||
packet_id: envelope.packet.id,
|
||||
created_at: {
|
||||
gte: new Date(Date.now() - 15000), // created in the last 15 seconds
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
// create metric if no duplicates found
|
||||
if(!existingDuplicateEnvironmentMetric){
|
||||
await prisma.environmentMetric.create({
|
||||
data: {
|
||||
node_id: envelope.packet.from,
|
||||
packet_id: envelope.packet.id,
|
||||
temperature: temperature,
|
||||
relative_humidity: relativeHumidity,
|
||||
barometric_pressure: barometricPressure,
|
||||
gas_resistance: gasResistance,
|
||||
voltage: voltage,
|
||||
current: current,
|
||||
iaq: iaq,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// update node telemetry in db
|
||||
if(Object.keys(data).length > 0){
|
||||
try {
|
||||
|
Reference in New Issue
Block a user