From 48a9226a8101d5807e31aca9d897b4e9005e09b9 Mon Sep 17 00:00:00 2001 From: JayWll Date: Sat, 16 May 2020 14:49:59 -0600 Subject: [PATCH] Completed weather and local time plugin --- .../calgaryconditions/Plugin.php | 13 ++++++++++ .../assets/javascript/currentinfo.js | 12 ++++++++++ .../components/CurrentInfo.php | 24 +++++++++++++++++-- .../components/currentinfo/default.htm | 4 +--- .../components/currentinfo/result.htm | 1 + .../calgaryconditions/models/Settings.php | 14 +++++++++++ .../models/settings/fields.yaml | 4 ++++ .../calgaryconditions/updates/version.yaml | 1 + 8 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 www/plugins/jasonwilliams/calgaryconditions/assets/javascript/currentinfo.js create mode 100644 www/plugins/jasonwilliams/calgaryconditions/components/currentinfo/result.htm create mode 100644 www/plugins/jasonwilliams/calgaryconditions/models/Settings.php create mode 100644 www/plugins/jasonwilliams/calgaryconditions/models/settings/fields.yaml diff --git a/www/plugins/jasonwilliams/calgaryconditions/Plugin.php b/www/plugins/jasonwilliams/calgaryconditions/Plugin.php index b67812e..0030b06 100644 --- a/www/plugins/jasonwilliams/calgaryconditions/Plugin.php +++ b/www/plugins/jasonwilliams/calgaryconditions/Plugin.php @@ -19,6 +19,19 @@ class Plugin extends PluginBase ]; } + public function registerSettings() + { + return [ + 'settings' => [ + 'label' => 'Weather Plugin', + 'description' => 'Manage API keys and settings related to the weather plugin', + 'icon' => 'icon-cog', + 'class' => 'JasonWilliams\CalgaryConditions\Models\Settings', + 'order' => 500, + ] + ]; + } + public function registerComponents() { return [ diff --git a/www/plugins/jasonwilliams/calgaryconditions/assets/javascript/currentinfo.js b/www/plugins/jasonwilliams/calgaryconditions/assets/javascript/currentinfo.js new file mode 100644 index 0000000..81e0c43 --- /dev/null +++ b/www/plugins/jasonwilliams/calgaryconditions/assets/javascript/currentinfo.js @@ -0,0 +1,12 @@ +$(() => { + const oneMinute = 1 * 60 * 1000 + const requestUpdate = () => { + $.request('CurrentInfo::onUpdateRequested', { + update: { '@result': '#currentinfo' } + }) + console.log('Weather and time info updated') + } + + setInterval(requestUpdate, oneMinute) + requestUpdate() +}) diff --git a/www/plugins/jasonwilliams/calgaryconditions/components/CurrentInfo.php b/www/plugins/jasonwilliams/calgaryconditions/components/CurrentInfo.php index 51882d3..4412b82 100644 --- a/www/plugins/jasonwilliams/calgaryconditions/components/CurrentInfo.php +++ b/www/plugins/jasonwilliams/calgaryconditions/components/CurrentInfo.php @@ -1,11 +1,14 @@ fields['time'] = date('g:ia'); + $fields['time'] = date('g:ia'); + $this->page['conditions'] = $fields; + $this->addJs('/plugins/jasonwilliams/calgaryconditions/assets/javascript/currentinfo.js'); + } + + public function onUpdateRequested() + { + $settings = Settings::instance(); + + $response = Cache::remember('weatherdata', 60, function() { + return json_decode(Http::get('http://api.weatherstack.com/current?access_key='.$settings->weatherstack_api_key.'&query=Calgary')); + }); + + date_default_timezone_set('America/Edmonton'); + $fields['time'] = date('g:ia'); + $fields['temperature'] = $response->current->temperature; + $fields['weather'] = strtolower($response->current->weather_descriptions[0]); + $this->page['conditions'] = $fields; } } diff --git a/www/plugins/jasonwilliams/calgaryconditions/components/currentinfo/default.htm b/www/plugins/jasonwilliams/calgaryconditions/components/currentinfo/default.htm index 70c8283..0b180d1 100644 --- a/www/plugins/jasonwilliams/calgaryconditions/components/currentinfo/default.htm +++ b/www/plugins/jasonwilliams/calgaryconditions/components/currentinfo/default.htm @@ -1,3 +1 @@ -{% set conditions = __SELF__.fields %} - -

Right now in Calgary the time is {{ conditions.time }}

+

Right now it's {{ conditions.time }} here in Calgary.

diff --git a/www/plugins/jasonwilliams/calgaryconditions/components/currentinfo/result.htm b/www/plugins/jasonwilliams/calgaryconditions/components/currentinfo/result.htm new file mode 100644 index 0000000..229ab41 --- /dev/null +++ b/www/plugins/jasonwilliams/calgaryconditions/components/currentinfo/result.htm @@ -0,0 +1 @@ +Right now it's {{ conditions.time }} here in Calgary, it's {{ conditions.temperature }}°c and it's {{ conditions.weather }}. diff --git a/www/plugins/jasonwilliams/calgaryconditions/models/Settings.php b/www/plugins/jasonwilliams/calgaryconditions/models/Settings.php new file mode 100644 index 0000000..f42aa2d --- /dev/null +++ b/www/plugins/jasonwilliams/calgaryconditions/models/Settings.php @@ -0,0 +1,14 @@ +