diff --git a/prisma/migrations/20240313014910_create_map_reports_table/migration.sql b/prisma/migrations/20240313014910_create_map_reports_table/migration.sql new file mode 100644 index 0000000..57fe61c --- /dev/null +++ b/prisma/migrations/20240313014910_create_map_reports_table/migration.sql @@ -0,0 +1,25 @@ +-- CreateTable +CREATE TABLE `map_reports` ( + `id` BIGINT NOT NULL AUTO_INCREMENT, + `node_id` BIGINT NOT NULL, + `long_name` VARCHAR(191) NOT NULL, + `short_name` VARCHAR(191) NOT NULL, + `role` INTEGER NOT NULL, + `hardware_model` INTEGER NOT NULL, + `firmware_version` VARCHAR(191) NOT NULL, + `region` INTEGER NULL, + `modem_preset` INTEGER NULL, + `has_default_channel` BOOLEAN NULL, + `latitude` INTEGER NULL, + `longitude` INTEGER NULL, + `altitude` INTEGER NULL, + `position_precision` INTEGER NULL, + `num_online_local_nodes` INTEGER NULL, + `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), + `updated_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), + + INDEX `map_reports_created_at_idx`(`created_at`), + INDEX `map_reports_updated_at_idx`(`updated_at`), + INDEX `map_reports_node_id_idx`(`node_id`), + PRIMARY KEY (`id`) +) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index d6ac5b1..4d17073 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -39,6 +39,33 @@ model Node { @@map("nodes") } +model MapReport { + id BigInt @id @default(autoincrement()) + node_id BigInt + long_name String + short_name String + role Int + hardware_model Int + firmware_version String + + region Int? + modem_preset Int? + has_default_channel Boolean? + latitude Int? + longitude Int? + altitude Int? + position_precision Int? + num_online_local_nodes Int? + + created_at DateTime @default(now()) + updated_at DateTime @default(now()) @updatedAt + + @@index(created_at) + @@index(updated_at) + @@index(node_id) + @@map("map_reports") +} + model NeighbourInfo { id BigInt @id @default(autoincrement()) node_id BigInt diff --git a/src/mqtt.js b/src/mqtt.js index dfd47b0..469aa45 100644 --- a/src/mqtt.js +++ b/src/mqtt.js @@ -19,6 +19,7 @@ root.resolvePath = (origin, target) => path.join(__dirname, "protos", target); root.loadSync('meshtastic/mqtt.proto'); const Data = root.lookupType("Data"); const ServiceEnvelope = root.lookupType("ServiceEnvelope"); +const MapReport = root.lookupType("MapReport"); const NeighborInfo = root.lookupType("NeighborInfo"); const Position = root.lookupType("Position"); const RouteDiscovery = root.lookupType("RouteDiscovery"); @@ -94,16 +95,20 @@ client.on("message", async (topic, message) => { } } + const logKnownPacketTypes = false; + const logUnknownPacketTypes = false; const portnum = envelope.packet?.decoded?.portnum; if(portnum === 3) { const position = Position.decode(envelope.packet.decoded.payload); - console.log("POSITION_APP", { - from: envelope.packet.from.toString(16), - position: position, - }); + if(logKnownPacketTypes){ + console.log("POSITION_APP", { + from: envelope.packet.from.toString(16), + position: position, + }); + } // update node position in db if(position.latitudeI != null && position.longitudeI){ @@ -129,10 +134,12 @@ client.on("message", async (topic, message) => { const user = User.decode(envelope.packet.decoded.payload); - console.log("NODEINFO_APP", { - from: envelope.packet.from.toString(16), - user: user, - }); + if(logKnownPacketTypes) { + console.log("NODEINFO_APP", { + from: envelope.packet.from.toString(16), + user: user, + }); + } // create or update node in db try { @@ -166,10 +173,12 @@ client.on("message", async (topic, message) => { const neighbourInfo = NeighborInfo.decode(envelope.packet.decoded.payload); - console.log("NEIGHBORINFO_APP", { - from: envelope.packet.from.toString(16), - neighbour_info: neighbourInfo, - }); + if(logKnownPacketTypes) { + console.log("NEIGHBORINFO_APP", { + from: envelope.packet.from.toString(16), + neighbour_info: neighbourInfo, + }); + } try { await prisma.neighbourInfo.create({ @@ -194,10 +203,12 @@ client.on("message", async (topic, message) => { const telemetry = Telemetry.decode(envelope.packet.decoded.payload); - console.log("TELEMETRY_APP", { - from: envelope.packet.from.toString(16), - telemetry: telemetry, - }); + if(logKnownPacketTypes) { + console.log("TELEMETRY_APP", { + from: envelope.packet.from.toString(16), + telemetry: telemetry, + }); + } // data to update const data = {}; @@ -230,10 +241,12 @@ client.on("message", async (topic, message) => { const routeDiscovery = RouteDiscovery.decode(envelope.packet.decoded.payload); - console.log("TRACEROUTE_APP", { - from: envelope.packet.from.toString(16), - route_discovery: routeDiscovery, - }); + if(logKnownPacketTypes) { + console.log("TRACEROUTE_APP", { + from: envelope.packet.from.toString(16), + route_discovery: routeDiscovery, + }); + } try { await prisma.traceRoute.create({ @@ -248,8 +261,46 @@ client.on("message", async (topic, message) => { } + else if(portnum === 73) { + + const mapReport = MapReport.decode(envelope.packet.decoded.payload); + + if(logKnownPacketTypes) { + console.log("MAP_REPORT_APP", { + from: envelope.packet.from.toString(16), + map_report: mapReport, + }); + } + + try { + await prisma.mapReport.create({ + data: { + node_id: envelope.packet.from, + long_name: mapReport.longName, + short_name: mapReport.shortName, + role: mapReport.role, + hardware_model: mapReport.hwModel, + firmware_version: mapReport.firmwareVersion, + region: mapReport.region, + modem_preset: mapReport.modemPreset, + has_default_channel: mapReport.hasDefaultChannel, + latitude: mapReport.latitudeI, + longitude: mapReport.longitudeI, + altitude: mapReport.altitude, + position_precision: mapReport.positionPrecision, + num_online_local_nodes: mapReport.numOnlineLocalNodes, + }, + }); + } catch (e) { + console.error(e); + } + + } + else { - // console.log(portnum, envelope); + if(logUnknownPacketTypes){ + console.log(portnum, envelope); + } } } catch(e) {