2019-07-17 14:15:31 +00:00
|
|
|
"use strict";
|
|
|
|
|
2019-07-29 13:17:28 +00:00
|
|
|
import haversine from 'haversine-distance';
|
|
|
|
|
2019-07-23 14:45:29 +00:00
|
|
|
import { normalise, clamp } from '../../common/Math.mjs';
|
2019-07-17 14:15:31 +00:00
|
|
|
|
|
|
|
class DatasetFetcher {
|
2019-07-29 13:17:28 +00:00
|
|
|
constructor({ settings, GatewayRepo, RSSIRepo, ReadingRepo }) {
|
2019-07-17 14:15:31 +00:00
|
|
|
this.settings = settings;
|
2019-07-29 13:17:28 +00:00
|
|
|
this.repo_gateway = GatewayRepo;
|
2019-07-17 14:15:31 +00:00
|
|
|
this.repo_rssi = RSSIRepo;
|
2019-07-29 13:17:28 +00:00
|
|
|
this.repo_reading = ReadingRepo;
|
2019-07-17 14:15:31 +00:00
|
|
|
}
|
|
|
|
|
2019-07-29 13:17:28 +00:00
|
|
|
normalise_latlng(lat, lng) {
|
2019-07-29 17:06:50 +00:00
|
|
|
return {
|
|
|
|
latitude: normalise(lat,
|
2019-07-29 13:17:28 +00:00
|
|
|
{ min: -90, max: +90 },
|
|
|
|
{ min: 0, max: 1 }
|
|
|
|
),
|
2019-07-29 17:06:50 +00:00
|
|
|
longitude: normalise(lng,
|
2019-07-29 13:17:28 +00:00
|
|
|
{ min: -180, max: +180 },
|
|
|
|
{ min: 0, max: 1 }
|
|
|
|
)
|
2019-07-29 17:06:50 +00:00
|
|
|
};
|
2019-07-29 13:17:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fetch_all(gateway_id) {
|
|
|
|
let gateway_location = this.repo_gateway.get_by_id(gateway_id);
|
|
|
|
|
2019-07-29 16:02:34 +00:00
|
|
|
let result = [];
|
2019-07-18 15:34:25 +00:00
|
|
|
for(let rssi of this.repo_rssi.iterate_gateway(gateway_id)) {
|
2019-07-29 13:17:28 +00:00
|
|
|
let next_input = this.normalise_latlng(rssi.latitude, rssi.longitude);
|
|
|
|
let distance_from_gateway = haversine(gateway_location, rssi);
|
|
|
|
|
2019-07-29 17:06:50 +00:00
|
|
|
next_input.distance = clamp(
|
2019-07-29 13:17:28 +00:00
|
|
|
normalise(distance_from_gateway,
|
|
|
|
{ min: 0, max: 20000 },
|
2019-07-18 15:34:25 +00:00
|
|
|
{ min: 0, max: 1 }
|
|
|
|
),
|
2019-07-29 17:06:50 +00:00
|
|
|
0, 1);
|
2019-07-29 13:17:28 +00:00
|
|
|
|
|
|
|
result.input.push(next_input);
|
|
|
|
|
|
|
|
console.log(`Distance from gateway: ${haversine(gateway_location, rssi)}m`);
|
|
|
|
|
2019-07-29 16:02:34 +00:00
|
|
|
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
|
|
|
|
});
|
2019-07-18 15:34:25 +00:00
|
|
|
}
|
2019-07-29 13:17:28 +00:00
|
|
|
|
|
|
|
for(let reading of this.repo_reading.iterate_unreceived()) {
|
|
|
|
result.input.push(this.normalise_latlng(
|
|
|
|
reading.latitude,
|
|
|
|
reading.longitude
|
|
|
|
));
|
|
|
|
|
|
|
|
result.output.push([ 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);
|
2019-07-18 15:34:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*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)
|
|
|
|
];
|
2019-07-17 14:15:31 +00:00
|
|
|
}
|
2019-07-29 13:17:28 +00:00
|
|
|
// 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 ];
|
|
|
|
}
|
2019-07-17 14:15:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default DatasetFetcher;
|