Add OpenWeatherMap and set it as the default when no key is provided
This commit is contained in:
@@ -218,6 +218,45 @@ function getYesterdayWeatherData( location, callback ) {
|
||||
} );
|
||||
}
|
||||
|
||||
// Retrieve weather data from Open Weather Map
|
||||
function getOWMWeatherData( location, callback ) {
|
||||
|
||||
// Generate URL using The Weather Company API v1 in Imperial units
|
||||
var OWM_API_KEY = process.env.OWM_API_KEY,
|
||||
url = "http://api.openweathermap.org/data/2.5/weather?appid=" + OWM_API_KEY + "&units=imperial&lat=" + location[ 0 ] + "&lon=" + location[ 1 ];
|
||||
|
||||
// Perform the HTTP request to retrieve the weather data
|
||||
httpRequest( url, function( data ) {
|
||||
console.log( url );
|
||||
try {
|
||||
data = JSON.parse( data );
|
||||
var weather = {
|
||||
timezone: timezone,
|
||||
sunrise: ( sunData.sunrise.getUTCHours() * 60 + sunData.sunrise.getUTCMinutes() ),
|
||||
sunset: ( sunData.sunset.getUTCHours() * 60 + sunData.sunset.getUTCMinutes() ),
|
||||
temp: parseInt( data.main.temp ),
|
||||
humidity: parseInt( data.main.humidity ),
|
||||
wind: parseInt( data.wind.speed )
|
||||
};
|
||||
|
||||
location = location.join( "," );
|
||||
|
||||
getCache( {
|
||||
key: "yesterdayHumidity",
|
||||
location: location,
|
||||
weather: weather,
|
||||
callback: callback
|
||||
} );
|
||||
|
||||
updateCache( location, weather );
|
||||
} catch ( err ) {
|
||||
|
||||
// Otherwise indicate the request failed
|
||||
callback( false );
|
||||
}
|
||||
} );
|
||||
}
|
||||
|
||||
// Calculate timezone and sun rise/set information
|
||||
function getTimeData( location, callback ) {
|
||||
timezoner.getTimeZone(
|
||||
@@ -406,7 +445,7 @@ exports.getWeather = function( req, res ) {
|
||||
// and also calculate if a restriction is met to prevent watering.
|
||||
finishRequest = function( weather ) {
|
||||
if ( !weather ) {
|
||||
res.send( "Error: No weather data found." );
|
||||
getTimeData( location, finishRequest );
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -499,8 +538,7 @@ exports.getWeather = function( req, res ) {
|
||||
location = [ parseFloat( location[ 0 ] ), parseFloat( location[ 1 ] ) ];
|
||||
|
||||
// Continue with the weather request
|
||||
getTimeData( location, finishRequest );
|
||||
|
||||
getOWMWeatherData( location, finishRequest );
|
||||
} else {
|
||||
|
||||
// Attempt to resolve provided location to GPS coordinates when it does not match
|
||||
@@ -512,24 +550,7 @@ exports.getWeather = function( req, res ) {
|
||||
}
|
||||
|
||||
location = result;
|
||||
getWeatherData( location, function( weather ) {
|
||||
if ( !weather ) {
|
||||
var tzOffset = getTimezone( timezone, "minutes" ),
|
||||
|
||||
// Calculate sunrise and sunset since Weather Underground does not provide it
|
||||
sunData = SunCalc.getTimes( new Date(), location[ 0 ], location[ 1 ] );
|
||||
|
||||
sunData.sunrise.setUTCMinutes( sunData.sunrise.getUTCMinutes() + tzOffset );
|
||||
sunData.sunset.setUTCMinutes( sunData.sunset.getUTCMinutes() + tzOffset );
|
||||
|
||||
weather = {
|
||||
timezone: timezone,
|
||||
sunrise: ( sunData.sunrise.getUTCHours() * 60 + sunData.sunrise.getUTCMinutes() ),
|
||||
sunset: ( sunData.sunset.getUTCHours() * 60 + sunData.sunset.getUTCMinutes() )
|
||||
};
|
||||
}
|
||||
finishRequest( weather );
|
||||
} );
|
||||
getOWMWeatherData( location, finishRequest );
|
||||
} );
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user