Update getOWMWateringData to modern TypeScript

This commit is contained in:
Matthew Oslan
2019-05-09 16:40:50 -04:00
parent 4401781dfa
commit bef43c9453

View File

@@ -61,13 +61,17 @@ async function getData( url: string ): Promise< any > {
} }
} }
// Retrieve data from Open Weather Map for water level calculations /**
async function getOWMWateringData( location, callback ) { * Retrieves weather data necessary for watering level calculations from the OWM API.
var OWM_API_KEY = process.env.OWM_API_KEY, * @param coordinates The coordinates to retrieve the watering data for.
forecastUrl = "http://api.openweathermap.org/data/2.5/forecast?appid=" + OWM_API_KEY + "&units=imperial&lat=" + location[ 0 ] + "&lon=" + location[ 1 ]; * @return A Promise that will be resolved with OWMWateringData if the API calls succeed, or just the TimeData if an
* error occurs while retrieving the weather data.
*/
async function getOWMWateringData( coordinates: GeoCoordinates ): Promise< OWMWateringData | TimeData > {
const OWM_API_KEY = process.env.OWM_API_KEY,
forecastUrl = "http://api.openweathermap.org/data/2.5/forecast?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 );
// Perform the HTTP request to retrieve the weather data // Perform the HTTP request to retrieve the weather data
let forecast; let forecast;
@@ -75,33 +79,32 @@ async function getOWMWateringData( location, callback ) {
forecast = await getData( forecastUrl ); forecast = await getData( forecastUrl );
} catch (err) { } catch (err) {
// Return just the time data if retrieving the forecast fails. // Return just the time data if retrieving the forecast fails.
callback( weather ); return timeData;
return;
} }
// Return just the time data if the forecast data is incomplete. // Return just the time data if the forecast data is incomplete.
if ( !forecast || !forecast.list ) { if ( !forecast || !forecast.list ) {
callback( weather ); return timeData;
return;
} }
weather.temp = 0; let totalTemp = 0,
weather.humidity = 0; totalHumidity = 0,
weather.precip = 0; totalPrecip = 0;
var periods = Math.min(forecast.list.length, 10); const periods = Math.min(forecast.list.length, 10);
for ( var index = 0; index < periods; index++ ) { for ( let index = 0; index < periods; index++ ) {
weather.temp += parseFloat( forecast.list[ index ].main.temp ); totalTemp += parseFloat( forecast.list[ index ].main.temp );
weather.humidity += parseInt( forecast.list[ index ].main.humidity ); totalHumidity += parseInt( forecast.list[ index ].main.humidity );
weather.precip += ( forecast.list[ index ].rain ? parseFloat( forecast.list[ index ].rain[ "3h" ] || 0 ) : 0 ); totalPrecip += ( forecast.list[ index ].rain ? parseFloat( forecast.list[ index ].rain[ "3h" ] || 0 ) : 0 );
} }
weather.temp = weather.temp / periods; return {
weather.humidity = weather.humidity / periods; ...timeData,
weather.precip = weather.precip / 25.4; temp: totalTemp / periods,
weather.raining = ( forecast.list[ 0 ].rain ? ( parseFloat( forecast.list[ 0 ].rain[ "3h" ] || 0 ) > 0 ) : false ); humidity: totalHumidity / periods,
precip: totalPrecip / 25.4,
callback( weather ); raining: ( forecast.list[ 0 ].rain ? ( parseFloat( forecast.list[ 0 ].rain[ "3h" ] || 0 ) > 0 ) : false )
};
} }
/** /**
@@ -390,6 +393,7 @@ exports.getWateringData = async function( req, res ) {
adjustmentOptions = false; adjustmentOptions = false;
} }
let coordinates: GeoCoordinates;
// Parse location string // Parse location string
if ( filters.pws.test( location ) ) { if ( filters.pws.test( location ) ) {
@@ -400,15 +404,13 @@ exports.getWateringData = async function( req, res ) {
// Handle GPS coordinates by storing each coordinate in an array // Handle GPS coordinates by storing each coordinate in an array
location = location.split( "," ); location = location.split( "," );
location = [ parseFloat( location[ 0 ] ), parseFloat( location[ 1 ] ) ]; coordinates = [ parseFloat( location[ 0 ] ), parseFloat( location[ 1 ] ) ];
location = coordinates;
// Continue with the weather request
getOWMWateringData( location, finishRequest );
} else { } else {
// Attempt to resolve provided location to GPS coordinates when it does not match // Attempt to resolve provided location to GPS coordinates when it does not match
// a GPS coordinate or Weather Underground location using Weather Underground autocomplete // a GPS coordinate or Weather Underground location using Weather Underground autocomplete
let coordinates: GeoCoordinates;
try { try {
coordinates = await resolveCoordinates( location ); coordinates = await resolveCoordinates( location );
} catch (err) { } catch (err) {
@@ -417,8 +419,11 @@ exports.getWateringData = async function( req, res ) {
} }
location = coordinates; location = coordinates;
getOWMWateringData( location, finishRequest );
} }
// Continue with the weather request
const wateringData: OWMWateringData | TimeData = await getOWMWateringData( coordinates );
finishRequest(wateringData);
}; };
/** /**
@@ -555,3 +560,14 @@ interface OWMWeatherDataForecast {
icon: string; icon: string;
description: string; description: string;
} }
interface OWMWateringData extends TimeData {
/** The average forecasted temperature over the next 30 hours (in Fahrenheit). */
temp: number;
/** The average forecasted humidity over the next 30 hours (as a percentage). */
humidity: number;
/** The forecasted total precipitation over the next 30 hours (in inches). */
precip: number;
/** A boolean indicating if it is currently raining. */
raining: boolean;
}