diff --git a/prisma/migrations/20240606114257_create_power_metrics_table/migration.sql b/prisma/migrations/20240606114257_create_power_metrics_table/migration.sql new file mode 100644 index 0000000..267b228 --- /dev/null +++ b/prisma/migrations/20240606114257_create_power_metrics_table/migration.sql @@ -0,0 +1,20 @@ +-- CreateTable +CREATE TABLE `power_metrics` ( + `id` BIGINT NOT NULL AUTO_INCREMENT, + `node_id` BIGINT NOT NULL, + `packet_id` BIGINT NULL, + `ch1_voltage` DECIMAL(65, 30) NULL, + `ch1_current` DECIMAL(65, 30) NULL, + `ch2_voltage` DECIMAL(65, 30) NULL, + `ch2_current` DECIMAL(65, 30) NULL, + `ch3_voltage` DECIMAL(65, 30) NULL, + `ch3_current` DECIMAL(65, 30) NULL, + `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), + `updated_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), + + INDEX `power_metrics_created_at_idx`(`created_at`), + INDEX `power_metrics_updated_at_idx`(`updated_at`), + INDEX `power_metrics_node_id_idx`(`node_id`), + INDEX `power_metrics_packet_id_idx`(`packet_id`), + PRIMARY KEY (`id`) +) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index b5b11f9..dc7aced 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -139,6 +139,28 @@ model EnvironmentMetric { @@map("environment_metrics") } +model PowerMetric { + id BigInt @id @default(autoincrement()) + node_id BigInt + packet_id BigInt? + + ch1_voltage Decimal? + ch1_current Decimal? + ch2_voltage Decimal? + ch2_current Decimal? + ch3_voltage Decimal? + ch3_current Decimal? + + created_at DateTime @default(now()) + updated_at DateTime @default(now()) @updatedAt + + @@index(created_at) + @@index(updated_at) + @@index(node_id) + @@index(packet_id) + @@map("power_metrics") +} + model Position { id BigInt @id @default(autoincrement()) node_id BigInt diff --git a/src/mqtt.js b/src/mqtt.js index 9e475e7..67a1fc7 100644 --- a/src/mqtt.js +++ b/src/mqtt.js @@ -703,6 +703,53 @@ client.on("message", async (topic, message) => { } + // handle power metrics + if(telemetry.powerMetrics){ + + // get metric values + const ch1Voltage = telemetry.powerMetrics.ch1Voltage !== 0 ? telemetry.powerMetrics.ch1Voltage : null; + const ch1Current = telemetry.powerMetrics.ch1Current !== 0 ? telemetry.powerMetrics.ch1Current : null; + const ch2Voltage = telemetry.powerMetrics.ch2Voltage !== 0 ? telemetry.powerMetrics.ch2Voltage : null; + const ch2Current = telemetry.powerMetrics.ch2Current !== 0 ? telemetry.powerMetrics.ch2Current : null; + const ch3Voltage = telemetry.powerMetrics.ch3Voltage !== 0 ? telemetry.powerMetrics.ch3Voltage : null; + const ch3Current = telemetry.powerMetrics.ch3Current !== 0 ? telemetry.powerMetrics.ch3Current : null; + + // create power metric + try { + + // find an existing metric with duplicate information created in the last 15 seconds + const existingDuplicatePowerMetric = await prisma.powerMetric.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(!existingDuplicatePowerMetric){ + await prisma.powerMetric.create({ + data: { + node_id: envelope.packet.from, + packet_id: envelope.packet.id, + ch1_voltage: ch1Voltage, + ch1_current: ch1Current, + ch2_voltage: ch2Voltage, + ch2_current: ch2Current, + ch3_voltage: ch3Voltage, + ch3_current: ch3Current, + }, + }); + } + + } catch (e) { + console.error(e); + } + + } + // update node telemetry in db if(Object.keys(data).length > 0){ try {