Fix remaining bugs in unified training mode

This commit is contained in:
Starbeamrainbowlabs 2019-08-06 13:18:40 +01:00
parent 6d21146b05
commit 06325a80bf
2 changed files with 26 additions and 1 deletions

View File

@ -36,6 +36,11 @@ class GatewayRepo {
return count > 0; return count > 0;
} }
/**
* Returns a garteway by it's id.
* @param {string} id The id to fetcht he gateway for.
* @return {object} The information recorded about the gateway with the specified id .
*/
get_by_id(id) { get_by_id(id) {
return this.db.prepare(`SELECT * FROM gateways WHERE id = :id`).get({ id }); return this.db.prepare(`SELECT * FROM gateways WHERE id = :id`).get({ id });
} }
@ -48,6 +53,24 @@ class GatewayRepo {
iterate() { iterate() {
return this.db.prepare(`SELECT * FROM gateways`).iterate(); return this.db.prepare(`SELECT * FROM gateways`).iterate();
} }
/**
* Returns the average location of allt he currently registered gateways.
* @return {{latitude: number, longitude: number}} The lat longg coordinates of the average location of all the gateways.
*/
get_average_location() {
let count = 0, lat_sum = 0, lng_sum = 0;
for(let gateway of this.iterate()) {
lat_sum += gateway.latitude;
lng_sum += gateway.longitude;
count++;
}
return {
latitude: lat_sum / count,
longitude: lng_sum / count
};
}
} }
export default GatewayRepo; export default GatewayRepo;

View File

@ -28,6 +28,8 @@ class AITrainer {
let training_result = await this.train_gateway(null, filepath); let training_result = await this.train_gateway(null, filepath);
let { latitude, longitude } = this.repo_gateway.get_average_location();
await fs.promises.writeFile( await fs.promises.writeFile(
path.join( path.join(
path.dirname(this.root_dir), path.dirname(this.root_dir),
@ -43,7 +45,7 @@ class AITrainer {
index: [{ index: [{
id: "Unified AI (not a real gateway)", id: "Unified AI (not a real gateway)",
filename: "ai.json", filename: "ai.json",
latitude: 0, longitude: 0, latitude, longitude,
net_settings: training_result.net_settings net_settings: training_result.net_settings
}] }]
}, null, "\t") }, null, "\t")