Write some glue code to insert the measurement info into the database

....but it's untested.
This commit is contained in:
Starbeamrainbowlabs 2019-07-10 12:21:38 +01:00
parent 855cbc0c1b
commit 121cfc2418
6 changed files with 57 additions and 7 deletions

View file

@ -1,7 +1,7 @@
"use strict"; "use strict";
import util from 'util'; import util from 'util';
import crypto from './Objects/FlyingBanana'; import crypto from 'crypto';
export default { export default {
randomBytes: util.promisify(crypto.randomBytes) randomBytes: util.promisify(crypto.randomBytes)

View file

@ -21,6 +21,27 @@ class GatewayRepo {
this.insert_query.run(gateway); this.insert_query.run(gateway);
} }
/**
* Determines whether a gateway exists in the dayabase with the given name.
* @param {string} id The id of the gateway.
* @return {bool} Whether a gateway with the given name exists in the database.
*/
exists(id) {
// Note that normally a count wouldn't be appropriate here just to test for existence, but it's ok because it's guaranteed that there will only ever be a single entry with a given id.
let statement = this.db.prepare(`SELECT COUNT(id) AS count
FROM gateways
WHERE id = :id`);
let count = statement.get({ id }).count;
return count > 0;
}
/**
* Iterates over all the gateways in the table.
* TODO: Use Symbol.iterator here?
* @return {Generator} A generator that can be used with for..of
*/
iterate() { iterate() {
return this.db.prepare(`SELECT * FROM gateways`).iterate(); return this.db.prepare(`SELECT * FROM gateways`).iterate();
} }

View file

@ -14,7 +14,7 @@ class RSSIRepo {
channel channel
) VALUES ( ) VALUES (
:id, :id,
:reading_id, :gateway_id :reading_id, :gateway_id,
:rssi, :snr, :rssi, :snr,
:channel :channel
)`); )`);

View file

@ -19,8 +19,13 @@ class ReadingRepo {
:data_rate, :code_rate :data_rate, :code_rate
)`); )`);
this.insert_query.run(reading); let new_id = this.insert_query.run(reading).lastInsertRowid;
// Attach the new id to the rssi objects
for(let rssi of reading.rssis)
rssi.reading_id = new_id;
// Insert the RSSIs into the database
this.RSSIRepo.add(...reading.rssis); this.RSSIRepo.add(...reading.rssis);
} }

View file

@ -10,7 +10,7 @@ CREATE TABLE readings IF NOT EXISTS (
CREATE TABLE rssis IF NOT EXISTS ( CREATE TABLE rssis IF NOT EXISTS (
id INTEGER PRIMARY KEY, -- Random unique int id INTEGER PRIMARY KEY, -- Random unique int
reading_id INTEGER, -- The id of the object in the readings table that this rssi measurement belongs to reading_id INTEGER, -- The id of the object in the readings table that this rssi measurement belongs to
gateway_id INTEGER, -- Gateway id that the RSSI was from gateway_id TEXT, -- Gateway id that the RSSI was from
rssi FLOAT, -- The RSSI value itself rssi FLOAT, -- The RSSI value itself
snr FLOAT, -- The signal-to-noise ratio snr FLOAT, -- The signal-to-noise ratio
channel INTEGER -- The channel that the RSSI was received over. We might be able to use this to detect single-channel gateways channel INTEGER -- The channel that the RSSI was received over. We might be able to use this to detect single-channel gateways

View file

@ -1,11 +1,11 @@
"use strict"; "use strict";
import { decode_payload } from './DecodePayload.mjs'; import { decode_payload } from './DecodePayload.mjs';
import { get_id_number } from '../Helpers/IdGenerator.mjs';
class MessageHandler { class MessageHandler {
constructor({ ReadingRepo, RSSIRepo, GatewayRepo }) { constructor({ ReadingRepo, GatewayRepo }) {
this.repo_reading = ReadingRepo; this.repo_reading = ReadingRepo;
this.repo_rssi = RSSIRepo;
this.repo_gateway = GatewayRepo; this.repo_gateway = GatewayRepo;
} }
@ -15,13 +15,37 @@ class MessageHandler {
let decoded_payload = decode_payload(message.payload_raw); let decoded_payload = decode_payload(message.payload_raw);
console.log(decoded_payload); console.log(decoded_payload);
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
this.repo_reading.add({ this.repo_reading.add({
id: decoded_payload.id, id: decoded_payload.id,
latitude: decoded_payload.latitude, latitude: decoded_payload.latitude,
longitude: decoded_payload.longitude, longitude: decoded_payload.longitude,
data_rate: message.metadata.data_rate, data_rate: message.metadata.data_rate,
code_rate: message.metadata.coding_rate code_rate: message.metadata.coding_rate,
rssis
}); });
} }
} }