Add endpoint for getting weather data
This commit is contained in:
2
package-lock.json
generated
2
package-lock.json
generated
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "os-weather-service",
|
"name": "os-weather-service",
|
||||||
"version": "0.0.1",
|
"version": "1.0.1",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
@@ -114,7 +114,7 @@ function getOWMWeatherData( location, callback ) {
|
|||||||
weather.humidity = weather.humidity / maxCount;
|
weather.humidity = weather.humidity / maxCount;
|
||||||
weather.wind = weather.wind / maxCount;
|
weather.wind = weather.wind / maxCount;
|
||||||
weather.precip = weather.precip / maxCount;
|
weather.precip = weather.precip / maxCount;
|
||||||
|
weather.icon = data.list[0].weather[0].icon;
|
||||||
|
|
||||||
location = location.join( "," );
|
location = location.join( "," );
|
||||||
|
|
||||||
@@ -225,6 +225,33 @@ function checkRainStatus( weather ) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
exports.showWeatherData = function( req, res ) {
|
||||||
|
let location = req.query.loc;
|
||||||
|
|
||||||
|
if ( filters.gps.test( location ) ) {
|
||||||
|
|
||||||
|
// Handle GPS coordinates by storing each coordinate in an array
|
||||||
|
location = location.split( "," );
|
||||||
|
location = [ parseFloat( location[ 0 ] ), parseFloat( location[ 1 ] ) ];
|
||||||
|
|
||||||
|
// Continue with the weather request
|
||||||
|
getOWMWeatherData( location, ( data ) => res.json( data ) );
|
||||||
|
} else {
|
||||||
|
|
||||||
|
// Attempt to resolve provided location to GPS coordinates when it does not match
|
||||||
|
// a GPS coordinate or Weather Underground location using Weather Underground autocomplete
|
||||||
|
resolveCoordinates( location, function( result ) {
|
||||||
|
if ( result === false ) {
|
||||||
|
res.send( "Error: Unable to resolve location" );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
location = result;
|
||||||
|
getOWMWeatherData( location, ( data ) => res.json( data ) );
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// API Handler when using the weatherX.py where X represents the
|
// API Handler when using the weatherX.py where X represents the
|
||||||
// adjustment method which is encoded to also carry the watering
|
// adjustment method which is encoded to also carry the watering
|
||||||
// restriction and therefore must be decoded
|
// restriction and therefore must be decoded
|
||||||
@@ -324,17 +351,10 @@ exports.getWeather = function( req, res ) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Parse location string
|
// Parse location string
|
||||||
if ( weatherUndergroundKey ) {
|
if ( filters.pws.test( location ) ) {
|
||||||
|
|
||||||
// The current weather script uses Weather Underground and during the transition period
|
// Weather Underground is discontinued and PWS or ICAO cannot be resolved
|
||||||
// both will be supported and users who provide a Weather Underground API key will continue
|
res.send( "Error: Weather Underground is discontinued." );
|
||||||
// using Weather Underground until The Weather Service becomes the default API
|
|
||||||
|
|
||||||
getWeatherUndergroundData( location, weatherUndergroundKey, finishRequest );
|
|
||||||
} else if ( filters.pws.test( location ) ) {
|
|
||||||
|
|
||||||
// If no key is provided for Weather Underground then the PWS or ICAO cannot be resolved
|
|
||||||
res.send( "Error: Weather Underground key required when using PWS or ICAO location." );
|
|
||||||
return;
|
return;
|
||||||
} else if ( filters.gps.test( location ) ) {
|
} else if ( filters.gps.test( location ) ) {
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,9 @@ if ( !process.env.HOST || !process.env.PORT ) {
|
|||||||
app.get( /weather(\d+)\.py/, weather.getWeather );
|
app.get( /weather(\d+)\.py/, weather.getWeather );
|
||||||
app.get( /(\d+)/, weather.getWeather );
|
app.get( /(\d+)/, weather.getWeather );
|
||||||
|
|
||||||
|
// Handle requests matching /weatherData
|
||||||
|
app.get( /weatherData/, weather.showWeatherData );
|
||||||
|
|
||||||
app.get( "/", function( req, res ) {
|
app.get( "/", function( req, res ) {
|
||||||
res.send( "OpenSprinkler Weather Service" );
|
res.send( "OpenSprinkler Weather Service" );
|
||||||
} );
|
} );
|
||||||
|
|||||||
Reference in New Issue
Block a user