diff --git a/prisma/migrations/20240331103020_change_is_licensed_column_on_nodes_table_to_be_nullable/migration.sql b/prisma/migrations/20240331103020_change_is_licensed_column_on_nodes_table_to_be_nullable/migration.sql new file mode 100644 index 0000000..d318771 --- /dev/null +++ b/prisma/migrations/20240331103020_change_is_licensed_column_on_nodes_table_to_be_nullable/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE `nodes` MODIFY `is_licensed` BOOLEAN NULL; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 754457f..5dd8a41 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -14,13 +14,13 @@ datasource db { } model Node { - id BigInt @id @default(autoincrement()) - node_id BigInt @unique + id BigInt @id @default(autoincrement()) + node_id BigInt @unique long_name String short_name String hardware_model Int - is_licensed Boolean role Int + is_licensed Boolean? latitude Int? longitude Int? diff --git a/src/mqtt.js b/src/mqtt.js index f330c10..e07b8d4 100644 --- a/src/mqtt.js +++ b/src/mqtt.js @@ -603,10 +603,6 @@ client.on("message", async (topic, message) => { else if(portnum === 73) { - if(!collectMapReports){ - return; - } - const mapReport = MapReport.decode(envelope.packet.decoded.payload); if(logKnownPacketTypes) { @@ -616,6 +612,38 @@ client.on("message", async (topic, message) => { }); } + // create or update node in db + try { + + // data to set on node + const data = { + long_name: mapReport.longName, + short_name: mapReport.shortName, + hardware_model: mapReport.hwModel, + role: mapReport.role, + latitude: mapReport.latitudeI, + longitude: mapReport.longitudeI, + altitude: mapReport.altitude !== 0 ? mapReport.altitude : null, + }; + + await prisma.node.upsert({ + where: { + node_id: envelope.packet.from, + }, + create: { + node_id: envelope.packet.from, + ...data, + }, + update: data, + }); + } catch (e) { + console.error(e); + } + + if(!collectMapReports){ + return; + } + try { // find an existing map with duplicate information created in the last 60 seconds