From 9152b0e62a2889c7d7c6528085dfb39d37ac3511 Mon Sep 17 00:00:00 2001 From: liamcottle Date: Wed, 27 Mar 2024 01:23:57 +1300 Subject: [PATCH] support custom mqtt servers --- package-lock.json | 47 +++++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + src/mqtt.js | 30 +++++++++++++++++++++++++++--- 3 files changed, 75 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 10d83eb..cf471cc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "license": "ISC", "dependencies": { "@prisma/client": "^5.11.0", + "command-line-args": "^5.2.1", "express": "^4.18.3", "mqtt": "^5.3.6", "protobufjs": "^7.2.6" @@ -198,6 +199,14 @@ "node": ">= 0.6" } }, + "node_modules/array-back": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/array-back/-/array-back-3.1.0.tgz", + "integrity": "sha512-TkuxA4UCOvxuDK6NZYXCalszEzj+TLszyASooky+i742l9TqsOdYCMJJupxRic61hwquNtppB3hgcuq9SVSH1Q==", + "engines": { + "node": ">=6" + } + }, "node_modules/array-flatten": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", @@ -310,6 +319,20 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/command-line-args": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/command-line-args/-/command-line-args-5.2.1.tgz", + "integrity": "sha512-H4UfQhZyakIjC74I9d34fGYDwk3XpSr17QhEd0Q3I9Xq1CETHo4Hcuo87WyWHpAF1aSLjLRf5lD9ZGX2qStUvg==", + "dependencies": { + "array-back": "^3.1.0", + "find-replace": "^3.0.0", + "lodash.camelcase": "^4.3.0", + "typical": "^4.0.0" + }, + "engines": { + "node": ">=4.0.0" + } + }, "node_modules/commist": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/commist/-/commist-3.2.0.tgz", @@ -546,6 +569,17 @@ "node": ">= 0.8" } }, + "node_modules/find-replace": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/find-replace/-/find-replace-3.0.0.tgz", + "integrity": "sha512-6Tb2myMioCAgv5kfvP5/PkZZ/ntTpVK39fHY7WkWBgvbeE+VHd/tZuZ4mrC+bxh4cfOZeYKVPaJIZtZXV7GNCQ==", + "dependencies": { + "array-back": "^3.0.1" + }, + "engines": { + "node": ">=4.0.0" + } + }, "node_modules/forwarded": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz", @@ -715,6 +749,11 @@ "url": "https://opencollective.com/js-sdsl" } }, + "node_modules/lodash.camelcase": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz", + "integrity": "sha512-TwuEnCnxbc3rAvhf/LbG7tJUDzhqXyFnv3dtzLOPgCG/hODL7WFnsbwktkD7yUV0RrreP/l1PALq/YSg6VvjlA==" + }, "node_modules/long": { "version": "5.2.3", "resolved": "https://registry.npmjs.org/long/-/long-5.2.3.tgz", @@ -1234,6 +1273,14 @@ "resolved": "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz", "integrity": "sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA==" }, + "node_modules/typical": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/typical/-/typical-4.0.0.tgz", + "integrity": "sha512-VAH4IvQ7BDFYglMd7BPRDfLgxZZX4O4TFcRDA6EN5X7erNJJq+McIEp8np9aVtxrCJ6qx4GTYVfOWNjcqwZgRw==", + "engines": { + "node": ">=8" + } + }, "node_modules/undici-types": { "version": "5.26.5", "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", diff --git a/package.json b/package.json index 0a8e437..6239298 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "license": "ISC", "dependencies": { "@prisma/client": "^5.11.0", + "command-line-args": "^5.2.1", "express": "^4.18.3", "mqtt": "^5.3.6", "protobufjs": "^7.2.6" diff --git a/src/mqtt.js b/src/mqtt.js index 3931e46..6f29252 100644 --- a/src/mqtt.js +++ b/src/mqtt.js @@ -2,15 +2,39 @@ const crypto = require("crypto"); const path = require("path"); const mqtt = require("mqtt"); const protobufjs = require("protobufjs"); +const commandLineArgs = require('command-line-args'); // create prisma db client const { PrismaClient } = require("@prisma/client"); const prisma = new PrismaClient(); +const options = commandLineArgs([ + { + name: "mqtt-broker-url", + type: String, + description: "MQTT Broker URL (e.g: mqtt://mqtt.meshtastic.org)", + }, + { + name: "mqtt-username", + type: String, + description: "MQTT Username (e.g: meshdev)", + }, + { + name: "mqtt-password", + type: String, + description: "MQTT Password (e.g: large4cats)", + }, +]); + +// get options and fallback to default values +const mqttBrokerUrl = options["mqtt-broker-url"] || "mqtt://mqtt.meshtastic.org"; +const mqttUsername = options["mqtt-username"] || "meshdev"; +const mqttPassword = options["mqtt-password"] || "large4cats"; + // create mqtt client -const client = mqtt.connect("mqtt://mqtt.meshtastic.org", { - username: "meshdev", - password: "large4cats", +const client = mqtt.connect(mqttBrokerUrl, { + username: mqttUsername, + password: mqttPassword, }); // load protobufs