diff --git a/class_weather.php b/class_weather.php index 97a0e91..75a6417 100644 --- a/class_weather.php +++ b/class_weather.php @@ -11,38 +11,54 @@ class Weather { public $sunrise = null; // Equals DateTime of Sunrise public $sunset = null; // Equals DateTime of Sunset - const WEATHERURL = "https://api.openweathermap.org/data/3.0/onecall" - . "?lat=" . WEATHERLATTITUDE - . "&lon=" . WEATHERLONGITUDE - . "&appid=" . WEATHERAPIKEY - . "&units=" . WEATHERUNITS; + private static function weatherURL(): string { + return "https://api.openweathermap.org/data/3.0/onecall?" . http_build_query([ + "lat" => WEATHERLATTITUDE, + "lon" => WEATHERLONGITUDE, + "appid" => WEATHERAPIKEY, + "units" => WEATHERUNITS, + "exclude" => "minutely,hourly,alerts", // only "current" and "daily" are used + ]); + } - public static function deleteOldWeather($days = 10) { + public static function deleteOldWeather(int $days = 10): bool { if ( !is_int($days) ) return false; if ( $days < 0 ) return false; $query = "DELETE FROM " . AppDB::TABLE_WEATHER . " WHERE DATEDIFF(NOW(), sampledate) > :days"; $sth = AppDB::getSTH($query); if ( $sth === false ) return false; $sth->bindValue(":days", $days, PDO::PARAM_INT); - $sth->execute(); + return $sth->execute(); } - public static function fetchWeather() { - $weather = file_get_contents(Weather::WEATHERURL); + public static function fetchWeather(): bool { + $context = stream_context_create(["http" => ["timeout" => 10]]); + $response = @file_get_contents(self::weatherURL(), false, $context); + if ( $response === false ) { + error_log("Tasks Weather: API request failed"); + return false; + } + $json = json_decode($response); + if ( !isset($data["current"]["dt"], $data["daily"][0]["temp"]) ) { + error_log("Tasks Weather: API response could not be parsed"); + return false; + } $query = "INSERT INTO " . AppDB::TABLE_WEATHER . " (sampledate, weather) VALUES(NOW(), :weather)"; $sth = AppDB::getSTH($query); if ( $sth === false ) return false; - $sth->bindValue(":weather", $weather, PDO::PARAM_STR); - $sth->execute(); + $sth->bindValue(":weather", $response, PDO::PARAM_STR); + return $sth->execute(); } - public static function getWeather() { + public static function getWeather(): bool|array { $query = "SELECT weather FROM " . AppDB::TABLE_WEATHER . " ORDER BY sampledate DESC LIMIT 1"; $sth = AppDB::getSTH($query); if ( $sth === false ) return false; $sth->execute(); if ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) { - return json_decode($row['weather'], true); + $json = json_decode($row['weather'], true); + if ( is_null($json) ) return false; + return $json; } else { return false; } @@ -50,6 +66,7 @@ class Weather { public function __construct() { $weather = self::getWeather(); + if ( $weather === false ) return; $this->weather = $weather; $this->coord = array("lat" => $weather["lat"], "lon" => $weather["lon"]); $this->conditions = $weather["current"]["weather"][0]; @@ -67,3 +84,5 @@ class Weather { $this->sunset->setTimeZone(new DateTimeZone(date_default_timezone_get())); } } + +// vim: et sw=4 ts=4 ai: