LoRaWAN-Signal-Mapping/server/ttn-app-server/TTNAppServer.mjs

35 lines
1,009 B
JavaScript

"use strict";
import MqttClient from 'simple-mqtt-client';
class TTNAppServer {
// Destructure the awilix container
constructor({ settings, ansi, message_handler }) {
this.settings = settings;
/** @type {Ansi} */
this.a = ansi;
/** @type {MessageHandler} */
this.message_handler = message_handler;
}
async start() {
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}`);
return false;
}
this.ttn_client = MqttClient.new().init()(
this.settings.ttn.app_id,
this.settings.ttn.access_key
);
this.ttn_client.on("uplink", this.handle_message.bind(this));
}
async handle_message(device_id, payload) {
await this.message_handler.handle(device_id, payload);
}
}
export default TTNAppServer;