Use mqtt directly instead of simple-mqtt-client
This commit is contained in:
parent
42841d07e8
commit
5aa990b421
3 changed files with 133 additions and 33 deletions
116
server/package-lock.json
generated
116
server/package-lock.json
generated
|
@ -29,6 +29,14 @@
|
||||||
"resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.0.tgz",
|
||||||
"integrity": "sha512-jp/uFnooOiO+L211eZOoSyzpOITMXx1rBITauYykG3BRYPu8h0UcxsPNB04RR5vo4Tyz3+ay17tR6JVf9qzYWg=="
|
"integrity": "sha512-jp/uFnooOiO+L211eZOoSyzpOITMXx1rBITauYykG3BRYPu8h0UcxsPNB04RR5vo4Tyz3+ay17tR6JVf9qzYWg=="
|
||||||
},
|
},
|
||||||
|
"async-mqtt": {
|
||||||
|
"version": "2.3.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/async-mqtt/-/async-mqtt-2.3.0.tgz",
|
||||||
|
"integrity": "sha512-4CopFTWjwgPMK0iG+xI+/cfmJ60U5dnRqaUpgMW6jE7x1PJgQAn0YnjKTUOCh4i54VLPZGX+YrRQ2mDrvZYnJQ==",
|
||||||
|
"requires": {
|
||||||
|
"mqtt": "^2.3.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
"awilix": {
|
"awilix": {
|
||||||
"version": "4.2.2",
|
"version": "4.2.2",
|
||||||
"resolved": "https://registry.npmjs.org/awilix/-/awilix-4.2.2.tgz",
|
"resolved": "https://registry.npmjs.org/awilix/-/awilix-4.2.2.tgz",
|
||||||
|
@ -129,11 +137,12 @@
|
||||||
"integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac="
|
"integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac="
|
||||||
},
|
},
|
||||||
"d": {
|
"d": {
|
||||||
"version": "1.0.0",
|
"version": "1.0.1",
|
||||||
"resolved": "https://registry.npmjs.org/d/-/d-1.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/d/-/d-1.0.1.tgz",
|
||||||
"integrity": "sha1-dUu1v+VUUdpppYuU1F9MWwRi1Y8=",
|
"integrity": "sha512-m62ShEObQ39CfralilEQRjH6oAMtNCV1xJyEx5LpRYUVN+EviphDgUc/F3hnYbADmkiNs67Y+3ylmlG7Lnu+FA==",
|
||||||
"requires": {
|
"requires": {
|
||||||
"es5-ext": "^0.10.9"
|
"es5-ext": "^0.10.50",
|
||||||
|
"type": "^1.0.1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"debug": {
|
"debug": {
|
||||||
|
@ -422,10 +431,50 @@
|
||||||
"ms": "^2.1.1"
|
"ms": "^2.1.1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"mqtt": {
|
||||||
|
"version": "2.18.8",
|
||||||
|
"resolved": "https://registry.npmjs.org/mqtt/-/mqtt-2.18.8.tgz",
|
||||||
|
"integrity": "sha512-3h6oHlPY/yWwtC2J3geraYRtVVoRM6wdI+uchF4nvSSafXPZnaKqF8xnX+S22SU/FcgEAgockVIlOaAX3fkMpA==",
|
||||||
|
"requires": {
|
||||||
|
"commist": "^1.0.0",
|
||||||
|
"concat-stream": "^1.6.2",
|
||||||
|
"end-of-stream": "^1.4.1",
|
||||||
|
"es6-map": "^0.1.5",
|
||||||
|
"help-me": "^1.0.1",
|
||||||
|
"inherits": "^2.0.3",
|
||||||
|
"minimist": "^1.2.0",
|
||||||
|
"mqtt-packet": "^5.6.0",
|
||||||
|
"pump": "^3.0.0",
|
||||||
|
"readable-stream": "^2.3.6",
|
||||||
|
"reinterval": "^1.1.0",
|
||||||
|
"split2": "^2.1.1",
|
||||||
|
"websocket-stream": "^5.1.2",
|
||||||
|
"xtend": "^4.0.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"mqtt-packet": {
|
||||||
|
"version": "5.6.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/mqtt-packet/-/mqtt-packet-5.6.1.tgz",
|
||||||
|
"integrity": "sha512-eaF9rO2uFrIYEHomJxziuKTDkbWW5psLBaIGCazQSKqYsTaB3n4SpvJ1PexKaDBiPnMLPIFWBIiTYT3IfEJfww==",
|
||||||
|
"requires": {
|
||||||
|
"bl": "^1.2.1",
|
||||||
|
"inherits": "^2.0.3",
|
||||||
|
"process-nextick-args": "^2.0.0",
|
||||||
|
"safe-buffer": "^5.1.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"ms": {
|
"ms": {
|
||||||
"version": "2.1.2",
|
"version": "2.1.2",
|
||||||
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
|
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
|
||||||
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
|
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
|
||||||
|
},
|
||||||
|
"split2": {
|
||||||
|
"version": "2.2.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/split2/-/split2-2.2.0.tgz",
|
||||||
|
"integrity": "sha512-RAb22TG39LhI31MbreBgIuKiIKhVsawfTgEGqKHTK87aG+ul/PB8Sqoi3I7kVdRWiCfrKxK3uo4/YUkpNvhPbw==",
|
||||||
|
"requires": {
|
||||||
|
"through2": "^2.0.2"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -551,9 +600,9 @@
|
||||||
"integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18="
|
"integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18="
|
||||||
},
|
},
|
||||||
"process-nextick-args": {
|
"process-nextick-args": {
|
||||||
"version": "2.0.0",
|
"version": "2.0.1",
|
||||||
"resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz",
|
||||||
"integrity": "sha512-MtEC1TqN0EU5nephaJ4rAtThHtC86dNN9qCuEhtshvpVBkAW5ZO7BASN9REnF9eoXGcRub+pFuKEpOHE+HbEMw=="
|
"integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag=="
|
||||||
},
|
},
|
||||||
"pump": {
|
"pump": {
|
||||||
"version": "3.0.0",
|
"version": "3.0.0",
|
||||||
|
@ -622,6 +671,48 @@
|
||||||
"manuh": "^1.4.3",
|
"manuh": "^1.4.3",
|
||||||
"manuh-bridge": "^0.0.2",
|
"manuh-bridge": "^0.0.2",
|
||||||
"mqtt": "^2.18.8"
|
"mqtt": "^2.18.8"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"mqtt": {
|
||||||
|
"version": "2.18.8",
|
||||||
|
"resolved": "https://registry.npmjs.org/mqtt/-/mqtt-2.18.8.tgz",
|
||||||
|
"integrity": "sha512-3h6oHlPY/yWwtC2J3geraYRtVVoRM6wdI+uchF4nvSSafXPZnaKqF8xnX+S22SU/FcgEAgockVIlOaAX3fkMpA==",
|
||||||
|
"requires": {
|
||||||
|
"commist": "^1.0.0",
|
||||||
|
"concat-stream": "^1.6.2",
|
||||||
|
"end-of-stream": "^1.4.1",
|
||||||
|
"es6-map": "^0.1.5",
|
||||||
|
"help-me": "^1.0.1",
|
||||||
|
"inherits": "^2.0.3",
|
||||||
|
"minimist": "^1.2.0",
|
||||||
|
"mqtt-packet": "^5.6.0",
|
||||||
|
"pump": "^3.0.0",
|
||||||
|
"readable-stream": "^2.3.6",
|
||||||
|
"reinterval": "^1.1.0",
|
||||||
|
"split2": "^2.1.1",
|
||||||
|
"websocket-stream": "^5.1.2",
|
||||||
|
"xtend": "^4.0.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"mqtt-packet": {
|
||||||
|
"version": "5.6.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/mqtt-packet/-/mqtt-packet-5.6.1.tgz",
|
||||||
|
"integrity": "sha512-eaF9rO2uFrIYEHomJxziuKTDkbWW5psLBaIGCazQSKqYsTaB3n4SpvJ1PexKaDBiPnMLPIFWBIiTYT3IfEJfww==",
|
||||||
|
"requires": {
|
||||||
|
"bl": "^1.2.1",
|
||||||
|
"inherits": "^2.0.3",
|
||||||
|
"process-nextick-args": "^2.0.0",
|
||||||
|
"safe-buffer": "^5.1.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"split2": {
|
||||||
|
"version": "2.2.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/split2/-/split2-2.2.0.tgz",
|
||||||
|
"integrity": "sha512-RAb22TG39LhI31MbreBgIuKiIKhVsawfTgEGqKHTK87aG+ul/PB8Sqoi3I7kVdRWiCfrKxK3uo4/YUkpNvhPbw==",
|
||||||
|
"requires": {
|
||||||
|
"through2": "^2.0.2"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"split2": {
|
"split2": {
|
||||||
|
@ -686,6 +777,11 @@
|
||||||
"is-negated-glob": "^1.0.0"
|
"is-negated-glob": "^1.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"type": {
|
||||||
|
"version": "1.0.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/type/-/type-1.0.1.tgz",
|
||||||
|
"integrity": "sha512-MAM5dBMJCJNKs9E7JXo4CXRAansRfG0nlJxW7Wf6GZzSOvH31zClSaHdIMWLehe/EGMBkqeC55rrkaOr5Oo7Nw=="
|
||||||
|
},
|
||||||
"typedarray": {
|
"typedarray": {
|
||||||
"version": "0.0.6",
|
"version": "0.0.6",
|
||||||
"resolved": "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz",
|
"resolved": "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz",
|
||||||
|
@ -749,9 +845,9 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"xtend": {
|
"xtend": {
|
||||||
"version": "4.0.1",
|
"version": "4.0.2",
|
||||||
"resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.1.tgz",
|
"resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz",
|
||||||
"integrity": "sha1-pcbVMr5lbiPbgg77lDofBJmNY68="
|
"integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ=="
|
||||||
},
|
},
|
||||||
"yallist": {
|
"yallist": {
|
||||||
"version": "3.0.3",
|
"version": "3.0.3",
|
||||||
|
|
|
@ -20,6 +20,7 @@
|
||||||
"license": "MPL-2.0",
|
"license": "MPL-2.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@iarna/toml": "^2.2.3",
|
"@iarna/toml": "^2.2.3",
|
||||||
|
"async-mqtt": "^2.3.0",
|
||||||
"awilix": "^4.2.2",
|
"awilix": "^4.2.2",
|
||||||
"better-sqlite3": "^5.4.0",
|
"better-sqlite3": "^5.4.0",
|
||||||
"debug": "^4.1.1",
|
"debug": "^4.1.1",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
import MqttClient from 'simple-mqtt-client';
|
import mqtt from 'async-mqtt';
|
||||||
|
|
||||||
class TTNAppServer {
|
class TTNAppServer {
|
||||||
get connection_string() {
|
get connection_string() {
|
||||||
|
@ -19,42 +19,45 @@ class TTNAppServer {
|
||||||
}
|
}
|
||||||
|
|
||||||
async run() {
|
async run() {
|
||||||
|
// 1: Sanity checks
|
||||||
if(this.settings.ttn.app_id == "CHANGE_THIS" || this.settings.ttn.access_key == "CHANGE_THIS") {
|
if(this.settings.ttn.app_id == "CHANGE_THIS" || this.settings.ttn.access_key == "CHANGE_THIS") {
|
||||||
this.log.error(`Error: No TTN app id specified. Try filling in the required values in settings.toml. If they don't exist yet, try using server/settings.default.toml as a reference.`);
|
this.log.error(`Error: No TTN app id specified. Try filling in the required values in settings.toml. If they don't exist yet, try using server/settings.default.toml as a reference.`);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.ttn_client = await this.connect();
|
if(this.settings.ttn.devices.includes("CHANGE_THIS")) {
|
||||||
|
this.log.error(`Error: No device names specified. Try filling in the required values in settings.toml. If they don't exist yet, try using server/settings.default.toml as a reference.`);
|
||||||
for(let device_name of this.settings.ttn.devices) {
|
return false;
|
||||||
if(device_name == "CHANGE_THIS") {
|
|
||||||
this.log.error(`Error: No device names specified. Try filling in the required values in settings.toml. If they don't exist yet, try using server/settings.default.toml as a reference.`);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
this.ttn_client.subscribe(`${device_name}/up`, this.handle_message.bind(this));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 2: Connect
|
||||||
|
this.ttn_client = await this.connect();
|
||||||
|
this.ttn_client.on("message", this.handle_message.bind(this));
|
||||||
this.log.log(`Connected to ${this.connection_string}`);
|
this.log.log(`Connected to ${this.connection_string}`);
|
||||||
|
|
||||||
|
|
||||||
|
// 3: Subscribe
|
||||||
|
let result = await this.ttn_client.subscribe(this.settings.ttn.devices.map(
|
||||||
|
(device_name) => `${this.settings.ttn.app_id}/devices/${device_name}/up`
|
||||||
|
));
|
||||||
|
|
||||||
|
this.log.log(`Subscribed to ${result.map((el) => el.topic).join(", ")}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
connect() {
|
connect() {
|
||||||
return new Promise((resolve, _reject) => {
|
return new Promise((resolve, _reject) => {
|
||||||
this.log.log(`Connecting to ${this.connection_string}`);
|
let client = mqtt.connect(this.connection_string, {
|
||||||
MqttClient.new().init(
|
username: this.settings.ttn.app_id,
|
||||||
this.connection_string,
|
password: this.settings.ttn.access_key
|
||||||
this.settings.ttn.app_id, // Username
|
});
|
||||||
this.settings.ttn.access_key, // Password
|
client.once("connect", () => resolve(client));
|
||||||
`${this.settings.ttn.app_id}/devices`, // Base topic
|
});
|
||||||
(client) => {
|
|
||||||
client;
|
|
||||||
resolve(client);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async handle_message(payload) {
|
async handle_message(topic, message_buffer) {
|
||||||
await this.message_handler.handle(payload);
|
console.log(arguments);
|
||||||
|
// await this.message_handler.handle(payload);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue