2015-07-11 16:55:00 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ownCloud - Weather
|
|
|
|
*
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later. See the COPYING file.
|
|
|
|
*
|
|
|
|
* @author Loic Blot <loic.blot@unix-experience.fr>
|
|
|
|
* @copyright Loic Blot 2015
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Weather\Controller;
|
|
|
|
|
2017-07-03 21:12:50 +00:00
|
|
|
use \OCP\IConfig;
|
2015-07-11 16:55:00 +00:00
|
|
|
use \OCP\IRequest;
|
|
|
|
use \OCP\AppFramework\Http\TemplateResponse;
|
|
|
|
use \OCP\AppFramework\Controller;
|
|
|
|
use \OCP\AppFramework\Http\JSONResponse;
|
|
|
|
use \OCP\AppFramework\Http;
|
2019-05-19 08:30:28 +00:00
|
|
|
use \OCP\IL10N;
|
2015-07-11 16:55:00 +00:00
|
|
|
|
|
|
|
use \OCA\Weather\Db\CityMapper;
|
2015-10-25 09:57:07 +00:00
|
|
|
use \OCA\Weather\Db\SettingsMapper;
|
2015-07-11 16:55:00 +00:00
|
|
|
|
2015-11-21 08:41:12 +00:00
|
|
|
use \OCA\Weather\Controller\IntermediateController;
|
|
|
|
|
|
|
|
class WeatherController extends IntermediateController {
|
2015-07-11 16:55:00 +00:00
|
|
|
|
|
|
|
private $userId;
|
|
|
|
private $mapper;
|
2015-10-25 09:57:07 +00:00
|
|
|
private $settingsMapper;
|
2015-10-25 10:30:05 +00:00
|
|
|
private $metric;
|
2017-07-03 21:13:40 +00:00
|
|
|
private $config;
|
2019-05-19 08:30:28 +00:00
|
|
|
private $trans;
|
2015-10-25 10:30:05 +00:00
|
|
|
private static $apiWeatherURL = "http://api.openweathermap.org/data/2.5/weather?mode=json&q=";
|
|
|
|
private static $apiForecastURL = "http://api.openweathermap.org/data/2.5/forecast?mode=json&q=";
|
2015-07-11 16:55:00 +00:00
|
|
|
|
2019-05-19 08:30:28 +00:00
|
|
|
public function __construct ($appName, IConfig $config, IRequest $request, $userId, CityMapper $mapper, SettingsMapper $settingsMapper, IL10N $trans) {
|
2015-07-11 16:55:00 +00:00
|
|
|
parent::__construct($appName, $request);
|
|
|
|
$this->userId = $userId;
|
|
|
|
$this->mapper = $mapper;
|
2015-10-25 09:57:07 +00:00
|
|
|
$this->settingsMapper = $settingsMapper;
|
2015-10-25 10:30:05 +00:00
|
|
|
$this->metric = $settingsMapper->getMetric($this->userId);
|
2017-07-03 21:13:40 +00:00
|
|
|
$this->config = $config;
|
2019-05-19 08:30:28 +00:00
|
|
|
$this->trans = $trans;
|
2015-07-11 16:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*/
|
|
|
|
public function get($name) {
|
|
|
|
$cityInfos = $this->getCityInformations($name);
|
|
|
|
if (!$cityInfos) {
|
2015-10-25 12:04:03 +00:00
|
|
|
return new JSONResponse(array(), $this->errorCode);
|
2015-07-11 16:55:00 +00:00
|
|
|
}
|
|
|
|
return new JSONResponse($cityInfos);
|
|
|
|
}
|
|
|
|
|
2019-05-19 08:30:28 +00:00
|
|
|
public function getLanguageCode() {
|
|
|
|
return $this->trans->getLanguageCode();
|
|
|
|
}
|
|
|
|
|
2015-07-11 16:55:00 +00:00
|
|
|
private function getCityInformations ($name) {
|
2019-05-19 08:30:28 +00:00
|
|
|
|
2017-07-03 21:12:50 +00:00
|
|
|
$apiKey = $this->config->getAppValue($this->appName, 'openweathermap_api_key');
|
2015-11-21 08:41:12 +00:00
|
|
|
$name = preg_replace("[ ]",'%20',$name);
|
2019-05-19 08:30:28 +00:00
|
|
|
|
|
|
|
$openWeatherMapLang = array("ar", "bg", "ca", "cz", "de", "el", "en", "fa", "fi", "fr", "gl", "hr", "hu", "it", "ja", "kr", "la", "lt", "mk", "nl", "pl", "pt", "ro", "ru", "se", "sk", "sl", "es", "tr", "ua", "vi");
|
|
|
|
$currentLang = \OC::$server->getL10N('core')->getLanguageCode();
|
|
|
|
|
|
|
|
if (preg_match("/_/i", $currentLang)) {
|
|
|
|
$currentLang = strstr($currentLang, '_', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_array($currentLang, $openWeatherMapLang)) {
|
|
|
|
$reqContent = $this->curlGET(WeatherController::$apiWeatherURL.$name."&APPID=".$apiKey."&units=".$this->metric."&lang=".$currentLang);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$reqContent = $this->curlGET(WeatherController::$apiWeatherURL.$name."&APPID=".$apiKey."&units=".$this->metric);
|
|
|
|
}
|
|
|
|
|
2015-10-25 12:04:03 +00:00
|
|
|
if ($reqContent[0] != Http::STATUS_OK) {
|
|
|
|
$this->errorCode = $reqContent[0];
|
2015-07-11 16:55:00 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-10-25 12:04:03 +00:00
|
|
|
$cityDatas = json_decode($reqContent[1], true);
|
2019-05-19 08:30:28 +00:00
|
|
|
$cityDatas["forecast"] = array();
|
|
|
|
|
|
|
|
if (in_array($currentLang, $openWeatherMapLang)) {
|
|
|
|
$forecast = json_decode(file_get_contents(WeatherController::$apiForecastURL.$name."&APPID=".$apiKey."&units=".$this->metric."&lang=".$currentLang), true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$forecast = json_decode(file_get_contents(WeatherController::$apiForecastURL.$name."&APPID=".$apiKey."&units=".$this->metric), true);
|
|
|
|
}
|
|
|
|
|
2015-07-15 19:38:29 +00:00
|
|
|
if ($forecast['cod'] == '200' && isset($forecast['cnt']) && is_numeric($forecast['cnt'])) {
|
|
|
|
// Show only 8 values max
|
|
|
|
// @TODO: setting ?
|
2019-05-19 08:30:28 +00:00
|
|
|
$maxFC = $forecast['cnt'] > 40 ? 40 : $forecast['cnt'];
|
2015-07-15 19:38:29 +00:00
|
|
|
for ($i = 0; $i < $maxFC; $i++) {
|
|
|
|
$cityDatas['forecast'][] = array(
|
2019-05-19 08:30:28 +00:00
|
|
|
'date' => $this->UnixTimeToString($forecast['list'][$i]['dt']),
|
2015-07-15 19:38:29 +00:00
|
|
|
'weather' => $forecast['list'][$i]['weather'][0]['description'],
|
|
|
|
'temperature' => $forecast['list'][$i]['main']['temp'],
|
|
|
|
'pressure' => $forecast['list'][$i]['main']['pressure'],
|
2019-05-27 15:36:04 +00:00
|
|
|
'humidity' => $forecast['list'][$i]['main']['humidity'],
|
2015-07-15 19:38:29 +00:00
|
|
|
'wind' => array(
|
|
|
|
'speed' => $forecast['list'][$i]['wind']['speed'],
|
|
|
|
'desc' => $this->windDegToString($forecast['list'][$i]['wind']['deg'])
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-11 16:55:00 +00:00
|
|
|
return $cityDatas;
|
|
|
|
}
|
2015-07-15 19:38:29 +00:00
|
|
|
|
2019-05-19 08:30:28 +00:00
|
|
|
private function windDegToString($deg) {
|
|
|
|
|
2015-07-15 19:38:29 +00:00
|
|
|
if ($deg > 0 && $deg < 23 ||
|
|
|
|
$deg > 333) {
|
2019-05-19 08:30:28 +00:00
|
|
|
return $this->trans->t('North');
|
2015-07-15 19:38:29 +00:00
|
|
|
}
|
|
|
|
else if ($deg > 22 && $deg < 67) {
|
2019-05-19 08:30:28 +00:00
|
|
|
return $this->trans->t('North-East');
|
2015-07-15 19:38:29 +00:00
|
|
|
}
|
|
|
|
else if ($deg > 66 && $deg < 113) {
|
2019-05-19 08:30:28 +00:00
|
|
|
return $this->trans->t('East');
|
2015-07-15 19:38:29 +00:00
|
|
|
}
|
|
|
|
else if ($deg > 112 && $deg < 157) {
|
2019-05-19 08:30:28 +00:00
|
|
|
return $this->trans->t('South-East');
|
2015-07-15 19:38:29 +00:00
|
|
|
}
|
|
|
|
else if ($deg > 156 && $deg < 201) {
|
2019-05-19 08:30:28 +00:00
|
|
|
return $this->trans->t('South');
|
2015-07-15 19:38:29 +00:00
|
|
|
}
|
|
|
|
else if ($deg > 200 && $deg < 245) {
|
2019-05-19 08:30:28 +00:00
|
|
|
return $this->trans->t('South-West');
|
2015-07-15 19:38:29 +00:00
|
|
|
}
|
|
|
|
else if ($deg > 244 && $deg < 289) {
|
2019-05-19 08:30:28 +00:00
|
|
|
return $this->trans->t('West');
|
2015-07-15 19:38:29 +00:00
|
|
|
}
|
|
|
|
else if ($deg > 288 && $deg < 334) {
|
2019-05-19 08:30:28 +00:00
|
|
|
return $this->trans->t('North-West');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function UnixTimeToString($unixtime) {
|
|
|
|
|
|
|
|
if (date("l", $unixtime) == "Monday") {
|
|
|
|
return $this->trans->t('Monday') . " " . date("H:i",$unixtime);
|
|
|
|
}
|
|
|
|
else if (date("l", $unixtime) == "Tuesday") {
|
|
|
|
return $this->trans->t('Tuesday') . " " . date("H:i",$unixtime);
|
|
|
|
}
|
|
|
|
else if (date("l", $unixtime) == "Wednesday") {
|
|
|
|
return $this->trans->t('Wednesday') . " " . date("H:i",$unixtime);
|
|
|
|
}
|
|
|
|
else if (date("l", $unixtime) == "Thursday") {
|
|
|
|
return $this->trans->t('Thursday') . " " . date("H:i",$unixtime);
|
|
|
|
}
|
|
|
|
else if (date("l", $unixtime) == "Friday") {
|
|
|
|
return $this->trans->t('Friday') . " " . date("H:i",$unixtime);
|
|
|
|
}
|
|
|
|
else if (date("l", $unixtime) == "Saturday") {
|
|
|
|
return $this->trans->t('Saturday') . " " . date("H:i",$unixtime);
|
|
|
|
}
|
|
|
|
else if (date("l", $unixtime) == "Sunday") {
|
|
|
|
return $this->trans->t('Sunday') . " " . date("H:i",$unixtime);
|
2015-07-15 19:38:29 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-11 16:55:00 +00:00
|
|
|
};
|
2019-05-19 08:30:28 +00:00
|
|
|
|
2015-07-11 16:55:00 +00:00
|
|
|
?>
|