Cookiepersistenz für eigene Schwellwerte
This commit is contained in:
parent
e9d44724f7
commit
50be9ab7c9
3 changed files with 168 additions and 40 deletions
11
config.php
Normal file
11
config.php
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
<?php /*
|
||||||
|
* Konfiguration
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* Link zur Wetterstation auf Wunderground
|
||||||
|
*/
|
||||||
|
define('WETTERSTATION', 'http://www.wunderground.com/cgi-bin/findweather/hdfForecast?query=51.330%2C12.363&sp=ISACHSEN121&apiref=5493fcc3357cb244');
|
||||||
|
/**
|
||||||
|
* Name der Stadt
|
||||||
|
*/
|
||||||
|
define('STADT', 'Leipzig');
|
91
index.php
91
index.php
|
@ -1,3 +1,18 @@
|
||||||
|
<?php
|
||||||
|
require_once('config.php');
|
||||||
|
require_once('wetter.php');
|
||||||
|
|
||||||
|
$daten = new wetter();
|
||||||
|
|
||||||
|
// Setze die Einstellungen als Cookie
|
||||||
|
if($_SERVER['REQUEST_METHOD'] === 'POST'){
|
||||||
|
setcookie('regen', $daten->filter_post_int('regenwahrscheinlichkeit'),
|
||||||
|
time()*9);
|
||||||
|
setcookie('templow', $daten->filter_post_int('temperaturunten'), time()*9);
|
||||||
|
setcookie('temphigh', $daten->filter_post_int('temperaturoben'), time()*9);
|
||||||
|
setcookie('wind', $daten->filter_post_int('wind'), time()*9);
|
||||||
|
}
|
||||||
|
?>
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="de-DE">
|
<html lang="de-DE">
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||||
|
@ -12,27 +27,12 @@
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
<div class="container">
|
||||||
|
<h2 class="text-center trigger"><a href="#">Fahrradwetter in
|
||||||
<?php
|
<?php
|
||||||
/*
|
|
||||||
* Konfiguration
|
|
||||||
*/
|
|
||||||
/**
|
|
||||||
* Link zur Wetterstation auf Wunderground
|
|
||||||
*/
|
|
||||||
define('WETTERSTATION', 'http://www.wunderground.com/cgi-bin/findweather/hdfForecast?query=51.330%2C12.363&sp=ISACHSEN121&apiref=5493fcc3357cb244');
|
|
||||||
/**
|
|
||||||
* Name der Stadt
|
|
||||||
*/
|
|
||||||
define('STADT', 'Leipzig');
|
|
||||||
|
|
||||||
require_once('wetter.php');
|
|
||||||
|
|
||||||
$daten = new wetter(WETTERSTATION, STADT);
|
|
||||||
|
|
||||||
$wetter = $daten->getWeather();
|
$wetter = $daten->getWeather();
|
||||||
|
|
||||||
echo '<div class="container">
|
echo STADT .
|
||||||
<h2 class="text-center trigger"><a href="#">Fahrradwetter in '. STADT .
|
|
||||||
'?</a> - '.$wetter[0]['rad'].'.</h2>
|
'?</a> - '.$wetter[0]['rad'].'.</h2>
|
||||||
<div class="toggle_container">
|
<div class="toggle_container">
|
||||||
<table class="table table-striped">';
|
<table class="table table-striped">';
|
||||||
|
@ -49,10 +49,63 @@ echo $daten->makeTable($wetter, 'rad', NULL, NULL, 'Fahrradwetter');
|
||||||
|
|
||||||
<p><br /></p>
|
<p><br /></p>
|
||||||
<div class="text-center bg-info"><p><small>Daten via <a href="http://www.wunderground.com/?apiref=5493fcc3357cb244">Wunderground</a>, alle 10 Minuten neu abgerufen.</small></p>
|
<div class="text-center bg-info"><p><small>Daten via <a href="http://www.wunderground.com/?apiref=5493fcc3357cb244">Wunderground</a>, alle 10 Minuten neu abgerufen.</small></p>
|
||||||
<p>Fahrradwetter hat eine Regenwahrscheinlichkeit unter 40%, Temperaturen zwischen 15 und 24°C und Wind durchschnittlich langsamer als 35km/h.</p>
|
<p>Fahrradwetter hat eine Regenwahrscheinlichkeit unter 40%, Temperaturen zwischen 15 und 24°C und Wind durchschnittlich langsamer als 35km/h, soweit nicht anders eingestellt.</p>
|
||||||
<h6>Immer trocken unterwegs mit <a href="http://mainboarder.de">Mainboarder</a> | Code auf <a href="https://github.com/mainboarder/Fahrradwetter">Github</a></h6>
|
<h6>Immer trocken unterwegs mit <a href="http://mainboarder.de">Mainboarder</a> | Code auf <a href="https://github.com/mainboarder/Fahrradwetter">Github</a></h6>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<h4 class="trigger"><a href="#">Optionen</a></h4>
|
||||||
|
<div class="toggle_container row">
|
||||||
|
<h3>Schwellwerte</h3>
|
||||||
|
<form action="index.php" method="post">
|
||||||
|
<div class="col-md-3">
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="regenwahrscheinlichkeit">Regenwahrscheinlichkeit</label>
|
||||||
|
<select name="regenwahrscheinlichkeit" size="1" class="form-control">
|
||||||
|
<option value="10">10%</option>
|
||||||
|
<option value="20">20%</option>
|
||||||
|
<option value="30">30%</option>
|
||||||
|
<option value="40" selected>40%</option>
|
||||||
|
<option value="50">50%</option>
|
||||||
|
<option value="60">60%</option>
|
||||||
|
<option value="70">70%</option>
|
||||||
|
<option value="80">80%</option>
|
||||||
|
<option value="90">90%</option>
|
||||||
|
<option value="100">100%</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<button type="submit" class="button">Speichern</button>
|
||||||
|
</div>
|
||||||
|
<div class="col-md-3">
|
||||||
|
<label for="temperaturunten">Untere Temperatur</label>
|
||||||
|
<select name="temperaturunten" size="1" class="form-control">
|
||||||
|
<option value="0">0°C</option>
|
||||||
|
<option value="5">5°C</option>
|
||||||
|
<option value="10">10°C</option>
|
||||||
|
<option value="15" selected>15°C</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div class="col-md-3">
|
||||||
|
<label for="temperaturoben">Obere Temperatur</label>
|
||||||
|
<select name="temperaturoben" size="1" class="form-control">
|
||||||
|
<option value="20">20°C</option>
|
||||||
|
<option value="25" selected>25°C</option>
|
||||||
|
<option value="30">30°C</option>
|
||||||
|
<option value="35">35°C</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div class="col-md-3">
|
||||||
|
<label for="wind">Max. Wind</label>
|
||||||
|
<select name="wind" size="1" class="form-control">
|
||||||
|
<option value="10">10km/h</option>
|
||||||
|
<option value="15">15km/h</option>
|
||||||
|
<option value="20">20km/h</option>
|
||||||
|
<option value="25">25km/h</option>
|
||||||
|
<option value="30" selected>30km/h</option>
|
||||||
|
<option value="35">35km/h</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
|
|
||||||
$(document).ready( function() {
|
$(document).ready( function() {
|
||||||
|
|
106
wetter.php
106
wetter.php
|
@ -11,7 +11,7 @@ class wetter {
|
||||||
* @param string Stadtname
|
* @param string Stadtname
|
||||||
* @return array Wetterdaten
|
* @return array Wetterdaten
|
||||||
*/
|
*/
|
||||||
function getWeather(){
|
public function getWeather(){
|
||||||
// JSON holen
|
// JSON holen
|
||||||
$json_string = file_get_contents('api.json');
|
$json_string = file_get_contents('api.json');
|
||||||
$json_gelesen = json_decode($json_string, true);
|
$json_gelesen = json_decode($json_string, true);
|
||||||
|
@ -29,25 +29,8 @@ class wetter {
|
||||||
$wind[$i] = $json_gelesen['forecast']['simpleforecast']
|
$wind[$i] = $json_gelesen['forecast']['simpleforecast']
|
||||||
['forecastday'][$i]['avewind']['kph'];
|
['forecastday'][$i]['avewind']['kph'];
|
||||||
|
|
||||||
// Fahrradwetter? - Grundsätzlich ja.
|
$fahrrad = $this->getEmpfehlung($i, $regen[$i], $tempMax[$i],
|
||||||
$fahrrad = '<b>Ja</b>';
|
$wind[$i]);
|
||||||
|
|
||||||
// Vielleicht, wenn Regenwahrscheinlichkeit größer als 40% oder
|
|
||||||
// Temperaturen nicht zwischen 15 und 24°C
|
|
||||||
// oder Wind 35 km/h oder schneller
|
|
||||||
if($regen[$i] >= 40 || $tempMax[$i] <= 15 || $tempMax[$i] > 24
|
|
||||||
|| $wind[$i] >= 35){
|
|
||||||
|
|
||||||
$fahrrad = '<a href="'. WETTERSTATION .'">Vielleicht</a>';
|
|
||||||
}
|
|
||||||
// Kein Fahrradwetter, wenn Regenwahrscheinlichkeit über 55%
|
|
||||||
// oder Temperaturen nicht zwischen 10 und 27°C
|
|
||||||
// oder Wind 40 km/h oder schneller
|
|
||||||
if($tempMax[$i] >= 27 || $tempMax[$i] <= 10 || $regen[$i] >= 55
|
|
||||||
|| $wind[$i] >= 40){
|
|
||||||
|
|
||||||
$fahrrad = 'Nein';
|
|
||||||
}
|
|
||||||
|
|
||||||
// Array mit den Daten für einen Tag zusammenbauen
|
// Array mit den Daten für einen Tag zusammenbauen
|
||||||
$wetter[$i] =
|
$wetter[$i] =
|
||||||
|
@ -80,7 +63,7 @@ class wetter {
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
function makeTable($array, $schluessel, $stringVor = NULL,
|
public function makeTable($array, $schluessel, $stringVor = NULL,
|
||||||
$stringNach = NULL, $first = NULL){
|
$stringNach = NULL, $first = NULL){
|
||||||
|
|
||||||
$string = '<td>'.$first.'</td>';
|
$string = '<td>'.$first.'</td>';
|
||||||
|
@ -95,4 +78,85 @@ class wetter {
|
||||||
<tr>' . $string . '</tr>
|
<tr>' . $string . '</tr>
|
||||||
';
|
';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filtere POST auf Integer
|
||||||
|
* @param string $name Variablenname
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function filter_post_int($name){
|
||||||
|
return filter_input(INPUT_POST, $name, FILTER_SANITIZE_NUMBER_INT);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filtere POST auf Integer
|
||||||
|
* @param string $name Variablenname
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function filter_cookie_int($name){
|
||||||
|
return filter_input(INPUT_COOKIE, $name, FILTER_SANITIZE_NUMBER_INT);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gib eine Empfehlung anhander der Nutzereinstellungenab, ob Rad gefahren
|
||||||
|
* werden kann.
|
||||||
|
* @param int $i Laufvariable
|
||||||
|
* @param int $regen aktueller Vorhersagewert für den Regen
|
||||||
|
* @param int $temp aktueller Vorhersagewert für die Temperatur
|
||||||
|
* @param int $wind aktueller Vorhersagewert für die Windgeschwindigkeit
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function getEmpfehlung($i, $regen, $temp, $wind){
|
||||||
|
|
||||||
|
// Werte einrichten
|
||||||
|
// Regen
|
||||||
|
if(isset($_COOKIE['regen'])){
|
||||||
|
$regenWert = $this->filter_cookie_int('regen');
|
||||||
|
}else{
|
||||||
|
$regenWert = 55;
|
||||||
|
}
|
||||||
|
|
||||||
|
// min. Temperatur
|
||||||
|
if(isset($_COOKIE['templow'])){
|
||||||
|
$tempLowWert = $this->filter_cookie_int('templow');
|
||||||
|
}else{
|
||||||
|
$tempLowWert = 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
// max. Temperatur
|
||||||
|
if(isset($_COOKIE['tempmax'])){
|
||||||
|
$tempMaxWert = $this->filter_cookie_int('tempmax');
|
||||||
|
}else{
|
||||||
|
$tempMaxWert = 27;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wind
|
||||||
|
if(isset($_COOKIE['wind'])){
|
||||||
|
$windWert = $this->filter_cookie_int('wind');
|
||||||
|
}else{
|
||||||
|
$windWert = 35;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fahrradwetter? - Grundsätzlich ja.
|
||||||
|
$fahrrad = '<b>Ja</b>';
|
||||||
|
|
||||||
|
// Vielleicht, wenn Regenwahrscheinlichkeit größer als 40% oder
|
||||||
|
// Temperaturen nicht zwischen 15 und 24°C
|
||||||
|
// oder Wind 35 km/h oder schneller
|
||||||
|
if(empty($_COOKIE['wind']) && $regen >= 40 || $temp <= 15 || $temp > 24
|
||||||
|
|| $wind >= $windWert){
|
||||||
|
|
||||||
|
$fahrrad = '<a href="'. WETTERSTATION .'">Vielleicht</a>';
|
||||||
|
}
|
||||||
|
// Kein Fahrradwetter, wenn Regenwahrscheinlichkeit über 55%
|
||||||
|
// oder Temperaturen nicht zwischen 10 und 27°C
|
||||||
|
// oder Wind 40 km/h oder schneller
|
||||||
|
if($temp >= $tempMaxWert || $temp <= $tempLowWert || $regen >= $regenWert
|
||||||
|
|| $wind >= $windWert){
|
||||||
|
|
||||||
|
$fahrrad = 'Nein';
|
||||||
|
}
|
||||||
|
|
||||||
|
return $fahrrad;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue