63 lines
1.7 KiB
JavaScript
63 lines
1.7 KiB
JavaScript
"use strict";
|
|
|
|
import { decode_payload } from './DecodePayload.mjs';
|
|
import { get_id_number } from '../Helpers/IdGenerator.mjs';
|
|
|
|
class MessageHandler {
|
|
get debug() {
|
|
return this.settings.logging.verbose;
|
|
}
|
|
constructor({ settings, ReadingRepo, GatewayRepo }) {
|
|
this.settings = settings;
|
|
this.repo_reading = ReadingRepo;
|
|
this.repo_gateway = GatewayRepo;
|
|
}
|
|
|
|
async handle(message) {
|
|
if(this.debug) console.log(message);
|
|
|
|
if(this.settings.ttn.encryption_key == "CHANGE_THIS") {
|
|
this.log.error(`Error: No encryption key 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;
|
|
}
|
|
|
|
let decoded_payload = decode_payload(message.payload_raw, this.settings.ttn.encryption_key);
|
|
if(this.debug) console.log(decoded_payload);
|
|
|
|
let rssis = [];
|
|
for(let gw of message.metadata.gateways) {
|
|
if(this.debug) console.log("gateway ", gw);
|
|
// Insert the gateway info into the database if it doesn't exist already
|
|
if(!this.repo_gateway.exists(gw.gtw_id)) {
|
|
this.repo_gateway.add({
|
|
id: gw.gtw_id,
|
|
latitude: gw.latitude, longitude: gw.longitude,
|
|
altitude: gw.altitude
|
|
});
|
|
}
|
|
|
|
// Generate an RSSI object
|
|
rssis.push({
|
|
id: await get_id_number(),
|
|
gateway_id: gw.gtw_id,
|
|
rssi: gw.rssi,
|
|
snr: gw.snr,
|
|
channel: gw.channel
|
|
});
|
|
}
|
|
|
|
// Add the reading and the associated RSSIs to the database
|
|
this.repo_reading.add({
|
|
id: decoded_payload.id,
|
|
latitude: decoded_payload.latitude,
|
|
longitude: decoded_payload.longitude,
|
|
|
|
data_rate: message.metadata.data_rate,
|
|
code_rate: message.metadata.coding_rate,
|
|
|
|
rssis
|
|
});
|
|
}
|
|
}
|
|
|
|
export default MessageHandler;
|