Finish switching over to simple-mqtt-client, but it's still broken
This commit is contained in:
parent
cc3c0d9d63
commit
42841d07e8
6 changed files with 56 additions and 1028 deletions
|
@ -17,7 +17,7 @@ class Log {
|
||||||
log_raw(func, ...items) {
|
log_raw(func, ...items) {
|
||||||
let prefix = (new Date()).toISOString();
|
let prefix = (new Date()).toISOString();
|
||||||
if(this.settings.logging.date_display_mode == "relative") {
|
if(this.settings.logging.date_display_mode == "relative") {
|
||||||
prefix = (new Date() - this.start_time).toFixed(5);
|
prefix = ((new Date() - this.start_time) / 1000).toFixed(4);
|
||||||
}
|
}
|
||||||
|
|
||||||
func(`${this.a.locol}[${prefix}]${this.a.reset}`, ...items);
|
func(`${this.a.locol}[${prefix}]${this.a.reset}`, ...items);
|
||||||
|
|
1020
server/package-lock.json
generated
1020
server/package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -22,8 +22,8 @@
|
||||||
"@iarna/toml": "^2.2.3",
|
"@iarna/toml": "^2.2.3",
|
||||||
"awilix": "^4.2.2",
|
"awilix": "^4.2.2",
|
||||||
"better-sqlite3": "^5.4.0",
|
"better-sqlite3": "^5.4.0",
|
||||||
"simple-mqtt-client": "^1.0.1",
|
"debug": "^4.1.1",
|
||||||
"ttn": "^2.3.2"
|
"simple-mqtt-client": "^1.0.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/better-sqlite3": "^5.4.0"
|
"@types/better-sqlite3": "^5.4.0"
|
||||||
|
|
|
@ -25,6 +25,7 @@ filename = "lorawan.sqlite"
|
||||||
# and also "Application Overview -> Handler"
|
# and also "Application Overview -> Handler"
|
||||||
host = "eu.thethings.network"
|
host = "eu.thethings.network"
|
||||||
# The port number to connect on.
|
# The port number to connect on.
|
||||||
|
# The Things Network uses 1883 for plain-text, and 8883 for TLS.
|
||||||
port = 8883
|
port = 8883
|
||||||
# Whether to use TLS or not.
|
# Whether to use TLS or not.
|
||||||
tls = true
|
tls = true
|
||||||
|
@ -39,6 +40,13 @@ app_id = "CHANGE_THIS"
|
||||||
# there.
|
# there.
|
||||||
access_key = "CHANGE_THIS"
|
access_key = "CHANGE_THIS"
|
||||||
|
|
||||||
|
# A list of devices to monitor.
|
||||||
|
# If a device isn't specified here, then we won't hear messages from it.
|
||||||
|
# FUTURE: Automatically fetch a list of devices from the TTN API
|
||||||
|
devices = [
|
||||||
|
"CHANGE_THIS"
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
[logging]
|
[logging]
|
||||||
# The format the date displayed when logging things should take.
|
# The format the date displayed when logging things should take.
|
||||||
|
|
|
@ -7,7 +7,7 @@ class MessageHandler {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async handle(device_id, payload) {
|
async handle(payload) {
|
||||||
console.log(arguments);
|
console.log(arguments);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,32 +3,58 @@
|
||||||
import MqttClient from 'simple-mqtt-client';
|
import MqttClient from 'simple-mqtt-client';
|
||||||
|
|
||||||
class TTNAppServer {
|
class TTNAppServer {
|
||||||
|
get connection_string() {
|
||||||
|
return `${this.settings.ttn.tls ? "mqtts" : "mqtt"}://${this.settings.ttn.host}:${this.settings.ttn.port}/`
|
||||||
|
}
|
||||||
|
|
||||||
// Destructure the awilix container
|
// Destructure the awilix container
|
||||||
constructor({ settings, ansi, message_handler }) {
|
constructor({ settings, ansi, MessageHandler, log }) {
|
||||||
this.settings = settings;
|
this.settings = settings;
|
||||||
/** @type {Ansi} */
|
/** @type {Ansi} */
|
||||||
this.a = ansi;
|
this.a = ansi;
|
||||||
|
/** @type {Log} */
|
||||||
|
this.log = log;
|
||||||
/** @type {MessageHandler} */
|
/** @type {MessageHandler} */
|
||||||
this.message_handler = message_handler;
|
this.message_handler = MessageHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
async start() {
|
async run() {
|
||||||
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") {
|
||||||
console.error(`${this.a.fred}${this.a.hicol}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.a.reset}`);
|
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();
|
||||||
|
|
||||||
this.ttn_client = MqttClient.new().init()(
|
for(let device_name of this.settings.ttn.devices) {
|
||||||
this.settings.ttn.app_id,
|
if(device_name == "CHANGE_THIS") {
|
||||||
this.settings.ttn.access_key
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
this.ttn_client.on("uplink", this.handle_message.bind(this));
|
this.log.log(`Connected to ${this.connection_string}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
async handle_message(device_id, payload) {
|
connect() {
|
||||||
await this.message_handler.handle(device_id, payload);
|
return new Promise((resolve, _reject) => {
|
||||||
|
this.log.log(`Connecting to ${this.connection_string}`);
|
||||||
|
MqttClient.new().init(
|
||||||
|
this.connection_string,
|
||||||
|
this.settings.ttn.app_id, // Username
|
||||||
|
this.settings.ttn.access_key, // Password
|
||||||
|
`${this.settings.ttn.app_id}/devices`, // Base topic
|
||||||
|
(client) => {
|
||||||
|
client;
|
||||||
|
resolve(client);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async handle_message(payload) {
|
||||||
|
await this.message_handler.handle(payload);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue