LoRaWAN-Signal-Mapping/server/process-data/DataProcessor.mjs

63 lines
1.7 KiB
JavaScript

"use strict";
import fs from 'fs';
import readline from 'readline';
/**
* Folds data from the microSD card into the SQLite database.
*/
class DataProcessor {
constructor({ settings, log, ReadingRepo }) {
this.settings = settings;
this.l = log;
this.repo_reading = ReadingRepo;
}
/**
* Given a filename, reads it and folds the data into the SQLite database.
* @param {string} filename The path tot he file to read in.
* @return {Promise} A promise that resolves when the folding is complete.
*/
process(filename) {
return new Promise((resolve, reject) => { // Arrow functions keep the scope of their parent functions
if(!fs.existsSync(filename)) {
this.l.log(`Error: ${filename} doesn't exist.`);
reject();
}
let reader = readline.createInterface({
input: fs.createReadStream(filename)
});
reader.on("line", this.process_line.bind(this));
reader.on("close", resolve);
})
}
process_line(line_text) {
let parts = line_text.split(/\s+/),
reading = {
id: parseInt(parts[0]),
latitude: parseFloat(parts[1]),
longitude: parseFloat(parts[2]),
// For inserting into the database
data_rate: null, code_rate: null, rssis: []
};
let log_message = `Processing reading with id ${reading.id}`;
if(!this.repo_reading.exists(reading.id)) {
// Since the reading doesn't exist in the database already, we should insert it now.
// We can deduce that it doesn't exist because no gateways picked up the message when it was transmitted - hence the IoT device was in a dead sopt at the time of transmission.
this.repo_reading.add(reading);
log_message += " no database entry detected, inserted new record";
}
this.l.log(log_message);
}
}
export default DataProcessor;