Finish switching over to simple-mqtt-client, but it's still broken

This commit is contained in:
Starbeamrainbowlabs 2019-07-08 15:42:05 +01:00
parent cc3c0d9d63
commit 42841d07e8
6 changed files with 56 additions and 1028 deletions

View File

@ -17,7 +17,7 @@ class Log {
log_raw(func, ...items) {
let prefix = (new Date()).toISOString();
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);

1020
server/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -22,8 +22,8 @@
"@iarna/toml": "^2.2.3",
"awilix": "^4.2.2",
"better-sqlite3": "^5.4.0",
"simple-mqtt-client": "^1.0.1",
"ttn": "^2.3.2"
"debug": "^4.1.1",
"simple-mqtt-client": "^1.0.1"
},
"devDependencies": {
"@types/better-sqlite3": "^5.4.0"

View File

@ -25,6 +25,7 @@ filename = "lorawan.sqlite"
# and also "Application Overview -> Handler"
host = "eu.thethings.network"
# The port number to connect on.
# The Things Network uses 1883 for plain-text, and 8883 for TLS.
port = 8883
# Whether to use TLS or not.
tls = true
@ -39,6 +40,13 @@ app_id = "CHANGE_THIS"
# there.
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]
# The format the date displayed when logging things should take.

View File

@ -7,7 +7,7 @@ class MessageHandler {
}
async handle(device_id, payload) {
async handle(payload) {
console.log(arguments);
}
}

View File

@ -3,32 +3,58 @@
import MqttClient from 'simple-mqtt-client';
class TTNAppServer {
get connection_string() {
return `${this.settings.ttn.tls ? "mqtts" : "mqtt"}://${this.settings.ttn.host}:${this.settings.ttn.port}/`
}
// Destructure the awilix container
constructor({ settings, ansi, message_handler }) {
constructor({ settings, ansi, MessageHandler, log }) {
this.settings = settings;
/** @type {Ansi} */
this.a = ansi;
/** @type {Log} */
this.log = log;
/** @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") {
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;
}
this.ttn_client = await this.connect();
this.ttn_client = MqttClient.new().init()(
this.settings.ttn.app_id,
this.settings.ttn.access_key
);
for(let device_name of this.settings.ttn.devices) {
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));
}
this.ttn_client.on("uplink", this.handle_message.bind(this));
this.log.log(`Connected to ${this.connection_string}`);
}
async handle_message(device_id, payload) {
await this.message_handler.handle(device_id, payload);
connect() {
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);
}
}