2019-07-11 13:03:27 +00:00
"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 )
} ) ;
2019-07-11 13:07:55 +00:00
reader . on ( "line" , this . process _line . bind ( this ) ) ;
2019-07-11 13:03:27 +00:00
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
2019-07-11 13:07:55 +00:00
data _rate : null , code _rate : null , rssis : [ ]
2019-07-11 13:03:27 +00:00
} ;
let log _message = ` Processing reading with id ${ reading . id } ` ;
if ( ! this . repo _reading . exists ( reading . id ) ) {
2019-07-11 13:07:55 +00:00
// 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.
2019-07-11 13:03:27 +00:00
this . repo _reading . add ( reading ) ;
log _message += " no database entry detected, inserted new record" ;
}
this . l . log ( log _message ) ;
}
}
export default DataProcessor ;