From ae3a72c79108ec1af06283ef2e54533b4e87de39 Mon Sep 17 00:00:00 2001 From: Jon Ziebell Date: Wed, 3 Feb 2021 22:35:50 -0500 Subject: [PATCH] Fixed #320 - Profiles don't always generate and sometimes break GUI --- api/thermostat.php | 2 +- js/component/card/comparison_settings.js | 27 +++++++++++- js/component/card/metrics.js | 23 ++++------- js/component/card/temperature_profiles.js | 50 +++++++++++++++++++++-- 4 files changed, 79 insertions(+), 23 deletions(-) diff --git a/api/thermostat.php b/api/thermostat.php index 111c2b9..95bda30 100644 --- a/api/thermostat.php +++ b/api/thermostat.php @@ -243,7 +243,7 @@ class thermostat extends cora\crud { * @param int $thermostat_id */ public function generate_profile($thermostat_id) { - return $this->update([ + $this->update([ 'thermostat_id' => $thermostat_id, 'profile' => $this->api('profile', 'generate', $thermostat_id) ]); diff --git a/js/component/card/comparison_settings.js b/js/component/card/comparison_settings.js index ea444e8..e6f4392 100644 --- a/js/component/card/comparison_settings.js +++ b/js/component/card/comparison_settings.js @@ -88,16 +88,24 @@ beestat.component.card.comparison_settings.prototype.decorate_contents_ = functi api.send(); }, 10000); - } else if (beestat.cache.data.metrics === undefined) { - this.show_loading_('Fetching'); } else { if (thermostat.profile === null) { + this.show_loading_('Fetching'); new beestat.api() .add_call( 'thermostat', 'generate_profile', { 'thermostat_id': this.thermostat_id_ + } + ) + .add_call( + 'thermostat', + 'read_id', + { + 'attributes': { + 'inactive': 0 + } }, 'thermostat' ) @@ -105,6 +113,21 @@ beestat.component.card.comparison_settings.prototype.decorate_contents_ = functi beestat.cache.set('thermostat', response.thermostat); }) .send(); + } else if (beestat.cache.data.metrics === undefined) { + this.show_loading_('Fetching'); + new beestat.api() + .add_call( + 'thermostat', + 'get_metrics', + { + 'thermostat_id': this.thermostat_id_, + 'attributes': beestat.comparisons.get_attributes() + } + ) + .set_callback(function(response) { + beestat.cache.set('data.metrics', response); + }) + .send(); } } }; diff --git a/js/component/card/metrics.js b/js/component/card/metrics.js index 2ea78f8..3aa3241 100644 --- a/js/component/card/metrics.js +++ b/js/component/card/metrics.js @@ -27,20 +27,6 @@ beestat.component.card.metrics = function(thermostat_id) { ); beestat.component.card.apply(this, arguments); - - new beestat.api() - .add_call( - 'thermostat', - 'get_metrics', - { - 'thermostat_id': this.thermostat_id_, - 'attributes': beestat.comparisons.get_attributes() - } - ) - .set_callback(function(response) { - beestat.cache.set('data.metrics', response); - }) - .send(); }; beestat.extend(beestat.component.card.metrics, beestat.component.card); @@ -196,6 +182,11 @@ beestat.component.card.metrics.prototype.decorate_top_right_ = function(parent) beestat.component.card.metrics.prototype.get_subtitle_ = function() { const thermostat = beestat.cache.thermostat[this.thermostat_id_]; + // If the profile has not yet been generated. + if (thermostat.profile === null) { + return null; + } + const generated_at_m = moment( thermostat.profile.metadata.generated_at ); @@ -214,7 +205,7 @@ beestat.component.card.metrics.prototype.get_subtitle_ = function() { } else { duration_text += duration_weeks + ' weeks'; } - duration_text += ' of data.'; + duration_text += ' of data'; - return 'Generated ' + generated_at_m.format('MMM Do @ h a') + ' (updated weekly) ' + duration_text; + return 'Generated ' + generated_at_m.format('MMM Do @ h a') + duration_text + ' (updated weekly).'; }; diff --git a/js/component/card/temperature_profiles.js b/js/component/card/temperature_profiles.js index c881a2c..cbf6885 100644 --- a/js/component/card/temperature_profiles.js +++ b/js/component/card/temperature_profiles.js @@ -1,5 +1,5 @@ /** - * Temperature profiles. + * Temperature Profiles. * * @param {number} thermostat_id The thermostat_id this card is displaying * data for. @@ -7,6 +7,22 @@ beestat.component.card.temperature_profiles = function(thermostat_id) { this.thermostat_id_ = thermostat_id; + var self = this; + + /* + * Debounce so that multiple setting changes don't re-trigger the same + * event. This fires on the trailing edge so that all changes are accounted + * for when rerendering. + */ + var change_function = beestat.debounce(function() { + self.rerender(); + }, 10); + + beestat.dispatcher.addEventListener( + ['cache.thermostat'], + change_function + ); + beestat.component.card.apply(this, arguments); }; beestat.extend(beestat.component.card.temperature_profiles, beestat.component.card); @@ -49,8 +65,29 @@ beestat.component.card.temperature_profiles.prototype.get_data_ = function() { if ( thermostat.profile === null ) { - this.chart_.render(parent); this.show_loading_('Fetching'); + new beestat.api() + .add_call( + 'thermostat', + 'generate_profile', + { + 'thermostat_id': this.thermostat_id_ + } + ) + .add_call( + 'thermostat', + 'read_id', + { + 'attributes': { + 'inactive': 0 + } + }, + 'thermostat' + ) + .set_callback(function(response) { + beestat.cache.set('thermostat', response.thermostat); + }) + .send(); } else { // Global x range. var x_min = Infinity; @@ -202,6 +239,11 @@ beestat.component.card.temperature_profiles.prototype.get_title_ = function() { beestat.component.card.temperature_profiles.prototype.get_subtitle_ = function() { const thermostat = beestat.cache.thermostat[this.thermostat_id_]; + // If the profile has not yet been generated. + if (thermostat.profile === null) { + return null; + } + const generated_at_m = moment( thermostat.profile.metadata.generated_at ); @@ -220,9 +262,9 @@ beestat.component.card.temperature_profiles.prototype.get_subtitle_ = function() } else { duration_text += duration_weeks + ' weeks'; } - duration_text += ' of data.'; + duration_text += ' of data'; - return 'Generated ' + generated_at_m.format('MMM Do @ h a') + ' (updated weekly) ' + duration_text; + return 'Generated ' + generated_at_m.format('MMM Do @ h a') + duration_text + ' (updated weekly).'; }; /**