Permit to set the application metric in the settings

master
Loic Blot 2015-10-25 11:30:05 +01:00
parent e863dd3dba
commit 96bd17cf4f
6 changed files with 103 additions and 15 deletions

View File

@ -25,5 +25,7 @@ $application->registerRoutes($this, array('routes' => array(
array('name' => 'settings#homeset', 'url' => '/settings/home/set', 'verb' => 'POST'), array('name' => 'settings#homeset', 'url' => '/settings/home/set', 'verb' => 'POST'),
array('name' => 'settings#apikeyset', 'url' => '/settings/apikey/set','verb' => 'POST'), array('name' => 'settings#apikeyset', 'url' => '/settings/apikey/set','verb' => 'POST'),
array('name' => 'settings#apikeyget', 'url' => '/settings/apikey/get','verb' => 'GET'), array('name' => 'settings#apikeyget', 'url' => '/settings/apikey/get','verb' => 'GET'),
array('name' => 'settings#metricset', 'url' => '/settings/metric/set','verb' => 'POST'),
array('name' => 'settings#metricget', 'url' => '/settings/metric/get','verb' => 'GET'),
))); )));
?> ?>

View File

@ -66,5 +66,27 @@ class SettingsController extends Controller {
return new JSONResponse(array("apikey" => $this->mapper->getApiKey($this->userId))); return new JSONResponse(array("apikey" => $this->mapper->getApiKey($this->userId)));
} }
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function metricSet ($metric) {
$this->mapper->setMetric($this->userId, $metric);
return new JSONResponse(array("set" => true));
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function metricGet () {
$metric = $this->mapper->getMetric($this->userId);
if ($metric === 0) {
$this->mapper->setMetric($this->userId, "metric");
$metric = "metric";
}
return new JSONResponse(array("metric" => $metric));
}
}; };
?> ?>

View File

@ -26,8 +26,9 @@ class WeatherController extends Controller {
private $mapper; private $mapper;
private $settingsMapper; private $settingsMapper;
private $apiKey; private $apiKey;
private static $apiWeatherURL = "http://api.openweathermap.org/data/2.5/weather?mode=json&units=metric&q="; private $metric;
private static $apiForecastURL = "http://api.openweathermap.org/data/2.5/forecast?mode=json&units=metric&q="; 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=";
public function __construct ($appName, IRequest $request, $userId, CityMapper $mapper, SettingsMapper $settingsMapper) { public function __construct ($appName, IRequest $request, $userId, CityMapper $mapper, SettingsMapper $settingsMapper) {
parent::__construct($appName, $request); parent::__construct($appName, $request);
@ -35,6 +36,7 @@ class WeatherController extends Controller {
$this->mapper = $mapper; $this->mapper = $mapper;
$this->settingsMapper = $settingsMapper; $this->settingsMapper = $settingsMapper;
$this->apiKey = $settingsMapper->getApiKey($this->userId); $this->apiKey = $settingsMapper->getApiKey($this->userId);
$this->metric = $settingsMapper->getMetric($this->userId);
} }
/** /**
@ -51,13 +53,13 @@ class WeatherController extends Controller {
private function getCityInformations ($name) { private function getCityInformations ($name) {
// @TODO setting for metric // @TODO setting for metric
$cityDatas = json_decode(file_get_contents(WeatherController::$apiWeatherURL.$name."&APPID=".$this->apiKey), true); $cityDatas = json_decode(file_get_contents(WeatherController::$apiWeatherURL.$name."&APPID=".$this->apiKey."&units=".$this->metric), true);
if ($cityDatas['cod'] != '200') { if ($cityDatas['cod'] != '200') {
return null; return null;
} }
$cityDatas["forecast"] = array(); $cityDatas["forecast"] = array();
$forecast = json_decode(file_get_contents(WeatherController::$apiForecastURL.$name."&APPID=".$this->apiKey), true); $forecast = json_decode(file_get_contents(WeatherController::$apiForecastURL.$name."&APPID=".$this->apiKey."&units=".$this->metric), true);
if ($forecast['cod'] == '200' && isset($forecast['cnt']) && is_numeric($forecast['cnt'])) { if ($forecast['cod'] == '200' && isset($forecast['cnt']) && is_numeric($forecast['cnt'])) {
// Show only 8 values max // Show only 8 values max
// @TODO: setting ? // @TODO: setting ?

View File

@ -32,10 +32,18 @@ class SettingsMapper extends Mapper {
$this->setSetting("apikey", $userId, $apiKey); $this->setSetting("apikey", $userId, $apiKey);
} }
public function getApiKey($userId) { public function getApiKey ($userId) {
return $this->getSetting($userId, "apikey"); return $this->getSetting($userId, "apikey");
} }
public function setMetric ($userId, $metric) {
$this->setSetting("metric", $userId, $metric);
}
public function getMetric ($userId) {
return $this->getSetting($userId, "metric");
}
public function setSetting ($settingName, $userId, $settingValue) { public function setSetting ($settingName, $userId, $settingValue) {
\OCP\DB::beginTransaction(); \OCP\DB::beginTransaction();
$query = \OCP\DB::prepare('DELETE FROM *PREFIX*weather_config ' . $query = \OCP\DB::prepare('DELETE FROM *PREFIX*weather_config ' .

View File

@ -26,6 +26,8 @@ app.controller('WeatherController', ['$scope', '$interval', '$timeout', '$compil
$scope.owncloudAppImgPath = ''; $scope.owncloudAppImgPath = '';
$scope.apiKey = ''; $scope.apiKey = '';
$scope.userId = ''; $scope.userId = '';
$scope.metric = 'metric';
$scope.metricRepresentation = '°C';
$scope.cities = []; $scope.cities = [];
$scope.showAddCity = false; $scope.showAddCity = false;
$scope.addCityError = ''; $scope.addCityError = '';
@ -60,27 +62,26 @@ app.controller('WeatherController', ['$scope', '$interval', '$timeout', '$compil
$scope.loadCities(); $scope.loadCities();
}); });
$timeout(function () { $timeout(function () { $scope.loadApiKey(); });
$scope.loadApiKey(); $timeout(function () { $scope.loadMetric(); });
});
$scope.modifyAPIKey = function () { $scope.modifyAPIKey = function () {
$http.post(OC.generateUrl('/apps/weather/settings/apikey/set'), {'apikey': $scope.apiKey}). $http.post(OC.generateUrl('/apps/weather/settings/apikey/set'), {'apikey': $scope.apiKey}).
success(function (data, status, headers, config) { success(function (data, status, headers, config) {
if (data != null && !undef(data['set'])) { if (data != null && !undef(data['set'])) {
// @TODO $scope.loadCity($scope.domCity);
} }
else { else {
$scope.setApiKeyError = 'Failed to set API key. Please contact your administrator'; $scope.settingError = 'Failed to set API key. Please contact your administrator';
} }
}). }).
error(function (data, status, headers, config) { error(function (data, status, headers, config) {
if (status == 403) { if (status == 403) {
$scope.setApiKeyError = "This key doesn't work. Please provide a valid OpenWeatherMap API key"; $scope.settingError = "This key doesn't work. Please provide a valid OpenWeatherMap API key";
} }
}). }).
fail(function (data, status, headers, config) { fail(function (data, status, headers, config) {
$scope.setApiKeyError = g_error500; $scope.settingError = g_error500;
}); });
} }
@ -96,6 +97,53 @@ app.controller('WeatherController', ['$scope', '$interval', '$timeout', '$compil
}); });
}; };
$scope.mapMetric = function () {
if ($scope.metric == 'kelvin') {
$scope.metricRepresentation = '°K';
}
else if ($scope.metric == 'imperial') {
$scope.metricRepresentation = '°F';
}
else {
$scope.metric = 'metric';
$scope.metricRepresentation = '°C';
}
};
$scope.modifyMetric = function () {
$http.post(OC.generateUrl('/apps/weather/settings/metric/set'), {'metric': $scope.metric}).
success(function (data, status, headers, config) {
if (data != null && !undef(data['set'])) {
$scope.mapMetric();
$scope.loadCity($scope.domCity);
}
else {
$scope.settingError = 'Failed to set metric. Please contact your administrator';
}
}).
error(function (data, status, headers, config) {
if (status == 404) {
$scope.settingError = "This metric is not known.";
}
}).
fail(function (data, status, headers, config) {
$scope.settingError = g_error500;
});
}
$scope.loadMetric = function () {
$http.get(OC.generateUrl('/apps/weather/settings/metric/get')).
success(function (data, status, headers, config) {
if (!undef(data['metric'])) {
$scope.metric = data['metric'];
$scope.mapMetric();
}
}).
fail(function (data, status, headers, config) {
$scope.fatalError();
});
};
$scope.loadCities = function () { $scope.loadCities = function () {
$http.get(OC.generateUrl('/apps/weather/city/getall')). $http.get(OC.generateUrl('/apps/weather/city/getall')).
success(function (data, status, headers, config) { success(function (data, status, headers, config) {

View File

@ -31,8 +31,14 @@
<button name="app settings" class="settings-button" data-apps-slide-toggle="#app-settings-content">Settings</button> <button name="app settings" class="settings-button" data-apps-slide-toggle="#app-settings-content">Settings</button>
</div> </div>
<div style="display: none;" id="app-settings-content"> <div style="display: none;" id="app-settings-content">
<h2>API Key</h2> <h2>OpenWeatherMap API Key</h2>
<input type="text" name="apikey" ng-change="modifyAPIKey()" ng-model="apiKey" ng-model-options="{debounce:1000}" /> <input type="text" name="apikey" ng-change="modifyAPIKey()" ng-model="apiKey" ng-model-options="{debounce:1000}" />
<h2>Metric</h2>
<select name="metric" ng-change="modifyMetric()" ng-model="metric">
<option value="metric">°C</option>
<option value="kelvin">°K</option>
<option value="imperial">°F</option>
</select>
</div> </div>
</div> </div>
</div> </div>
@ -46,7 +52,7 @@
<img ng-show="selectedCityId == homeCity" src="{{ owncloudAppImgPath }}/img/home-pick.png" /> <img ng-show="selectedCityId == homeCity" src="{{ owncloudAppImgPath }}/img/home-pick.png" />
<img class="home-icon" ng-click="setHome(selectedCityId);" ng-show="selectedCityId != homeCity" src="{{ owncloudAppImgPath }}/img/home-nopick.png" /> <img class="home-icon" ng-click="setHome(selectedCityId);" ng-show="selectedCityId != homeCity" src="{{ owncloudAppImgPath }}/img/home-nopick.png" />
</div> </div>
<div class="city-current-temp">{{ currentCity.main.temp }}°C</div> <div class="city-current-temp">{{ currentCity.main.temp }}{{ metricRepresentation }}</div>
<div class="city-current-pressure">Pressure: {{ currentCity.main.pressure }} hpa</div> <div class="city-current-pressure">Pressure: {{ currentCity.main.pressure }} hpa</div>
<div class="city-current-humidity">Humidity: {{ currentCity.main.humidity}}%</div> <div class="city-current-humidity">Humidity: {{ currentCity.main.humidity}}%</div>
<div class="city-current-weather">Cloudiness: {{ currentCity.weather[0].description }}</div> <div class="city-current-weather">Cloudiness: {{ currentCity.weather[0].description }}</div>
@ -58,7 +64,7 @@
<tr><th>Hour</th><th>Temperature</th><th>Weather</th><th>Pressure</th><th>Wind</th></tr> <tr><th>Hour</th><th>Temperature</th><th>Weather</th><th>Pressure</th><th>Wind</th></tr>
<tr ng-repeat="forecast in currentCity.forecast"> <tr ng-repeat="forecast in currentCity.forecast">
<td>{{ forecast.hour * 1000 | date:'HH:mm'}}</td> <td>{{ forecast.hour * 1000 | date:'HH:mm'}}</td>
<td>{{ forecast.temperature }}°C</td> <td>{{ forecast.temperature }}{{ metricRepresentation }}</td>
<td>{{ forecast.weather }}</td> <td>{{ forecast.weather }}</td>
<td>{{ forecast.pressure }}</td> <td>{{ forecast.pressure }}</td>
<td>{{ forecast.wind.speed }} m/s - {{ forecast.wind.desc }}</td> <td>{{ forecast.wind.speed }} m/s - {{ forecast.wind.desc }}</td>