LoRaWAN-Signal-Mapping/server/train-ai/DatasetFetcher.mjs

104 lines
2.5 KiB
JavaScript

"use strict";
import haversine from 'haversine-distance';
import { normalise, clamp } from '../../common/Math.mjs';
class DatasetFetcher {
constructor({ settings, GatewayRepo, RSSIRepo, ReadingRepo }) {
this.settings = settings;
this.repo_gateway = GatewayRepo;
this.repo_rssi = RSSIRepo;
this.repo_reading = ReadingRepo;
}
normalise_latlng(lat, lng) {
return {
latitude: normalise(lat,
{ min: -90, max: +90 },
{ min: 0, max: 1 }
),
longitude: normalise(lng,
{ min: -180, max: +180 },
{ min: 0, max: 1 }
)
};
}
fetch_all(gateway_id) {
let gateway_location = this.repo_gateway.get_by_id(gateway_id);
let result = [];
for(let rssi of this.repo_rssi.iterate_gateway(gateway_id)) {
let next_input = this.normalise_latlng(rssi.latitude, rssi.longitude);
let distance_from_gateway = haversine(gateway_location, rssi);
next_input.distance = clamp(
normalise(distance_from_gateway,
{ min: 0, max: 20000 },
{ min: 0, max: 1 }
),
0, 1);
// console.log(`Distance from gateway: ${haversine(gateway_location, rssi)}m`);
let next_output = [
clamp(normalise(rssi.rssi,
{ min: this.settings.ai.rssi_min, max: this.settings.ai.rssi_max },
{ min: 0, max: 1 }
), 0, 1)
];
result.push({
input: next_input,
output: next_output
});
}
for(let reading of this.repo_reading.iterate_unreceived()) {
let next_input = this.normalise_latlng(
reading.latitude,
reading.longitude
);
next_input.distance = clamp(
normalise(haversine(gateway_location, reading),
{ min: 0, max: 20000 },
{ min: 0, max: 1 }
),
0, 1);
result.push({
input: next_input,
output: [ 0 ]
});
}
return result;
}
*fetch_input(gateway_id) {
for(let rssi of this.repo_rssi.iterate_gateway(gateway_id))
yield this.normalise_latlng(rssi.latitude, rssi.longitude);
for(let rssi of this.repo_reading.iterate_unreceived())
yield this.normalise_latlng(rssi.latitude, rssi.longitude);
}
*fetch_output(gateway_id) {
for(let rssi of this.repo_rssi.iterate_gateway(gateway_id)) {
yield [
clamp(normalise(rssi.rssi,
{ min: this.settings.ai.rssi_min, max: this.settings.ai.rssi_max },
{ min: 0, max: 1 }
), 0, 1)
];
}
// Yield 0 for every unreceived message, since we want to train it to predict a *terrible* signal where the gateway is not
for(let rssi of this.repo_reading.iterate_unreceived()) {
yield [ 0 ];
}
}
}
export default DatasetFetcher;