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;
|
|
|
|
|
|
|
|
use \OCP\IRequest;
|
|
|
|
use \OCP\AppFramework\Http\TemplateResponse;
|
|
|
|
use \OCP\AppFramework\Controller;
|
|
|
|
use \OCP\AppFramework\Http\JSONResponse;
|
|
|
|
use \OCP\AppFramework\Http;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
class WeatherController extends Controller {
|
|
|
|
|
|
|
|
private $userId;
|
|
|
|
private $mapper;
|
2015-10-25 09:57:07 +00:00
|
|
|
private $settingsMapper;
|
|
|
|
private $apiKey;
|
2015-10-25 10:30:05 +00:00
|
|
|
private $metric;
|
|
|
|
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
|
|
|
|
2015-10-25 09:57:07 +00:00
|
|
|
public function __construct ($appName, IRequest $request, $userId, CityMapper $mapper, SettingsMapper $settingsMapper) {
|
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;
|
|
|
|
$this->apiKey = $settingsMapper->getApiKey($this->userId);
|
2015-10-25 10:30:05 +00:00
|
|
|
$this->metric = $settingsMapper->getMetric($this->userId);
|
2015-07-11 16:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*/
|
|
|
|
public function get($name) {
|
|
|
|
$cityInfos = $this->getCityInformations($name);
|
|
|
|
if (!$cityInfos) {
|
|
|
|
return new JSONResponse(array(), Http::STATUS_NOT_FOUND);
|
|
|
|
}
|
|
|
|
return new JSONResponse($cityInfos);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getCityInformations ($name) {
|
|
|
|
// @TODO setting for metric
|
2015-10-25 10:30:05 +00:00
|
|
|
$cityDatas = json_decode(file_get_contents(WeatherController::$apiWeatherURL.$name."&APPID=".$this->apiKey."&units=".$this->metric), true);
|
2015-07-11 16:55:00 +00:00
|
|
|
if ($cityDatas['cod'] != '200') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-07-15 19:38:29 +00:00
|
|
|
$cityDatas["forecast"] = array();
|
2015-10-25 10:30:05 +00:00
|
|
|
$forecast = json_decode(file_get_contents(WeatherController::$apiForecastURL.$name."&APPID=".$this->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 ?
|
|
|
|
$maxFC = $forecast['cnt'] > 8 ? 8 : $forecast['cnt'];
|
|
|
|
for ($i = 0; $i < $maxFC; $i++) {
|
|
|
|
$cityDatas['forecast'][] = array(
|
|
|
|
'hour' => $forecast['list'][$i]['dt'],
|
|
|
|
'weather' => $forecast['list'][$i]['weather'][0]['description'],
|
|
|
|
'temperature' => $forecast['list'][$i]['main']['temp'],
|
|
|
|
'pressure' => $forecast['list'][$i]['main']['pressure'],
|
|
|
|
'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
|
|
|
|
|
|
|
private function windDegToString($deg) {
|
|
|
|
if ($deg > 0 && $deg < 23 ||
|
|
|
|
$deg > 333) {
|
|
|
|
return "North";
|
|
|
|
}
|
|
|
|
else if ($deg > 22 && $deg < 67) {
|
|
|
|
return "North-East";
|
|
|
|
}
|
|
|
|
else if ($deg > 66 && $deg < 113) {
|
|
|
|
return "East";
|
|
|
|
}
|
|
|
|
else if ($deg > 112 && $deg < 157) {
|
|
|
|
return "South-East";
|
|
|
|
}
|
|
|
|
else if ($deg > 156 && $deg < 201) {
|
|
|
|
return "South";
|
|
|
|
}
|
|
|
|
else if ($deg > 200 && $deg < 245) {
|
|
|
|
return "South-West";
|
|
|
|
}
|
|
|
|
else if ($deg > 244 && $deg < 289) {
|
|
|
|
return "West";
|
|
|
|
}
|
|
|
|
else if ($deg > 288 && $deg < 334) {
|
|
|
|
return "North-West";
|
|
|
|
}
|
|
|
|
}
|
2015-07-11 16:55:00 +00:00
|
|
|
};
|
|
|
|
?>
|