Completed weather and local time plugin

This commit is contained in:
JayWll 2020-05-16 14:49:59 -06:00
parent 8aeceee0eb
commit 48a9226a81
8 changed files with 68 additions and 5 deletions

View File

@ -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 [

View File

@ -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()
})

View File

@ -1,11 +1,14 @@
<?php namespace JasonWilliams\CalgaryConditions\Components;
use Cms\Classes\ComponentBase;
use October\Rain\Network\Http;
use JasonWilliams\CalgaryConditions\Models\Settings;
use Cache;
use ApplicationException;
class CurrentInfo extends ComponentBase
{
public $fields;
//public $fields;
public function componentDetails()
{
@ -18,6 +21,23 @@ class CurrentInfo extends ComponentBase
public function onRun()
{
date_default_timezone_set('America/Edmonton');
$this->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;
}
}

View File

@ -1,3 +1 @@
{% set conditions = __SELF__.fields %}
<p>Right now in Calgary the time is {{ conditions.time }}</p>
<p id="currentinfo">Right now it's {{ conditions.time }} here in Calgary.</p>

View File

@ -0,0 +1 @@
Right now it's {{ conditions.time }} here in Calgary, it's {{ conditions.temperature }}&deg;c and it's {{ conditions.weather }}.

View File

@ -0,0 +1,14 @@
<?php namespace JasonWilliams\CalgaryConditions\Models;
use Model;
class Settings extends Model
{
public $implement = ['System.Behaviors.SettingsModel'];
// A unique code
public $settingsCode = 'jasonwilliams_calgaryconditions_settings';
// Reference to field configuration
public $settingsFields = 'fields.yaml';
}

View File

@ -0,0 +1,4 @@
fields:
weatherstack_api_key:
label: Weatherstack API key
description: Your API key, obtained from weaterstack.com

View File

@ -1 +1,2 @@
0.0.1: Initial version
1.0.0: Working version