From 06325a80bfa64304b1a4d440c6c82f0a15fd5fc8 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Tue, 6 Aug 2019 13:18:40 +0100 Subject: [PATCH] Fix remaining bugs in unified training mode --- server/Repos.SQLite/GatewayRepo.mjs | 23 +++++++++++++++++++++++ server/train-ai/AITrainer.mjs | 4 +++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/server/Repos.SQLite/GatewayRepo.mjs b/server/Repos.SQLite/GatewayRepo.mjs index aeefc4d..0a487ea 100644 --- a/server/Repos.SQLite/GatewayRepo.mjs +++ b/server/Repos.SQLite/GatewayRepo.mjs @@ -36,6 +36,11 @@ class GatewayRepo { 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) { return this.db.prepare(`SELECT * FROM gateways WHERE id = :id`).get({ id }); } @@ -48,6 +53,24 @@ class GatewayRepo { 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; diff --git a/server/train-ai/AITrainer.mjs b/server/train-ai/AITrainer.mjs index 27f115e..0694f41 100644 --- a/server/train-ai/AITrainer.mjs +++ b/server/train-ai/AITrainer.mjs @@ -28,6 +28,8 @@ class AITrainer { let training_result = await this.train_gateway(null, filepath); + let { latitude, longitude } = this.repo_gateway.get_average_location(); + await fs.promises.writeFile( path.join( path.dirname(this.root_dir), @@ -43,7 +45,7 @@ class AITrainer { index: [{ id: "Unified AI (not a real gateway)", filename: "ai.json", - latitude: 0, longitude: 0, + latitude, longitude, net_settings: training_result.net_settings }] }, null, "\t")