Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
brasileric committed Oct 1, 2018
1 parent 6ae2218 commit be531e6
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 51 deletions.
47 changes: 44 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# SilverStripe OpenWeatherMap #

Adds OpenWeatherMap to your SilverStripe website.
Adds a simple 1 to 5 day weather forecast to your SilverStripe website with data from the free version of OpenWeatherMap (https://openweathermap.org/).
This free version gives only a 5 day/3 hours forecast, but this module turns the collected date in 1 to 5 day full day forecast.

### Requirements ###

SilverStripe 4
SilverStripe 4<br>
cmfcmf/openweathermap-php-api

### Version ###

Expand All @@ -18,6 +20,8 @@ composer require "hestec/silverstripe-openweathermap": "1.*"

### Configuration ###

Signup for a (free) account at https://openweathermap.org/ and get you API key.

Add the OpenWeatherMap API key to your yaml file:
```
Hestec\OpenWeatherMap\OpenWeatherMapExtension:
Expand All @@ -26,12 +30,49 @@ Hestec\OpenWeatherMap\OpenWeatherMapExtension:

do a dev/build and flush.

### Working ###

The forecast data of the free version of OpenWeatherMap (maximum 5 days) will be stored in the table OpenWeatherMapData and is updated just once in 3 hours when someone visits the website. This method prevents for too many api calls and will show the weather even if the OpenWeatherMap service is not available for a while.

### Usage ###

In your CMS Settings you will find a tab OpenWeatherMap.
Include OpenWeatherMap in your Page:
```
<% include OpenWeatherMap %>
```
This gives a 5 day weather forecast of the city Rio de Janeiro. I guess you want the forecast(s) of other cities. For that copy the template \silverstripe-openweathermap\templates\Includes\OpenWeatherMap.ss to your templates\Includes folder in your theme and changes the parameters of the loop call:
```
<% loop WeatherForecast(5, 3451190) %>
```
Where the number is the days of forecasts (maximum 5) en the code is the city code used on OpenWeathetMap. Get the code of your city on https://openweathermap.org/city: search the name of the city and find the code in the url.

Or simply put this loop in your page(s):
```
<% loop WeatherForecast(5, 3451190) %>
<p>$CityName, $CityCountry $Date:<br>
Minimum temperatuur: $TemperatureMin<br>
Maximum temperatuur: $TemperatureMax<br>
<img src="$IconUrl">
</p>
<% end_loop %>
```

If you want to use your own icons instead of the standard OpenWeatherMap icons, use just $Icon and the path to your icons. For example:
```
<% loop WeatherForecast(5, 3451190) %>
<p>$CityName, $CityCountry $Date:<br>
Minimum temperatuur: $TemperatureMin<br>
Maximum temperatuur: $TemperatureMax<br>
<img src="/themes/simple/img/weather/$IconUrl.png">
</p>
<% end_loop %>
```
Find a list of the necessary icons here: https://openweathermap.org/weather-conditions

### Issues ###

No known issues.

### Todo ###

Make an elemental extension.
2 changes: 0 additions & 2 deletions lang/nl.yml

This file was deleted.

106 changes: 60 additions & 46 deletions src/OpenWeatherMapExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,25 @@
use Cmfcmf\OpenWeatherMap;
use SilverStripe\ORM\ArrayList;
use SilverStripe\View\ArrayData;
use SilverStripe\Core\Injector\Injector;
use Psr\Log\LoggerInterface;
use SilverStripe\Core\Config\Config;

class OpenWeatherMapExtension extends DataExtension {

public function WeatherForecast($days = 6, $city){
public function WeatherForecast($days = 5, $city){

$today = new \DateTime();

// maximum forecast days with the free account is today + 5, so 6
if ($days > 6){
$days = 6;
if ($days > 5){
$days = 5;
}

if ($data = OpenWeatherMapData::get()->filter(array('CityId' => $city, 'Date' => $today->format('Y-m-d')))->first()){
if ($data = OpenWeatherMapData::get()->filter(array('CityId' => $city))->first()){

$now = new \DateTime();
$now->modify("- 1 hour");
$now->modify("- 1 minute");

if ($data->Created < $now->format('Y-m-d H:i')){

Expand All @@ -42,73 +45,84 @@ public function WeatherForecast($days = 6, $city){

$date = $today->format('Y-m-d');

$weather = $this->getWeatherData($date, $city);

$output->push(
new ArrayData(array(
'CityName' => $weather->CityName,
'CityCountry' => $weather->CityCountry,
'Date' => $date,
'TemperatureMin' => round($weather->TemperatureMin),
'TemperatureMax' => round($weather->Temperature),
'Icon' => $weather->Icon,
'IconUrl' => $weather->IconUrl
))
);

/*$output[]['Date'] = $date;
$output[]['TemparatureMin'] = $this->getTemperatureMin($date);*/
$today->modify("+ 1 day");
if ($weather = $this->getWeatherData($date, $city)){
$output->push(
new ArrayData(array(
'CityName' => $weather->CityName,
'CityCountry' => $weather->CityCountry,
'Date' => $date,
'TemperatureMin' => round($weather->TemperatureMin),
'TemperatureMax' => round($weather->Temperature),
'Icon' => $weather->Icon,
'IconUrl' => $weather->IconUrl
))
);

}else{
Injector::inst()->get(LoggerInterface::class)->warn('OpenWeatherMap -> no records in table for date: ' . $date);
}

$today->modify("+ 1 day");
$count++;

}

//print_r($output);

return $output;

}

public function UpdateOwm($city){

$list = OpenWeatherMapData::get();
$owm = new OpenWeatherMap(Config::inst()->get(__CLASS__, 'ApiKey'));

foreach($list as $item) {
$item->delete();
}
try {

$forecast = $owm->getWeatherForecast($city, 'metric', 'nl', '', 5);

$list = OpenWeatherMapData::get();

foreach ($list as $item) {
$item->delete();
}

$owm = new OpenWeatherMap('6a57afde213d03c6209c3c2c38beac75');

$forecast = $owm->getWeatherForecast($city, 'metric', 'nl', '', 5);
foreach ($forecast as $weather) {

foreach ($forecast as $weather) {
$add = new OpenWeatherMapData();
$add->CityId = $forecast->city->id;
$add->CityName = $forecast->city->name;
$add->CityCountry = $forecast->city->country;
$add->Date = $weather->time->day->format('Y-m-d');
$add->Temperature = $weather->temperature->getValue();
$add->Icon = $weather->weather->icon;
$add->IconUrl = $weather->weather->getIconUrl();
$add->write();

}

$add = new OpenWeatherMapData();
$add->CityId = $forecast->city->id;
$add->CityName = $forecast->city->name;
$add->CityCountry = $forecast->city->country;
$add->Date = $weather->time->day->format('Y-m-d');
$add->Temperature = $weather->temperature->getValue();
$add->Icon = $weather->weather->icon;
$add->IconUrl = $weather->weather->getIconUrl();
$add->write();
} catch(OWMException $e) {
Injector::inst()->get(LoggerInterface::class)->err('OpenWeatherMap exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').');

} catch(\Exception $e) {
Injector::inst()->get(LoggerInterface::class)->err('General exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').');
}

}

public function getWeatherData($date, $city){

$output = OpenWeatherMapData::get()->filter(array('Date' => $date, 'CityId' => $city))->sort('Temperature')->last();
if ($output = OpenWeatherMapData::get()->filter(array('Date' => $date, 'CityId' => $city))->sort('Temperature')->last()){

$min = OpenWeatherMapData::get()->filter(array('Date' => $date, 'CityId' => $city))->sort('Temperature')->first();
$min = OpenWeatherMapData::get()->filter(array('Date' => $date, 'CityId' => $city))->sort('Temperature')->first();

$output = $output->customise([
'TemperatureMin' => $min->Temperature
]);
$output = $output->customise([
'TemperatureMin' => $min->Temperature
]);

return $output;
return $output;

}
return false;

}

Expand Down
9 changes: 9 additions & 0 deletions templates/Includes/OpenWeatherMap.ss
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<% loop WeatherForecast(5, 3451190) %>

<p>$CityName, $CityCountry $Date:<br>
Minimum temperatuur: $TemperatureMin<br>
Maximum temperatuur: $TemperatureMax<br>
<img src="$IconUrl">
</p>

<% end_loop %>

0 comments on commit be531e6

Please sign in to comment.