2015-07-08 21:29:55 +00:00
/ * *
2015-07-08 21:40:17 +00:00
* ownCloud - Weather
2015-07-08 21:29:55 +00:00
*
* 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 2014 - 2015
* /
var app = angular . module ( 'Weather' , [ ] ) ;
2019-05-05 16:41:07 +00:00
var g _error500 = t ( 'weather' , 'Fatal Error: please check your nextcloud.log and send a bug report here: https://github.com/nextcloud/weather/issues' ) ;
2015-07-08 21:29:55 +00:00
function undef ( obj ) {
2019-05-22 14:05:10 +00:00
return typeof obj === 'undefined' || obj === undefined ;
2015-07-08 21:29:55 +00:00
}
function emptyStr ( obj ) {
return undef ( obj ) || obj == '' ;
}
app . controller ( 'WeatherController' , [ '$scope' , '$interval' , '$timeout' , '$compile' , '$http' ,
function ( $scope , $interval , $timeout , $compile , $http ) {
2015-07-18 07:46:06 +00:00
$scope . owncloudAppImgPath = '' ;
2015-07-08 21:29:55 +00:00
$scope . userId = '' ;
2015-10-25 10:30:05 +00:00
$scope . metric = 'metric' ;
$scope . metricRepresentation = '°C' ;
2015-07-08 21:29:55 +00:00
$scope . cities = [ ] ;
2015-07-08 22:05:57 +00:00
$scope . showAddCity = false ;
$scope . addCityError = '' ;
2015-07-08 21:29:55 +00:00
2015-07-09 19:35:17 +00:00
$scope . cityLoadError = '' ;
2015-10-25 12:04:03 +00:00
$scope . cityLoadNeedsAPIKey = false ;
2015-07-11 15:01:30 +00:00
$scope . currentCity = null ;
2015-07-18 08:01:38 +00:00
$scope . selectedCityId = 0 ;
2015-07-11 16:54:23 +00:00
$scope . domCity = null ;
2015-07-18 07:46:06 +00:00
$scope . homeCity = '' ;
2015-07-09 19:35:17 +00:00
2015-07-11 15:07:40 +00:00
$scope . imageMapper = {
2019-11-09 09:58:01 +00:00
"Clear" : "sun.jpg" ,
2015-07-11 16:16:41 +00:00
"Clouds" : "clouds.png" ,
2017-08-17 10:56:17 +00:00
"Drizzle" : "drizzle.jpg" ,
2019-10-23 05:55:42 +00:00
"Smoke" : "todo.png" ,
"Dust" : "todo.png" ,
2019-10-23 05:56:27 +00:00
"Sand" : "sand.jpg" ,
2019-10-23 05:55:42 +00:00
"Ash" : "todo.png" ,
"Squall" : "todo.png" ,
"Tornado" : "tornado.jpg" ,
2019-11-09 09:58:01 +00:00
"Haze" : "mist.jpg" ,
"Mist" : "mist.jpg" ,
2015-07-11 15:43:34 +00:00
"Rain" : "rain.jpg" ,
2015-07-11 16:16:41 +00:00
"Snow" : "snow.png" ,
2019-11-09 09:58:01 +00:00
"Thunderstorm" : "thunderstorm.jpg" ,
"Fog" : "fog.jpg" ,
2015-07-11 15:07:40 +00:00
}
2015-07-11 16:54:23 +00:00
// Reload weather information each minute
$interval ( function ( ) {
if ( $scope . currentCity != null ) {
$scope . loadCity ( $scope . domCity ) ;
}
} , 60000 ) ;
2015-10-25 09:57:07 +00:00
// timeout functions internal calls cannot be serialized
2015-07-08 21:29:55 +00:00
$timeout ( function ( ) {
2016-12-27 22:41:14 +00:00
var imgPath = OC . filePath ( 'weather' , 'img' , '' ) . replace ( 'index.php/' , '' ) ;
2015-07-18 07:46:06 +00:00
$scope . owncloudAppImgPath = imgPath ;
2015-07-08 21:29:55 +00:00
$scope . loadCities ( ) ;
} ) ;
2015-10-25 10:30:05 +00:00
$timeout ( function ( ) { $scope . loadMetric ( ) ; } ) ;
2015-10-25 09:57:07 +00:00
2015-10-25 10:30:05 +00:00
$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 } ) .
2017-05-02 10:52:18 +00:00
then ( function ( r ) {
if ( r . data != null && ! undef ( r . data [ 'set' ] ) ) {
2015-10-25 10:30:05 +00:00
$scope . mapMetric ( ) ;
$scope . loadCity ( $scope . domCity ) ;
}
else {
2019-05-05 16:41:07 +00:00
$scope . settingError = t ( 'weather' , 'Failed to set metric. Please contact your administrator' ) ;
2015-10-25 10:30:05 +00:00
}
2017-05-02 10:52:18 +00:00
} ,
function ( r ) {
if ( r . status == 404 ) {
2019-05-05 16:41:07 +00:00
$scope . settingError = t ( 'weather' , 'This metric is not known.' ) ;
2015-10-25 10:30:05 +00:00
}
2017-05-02 10:52:18 +00:00
else {
$scope . settingError = g _error500 ;
}
2015-10-25 10:30:05 +00:00
} ) ;
}
$scope . loadMetric = function ( ) {
$http . get ( OC . generateUrl ( '/apps/weather/settings/metric/get' ) ) .
2017-05-02 10:52:18 +00:00
then ( function ( r ) {
if ( ! undef ( r . data [ 'metric' ] ) ) {
$scope . metric = r . data [ 'metric' ] ;
2015-10-25 10:30:05 +00:00
$scope . mapMetric ( ) ;
}
2017-05-02 10:52:18 +00:00
} ,
function ( r ) {
2015-10-25 10:30:05 +00:00
$scope . fatalError ( ) ;
} ) ;
} ;
2015-07-08 21:29:55 +00:00
$scope . loadCities = function ( ) {
2015-07-08 22:05:57 +00:00
$http . get ( OC . generateUrl ( '/apps/weather/city/getall' ) ) .
2017-05-02 10:52:18 +00:00
then ( function ( r ) {
if ( ! undef ( r . data [ 'cities' ] ) ) {
$scope . cities = r . data [ 'cities' ] ;
2015-07-08 21:29:55 +00:00
}
2017-05-02 10:52:18 +00:00
if ( ! undef ( r . data [ 'userid' ] ) ) {
$scope . userId = r . data [ 'userid' ] ;
2015-07-08 21:29:55 +00:00
}
2015-07-18 13:58:51 +00:00
2017-05-02 10:52:18 +00:00
if ( ! undef ( r . data [ 'home' ] ) ) {
$scope . homeCity = r . data [ 'home' ] ;
2015-07-18 13:58:51 +00:00
if ( $scope . homeCity ) {
2019-05-22 14:05:10 +00:00
for ( var i = 0 ; i < $scope . cities . length ; i ++ ) {
2015-07-18 13:58:51 +00:00
if ( $scope . cities [ i ] . id == $scope . homeCity ) {
$scope . loadCity ( $scope . cities [ i ] ) ;
return ;
}
}
}
}
2017-05-02 15:30:24 +00:00
// If no home found, load first city found
if ( $scope . cities . length > 0 ) {
$scope . loadCity ( $scope . cities [ 0 ] ) ;
}
2017-05-02 10:52:18 +00:00
} ,
function ( r ) {
2015-07-08 21:29:55 +00:00
$scope . fatalError ( ) ;
} ) ;
} ;
2015-07-08 22:05:57 +00:00
2015-07-09 19:35:17 +00:00
$scope . loadCity = function ( city ) {
if ( undef ( city ) || emptyStr ( city . name ) ) {
alert ( g _error500 ) ;
return ;
}
2015-07-11 15:01:30 +00:00
$http . get ( OC . generateUrl ( '/apps/weather/weather/get?name=' + city . name ) ) .
2017-05-02 10:52:18 +00:00
then ( function ( r ) {
if ( r . data != null ) {
2015-07-11 16:54:23 +00:00
$scope . domCity = city ;
2017-05-02 10:52:18 +00:00
$scope . currentCity = r . data ;
2015-07-18 08:01:38 +00:00
$scope . selectedCityId = city . id ;
2015-07-11 15:07:40 +00:00
$scope . currentCity . image = $scope . imageMapper [ $scope . currentCity . weather [ 0 ] . main ] ;
2015-07-11 15:43:34 +00:00
$scope . currentCity . wind . desc = "" ;
if ( $scope . currentCity . wind . deg > 0 && $scope . currentCity . wind . deg < 23 ||
$scope . currentCity . wind . deg > 333 ) {
2019-05-05 16:41:07 +00:00
$scope . currentCity . wind . desc = t ( 'weather' , 'North' ) ;
2015-07-11 15:43:34 +00:00
}
else if ( $scope . currentCity . wind . deg > 22 && $scope . currentCity . wind . deg < 67 ) {
2019-05-05 16:41:07 +00:00
$scope . currentCity . wind . desc = t ( 'weather' , 'North-East' ) ;
2015-07-11 15:43:34 +00:00
}
else if ( $scope . currentCity . wind . deg > 66 && $scope . currentCity . wind . deg < 113 ) {
2019-05-05 16:41:07 +00:00
$scope . currentCity . wind . desc = t ( 'weather' , 'East' ) ;
2015-07-11 15:43:34 +00:00
}
else if ( $scope . currentCity . wind . deg > 112 && $scope . currentCity . wind . deg < 157 ) {
2019-05-05 16:41:07 +00:00
$scope . currentCity . wind . desc = t ( 'weather' , 'South-East' ) ;
2015-07-11 15:43:34 +00:00
}
else if ( $scope . currentCity . wind . deg > 156 && $scope . currentCity . wind . deg < 201 ) {
2019-05-05 16:41:07 +00:00
$scope . currentCity . wind . desc = t ( 'weather' , 'South' ) ;
2015-07-11 15:43:34 +00:00
}
else if ( $scope . currentCity . wind . deg > 200 && $scope . currentCity . wind . deg < 245 ) {
2019-05-05 16:41:07 +00:00
$scope . currentCity . wind . desc = t ( 'weather' , 'South-West' ) ;
2015-07-11 15:43:34 +00:00
}
else if ( $scope . currentCity . wind . deg > 244 && $scope . currentCity . wind . deg < 289 ) {
2019-05-05 16:41:07 +00:00
$scope . currentCity . wind . desc = t ( 'weather' , 'West' ) ;
2015-07-11 15:43:34 +00:00
}
2019-05-22 14:05:10 +00:00
else if ( $scope . currentCity . wind . deg > 288 ) {
2019-05-05 16:41:07 +00:00
$scope . currentCity . wind . desc = t ( 'weather' , 'North-West' ) ;
2015-07-11 15:43:34 +00:00
}
2015-10-25 10:55:17 +00:00
$scope . cityLoadError = '' ;
2015-07-09 19:35:17 +00:00
}
else {
2019-05-05 16:41:07 +00:00
$scope . cityLoadError = t ( 'weather' , 'Failed to get city weather informations. Please contact your administrator' ) ;
2015-07-09 19:35:17 +00:00
}
2015-10-25 12:04:03 +00:00
$scope . cityLoadNeedsAPIKey = false ;
2017-05-02 10:52:18 +00:00
} ,
function ( r ) {
if ( r . status == 404 ) {
2019-05-05 16:41:07 +00:00
$scope . cityLoadError = t ( 'weather' , 'No city with this name found.' ) ;
2015-10-25 12:04:03 +00:00
$scope . cityLoadNeedsAPIKey = false ;
}
2017-05-02 10:52:18 +00:00
else if ( r . status == 401 ) {
2022-09-04 12:57:16 +00:00
$scope . cityLoadError = t ( 'weather' , 'Your OpenWeatherMap API key is invalid. Contact your administrator to configure a valid API key in Additional settings of the Administration' ) ;
2015-10-25 12:04:03 +00:00
$scope . cityLoadNeedsAPIKey = true ;
2015-07-09 19:35:17 +00:00
}
2017-05-02 10:52:18 +00:00
else {
2015-07-09 19:35:17 +00:00
$scope . cityLoadError = g _error500 ;
2015-10-25 12:04:03 +00:00
$scope . cityLoadNeedsAPIKey = false ;
2015-07-09 19:35:17 +00:00
}
} ) ;
}
2015-07-08 22:05:57 +00:00
$scope . addCity = function ( city ) {
if ( undef ( city ) || emptyStr ( city . name ) ) {
2019-11-23 09:35:38 +00:00
$scope . addCityError = t ( 'weather' , 'Empty city name!' ) ;
2015-07-08 22:05:57 +00:00
return ;
}
$http . post ( OC . generateUrl ( '/apps/weather/city/add' ) , { 'name' : city . name } ) .
2017-05-02 10:52:18 +00:00
then ( function ( r ) {
if ( r . data != null && ! undef ( r . data [ 'id' ] ) ) {
$scope . cities . push ( { "name" : city . name , "id" : r . data [ 'id' ] } )
2015-07-08 22:05:57 +00:00
$scope . showAddCity = false ;
2017-05-02 14:37:49 +00:00
if ( ! undef ( r . data [ 'load' ] ) && r . data [ 'load' ] ) {
loadingCity = angular . copy ( city ) ;
loadingCity . id = r . data [ 'id' ] ;
$scope . loadCity ( loadingCity ) ;
}
2017-05-02 11:08:39 +00:00
city . name = "" ;
2015-07-08 22:05:57 +00:00
}
else {
2019-05-05 16:41:07 +00:00
$scope . addCityError = t ( 'weather' , 'Failed to add city. Please contact your administrator' ) ;
2015-07-08 22:05:57 +00:00
}
2017-05-02 10:52:18 +00:00
} ,
function ( r ) {
2017-07-03 20:30:38 +00:00
if ( r . status == 401 ) {
2022-09-04 12:57:16 +00:00
$scope . addCityError = t ( 'weather' , 'Your OpenWeatherMap API key is invalid. Contact your administrator to configure a valid API key in Additional settings of the Administration' ) ;
2017-07-03 20:30:38 +00:00
}
else if ( r . status == 404 ) {
2019-05-05 16:41:07 +00:00
$scope . addCityError = t ( 'weather' , 'No city with this name found.' ) ;
2015-07-09 19:35:17 +00:00
}
2017-05-02 10:52:18 +00:00
else if ( r . status == 409 ) {
2019-05-05 16:41:07 +00:00
$scope . addCityError = t ( 'weather' , 'This city is already registered for your account.' ) ;
2015-07-09 19:35:17 +00:00
}
2017-05-02 10:52:18 +00:00
else {
$scope . addCityError = g _error500 ;
}
2015-07-08 22:05:57 +00:00
} ) ;
} ;
2015-07-09 19:05:43 +00:00
$scope . deleteCity = function ( city ) {
if ( undef ( city ) ) {
alert ( g _error500 ) ;
return ;
}
$http . post ( OC . generateUrl ( '/apps/weather/city/delete' ) , { 'id' : city . id } ) .
2017-05-02 10:52:18 +00:00
then ( function ( r ) {
if ( r . data != null && ! undef ( r . data [ 'deleted' ] ) ) {
2015-07-09 19:05:43 +00:00
for ( var i = 0 ; i < $scope . cities . length ; i ++ ) {
if ( $scope . cities [ i ] . id === city . id ) {
$scope . cities . splice ( i , 1 ) ;
// If current city is the removed city, close it
if ( $scope . selectedCityId === city . id ) {
2019-05-27 15:04:04 +00:00
$scope . currentCity = null ;
2015-07-09 19:05:43 +00:00
$scope . selectedCityId = 0 ;
}
return ;
}
}
}
else {
2019-05-05 16:41:07 +00:00
alert ( t ( 'weather' , 'Failed to remove city. Please contact your administrator' ) ) ;
2015-07-09 19:05:43 +00:00
}
2017-05-02 10:52:18 +00:00
} ,
function ( r ) {
2015-07-09 19:05:43 +00:00
alert ( g _error500 ) ;
} ) ;
} ;
2015-07-18 13:58:51 +00:00
$scope . setHome = function ( cityId ) {
if ( undef ( cityId ) ) {
alert ( g _error500 ) ;
return ;
}
$http . post ( OC . generateUrl ( '/apps/weather/settings/home/set' ) , { 'city' : cityId } ) .
2017-05-02 10:52:18 +00:00
then ( function ( r ) {
if ( r . data != null && ! undef ( r . data [ 'set' ] ) ) {
2015-07-18 13:58:51 +00:00
$scope . homeCity = cityId ;
}
else {
2019-05-05 16:41:07 +00:00
alert ( t ( 'weather' , 'Failed to set home. Please contact your administrator' ) ) ;
2015-07-18 13:58:51 +00:00
}
2017-05-02 10:52:18 +00:00
} ,
function ( r ) {
2015-07-18 13:58:51 +00:00
alert ( g _error500 ) ;
} ) ;
}
2015-07-08 21:40:17 +00:00
}
2015-07-08 21:29:55 +00:00
] ) ;