collect device metrics telemetry

This commit is contained in:
liamcottle
2024-03-13 04:14:30 +13:00
parent 03c8215342
commit a9c129cea0
3 changed files with 47 additions and 0 deletions

View File

@ -0,0 +1,5 @@
-- AlterTable
ALTER TABLE `nodes` ADD COLUMN `air_util_tx` DECIMAL(65, 30) NULL,
ADD COLUMN `battery_level` INTEGER NULL,
ADD COLUMN `channel_utilization` DECIMAL(65, 30) NULL,
ADD COLUMN `voltage` DECIMAL(65, 30) NULL;

View File

@ -26,6 +26,11 @@ model Node {
longitude Int?
altitude Int?
battery_level Int?
voltage Decimal?
channel_utilization Decimal?
air_util_tx Decimal?
created_at DateTime @default(now())
updated_at DateTime @default(now()) @updatedAt

View File

@ -20,6 +20,7 @@ root.loadSync('meshtastic/mqtt.proto');
const Data = root.lookupType("Data");
const ServiceEnvelope = root.lookupType("ServiceEnvelope");
const Position = root.lookupType("Position");
const Telemetry = root.lookupType("Telemetry");
const User = root.lookupType("User");
function createNonce(packetId, fromNode) {
@ -159,6 +160,42 @@ client.on("message", async (topic, message) => {
}
if(portnum === 67) {
const telemetry = Telemetry.decode(envelope.packet.decoded.payload);
console.log("TELEMETRY_APP", {
from: envelope.packet.from.toString(16),
telemetry: telemetry,
});
// data to update
const data = {};
// handle device metrics
if(telemetry.deviceMetrics){
data.battery_level = telemetry.deviceMetrics.batteryLevel !== 0 ? telemetry.deviceMetrics.batteryLevel : null;
data.voltage = telemetry.deviceMetrics.voltage !== 0 ? telemetry.deviceMetrics.voltage : null;
data.channel_utilization = telemetry.deviceMetrics.channelUtilization !== 0 ? telemetry.deviceMetrics.channelUtilization : null;
data.air_util_tx = telemetry.deviceMetrics.airUtilTx !== 0 ? telemetry.deviceMetrics.airUtilTx : null;
}
// update node telemetry in db
if(Object.keys(data).length > 0){
try {
await prisma.node.updateMany({
where: {
node_id: envelope.packet.from,
},
data: data,
});
} catch (e) {
console.error(e);
}
}
}
} catch(e) {
// ignore errors
}