()
| 387 | FUNCTION fetchWeather |
| 388 | ------------------------------------------------------------------*/ |
| 389 | async function fetchWeather(){ |
| 390 | if (!WEATHER_SHOW_WEATHER) return unknownWeatherData; |
| 391 | |
| 392 | if (TESTING) return defaultWeatherDataForTesting; |
| 393 | |
| 394 | let response; |
| 395 | let isNight; |
| 396 | |
| 397 | Location.setAccuracyToThreeKilometers(); |
| 398 | locationData = await Location.current(); |
| 399 | const latitude = locationData.latitude; |
| 400 | const longitude = locationData.longitude; |
| 401 | const address = await Location.reverseGeocode(locationData.latitude, locationData.longitude); |
| 402 | const weatherURL = baseWeatherURL + '?lat=' + latitude + '&lon=' + longitude + '&exclude=' + WEATHER_EXCLUDE + '&units=' + WEATHER_UNITS + '&lang=' + WEATHER_LANG + '&appid=' + WEATHER_API_KEY; |
| 403 | |
| 404 | try { |
| 405 | writeLOG(`Fetching url: ${weatherURL}`); |
| 406 | const request = new Request(weatherURL); |
| 407 | response = await request.loadJSON(); |
| 408 | } catch (error) { |
| 409 | writeLOG(`Couldn't fetch ${weatherURL}`); |
| 410 | return unknownWeatherData; |
| 411 | } |
| 412 | try { |
| 413 | if (response.cod == 401) { |
| 414 | writeLOG('Invalid API Key. Proceeding with dummy data...'); |
| 415 | return unknownWeatherData; |
| 416 | } |
| 417 | } catch(error) { |
| 418 | writeLOG("JSON Data: " + JSON.stringify(response)); |
| 419 | } |
| 420 | const currentTime = new Date().getTime() / 1000; |
| 421 | isNight = currentTime >= response.current.sunset || currentTime <= response.current.sunrise; |
| 422 | return { |
| 423 | loc: address[0].locality.toUpperCase(), |
| 424 | weatherID: response.current.weather[0].id.toString(), |
| 425 | desc: response.current.weather[0].description, |
| 426 | mainDesc: response.current.weather[0].main, |
| 427 | temp: Math.round(response.current.temp).toString(), |
| 428 | feelsLike: Math.round(response.current.feels_like).toString(), |
| 429 | humidity: Math.round(response.current.humidity).toString(), |
| 430 | dewPoint: Math.round(response.current.dew_point).toString(), |
| 431 | visibility: (Math.round(response.current.visibility) / 1000).toString(), |
| 432 | pressure: response.current.pressure.toString(), |
| 433 | uvi: response.current.uvi.toString(), |
| 434 | wind: Math.round(response.current.wind_speed).toString(), |
| 435 | high: Math.round(response.daily[0].temp.max).toString(), |
| 436 | low: Math.round(response.daily[0].temp.min).toString(), |
| 437 | icon: response.current.weather[0].icon, |
| 438 | sunrise: convertFromUTC(response.current.sunrise), |
| 439 | sunset: convertFromUTC(response.current.sunset), |
| 440 | isNight: isNight |
| 441 | } |
| 442 | } |
| 443 | /*------------------------------------------------------------------ |
| 444 | FUNCTION getWeatherIcon |
| 445 | ------------------------------------------------------------------*/ |
no test coverage detected