2019-07-04 16:18:38 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
import { decode_payload } from './DecodePayload.mjs';
|
2019-07-10 11:21:38 +00:00
|
|
|
import { get_id_number } from '../Helpers/IdGenerator.mjs';
|
2019-07-04 16:18:38 +00:00
|
|
|
|
|
|
|
class MessageHandler {
|
2019-07-10 11:21:38 +00:00
|
|
|
constructor({ ReadingRepo, GatewayRepo }) {
|
2019-07-09 16:31:08 +00:00
|
|
|
this.repo_reading = ReadingRepo;
|
|
|
|
this.repo_gateway = GatewayRepo;
|
2019-07-04 16:18:38 +00:00
|
|
|
}
|
|
|
|
|
2019-07-09 12:14:25 +00:00
|
|
|
async handle(message) {
|
|
|
|
console.log(message);
|
|
|
|
|
|
|
|
let decoded_payload = decode_payload(message.payload_raw);
|
|
|
|
console.log(decoded_payload);
|
2019-07-09 16:31:08 +00:00
|
|
|
|
2019-07-10 11:21:38 +00:00
|
|
|
let rssis = [];
|
|
|
|
for(let gw in message.metadata.gateways) {
|
|
|
|
// 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: get_id_number,
|
|
|
|
gateway_id: this.repo_gateway,
|
|
|
|
rssi: gw.rssi,
|
|
|
|
snr: gw.snr,
|
|
|
|
channel: gw.channel
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the reading and the associated RSSIs to the database
|
2019-07-09 16:31:08 +00:00
|
|
|
this.repo_reading.add({
|
|
|
|
id: decoded_payload.id,
|
|
|
|
latitude: decoded_payload.latitude,
|
|
|
|
longitude: decoded_payload.longitude,
|
|
|
|
|
|
|
|
data_rate: message.metadata.data_rate,
|
2019-07-10 11:21:38 +00:00
|
|
|
code_rate: message.metadata.coding_rate,
|
|
|
|
|
|
|
|
rssis
|
2019-07-09 16:31:08 +00:00
|
|
|
});
|
2019-07-04 16:18:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default MessageHandler;
|