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

97 lines
2.4 KiB
JavaScript
Raw Normal View History

"use strict";
import haversine from 'haversine-distance';
2019-07-23 14:45:29 +00:00
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);
2019-07-30 14:15:34 +00:00
// 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()) {
2019-07-30 14:15:34 +00:00
let next_input = this.normalise_latlng(
reading.latitude,
reading.longitude
2019-07-30 14:15:34 +00:00
);
next_input.distance = haversine(gateway_location, reading);
2019-07-30 14:15:34 +00:00
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;