Update getOWMWeatherData to modern TypeScript

This commit is contained in:
Matthew Oslan
2019-05-09 16:29:47 -04:00
parent cc14e089e2
commit 4401781dfa

View File

@@ -104,14 +104,18 @@ async function getOWMWateringData( location, callback ) {
callback( weather ); callback( weather );
} }
// Retrieve weather data from Open Weather Map for App /**
async function getOWMWeatherData( location, callback ) { * Retrieves the current weather data from OWM for usage in the mobile app.
var OWM_API_KEY = process.env.OWM_API_KEY, * @param coordinates The coordinates to retrieve the weather for
currentUrl = "http://api.openweathermap.org/data/2.5/weather?appid=" + OWM_API_KEY + "&units=imperial&lat=" + location[ 0 ] + "&lon=" + location[ 1 ], * @return A Promise that will be resolved with the OWMWeatherData if the API calls succeed, or just the TimeData if
forecastDailyUrl = "http://api.openweathermap.org/data/2.5/forecast/daily?appid=" + OWM_API_KEY + "&units=imperial&lat=" + location[ 0 ] + "&lon=" + location[ 1 ]; * an error occurs while retrieving the weather data.
*/
async function getOWMWeatherData( coordinates: GeoCoordinates ): Promise< OWMWeatherData | TimeData > {
const OWM_API_KEY = process.env.OWM_API_KEY,
currentUrl = "http://api.openweathermap.org/data/2.5/weather?appid=" + OWM_API_KEY + "&units=imperial&lat=" + coordinates[ 0 ] + "&lon=" + coordinates[ 1 ],
forecastDailyUrl = "http://api.openweathermap.org/data/2.5/forecast/daily?appid=" + OWM_API_KEY + "&units=imperial&lat=" + coordinates[ 0 ] + "&lon=" + coordinates[ 1 ];
// TODO change the type of this after defining the appropriate type const timeData: TimeData = getTimeData( coordinates );
const weather: any = getTimeData( location );
let current, forecast; let current, forecast;
try { try {
@@ -119,30 +123,31 @@ async function getOWMWeatherData( location, callback ) {
forecast = await getData( forecastDailyUrl ); forecast = await getData( forecastDailyUrl );
} catch (err) { } catch (err) {
// Return just the time data if retrieving weather data fails. // Return just the time data if retrieving weather data fails.
callback( weather ); return timeData;
return;
} }
// Return just the time data if the weather data is incomplete. // Return just the time data if the weather data is incomplete.
if ( !current || !current.main || !current.wind || !current.weather || !forecast || !forecast.list ) { if ( !current || !current.main || !current.wind || !current.weather || !forecast || !forecast.list ) {
callback( weather ); return timeData;
return;
} }
weather.temp = parseInt( current.main.temp ); const weather: OWMWeatherData = {
weather.humidity = parseInt( current.main.humidity ); ...timeData,
weather.wind = parseInt( current.wind.speed ); temp: parseInt( current.main.temp ),
weather.description = current.weather[0].description; humidity: parseInt( current.main.humidity ),
weather.icon = current.weather[0].icon; wind: parseInt( current.wind.speed ),
description: current.weather[0].description,
icon: current.weather[0].icon,
weather.region = forecast.city.country; region: forecast.city.country,
weather.city = forecast.city.name; city: forecast.city.name,
weather.minTemp = parseInt( forecast.list[ 0 ].temp.min ); minTemp: parseInt( forecast.list[ 0 ].temp.min ),
weather.maxTemp = parseInt( forecast.list[ 0 ].temp.max ); maxTemp: parseInt( forecast.list[ 0 ].temp.max ),
weather.precip = ( forecast.list[ 0 ].rain ? parseFloat( forecast.list[ 0 ].rain || 0 ) : 0 ) / 25.4; precip: ( forecast.list[ 0 ].rain ? parseFloat( forecast.list[ 0 ].rain || 0 ) : 0 ) / 25.4,
weather.forecast = []; forecast: []
};
for ( var index = 0; index < forecast.list.length; index++ ) { for ( let index = 0; index < forecast.list.length; index++ ) {
weather.forecast.push( { weather.forecast.push( {
temp_min: parseInt( forecast.list[ index ].temp.min ), temp_min: parseInt( forecast.list[ index ].temp.min ),
temp_max: parseInt( forecast.list[ index ].temp.max ), temp_max: parseInt( forecast.list[ index ].temp.max ),
@@ -152,7 +157,7 @@ async function getOWMWeatherData( location, callback ) {
} ); } );
} }
callback( weather ); return weather;
} }
/** /**
@@ -254,9 +259,10 @@ exports.getWeatherData = async function( req, res ) {
location = [ parseFloat( location[ 0 ] ), parseFloat( location[ 1 ] ) ]; location = [ parseFloat( location[ 0 ] ), parseFloat( location[ 1 ] ) ];
// Continue with the weather request // Continue with the weather request
getOWMWeatherData( location, function( data ) { const weatherData: OWMWeatherData | TimeData = await getOWMWeatherData( location );
data.location = location; res.json( {
res.json( data ); ...weatherData,
location: location
} ); } );
} else { } else {
@@ -271,9 +277,10 @@ exports.getWeatherData = async function( req, res ) {
} }
location = coordinates; location = coordinates;
getOWMWeatherData( location, function( data ) { const weatherData: OWMWeatherData | TimeData = await getOWMWeatherData( location );
data.location = location; res.json( {
res.json( data ); ...weatherData,
location: location
} ); } );
} }
}; };
@@ -524,3 +531,27 @@ interface TimeData {
/** The time of sunset, in minutes from UTC midnight. */ /** The time of sunset, in minutes from UTC midnight. */
sunset: number; sunset: number;
} }
interface OWMWeatherData extends TimeData {
/** The current temperature (in Fahrenheit). */
temp: number;
/** The current humidity (as a percentage). */
humidity: number;
wind: number;
description: string;
icon: string;
region: string;
city: string;
minTemp: number;
maxTemp: number;
precip: number;
forecast: OWMWeatherDataForecast[]
}
interface OWMWeatherDataForecast {
temp_min: number;
temp_max: number;
date: number;
icon: string;
description: string;
}